@@ -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)
199215TagsEdit::~TagsEdit () = default ;
200216
201217void TagsEdit::resizeEvent (QResizeEvent* event) {
202- impl->calcRects ();
203- impl->updateVScrollRange ();
204- impl->updateHScrollRange ();
205218 QAbstractScrollArea::resizeEvent (event);
219+ impl->calcRectsUpdateScrollRanges ();
206220}
207221
208222void 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
0 commit comments