From 6f7408cbe91523a3dae80952790a4a8ec0d87cf4 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 2 Sep 2020 13:29:53 +0300 Subject: DocumentWidget: Dynamic context menu --- src/ui/documentwidget.c | 46 +++++++++++++++++++++++++++++++++------------- src/ui/window.c | 8 ++++---- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 6ca6e4c2..fb3c9ad5 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -241,18 +241,12 @@ void init_DocumentWidget(iDocumentWidget *d) { init_PtrArray(&d->visibleLinks); init_Click(&d->click, d, SDL_BUTTON_LEFT); addChild_Widget(w, iClob(d->scroll = new_ScrollWidget())); - d->menu = - makeMenu_Widget(w, - (iMenuItem[]){ { "Go Back", SDLK_LEFT, KMOD_PRIMARY, "navigate.back" }, - { "Go Forward", SDLK_RIGHT, KMOD_PRIMARY, "navigate.forward" }, - { "Reload Page", 0, 0, "navigate.reload" }, - { "---", 0, 0, NULL }, - { "Copy", 'c', KMOD_PRIMARY, "copy" }, - { "Copy Link", 0, 0, "document.copylink" } }, - 6); + d->menu = NULL; /* created when clicking */ #if !defined (iPlatformApple) /* in system menu */ addAction_Widget(w, SDLK_w, KMOD_PRIMARY, "tabs.close"); #endif + addAction_Widget(w, SDLK_LEFT, KMOD_PRIMARY, "navigate.back"); + addAction_Widget(w, SDLK_RIGHT, KMOD_PRIMARY, "navigate.forward"); } void deinit_DocumentWidget(iDocumentWidget *d) { @@ -1429,11 +1423,37 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e postCommand_App("navigate.forward"); return iTrue; } + if (ev->button.button == SDL_BUTTON_RIGHT) { + if (!d->menu || !isVisible_Widget(d->menu)) { + d->contextLink = d->hoverLink; + if (d->menu) { + destroy_Widget(d->menu); + } + iArray items; + init_Array(&items, sizeof(iMenuItem)); + pushBackN_Array( + &items, + (iMenuItem[]){ + { "Go Back", SDLK_LEFT, KMOD_PRIMARY, "navigate.back" }, + { "Go Forward", SDLK_RIGHT, KMOD_PRIMARY, "navigate.forward" }, + { "Reload Page", 0, 0, "navigate.reload" }, + { "---", 0, 0, NULL }, + { d->contextLink ? "Copy Link URL" : "Copy Page URL", + 0, + 0, + "document.copylink" }, + { isEmpty_Range(&d->selectMark) ? "Copy Full Source" : "Copy Selected", + 'c', + KMOD_PRIMARY, + "copy" }, + }, + 6); + d->menu = makeMenu_Widget(w, data_Array(&items), size_Array(&items)); + deinit_Array(&items); + } + processContextMenuEvent_Widget(d->menu, ev, d->hoverLink = NULL); + } } - if (!isVisible_Widget(d->menu)) { - d->contextLink = d->hoverLink; - } - processContextMenuEvent_Widget(d->menu, ev, d->hoverLink = NULL); switch (processEvent_Click(&d->click, ev)) { case started_ClickResult: d->selecting = iFalse; diff --git a/src/ui/window.c b/src/ui/window.c index 6f60fbb2..69730445 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -329,10 +329,10 @@ static void setupUserInterface_Window(iWindow *d) { addChild_Widget(div, iClob(navBar)); setBackgroundColor_Widget(navBar, uiBackground_ColorId); setCommandHandler_Widget(navBar, handleNavBarCommands_); - addChild_Widget(navBar, iClob(newIcon_LabelWidget("\U0001f850", 0, 0, "navigate.back"))); - addChild_Widget(navBar, iClob(newIcon_LabelWidget("\U0001f852", 0, 0, "navigate.forward"))); - iLabelWidget *idMenu = - makeMenuButton_LabelWidget("\U0001f464", identityButtonMenuItems, iElemCount(identityButtonMenuItems)); + addChild_Widget(navBar, iClob(newIcon_LabelWidget("\U0001f870", 0, 0, "navigate.back"))); + addChild_Widget(navBar, iClob(newIcon_LabelWidget("\U0001f872", 0, 0, "navigate.forward"))); + iLabelWidget *idMenu = makeMenuButton_LabelWidget( + "\U0001f464", identityButtonMenuItems, iElemCount(identityButtonMenuItems)); setAlignVisually_LabelWidget(idMenu, iTrue); addChild_Widget(navBar, iClob(idMenu)); setId_Widget(as_Widget(idMenu), "navbar.ident"); -- cgit v1.2.3