From ef30731ff7ecc0687ebf4b85db89dc83a4748266 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Mon, 10 Aug 2020 06:53:55 +0300 Subject: Tab bar is hidden when there is a single tab Needed some fixes in the widget arrangement behavior. --- src/app.c | 3 ++- src/ui/util.c | 22 +++++++++++--------- src/ui/widget.c | 18 +++++++++-------- src/ui/widget.h | 62 ++++++++++++++++++++++++++++++--------------------------- src/ui/window.c | 43 ++++++++++++++++++++++++++++++--------- 5 files changed, 92 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/app.c b/src/app.c index cc8dfaff..41e96b2d 100644 --- a/src/app.c +++ b/src/app.c @@ -394,6 +394,7 @@ iBool handleCommand_App(const char *cmd) { } else if (equal_Command(cmd, "tabs.new")) { iWidget *tabs = findWidget_App("doctabs"); + setFlags_Widget(tabs, hidden_WidgetFlag, iFalse); iWidget *newTabButton = findChild_Widget(tabs, "newtab"); removeChild_Widget(newTabButton->parent, newTabButton); iDocumentWidget *newDoc; @@ -410,7 +411,7 @@ iBool handleCommand_App(const char *cmd) { postCommandf_App("tabs.switch page:%p", newDoc); if (!isDuplicate) { postCommand_App("navigate.home"); - } + } arrange_Widget(tabs); refresh_Widget(tabs); return iTrue; diff --git a/src/ui/util.c b/src/ui/util.c index 5868e9d7..94528817 100644 --- a/src/ui/util.c +++ b/src/ui/util.c @@ -297,10 +297,9 @@ static iBool tabSwitcher_(iWidget *tabs, const char *cmd) { iWidget *makeTabs_Widget(iWidget *parent) { iWidget *tabs = makeVDiv_Widget(); iWidget *buttons = addChild_Widget(tabs, iClob(new_Widget())); - buttons->rect.size.y = 2 * gap_UI + lineHeight_Text(default_FontId); setFlags_Widget(buttons, - resizeChildren_WidgetFlag | arrangeHorizontal_WidgetFlag | - fixedHeight_WidgetFlag, + resizeWidthOfChildren_WidgetFlag | arrangeHorizontal_WidgetFlag | + arrangeHeight_WidgetFlag, iTrue); setId_Widget(buttons, "tabs.buttons"); iWidget *pages = addChildFlags_Widget( @@ -313,12 +312,14 @@ iWidget *makeTabs_Widget(iWidget *parent) { static void addTabPage_Widget_(iWidget *tabs, enum iWidgetAddPos addPos, iWidget *page, const char *label, int key, int kmods) { - iWidget * pages = findChild_Widget(tabs, "tabs.pages"); - const iBool isSel = childCount_Widget(pages) == 0; - iWidget * button = addChildPos_Widget( - findChild_Widget(tabs, "tabs.buttons"), + iWidget * pages = findChild_Widget(tabs, "tabs.pages"); + const iBool isSel = childCount_Widget(pages) == 0; + iWidget * buttons = findChild_Widget(tabs, "tabs.buttons"); + iWidget * button = addChildPos_Widget( + buttons, iClob(new_LabelWidget(label, key, kmods, format_CStr("tabs.switch page:%p", page))), addPos); + setFlags_Widget(buttons, hidden_WidgetFlag, iFalse); setFlags_Widget(button, selected_WidgetFlag, isSel); setFlags_Widget(button, commandOnClick_WidgetFlag | expand_WidgetFlag, iTrue); addChildPos_Widget(pages, page, addPos); @@ -340,13 +341,16 @@ iWidget *tabPage_Widget(iWidget *tabs, size_t index) { iWidget *removeTabPage_Widget(iWidget *tabs, size_t index) { iWidget *buttons = findChild_Widget(tabs, "tabs.buttons"); - iWidget *pages = findChild_Widget(tabs, "tabs.pages"); - iWidget *button = removeChild_Widget(buttons, child_Widget(buttons, index)); + iWidget *pages = findChild_Widget(tabs, "tabs.pages"); + iWidget *button = removeChild_Widget(buttons, child_Widget(buttons, index)); iRelease(button); iWidget *page = child_Widget(pages, index); ref_Object(page); setFlags_Widget(page, hidden_WidgetFlag | disabled_WidgetFlag, iFalse); removeChild_Widget(pages, page); + if (tabCount_Widget(tabs) <= 1 && flags_Widget(buttons) & collapse_WidgetFlag) { + setFlags_Widget(buttons, hidden_WidgetFlag, iTrue); + } return page; } diff --git a/src/ui/widget.c b/src/ui/widget.c index 51841d64..3e34c285 100644 --- a/src/ui/widget.c +++ b/src/ui/widget.c @@ -186,6 +186,8 @@ void arrange_Widget(iWidget *d) { /* Resize children to fill the parent widget. */ const size_t childCount = size_ObjectList(d->children); if (d->flags & resizeChildren_WidgetFlag) { + const iInt2 dirs = init_I2((d->flags & resizeWidthOfChildren_WidgetFlag) != 0, + (d->flags & resizeHeightOfChildren_WidgetFlag) != 0); /* Collapse hidden children. */ iForEach(ObjectList, c, d->children) { iWidget *child = as_Widget(c.object); @@ -221,21 +223,21 @@ void arrange_Widget(iWidget *d) { if (isCollapsed_Widget_(child)) continue; if (child->flags & expand_WidgetFlag) { if (d->flags & arrangeHorizontal_WidgetFlag) { - setWidth_Widget_(child, avail.x); - setHeight_Widget_(child, d->rect.size.y); + if (dirs.x) setWidth_Widget_(child, avail.x); + if (dirs.y) setHeight_Widget_(child, d->rect.size.y); } else if (d->flags & arrangeVertical_WidgetFlag) { - setWidth_Widget_(child, d->rect.size.x); - setHeight_Widget_(child, avail.y); + if (dirs.x) setWidth_Widget_(child, d->rect.size.x); + if (dirs.y) setHeight_Widget_(child, avail.y); } } else { /* Fill the off axis, though. */ if (d->flags & arrangeHorizontal_WidgetFlag) { - setHeight_Widget_(child, d->rect.size.y); + if (dirs.y) setHeight_Widget_(child, d->rect.size.y); } else if (d->flags & arrangeVertical_WidgetFlag) { - setWidth_Widget_(child, d->rect.size.x); + if (dirs.x) setWidth_Widget_(child, d->rect.size.x); } } } @@ -252,8 +254,8 @@ void arrange_Widget(iWidget *d) { iForEach(ObjectList, i, d->children) { iWidget *child = as_Widget(i.object); if (!isCollapsed_Widget_(child)) { - setWidth_Widget_(child, childSize.x); - setHeight_Widget_(child, childSize.y); + if (dirs.x) setWidth_Widget_(child, childSize.x); + if (dirs.y) setHeight_Widget_(child, childSize.y); } } } diff --git a/src/ui/widget.h b/src/ui/widget.h index 1cea7147..bf6489a0 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -22,35 +22,39 @@ iBeginDeclareClass(Widget) iEndDeclareClass(Widget) enum iWidgetFlag { - hidden_WidgetFlag = iBit(1), - disabled_WidgetFlag = iBit(2), - hover_WidgetFlag = iBit(3), /* eligible for mouse hover */ - selected_WidgetFlag = iBit(4), - pressed_WidgetFlag = iBit(5), - alignLeft_WidgetFlag = iBit(6), - alignRight_WidgetFlag = iBit(7), - frameless_WidgetFlag = iBit(8), - commandOnClick_WidgetFlag = iBit(9), - drawKey_WidgetFlag = iBit(10), - focusable_WidgetFlag = iBit(11), - tight_WidgetFlag = iBit(31), /* smaller padding */ - keepOnTop_WidgetFlag = iBit(12), /* gets events first; drawn last */ - arrangeHorizontal_WidgetFlag = iBit(17), /* arrange children horizontally */ - arrangeVertical_WidgetFlag = iBit(18), /* arrange children vertically */ - arrangeWidth_WidgetFlag = iBit(19), /* area of children becomes parent size */ - arrangeHeight_WidgetFlag = iBit(20), /* area of children becomes parent size */ - arrangeSize_WidgetFlag = arrangeWidth_WidgetFlag | arrangeHeight_WidgetFlag, - resizeChildren_WidgetFlag = iBit(21), /* resize children to fill parent size */ - expand_WidgetFlag = iBit(22), - fixedWidth_WidgetFlag = iBit(23), - fixedHeight_WidgetFlag = iBit(24), - fixedSize_WidgetFlag = fixedWidth_WidgetFlag | fixedHeight_WidgetFlag, - resizeChildrenToWidestChild_WidgetFlag = iBit(25), - resizeToParentWidth_WidgetFlag = iBit(26), - resizeToParentHeight_WidgetFlag = iBit(27), - moveToParentRightEdge_WidgetFlag = iBit(28), - collapse_WidgetFlag = iBit(29), /* when hidden, arrange size to zero */ - wasCollapsed_WidgetFlag = iBit(30), + hidden_WidgetFlag = iBit(1), + disabled_WidgetFlag = iBit(2), + hover_WidgetFlag = iBit(3), /* eligible for mouse hover */ + selected_WidgetFlag = iBit(4), + pressed_WidgetFlag = iBit(5), + alignLeft_WidgetFlag = iBit(6), + alignRight_WidgetFlag = iBit(7), + frameless_WidgetFlag = iBit(8), + commandOnClick_WidgetFlag = iBit(9), + drawKey_WidgetFlag = iBit(10), + focusable_WidgetFlag = iBit(11), + tight_WidgetFlag = iBit(12), /* smaller padding */ + keepOnTop_WidgetFlag = iBit(13), /* gets events first; drawn last */ + /* arrange behavior */ + arrangeHorizontal_WidgetFlag = iBit(17), /* arrange children horizontally */ + arrangeVertical_WidgetFlag = iBit(18), /* arrange children vertically */ + arrangeWidth_WidgetFlag = iBit(19), /* area of children becomes parent size */ + arrangeHeight_WidgetFlag = iBit(20), /* area of children becomes parent size */ + resizeWidthOfChildren_WidgetFlag = iBit(21), + resizeHeightOfChildren_WidgetFlag = iBit(22), + expand_WidgetFlag = iBit(23), + fixedWidth_WidgetFlag = iBit(24), + fixedHeight_WidgetFlag = iBit(25), + resizeChildrenToWidestChild_WidgetFlag = iBit(26), + resizeToParentWidth_WidgetFlag = iBit(27), + resizeToParentHeight_WidgetFlag = iBit(28), + moveToParentRightEdge_WidgetFlag = iBit(29), + collapse_WidgetFlag = iBit(30), /* when hidden, arrange size to zero */ + wasCollapsed_WidgetFlag = iBit(31), + /* combinations */ + arrangeSize_WidgetFlag = arrangeWidth_WidgetFlag | arrangeHeight_WidgetFlag, + resizeChildren_WidgetFlag = resizeWidthOfChildren_WidgetFlag | resizeHeightOfChildren_WidgetFlag, + fixedSize_WidgetFlag = fixedWidth_WidgetFlag | fixedHeight_WidgetFlag, }; enum iWidgetAddPos { diff --git a/src/ui/window.c b/src/ui/window.c index 296b8d05..e5050308 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -61,11 +61,28 @@ static iBool handleRootCommands_(iWidget *root, const char *cmd) { return iFalse; } -static const iMenuItem fileMenuItems[] = { #if !defined (iPlatformApple) +# define iHaveNativeMenus +#endif + +#if !defined (iHaveNativeMenus) +static const iMenuItem navMenuItems[] = { + { "New Tab", 't', KMOD_PRIMARY, "tabs.new" }, + { "Open Location...", SDLK_l, KMOD_PRIMARY, "focus.set id:url" }, + { "---", 0, 0, NULL }, + { "Preferences...", SDLK_COMMA, KMOD_PRIMARY, "preferences" }, + { "---", 0, 0, NULL }, { "Quit Lagrange", 'q', KMOD_PRIMARY, "quit" } +}; #endif + +#if defined (iHaveNativeMenus) +/* Using native menus. */ +static const iMenuItem fileMenuItems[] = { + { "New Tab", SDLK_t, KMOD_PRIMARY, "tabs.new" }, + { "Open Location...", SDLK_l, KMOD_PRIMARY, "focus.set id:url" }, }; +#endif static const iMenuItem editMenuItems[] = { #if !defined (iPlatformApple) @@ -136,9 +153,11 @@ static iBool handleNavBarCommands_(iWidget *navBar, const char *cmd) { else if (equal_Command(cmd, "tabs.changed")) { /* Update navbar according to the current tab. */ iDocumentWidget *doc = document_App(); - setText_InputWidget(findWidget_App("url"), url_DocumentWidget(doc)); - updateTextCStr_LabelWidget(findChild_Widget(navBar, "reload"), - isRequestOngoing_DocumentWidget(doc) ? stopCStr_ : reloadCStr_); + if (doc) { + setText_InputWidget(findWidget_App("url"), url_DocumentWidget(doc)); + updateTextCStr_LabelWidget(findChild_Widget(navBar, "reload"), + isRequestOngoing_DocumentWidget(doc) ? stopCStr_ : reloadCStr_); + } } else if (equal_Command(cmd, "mouse.clicked")) { iWidget *widget = pointer_Command(cmd); @@ -241,10 +260,14 @@ static void setupUserInterface_Window(iWindow *d) { "reload"); addChild_Widget(navBar, iClob(newIcon_LabelWidget("\U0001f464", 0, 0, "cert.client"))); - iLabelWidget *fileMenu = - makeMenuButton_LabelWidget("\U0001d362", fileMenuItems, iElemCount(fileMenuItems)); - setAlignVisually_LabelWidget(fileMenu, iTrue); - addChild_Widget(navBar, iClob(fileMenu)); +#if !defined (iHaveNativeMenus) + iLabelWidget *navMenu = + makeMenuButton_LabelWidget("\U0001d362", navMenuItems, iElemCount(navMenuItems)); + setAlignVisually_LabelWidget(navMenu, iTrue); + addChild_Widget(navBar, iClob(navMenu)); +#else + insertMenuItems_MacOS("File", fileMenuItems, iElemCount(fileMenuItems)); +#endif } /* Tab bar. */ { iWidget *tabBar = makeTabs_Widget(div); @@ -252,8 +275,10 @@ static void setupUserInterface_Window(iWindow *d) { setFlags_Widget(tabBar, expand_WidgetFlag, iTrue); setBackgroundColor_Widget(tabBar, gray25_ColorId); appendTabPage_Widget(tabBar, iClob(new_DocumentWidget()), "Document", '1', KMOD_PRIMARY); + iWidget *buttons = findChild_Widget(tabBar, "tabs.buttons"); + setFlags_Widget(buttons, collapse_WidgetFlag | hidden_WidgetFlag, iTrue); setId_Widget( - addChild_Widget(findChild_Widget(tabBar, "tabs.buttons"), + addChild_Widget(buttons, iClob(newIcon_LabelWidget("\u2795", 't', KMOD_PRIMARY, "tabs.new"))), "newtab"); } -- cgit v1.2.3