Skip to content

Commit

Permalink
Make a single IPC call for the navigation history
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue committed Dec 12, 2024
1 parent c686b84 commit c3a6332
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 56 deletions.
5 changes: 1 addition & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
39 changes: 28 additions & 11 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 7 additions & 41 deletions src/renderer/components/top-nav/top-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
}
},

Expand Down

0 comments on commit c3a6332

Please sign in to comment.