Skip to content

Commit 9c7347b

Browse files
committed
Fix initial scroll range on both widgets
1 parent 90f9c34 commit 9c7347b

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/everload_tags/tags_edit.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,28 @@ struct TagsEdit::Impl : Common {
105105
}
106106

107107
void updateVScrollRange() {
108+
if (tags.size() == 1 && tags.front().text.isEmpty()) {
109+
ifce->verticalScrollBar()->setRange(0, 0);
110+
return;
111+
}
112+
108113
auto const fm = ifce->fontMetrics();
109114
auto const row_h = pillHeight(fm.height()) + tag_v_spacing;
110115
ifce->verticalScrollBar()->setPageStep(row_h);
111116
assert(!tags.empty()); // Invariant-1
112-
auto const h = tags.back().rect.bottom() - tags.front().rect.top() + 1;
117+
118+
int top = tags.front().rect.top();
119+
int bottom = tags.back().rect.bottom();
120+
121+
if (editing_index == 0 && !(cursorVisible() || !editorText().isEmpty())) {
122+
top = tags[1].rect.top();
123+
} else if (editing_index == tags.size() - 1 && !(cursorVisible() || !editorText().isEmpty())) {
124+
bottom = tags[tags.size() - 2].rect.bottom();
125+
}
126+
127+
auto const h = bottom - top + 1;
113128
auto const contents_rect = contentsRect();
129+
114130
if (contents_rect.height() < h) {
115131
ifce->verticalScrollBar()->setRange(0, h - contents_rect.height());
116132
} else {
@@ -199,18 +215,16 @@ TagsEdit::TagsEdit(QWidget* parent, Config config)
199215
TagsEdit::~TagsEdit() = default;
200216

201217
void TagsEdit::resizeEvent(QResizeEvent* event) {
202-
impl->calcRects();
203-
impl->updateVScrollRange();
204-
impl->updateHScrollRange();
205218
QAbstractScrollArea::resizeEvent(event);
219+
impl->calcRectsUpdateScrollRanges();
206220
}
207221

208222
void TagsEdit::focusInEvent(QFocusEvent* event) {
209223
QAbstractScrollArea::focusInEvent(event);
210224
impl->focused_at = std::chrono::steady_clock::now();
211225
impl->setCursorVisible(true, this);
212226
impl->updateDisplayText();
213-
impl->calcRects();
227+
impl->calcRectsUpdateScrollRanges();
214228
impl->ensureCursorIsVisibleH();
215229
impl->ensureCursorIsVisibleV();
216230
viewport()->update();
@@ -220,7 +234,7 @@ void TagsEdit::focusOutEvent(QFocusEvent* event) {
220234
QAbstractScrollArea::focusOutEvent(event);
221235
impl->setCursorVisible(false, this);
222236
impl->updateDisplayText();
223-
impl->calcRects();
237+
impl->calcRectsUpdateScrollRanges();
224238
viewport()->update();
225239
}
226240

src/everload_tags/tags_line_edit.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,20 @@ struct TagsLineEdit::Impl : Common {
9191
}
9292

9393
int pillsWidth() const {
94-
return tags.back().rect.right() - tags.front().rect.left();
94+
if (tags.size() == 1 && tags.front().text.isEmpty()) {
95+
return 0;
96+
}
97+
98+
int left = tags.front().rect.left();
99+
int right = tags.back().rect.right();
100+
101+
if (editing_index == 0 && !(cursorVisible() || !editorText().isEmpty())) {
102+
left = tags[1].rect.left();
103+
} else if (editing_index == tags.size() - 1 && !(cursorVisible() || !editorText().isEmpty())) {
104+
right = tags[tags.size() - 2].rect.right();
105+
}
106+
107+
return right - left + 1;
95108
}
96109

97110
void updateHScrollRange() {

0 commit comments

Comments
 (0)