Skip to content

Commit

Permalink
feat(TextEdit): auto adjust scrollbar margins.
Browse files Browse the repository at this point in the history
Change-Id: Ia5f5533b5e2a66893f72d6d68a072369fe413929
  • Loading branch information
rekols committed Sep 18, 2018
1 parent 9d05c9c commit 9255975
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
22 changes: 10 additions & 12 deletions editor/src/texteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ TextEditor::TextEditor(QPlainTextEdit *parent)
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &TextEditor::adjustScrollbarMargins);
connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &TextEditor::adjustScrollbarMargins, Qt::QueuedConnection);
}

int TextEditor::getCurrentLine()
Expand Down Expand Up @@ -2312,19 +2312,17 @@ bool TextEditor::eventFilter(QObject *, QEvent *event)

void TextEditor::adjustScrollbarMargins()
{
if (isVisible() && !m_scrollbarLock) {
m_scrollbarLock = true;

int documentHeight = (verticalScrollBar()->maximum() - verticalScrollBar()->minimum() + verticalScrollBar()->pageStep()) * fontMetrics().height();
if (!isVisible()) {
return;
}

if (documentHeight > rect().height()) {
// setViewportMargins(0, 0, -verticalScrollBar()->sizeHint().width(), -horizontalScrollBar()->sizeHint().height() + m_scrollbarMargin);
setViewportMargins(0, 0, -verticalScrollBar()->sizeHint().width(), 0);
} else {
setViewportMargins(0, 0, 0, 0);
}
QEvent event(QEvent::LayoutRequest);
QApplication::sendEvent(this, &event);

m_scrollbarLock = false;
if (!verticalScrollBar()->visibleRegion().isEmpty()) {
setViewportMargins(0, 0, -verticalScrollBar()->sizeHint().width(), 0);
} else {
setViewportMargins(0, 0, 0, 0);
}
}

Expand Down
2 changes: 0 additions & 2 deletions editor/src/texteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ public slots:
QPoint m_mouseClickPos;

bool m_highlighted = false;
bool m_scrollbarLock = false;
int m_scrollbarMargin = 0;
};

#endif
4 changes: 4 additions & 0 deletions editor/src/theme_module/themelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ ThemeListView::~ThemeListView()

void ThemeListView::adjustScrollbarMargins()
{
if (!isVisible()) {
return;
}

QEvent event(QEvent::LayoutRequest);
QApplication::sendEvent(this, &event);

Expand Down
5 changes: 5 additions & 0 deletions editor/src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,11 @@ void Window::loadTheme(const QString &path)
editor->textEditor->setThemeWithPath(path);
}

// set background.
QPalette palette = this->palette();
palette.setColor(QPalette::Background, QColor(backgroundColor));
setPalette(palette);

m_themePanel->setBackground(backgroundColor);
m_jumpLineBar->setBackground(backgroundColor);
m_replaceBar->setBackground(backgroundColor);
Expand Down

0 comments on commit 9255975

Please sign in to comment.