From 37fefc85a9a23be7c3bc6dcddb8a30047459f534 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Tue, 26 Jan 2021 07:34:22 +0200 Subject: Keybindings for new tab, close tab, add bookmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added new keybindings for opening and closing a tab. Also added a keybinding for adding a bookmark so ⌘D can be made available. On macOS, there is a default Window > Close menu item, so treat that as a "tabs.close" command to make it bindable. IssueID #121 --- src/macos.m | 26 ++++++++++++++++++++------ src/ui/documentwidget.c | 2 +- src/ui/keys.c | 4 +++- src/ui/keys.h | 4 ++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/macos.m b/src/macos.m index 4e214923..9d46d84b 100644 --- a/src/macos.m +++ b/src/macos.m @@ -241,10 +241,20 @@ static void appearanceChanged_MacOS_(NSString *name) { - (void)showPreferences { postCommand_App("preferences"); + ignoreImmediateKeyDownEvents_(); +} + +static void ignoreImmediateKeyDownEvents_(void) { + /* SDL ignores menu key equivalents so the keydown events will be posted regardless. + However, we shouldn't double-activate menu items when a shortcut key is used in our + widgets. Quite a kludge: take advantage of Window's focus-acquisition threshold to + ignore the immediately following key down events. */ + get_Window()->focusGainedAt = SDL_GetTicks(); } - (void)closeTab { postCommand_App("tabs.close"); + ignoreImmediateKeyDownEvents_(); } - (NSString *)commandForItem:(NSMenuItem *)menuItem { @@ -255,11 +265,7 @@ static void appearanceChanged_MacOS_(NSString *name) { NSString *command = [menuCommands objectForKey:[(NSMenuItem *)sender title]]; if (command) { postCommand_App([command cStringUsingEncoding:NSUTF8StringEncoding]); - /* Shouldn't double-activate menu items in case the same key is used in our widgets. - SDL ignores menu key equivalents so the keydown events will be posted regardless. - This is quite a kludge, but we can achieve this by taking advantage of Window's - focus-acquisition threshold for ignoring key events. */ - get_Window()->focusGainedAt = SDL_GetTicks(); + ignoreImmediateKeyDownEvents_(); } } @@ -501,11 +507,17 @@ void handleCommand_MacOS(const char *cmd) { NSApplication *app = [NSApplication sharedApplication]; MyDelegate *myDel = (MyDelegate *) app.delegate; NSMenu *appMenu = [app mainMenu]; + int mainIndex = 0; for (NSMenuItem *mainMenuItem in appMenu.itemArray) { NSMenu *menu = mainMenuItem.submenu; if (menu) { - for (NSMenuItem *menuItem in menu.itemArray) { + int itemIndex = 0; + for (NSMenuItem *menuItem in menu.itemArray) { NSString *command = [myDel commandForItem:menuItem]; + if (!command && mainIndex == 6 && itemIndex == 0) { + /* Window > Close */ + command = @"tabs.close"; + } if (command) { const iBinding *bind = findCommand_Keys( [command cStringUsingEncoding:NSUTF8StringEncoding]); @@ -513,8 +525,10 @@ void handleCommand_MacOS(const char *cmd) { setShortcut_NSMenuItem_(menuItem, bind->key, bind->mods); } } + itemIndex++; } } + mainIndex++; } } } diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index c725a8f6..68f3c966 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -253,7 +253,7 @@ void init_DocumentWidget(iDocumentWidget *d) { resizeToParentWidth_WidgetFlag | resizeToParentHeight_WidgetFlag); #if !defined (iPlatformApple) /* in system menu */ addAction_Widget(w, reload_KeyShortcut, "navigate.reload"); - addAction_Widget(w, SDLK_w, KMOD_PRIMARY, "tabs.close"); + addAction_Widget(w, closeTab_KeyShortcut, "tabs.close"); addAction_Widget(w, SDLK_d, KMOD_PRIMARY, "bookmark.add"); addAction_Widget(w, subscribeToPage_KeyModifier, "feeds.subscribe"); #endif diff --git a/src/ui/keys.c b/src/ui/keys.c index 5cf071fb..656a52ac 100644 --- a/src/ui/keys.c +++ b/src/ui/keys.c @@ -80,12 +80,14 @@ static const struct { int id; iMenuItem bind; int flags; } defaultBindings_[] = { 45, { "Open link in new tab via home row keys", 'f', KMOD_SHIFT, "document.linkkeys arg:1 newtab:1" }, 0 }, { 46, { "Hover on link via home row keys", 'h', 0, "document.linkkeys arg:1 hover:1" }, 0 }, { 47, { "Next set of home row key links", '.', 0, "document.linkkeys more:1" }, 0 }, + { 50, { "Add bookmark", 'd', KMOD_PRIMARY, "bookmark.add" }, 0 }, { 70, { "Zoom in", SDLK_EQUALS, KMOD_PRIMARY, "zoom.delta arg:10" }, 0 }, { 71, { "Zoom out", SDLK_MINUS, KMOD_PRIMARY, "zoom.delta arg:-10" }, 0 }, { 72, { "Reset zoom", SDLK_0, KMOD_PRIMARY, "zoom.set arg:100" }, 0 }, + { 76, { "New tab", newTab_KeyShortcut, "tabs.new" }, 0 }, + { 77, { "Close tab", closeTab_KeyShortcut, "tabs.close" }, 0 }, { 80, { "Previous tab", prevTab_KeyShortcut, "tabs.prev" }, 0 }, { 81, { "Next tab", nextTab_KeyShortcut, "tabs.next" }, 0 }, - { 82, { "Close tab", 'c', 0, "tabs.close" }, 0 }, { 100,{ "Toggle show URL on hover", '/', KMOD_PRIMARY, "prefs.hoverlink.toggle" }, 0 }, /* The following cannot currently be changed (built-in duplicates). */ { 1000, { NULL, SDLK_SPACE, KMOD_SHIFT, "scroll.page arg:-1" }, argRepeat_BindFlag }, diff --git a/src/ui/keys.h b/src/ui/keys.h index 8bcd4f53..f6f0b465 100644 --- a/src/ui/keys.h +++ b/src/ui/keys.h @@ -28,6 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #if defined (iPlatformApple) # define reload_KeyShortcut SDLK_r, KMOD_PRIMARY +# define newTab_KeyShortcut SDLK_t, KMOD_PRIMARY +# define closeTab_KeyShortcut SDLK_w, KMOD_PRIMARY # define prevTab_KeyShortcut SDLK_LEFTBRACKET, KMOD_SHIFT | KMOD_PRIMARY # define nextTab_KeyShortcut SDLK_RIGHTBRACKET, KMOD_SHIFT | KMOD_PRIMARY # define navigateBack_KeyShortcut SDLK_LEFT, KMOD_PRIMARY @@ -40,6 +42,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ # define subscribeToPage_KeyModifier SDLK_d, KMOD_SHIFT | KMOD_PRIMARY #else # define reload_KeyShortcut SDLK_r, KMOD_PRIMARY +# define newTab_KeyShortcut SDLK_t, KMOD_PRIMARY +# define closeTab_KeyShortcut SDLK_w, KMOD_PRIMARY # define prevTab_KeyShortcut SDLK_PAGEUP, KMOD_PRIMARY # define nextTab_KeyShortcut SDLK_PAGEDOWN, KMOD_PRIMARY # define navigateBack_KeyShortcut SDLK_LEFT, KMOD_ALT -- cgit v1.2.3