From aeffacbe0a9e2ae416b9a237e21ecbd99aa2977e Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Tue, 24 Dec 2024 11:12:18 +0800 Subject: [PATCH] * Make Cmd left/right go back/forward --- src/constants.js | 2 ++ src/main/index.js | 28 +++++++++++++++++++ .../FtKeyboardShortcutPrompt.vue | 8 ++++++ src/renderer/components/top-nav/top-nav.js | 14 ++++++++-- src/renderer/helpers/utils.js | 12 +++++--- static/locales/en-US.yaml | 2 ++ 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/constants.js b/src/constants.js index c21ace4448a8c..1cf16284c7492 100644 --- a/src/constants.js +++ b/src/constants.js @@ -127,6 +127,8 @@ const KeyboardShortcuts = { SHOW_SHORTCUTS: 'shift+?', HISTORY_BACKWARD: 'alt+arrowleft', HISTORY_FORWARD: 'alt+arrowright', + HISTORY_BACKWARD_ALT_MAC: 'cmd+arrowleft', + HISTORY_FORWARD_ALT_MAC: 'cmd+arrowright', FULLSCREEN: 'f11', NAVIGATE_TO_SETTINGS: 'ctrl+,', NAVIGATE_TO_HISTORY: 'ctrl+H', diff --git a/src/main/index.js b/src/main/index.js index 739ff9ee78f47..eeb5508ef121f 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1702,6 +1702,20 @@ function runApp() { }, type: 'normal', }, + ...(process.platform === 'darwin' + ? [ + { + label: 'Back', + accelerator: 'Cmd+Left', + click: (_menuItem, browserWindow, _event) => { + if (browserWindow == null) { return } + + browserWindow.webContents.navigationHistory.goBack() + }, + visible: false, + }, + ] + : []), { label: 'Forward', accelerator: 'Alt+Right', @@ -1712,6 +1726,20 @@ function runApp() { }, type: 'normal', }, + ...(process.platform === 'darwin' + ? [ + { + label: 'Forward', + accelerator: 'Cmd+Right', + click: (_menuItem, browserWindow, _event) => { + if (browserWindow == null) { return } + + browserWindow.webContents.navigationHistory.goForward() + }, + visible: false, + }, + ] + : []), ] }, { diff --git a/src/renderer/components/FtKeyboardShortcutPrompt/FtKeyboardShortcutPrompt.vue b/src/renderer/components/FtKeyboardShortcutPrompt/FtKeyboardShortcutPrompt.vue index 409f0d5ef9575..7e10ebc7ff252 100644 --- a/src/renderer/components/FtKeyboardShortcutPrompt/FtKeyboardShortcutPrompt.vue +++ b/src/renderer/components/FtKeyboardShortcutPrompt/FtKeyboardShortcutPrompt.vue @@ -115,6 +115,14 @@ const localizedShortcutNameDictionary = computed(() => { ['SHOW_SHORTCUTS', t('KeyboardShortcutPrompt.Show Keyboard Shortcuts')], ['HISTORY_BACKWARD', t('KeyboardShortcutPrompt.History Backward')], ['HISTORY_FORWARD', t('KeyboardShortcutPrompt.History Forward')], + ...( + isMac + ? [ + ['HISTORY_BACKWARD_ALT_MAC', t('KeyboardShortcutPrompt.History Backward (Mac only)')], + ['HISTORY_FORWARD_ALT_MAC', t('KeyboardShortcutPrompt.History Forward (Mac only)')], + ] + : [] + ), ['FULLSCREEN', t('KeyboardShortcutPrompt.Fullscreen')], ['NAVIGATE_TO_SETTINGS', t('KeyboardShortcutPrompt.Navigate to Settings')], ( diff --git a/src/renderer/components/top-nav/top-nav.js b/src/renderer/components/top-nav/top-nav.js index f86c1dd2fa5fa..c4124efe749d3 100644 --- a/src/renderer/components/top-nav/top-nav.js +++ b/src/renderer/components/top-nav/top-nav.js @@ -101,16 +101,26 @@ export default defineComponent({ }, forwardText: function () { + const shortcuts = [KeyboardShortcuts.APP.GENERAL.HISTORY_FORWARD] + if (process.platform === 'darwin') { + shortcuts.push(KeyboardShortcuts.APP.GENERAL.HISTORY_FORWARD_ALT_MAC) + } + return localizeAndAddKeyboardShortcutToActionTitle( this.$t('Forward'), - KeyboardShortcuts.APP.GENERAL.HISTORY_FORWARD + shortcuts, ) + this.navigationHistoryAddendum }, backwardText: function () { + const shortcuts = [KeyboardShortcuts.APP.GENERAL.HISTORY_BACKWARD] + if (process.platform === 'darwin') { + shortcuts.push(KeyboardShortcuts.APP.GENERAL.HISTORY_BACKWARD_ALT_MAC) + } + return localizeAndAddKeyboardShortcutToActionTitle( this.$t('Back'), - KeyboardShortcuts.APP.GENERAL.HISTORY_BACKWARD + shortcuts, ) + this.navigationHistoryAddendum }, diff --git a/src/renderer/helpers/utils.js b/src/renderer/helpers/utils.js index 5a54c2bc1782f..444ddfbd1a071 100644 --- a/src/renderer/helpers/utils.js +++ b/src/renderer/helpers/utils.js @@ -1021,10 +1021,14 @@ export function addKeyboardShortcutToActionTitle(actionTitle, shortcut) { /** * @param {string} localizedActionTitle - * @param {string} unlocalizedShortcut + * @param {string|string[]} sometimesManyUnlocalizedShortcuts * @returns {string} the localized action title with keyboard shortcut */ -export function localizeAndAddKeyboardShortcutToActionTitle(localizedActionTitle, unlocalizedShortcut) { - const localizedShortcut = getLocalizedShortcut(unlocalizedShortcut) - return addKeyboardShortcutToActionTitle(localizedActionTitle, localizedShortcut) +export function localizeAndAddKeyboardShortcutToActionTitle(localizedActionTitle, sometimesManyUnlocalizedShortcuts) { + let unlocalizedShortcuts = sometimesManyUnlocalizedShortcuts + if (!Array.isArray(sometimesManyUnlocalizedShortcuts)) { + unlocalizedShortcuts = [unlocalizedShortcuts] + } + const localizedShortcuts = unlocalizedShortcuts.map((s) => getLocalizedShortcut(s)) + return addKeyboardShortcutToActionTitle(localizedActionTitle, localizedShortcuts.join('/')) } diff --git a/static/locales/en-US.yaml b/static/locales/en-US.yaml index 11599e1b60b97..29320bbdb1e0f 100644 --- a/static/locales/en-US.yaml +++ b/static/locales/en-US.yaml @@ -1146,6 +1146,8 @@ KeyboardShortcutPrompt: Show Keyboard Shortcuts: Show keyboard shortcuts History Backward: Go back one page History Forward: Go forward one page + History Backward (Mac only): Go back one page (Mac only) + History Forward (Mac only): Go forward one page (Mac only) New Window: Create a new window Navigate to Settings: Navigate to the Settings page Navigate to History: Navigate to the History page