Keep scrolled down views through resizeEvents
authorSebastian Goth <seezer@roath.org>
Tue, 17 Jun 2014 17:26:50 +0000 (19:26 +0200)
committerDaniel Albers <daniel@lbe.rs>
Mon, 28 Jul 2014 13:55:31 +0000 (15:55 +0200)
There's a reason for everything...
ChatView is rendered into the viewport from top to bottom.
If we don't scroll to bottom, the topmost line doesn't change and therefore
the scrollbar will move upwards on shrinking events.
So if the scrollbar was at bottom before the resize, make it stay there.

src/qtui/chatview.cpp

index b40b290..b8cb2f1 100644 (file)
@@ -126,11 +126,26 @@ bool ChatView::event(QEvent *event)
 
 void ChatView::resizeEvent(QResizeEvent *event)
 {
+    // if view is currently scrolled to bottom, we want it that way after resizing
+    bool atBottom = (_lastScrollbarPos == verticalScrollBar()->maximum());
+
     QGraphicsView::resizeEvent(event);
 
+    // if scrolling to bottom, do it immediately.
+    if(atBottom)
+    {
+        // we can reduce viewport updates if we scroll to the bottom allready at the beginning
+        verticalScrollBar()->setValue(verticalScrollBar()->maximum());
+    }
+
     scene()->updateForViewport(viewport()->width(), viewport()->height());
     adjustSceneRect();
 
+    if(atBottom)
+    {
+        _lastScrollbarPos = verticalScrollBar()->maximum();
+        verticalScrollBar()->setValue(verticalScrollBar()->maximum());
+    }
     checkChatLineCaches();
 }