Skip to content

Commit c3a6332

Browse files
committed
Make a single IPC call for the navigation history
1 parent c686b84 commit c3a6332

File tree

3 files changed

+36
-56
lines changed

3 files changed

+36
-56
lines changed

src/constants.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ const IpcChannels = {
55
OPEN_EXTERNAL_LINK: 'open-external-link',
66
GET_SYSTEM_LOCALE: 'get-system-locale',
77
GET_PICTURES_PATH: 'get-pictures-path',
8-
GET_NAV_HISTORY_ENTRY_TITLE_AT_INDEX: 'get-navigation-history-entry-at-index',
9-
GET_NAV_HISTORY_ACTIVE_INDEX: 'get-navigation-history-active-index',
10-
GET_NAV_HISTORY_LENGTH: 'get-navigation-history-length',
11-
GO_TO_NAV_HISTORY_OFFSET: 'go-to-navigation-history-index',
8+
GET_NAVIGATION_HISTORY: 'get-navigation-history',
129
SHOW_OPEN_DIALOG: 'show-open-dialog',
1310
SHOW_SAVE_DIALOG: 'show-save-dialog',
1411
STOP_POWER_SAVE_BLOCKER: 'stop-power-save-blocker',

src/main/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -898,20 +898,37 @@ function runApp() {
898898

899899
// #region navigation history
900900

901-
ipcMain.on(IpcChannels.GO_TO_NAV_HISTORY_OFFSET, ({ sender }, offset) => {
902-
sender.navigationHistory.goToOffset(offset)
903-
})
901+
const NAV_HISTORY_DISPLAY_LIMIT = 15
902+
// Math.trunc but with a bitwise OR so that it can be calcuated at build time and the number inlined
903+
const HALF_OF_NAV_HISTORY_DISPLAY_LIMIT = (NAV_HISTORY_DISPLAY_LIMIT / 2) | 0
904904

905-
ipcMain.handle(IpcChannels.GET_NAV_HISTORY_ENTRY_TITLE_AT_INDEX, async ({ sender }, index) => {
906-
return sender.navigationHistory.getEntryAtIndex(index)?.title
907-
})
905+
ipcMain.handle(IpcChannels.GET_NAVIGATION_HISTORY, ({ sender }) => {
906+
const activeIndex = sender.navigationHistory.getActiveIndex()
907+
const length = sender.navigationHistory.length()
908908

909-
ipcMain.handle(IpcChannels.GET_NAV_HISTORY_ACTIVE_INDEX, async ({ sender }) => {
910-
return sender.navigationHistory.getActiveIndex()
911-
})
909+
let end
910+
911+
if (activeIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT) {
912+
end = Math.min(length - 1, NAV_HISTORY_DISPLAY_LIMIT - 1)
913+
} else if (length - activeIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT + 1) {
914+
end = length - 1
915+
} else {
916+
end = activeIndex + HALF_OF_NAV_HISTORY_DISPLAY_LIMIT
917+
}
918+
919+
const dropdownOptions = []
920+
921+
for (let index = end; index >= Math.max(0, end + 1 - NAV_HISTORY_DISPLAY_LIMIT); --index) {
922+
const routeLabel = sender.navigationHistory.getEntryAtIndex(index)?.title
923+
924+
dropdownOptions.push({
925+
label: routeLabel,
926+
value: index - activeIndex,
927+
active: index === activeIndex
928+
})
929+
}
912930

913-
ipcMain.handle(IpcChannels.GET_NAV_HISTORY_LENGTH, async ({ sender }) => {
914-
return sender.navigationHistory.length()
931+
return dropdownOptions
915932
})
916933

917934
// #endregion navigation history

src/renderer/components/top-nav/top-nav.js

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import { translateWindowTitle } from '../../helpers/strings'
1111
import { clearLocalSearchSuggestionsSession, getLocalSearchSuggestions } from '../../helpers/api/local'
1212
import { getInvidiousSearchSuggestions } from '../../helpers/api/invidious'
1313

14-
const NAV_HISTORY_DISPLAY_LIMIT = 15
15-
const HALF_OF_NAV_HISTORY_DISPLAY_LIMIT = Math.floor(NAV_HISTORY_DISPLAY_LIMIT / 2)
16-
1714
export default defineComponent({
1815
name: 'TopNav',
1916
components: {
@@ -343,52 +340,21 @@ export default defineComponent({
343340
this.showSearchContainer = !this.showSearchContainer
344341
},
345342

346-
getNavigationHistoryResultEndIndex: function (navigationHistoryActiveIndex, navigationHistoryLength) {
347-
if (navigationHistoryActiveIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT) {
348-
return Math.min(navigationHistoryLength - 1, NAV_HISTORY_DISPLAY_LIMIT - 1)
349-
} else if (navigationHistoryLength - navigationHistoryActiveIndex < HALF_OF_NAV_HISTORY_DISPLAY_LIMIT + 1) {
350-
return navigationHistoryLength - 1
351-
} else {
352-
return navigationHistoryActiveIndex + HALF_OF_NAV_HISTORY_DISPLAY_LIMIT
353-
}
354-
},
355-
356-
getNavigationHistoryDropdownOptions: async function (ipcRenderer, navigationHistoryActiveIndex, navigationHistoryLength) {
357-
const dropdownOptions = []
358-
const end = this.getNavigationHistoryResultEndIndex(navigationHistoryActiveIndex, navigationHistoryLength)
359-
360-
for (let index = end; index >= Math.max(0, end + 1 - NAV_HISTORY_DISPLAY_LIMIT); --index) {
361-
const routeLabel = await ipcRenderer.invoke(IpcChannels.GET_NAV_HISTORY_ENTRY_TITLE_AT_INDEX, index)
362-
const isActiveIndex = index === navigationHistoryActiveIndex
363-
const dropdownOption = {
364-
label: routeLabel,
365-
value: index - navigationHistoryActiveIndex,
366-
active: isActiveIndex
367-
}
368-
369-
dropdownOptions.push(dropdownOption)
370-
371-
if (isActiveIndex) {
372-
this.navigationHistoryDropdownActiveEntry = dropdownOption
373-
}
374-
}
375-
return dropdownOptions
376-
},
377-
378343
setNavigationHistoryDropdownOptions: async function() {
379344
if (process.env.IS_ELECTRON) {
380345
const { ipcRenderer } = require('electron')
381-
const navigationHistoryLength = await ipcRenderer.invoke(IpcChannels.GET_NAV_HISTORY_LENGTH)
382-
const navigationHistoryActiveIndex = await ipcRenderer.invoke(IpcChannels.GET_NAV_HISTORY_ACTIVE_INDEX)
383346

384-
this.navigationHistoryDropdownOptions = await this.getNavigationHistoryDropdownOptions(ipcRenderer, navigationHistoryActiveIndex, navigationHistoryLength)
347+
const dropdownOptions = await ipcRenderer.invoke(IpcChannels.GET_NAVIGATION_HISTORY)
348+
349+
this.navigationHistoryDropdownOptions = dropdownOptions
350+
this.navigationHistoryDropdownActiveEntry = dropdownOptions.find(option => option.active)
385351
}
386352
},
387353

388354
goToOffset: function (offset) {
389-
if (process.env.IS_ELECTRON) {
390-
const { ipcRenderer } = require('electron')
391-
ipcRenderer.send(IpcChannels.GO_TO_NAV_HISTORY_OFFSET, offset)
355+
// no point navigating to the current route
356+
if (offset !== 0) {
357+
this.$router.go(offset)
392358
}
393359
},
394360

0 commit comments

Comments
 (0)