From 63af9a9cd4b048380a763cf4003e977bb0d6275a Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 7 Apr 2021 09:20:25 +0300 Subject: SidebarWidget: Store width as resolution independent units DPI and pixel ratio changes should not radically change width of the sidebar. IssueID #239 --- src/app.c | 4 ++-- src/ui/sidebarwidget.c | 29 +++++++++++++++++------------ src/ui/sidebarwidget.h | 4 ++-- src/ui/window.c | 4 ++-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/app.c b/src/app.c index a1475d1c..9f785a98 100644 --- a/src/app.c +++ b/src/app.c @@ -180,8 +180,8 @@ static iString *serializePrefs_App_(const iApp *d) { w = d->window->place.normalRect.size.x; h = d->window->place.normalRect.size.y; appendFormat_String(str, "window.setrect width:%d height:%d coord:%d %d\n", w, h, x, y); - appendFormat_String(str, "sidebar.width arg:%d\n", width_SidebarWidget(sidebar)); - appendFormat_String(str, "sidebar2.width arg:%d\n", width_SidebarWidget(sidebar2)); + appendFormat_String(str, "sidebar.width arg:%f gaps:1\n", width_SidebarWidget(sidebar)); + appendFormat_String(str, "sidebar2.width arg:%f gaps:1\n", width_SidebarWidget(sidebar2)); /* On macOS, maximization should be applied at creation time or the window will take a moment to animate to its maximized size. */ #if defined (LAGRANGE_CUSTOM_FRAME) diff --git a/src/ui/sidebarwidget.c b/src/ui/sidebarwidget.c index 73eee12d..74784547 100644 --- a/src/ui/sidebarwidget.c +++ b/src/ui/sidebarwidget.c @@ -97,7 +97,7 @@ struct Impl_SidebarWidget { int modeScroll[max_SidebarMode]; iLabelWidget * modeButtons[max_SidebarMode]; int maxButtonLabelWidth; - int width; + int widthAsGaps; int itemFonts[2]; size_t numUnreadEntries; iWidget * resizer; @@ -461,8 +461,8 @@ enum iSidebarMode mode_SidebarWidget(const iSidebarWidget *d) { return d ? d->mode : 0; } -int width_SidebarWidget(const iSidebarWidget *d) { - return d ? d->width : 0; +float width_SidebarWidget(const iSidebarWidget *d) { + return d ? d->widthAsGaps : 0; } static const char *normalModeLabels_[max_SidebarMode] = { @@ -520,13 +520,13 @@ void init_SidebarWidget(iSidebarWidget *d, enum iSidebarSide side) { d->itemFonts[0] = uiContent_FontId; d->itemFonts[1] = uiContentBold_FontId; #if defined (iPlatformAppleMobile) - d->width = 73 * gap_UI; + d->widthAsGaps = 73; if (deviceType_App() == phone_AppDeviceType) { d->itemFonts[0] = defaultBig_FontId; d->itemFonts[1] = defaultBigBold_FontId; } #else - d->width = 60 * gap_UI; + d->widthAsGaps = 60; #endif setFlags_Widget(w, fixedWidth_WidgetFlag, iTrue); iWidget *vdiv = makeVDiv_Widget(); @@ -720,16 +720,17 @@ static void checkModeButtonLayout_SidebarWidget_(iSidebarWidget *d) { } } -void setWidth_SidebarWidget(iSidebarWidget *d, int width) { - const iBool isFixedWidth = deviceType_App() == phone_AppDeviceType; +void setWidth_SidebarWidget(iSidebarWidget *d, float widthAsGaps) { iWidget *w = as_Widget(d); + const iBool isFixedWidth = deviceType_App() == phone_AppDeviceType; + int width = widthAsGaps * gap_UI; if (!isFixedWidth) { /* Even less space if the other sidebar is visible, too. */ const int otherWidth = width_Widget(findWidget_App(d->side == left_SideBarSide ? "sidebar2" : "sidebar")); width = iClamp(width, 30 * gap_UI, rootSize_Window(get_Window()).x - 50 * gap_UI - otherWidth); } - d->width = width; + d->widthAsGaps = (float) width / (float) gap_UI; if (isVisible_Widget(w)) { w->rect.size.x = width; } @@ -780,7 +781,8 @@ iBool handleBookmarkEditorCommands_SidebarWidget_(iWidget *editor, const char *c static iBool handleSidebarCommand_SidebarWidget_(iSidebarWidget *d, const char *cmd) { iWidget *w = as_Widget(d); if (equal_Command(cmd, "width")) { - setWidth_SidebarWidget(d, arg_Command(cmd)); + setWidth_SidebarWidget(d, arg_Command(cmd) * + (argLabel_Command(cmd, "gaps") ? 1.0f : (1.0f / gap_UI))); return iTrue; } else if (equal_Command(cmd, "mode")) { @@ -802,7 +804,7 @@ static iBool handleSidebarCommand_SidebarWidget_(iSidebarWidget *d, const char * } setFlags_Widget(w, hidden_WidgetFlag, isVisible_Widget(w)); if (isVisible_Widget(w)) { - w->rect.size.x = d->width; + w->rect.size.x = d->widthAsGaps * gap_UI; invalidate_ListWidget(d->list); } arrange_Widget(w->parent); @@ -826,6 +828,9 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) checkModeButtonLayout_SidebarWidget_(d); } else if (isMetricsChange_UserEvent(ev)) { + if (isVisible_Widget(w)) { + w->rect.size.x = d->widthAsGaps * gap_UI; + } updateMetrics_SidebarWidget_(d); arrange_Widget(w); checkModeButtonLayout_SidebarWidget_(d); @@ -889,10 +894,10 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) const int resMid = d->resizer->rect.size.x / 2; setWidth_SidebarWidget( d, - (d->side == left_SideBarSide + ((d->side == left_SideBarSide ? local.x : (rootSize_Window(get_Window()).x - coord_Command(cmd).x)) + - resMid); + resMid) / (float) gap_UI); } return iTrue; } diff --git a/src/ui/sidebarwidget.h b/src/ui/sidebarwidget.h index d7029b0f..8027adc0 100644 --- a/src/ui/sidebarwidget.h +++ b/src/ui/sidebarwidget.h @@ -47,5 +47,5 @@ iBool setMode_SidebarWidget (iSidebarWidget *, enum iSidebar void setButtonFont_SidebarWidget (iSidebarWidget *, int font); enum iSidebarMode mode_SidebarWidget (const iSidebarWidget *); -int width_SidebarWidget (const iSidebarWidget *); -void setWidth_SidebarWidget (iSidebarWidget *, int width); +float width_SidebarWidget (const iSidebarWidget *); +void setWidth_SidebarWidget (iSidebarWidget *, float widthAsGaps); diff --git a/src/ui/window.c b/src/ui/window.c index 82fb8613..ce9d730e 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -161,14 +161,14 @@ static iBool handleRootCommands_(iWidget *root, const char *cmd) { isPortrait_App()); if (isLandscape_App()) { addChildPos_Widget(findChild_Widget(root, "tabs.content"), iClob(sidebar), front_WidgetAddPos); - setWidth_SidebarWidget(sidebar, 73 * gap_UI); + setWidth_SidebarWidget(sidebar, 73.0f); if (isVisible_Widget(findWidget_App("sidebar2"))) { postCommand_App("sidebar2.toggle"); } } else { addChildPos_Widget(findChild_Widget(root, "stack"), iClob(sidebar), back_WidgetAddPos); - setWidth_SidebarWidget(sidebar, width_Widget(root)); + setWidth_SidebarWidget(sidebar, (float) width_Widget(root) / (float) gap_UI); } return iFalse; } -- cgit v1.2.3