diff --git a/src/constants.js b/src/constants.js index 0ae194d19a6cd..97be9daf6fe9f 100644 --- a/src/constants.js +++ b/src/constants.js @@ -5,10 +5,7 @@ const IpcChannels = { OPEN_EXTERNAL_LINK: 'open-external-link', GET_SYSTEM_LOCALE: 'get-system-locale', GET_PICTURES_PATH: 'get-pictures-path', - GET_NAV_HISTORY_ENTRY_TITLE_AT_INDEX: 'get-navigation-history-entry-at-index', - GET_NAV_HISTORY_ACTIVE_INDEX: 'get-navigation-history-active-index', - GET_NAV_HISTORY_LENGTH: 'get-navigation-history-length', - GO_TO_NAV_HISTORY_OFFSET: 'go-to-navigation-history-index', + GET_NAVIGATION_HISTORY: 'get-navigation-history', SHOW_OPEN_DIALOG: 'show-open-dialog', SHOW_SAVE_DIALOG: 'show-save-dialog', STOP_POWER_SAVE_BLOCKER: 'stop-power-save-blocker', diff --git a/src/main/index.js b/src/main/index.js index 4bd741a4accb9..57bb06ca27b0a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -898,20 +898,37 @@ function runApp() { // #region navigation history - ipcMain.on(IpcChannels.GO_TO_NAV_HISTORY_OFFSET, ({ sender }, offset) => { - sender.navigationHistory.goToOffset(offset) - }) + const NAV_HISTORY_DISPLAY_LIMIT = 15 + // Math.trunc but with a bitwise OR so that it can be calcuated at build time and the number inlined + const HALF_OF_NAV_HISTORY_DISPLAY_LIMIT = (NAV_HISTORY_DISPLAY_LIMIT / 2) | 0 - ipcMain.handle(IpcChannels.GET_NAV_HISTORY_ENTRY_TITLE_AT_INDEX, async ({ sender }, index) => { - return sender.navigationHistory.getEntryAtIndex(index)?.title - }) + ipcMain.handle(IpcChannels.GET_NAVIGATION_HISTORY, ({ sender }) => { + const activeIndex = sender.navigationHistory.getActiveIndex() + const length = sender.navigationHistory.length() - ipcMain.handle(IpcChannels.GET_NAV_HISTORY_ACTIVE_INDEX, async ({ sender }) => { - return sender.navigationHistory.getActiveIndex() - }) + let end + + if (activeIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT) { + end = Math.min(length - 1, NAV_HISTORY_DISPLAY_LIMIT - 1) + } else if (length - activeIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT + 1) { + end = length - 1 + } else { + end = activeIndex + HALF_OF_NAV_HISTORY_DISPLAY_LIMIT + } + + const dropdownOptions = [] + + for (let index = end; index >= Math.max(0, end + 1 - NAV_HISTORY_DISPLAY_LIMIT); --index) { + const routeLabel = sender.navigationHistory.getEntryAtIndex(index)?.title + + dropdownOptions.push({ + label: routeLabel, + value: index - activeIndex, + active: index === activeIndex + }) + } - ipcMain.handle(IpcChannels.GET_NAV_HISTORY_LENGTH, async ({ sender }) => { - return sender.navigationHistory.length() + return dropdownOptions }) // #endregion navigation history diff --git a/src/renderer/components/top-nav/top-nav.js b/src/renderer/components/top-nav/top-nav.js index cf580cba750c6..27bc4ae01c46a 100644 --- a/src/renderer/components/top-nav/top-nav.js +++ b/src/renderer/components/top-nav/top-nav.js @@ -11,9 +11,6 @@ import { translateWindowTitle } from '../../helpers/strings' import { clearLocalSearchSuggestionsSession, getLocalSearchSuggestions } from '../../helpers/api/local' import { getInvidiousSearchSuggestions } from '../../helpers/api/invidious' -const NAV_HISTORY_DISPLAY_LIMIT = 15 -const HALF_OF_NAV_HISTORY_DISPLAY_LIMIT = Math.floor(NAV_HISTORY_DISPLAY_LIMIT / 2) - export default defineComponent({ name: 'TopNav', components: { @@ -343,52 +340,21 @@ export default defineComponent({ this.showSearchContainer = !this.showSearchContainer }, - getNavigationHistoryResultEndIndex: function (navigationHistoryActiveIndex, navigationHistoryLength) { - if (navigationHistoryActiveIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT) { - return Math.min(navigationHistoryLength - 1, NAV_HISTORY_DISPLAY_LIMIT - 1) - } else if (navigationHistoryLength - navigationHistoryActiveIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT + 1) { - return navigationHistoryLength - 1 - } else { - return navigationHistoryActiveIndex + HALF_OF_NAV_HISTORY_DISPLAY_LIMIT - } - }, - - getNavigationHistoryDropdownOptions: async function (ipcRenderer, navigationHistoryActiveIndex, navigationHistoryLength) { - const dropdownOptions = [] - const end = this.getNavigationHistoryResultEndIndex(navigationHistoryActiveIndex, navigationHistoryLength) - - for (let index = end; index >= Math.max(0, end + 1 - NAV_HISTORY_DISPLAY_LIMIT); --index) { - const routeLabel = await ipcRenderer.invoke(IpcChannels.GET_NAV_HISTORY_ENTRY_TITLE_AT_INDEX, index) - const isActiveIndex = index === navigationHistoryActiveIndex - const dropdownOption = { - label: routeLabel, - value: index - navigationHistoryActiveIndex, - active: isActiveIndex - } - - dropdownOptions.push(dropdownOption) - - if (isActiveIndex) { - this.navigationHistoryDropdownActiveEntry = dropdownOption - } - } - return dropdownOptions - }, - setNavigationHistoryDropdownOptions: async function() { if (process.env.IS_ELECTRON) { const { ipcRenderer } = require('electron') - const navigationHistoryLength = await ipcRenderer.invoke(IpcChannels.GET_NAV_HISTORY_LENGTH) - const navigationHistoryActiveIndex = await ipcRenderer.invoke(IpcChannels.GET_NAV_HISTORY_ACTIVE_INDEX) - this.navigationHistoryDropdownOptions = await this.getNavigationHistoryDropdownOptions(ipcRenderer, navigationHistoryActiveIndex, navigationHistoryLength) + const dropdownOptions = await ipcRenderer.invoke(IpcChannels.GET_NAVIGATION_HISTORY) + + this.navigationHistoryDropdownOptions = dropdownOptions + this.navigationHistoryDropdownActiveEntry = dropdownOptions.find(option => option.active) } }, goToOffset: function (offset) { - if (process.env.IS_ELECTRON) { - const { ipcRenderer } = require('electron') - ipcRenderer.send(IpcChannels.GO_TO_NAV_HISTORY_OFFSET, offset) + // no point navigating to the current route + if (offset !== 0) { + this.$router.go(offset) } },