From 7eea6f318d2252ccd92f6ed50e0320c143c40951 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 12 Aug 2020 13:36:28 +0300 Subject: Working on sidebar resizing --- src/ui/documentwidget.c | 9 +++++++- src/ui/documentwidget.h | 2 ++ src/ui/sidebarwidget.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- src/ui/widget.c | 11 +++++++--- 4 files changed, 74 insertions(+), 6 deletions(-) (limited to 'src/ui') diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 57709384..483dc983 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -159,6 +159,7 @@ void init_DocumentWidget(iDocumentWidget *d) { d->isRequestUpdated = iFalse; d->media = new_ObjectList(); d->doc = new_GmDocument(); + d->initialScrollY = 0; d->scrollY = 0; d->selecting = iFalse; d->selectMark = iNullRange; @@ -558,7 +559,7 @@ static iBool updateFromHistory_DocumentWidget_(iDocumentWidget *d) { const iGmResponse *resp = recent->cachedResponse; d->state = fetching_RequestState; /* Use the cached response data. */ - d->scrollY = recent->scrollY; + d->scrollY = d->initialScrollY = recent->scrollY; updateTrust_DocumentWidget_(d, resp); updateDocument_DocumentWidget_(d, resp); d->state = ready_RequestState; @@ -824,6 +825,11 @@ static void changeTextSize_DocumentWidget_(iDocumentWidget *d, int delta) { postCommandf_App("font.setfactor arg:%d", d->mod.textSizePercent); } +void updateSize_DocumentWidget(iDocumentWidget *d) { + setWidth_GmDocument(d->doc, documentWidth_DocumentWidget_(d)); + updateVisible_DocumentWidget_(d); +} + 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")) { @@ -956,6 +962,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) return handleMediaCommand_DocumentWidget_(d, cmd); } else if (equal_Command(cmd, "document.reload") && document_App() == d) { + d->initialScrollY = d->scrollY; fetch_DocumentWidget_(d); return iTrue; } diff --git a/src/ui/documentwidget.h b/src/ui/documentwidget.h index 810c1392..05b8b50f 100644 --- a/src/ui/documentwidget.h +++ b/src/ui/documentwidget.h @@ -22,3 +22,5 @@ const iGmDocument * document_DocumentWidget (const iDocumentWidget *); void setUrl_DocumentWidget (iDocumentWidget *, const iString *url); void setUrlFromCache_DocumentWidget (iDocumentWidget *, const iString *url, iBool isFromCache); void setInitialScroll_DocumentWidget (iDocumentWidget *, int scrollY); /* set after content received */ + +void updateSize_DocumentWidget (iDocumentWidget *); diff --git a/src/ui/sidebarwidget.c b/src/ui/sidebarwidget.c index fa550176..c5e7d05f 100644 --- a/src/ui/sidebarwidget.c +++ b/src/ui/sidebarwidget.c @@ -9,6 +9,7 @@ #include "app.h" #include +#include iDeclareType(SidebarItem) @@ -50,10 +51,16 @@ struct Impl_SidebarWidget { iArray items; size_t hoverItem; iClick click; + iWidget *resizer; + SDL_Cursor *resizeCursor; }; iDefineObjectConstruction(SidebarWidget) +static iBool isResizing_SidebarWidget_(const iSidebarWidget *d) { + return (flags_Widget(d->resizer) & pressed_WidgetFlag) != 0; +} + static void clearItems_SidebarWidget_(iSidebarWidget *d) { iForEach(Array, i, &d->items) { deinit_SidebarItem(i.value); @@ -150,9 +157,19 @@ void init_SidebarWidget(iSidebarWidget *d) { addChild_Widget(w, iClob(d->scroll = new_ScrollWidget())); setThumb_ScrollWidget(d->scroll, 0, 0); setMode_SidebarWidget(d, documentOutline_SidebarMode); + d->resizer = addChildFlags_Widget( + w, + iClob(new_Widget()), + hover_WidgetFlag | commandOnClick_WidgetFlag | fixedWidth_WidgetFlag | + resizeToParentHeight_WidgetFlag | moveToParentRightEdge_WidgetFlag); + setId_Widget(d->resizer, "sidebar.grab"); + d->resizer->rect.size.x = gap_UI; + setBackgroundColor_Widget(d->resizer, red_ColorId); + d->resizeCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE); } void deinit_SidebarWidget(iSidebarWidget *d) { + SDL_FreeCursor(d->resizeCursor); clearItems_SidebarWidget_(d); deinit_Array(&d->items); } @@ -206,6 +223,37 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) if (isResize_UserEvent(ev)) { updateVisible_SidebarWidget_(d); } + else if (isCommand_Widget(w, ev, "mouse.clicked")) { + const char *cmd = command_UserEvent(ev); + if (argLabel_Command(cmd, "button") == SDL_BUTTON_LEFT) { + if (arg_Command(cmd)) { + setFlags_Widget(d->resizer, pressed_WidgetFlag, iTrue); + setBackgroundColor_Widget(d->resizer, orange_ColorId); + setMouseGrab_Widget(d->resizer); + refresh_Widget(d->resizer); + } + else { + setFlags_Widget(d->resizer, pressed_WidgetFlag, iFalse); + setBackgroundColor_Widget(d->resizer, red_ColorId); + setMouseGrab_Widget(NULL); + refresh_Widget(d->resizer); + } + } + return iTrue; + } + else if (isCommand_Widget(w, ev, "mouse.moved")) { + const char *cmd = command_UserEvent(ev); + if (isResizing_SidebarWidget_(d)) { + const iInt2 local = localCoord_Widget(w, coord_Command(cmd)); + w->rect.size.x = local.x + d->resizer->rect.size.x / 2; + arrange_Widget(findWidget_App("doctabs")); + if (!isRefreshPending_App()) { + updateSize_DocumentWidget(document_App()); + refresh_Widget(w); + } + } + return iTrue; + } else if (isCommand_Widget(w, ev, "scroll.moved")) { d->scrollY = arg_Command(command_UserEvent(ev)); d->hoverItem = iInvalidPos; @@ -226,8 +274,14 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) if (ev->type == SDL_MOUSEMOTION) { const iInt2 mouse = init_I2(ev->motion.x, ev->motion.y); size_t hover = iInvalidPos; - if (contains_Widget(w, mouse)) { - hover = itemIndex_SidebarWidget_(d, mouse); + if (contains_Widget(d->resizer, mouse)) { + SDL_SetCursor(d->resizeCursor); + } + else { + SDL_SetCursor(NULL); + if (contains_Widget(w, mouse)) { + hover = itemIndex_SidebarWidget_(d, mouse); + } } if (hover != d->hoverItem) { d->hoverItem = hover; diff --git a/src/ui/widget.c b/src/ui/widget.c index b9fb12b9..2a889683 100644 --- a/src/ui/widget.c +++ b/src/ui/widget.c @@ -424,9 +424,9 @@ iBool processEvent_Widget(iWidget *d, const SDL_Event *ev) { return iTrue; } } - else if (d->flags & commandOnClick_WidgetFlag && (ev->type == SDL_MOUSEBUTTONDOWN || - ev->type == SDL_MOUSEBUTTONUP) && - contains_Widget(d, init_I2(ev->button.x, ev->button.y))) { + else if (d->flags & commandOnClick_WidgetFlag && + (ev->type == SDL_MOUSEBUTTONDOWN || ev->type == SDL_MOUSEBUTTONUP) && + (mouseGrab_Widget() == d || contains_Widget(d, init_I2(ev->button.x, ev->button.y)))) { postCommand_Widget(d, "mouse.clicked arg:%d button:%d coord:%d %d", ev->type == SDL_MOUSEBUTTONDOWN ? 1 : 0, @@ -435,6 +435,11 @@ iBool processEvent_Widget(iWidget *d, const SDL_Event *ev) { ev->button.y); return iTrue; } + else if (d->flags & commandOnClick_WidgetFlag && + mouseGrab_Widget() == d && ev->type == SDL_MOUSEMOTION) { + postCommand_Widget(d, "mouse.moved coord:%d %d", ev->motion.x, ev->motion.y); + return iTrue; + } switch (ev->type) { case SDL_USEREVENT: { if (ev->user.code == command_UserEventCode && d->commandHandler && -- cgit v1.2.3