From 15aa8e1f486e4e1d48adbfc0f0f5922bd478db29 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 10 Feb 2021 10:59:19 +0200 Subject: Window: Keep scroll position when resizing vertically IssueID #138 --- src/ui/documentwidget.c | 23 +++++++++++++++-------- src/ui/window.c | 10 +++++++++- src/ui/window.h | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 4063503f..b9aa551f 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -1533,18 +1533,25 @@ static const int homeRowKeys_[] = { static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) { iWidget *w = as_Widget(d); if (equal_Command(cmd, "window.resized") || equal_Command(cmd, "font.changed")) { - const iGmRun *mid = middleRun_DocumentWidget_(d); - const char *midLoc = (mid ? mid->text.start : NULL); + const iBool isHorizontalOnly = + argLabel_Command(cmd, "horiz") != 0 && !argLabel_Command(cmd, "vert"); /* Alt/Option key may be involved in window size changes. */ iChangeFlags(d->flags, showLinkNumbers_DocumentWidgetFlag, iFalse); - setWidth_GmDocument(d->doc, documentWidth_DocumentWidget_(d)); - scroll_DocumentWidget_(d, 0); - if (midLoc) { - mid = findRunAtLoc_GmDocument(d->doc, midLoc); - if (mid) { - scrollTo_DocumentWidget_(d, mid_Rect(mid->bounds).y, iTrue); + if (isHorizontalOnly) { + const iGmRun *mid = middleRun_DocumentWidget_(d); + const char *midLoc = (mid ? mid->text.start : NULL); + setWidth_GmDocument(d->doc, documentWidth_DocumentWidget_(d)); + scroll_DocumentWidget_(d, 0); + if (midLoc) { + mid = findRunAtLoc_GmDocument(d->doc, midLoc); + if (mid) { + scrollTo_DocumentWidget_(d, mid_Rect(mid->bounds).y, iTrue); + } } } + else { + scroll_DocumentWidget_(d, 0); /* prevent overscroll */ + } updateSideIconBuf_DocumentWidget_(d); updateOutline_DocumentWidget_(d); invalidate_DocumentWidget_(d); diff --git a/src/ui/window.c b/src/ui/window.c index 5c7fcaab..f9e58acb 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -667,9 +667,16 @@ static void updateRootSize_Window_(iWindow *d, iBool notifyAlways) { const iInt2 oldSize = *size; SDL_GetRendererOutputSize(d->render, &size->x, &size->y); if (notifyAlways || !isEqual_I2(oldSize, *size)) { + const iBool isHoriz = (d->lastNotifiedSize.x != size->x); + const iBool isVert = (d->lastNotifiedSize.y != size->y); arrange_Widget(d->root); - postCommandf_App("window.resized width:%d height:%d", size->x, size->y); + postCommandf_App("window.resized width:%d height:%d horiz:%d vert:%d", + size->x, + size->y, + isHoriz, + isVert); postRefresh_App(); + d->lastNotifiedSize = *size; } } @@ -719,6 +726,7 @@ void init_Window(iWindow *d, iRect rect) { iZap(d->cursors); d->initialPos = rect.pos; d->lastRect = rect; + d->lastNotifiedSize = zero_I2(); d->pendingCursor = NULL; d->isDrawFrozen = iTrue; d->isMouseInside = iTrue; diff --git a/src/ui/window.h b/src/ui/window.h index 8cadad1b..9e3f16bc 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -36,6 +36,7 @@ struct Impl_Window { SDL_Window * win; iInt2 initialPos; iRect lastRect; /* updated when window is moved/resized */ + iInt2 lastNotifiedSize; /* keep track of horizontal/vertical notifications */ iBool isDrawFrozen; /* avoids premature draws while restoring window state */ iBool isMouseInside; uint32_t focusGainedAt; -- cgit v1.2.3