Skip to content

Commit

Permalink
Change CREATE_NEW_WINDOW IPC call to accept a path and query
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue committed Jan 16, 2025
1 parent 7ad5873 commit aff18dd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
49 changes: 35 additions & 14 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ function runApp() {
}
}

const ROOT_APP_URL = process.env.NODE_ENV === 'development' ? 'http://localhost:9080' : 'app://bundle/index.html'

async function createWindow(
{
replaceMainWindow = true,
Expand Down Expand Up @@ -774,18 +776,10 @@ function runApp() {
}

// load root file/url
if (process.env.NODE_ENV === 'development') {
let devStartupURL = 'http://localhost:9080'
if (windowStartupUrl != null) {
devStartupURL = windowStartupUrl
}
newWindow.loadURL(devStartupURL)
if (windowStartupUrl != null) {
newWindow.loadURL(windowStartupUrl)
} else {
if (windowStartupUrl != null) {
newWindow.loadURL(windowStartupUrl)
} else {
newWindow.loadURL('app://bundle/index.html')
}
newWindow.loadURL(ROOT_APP_URL)
}

if (typeof searchQueryText === 'string' && searchQueryText.length > 0) {
Expand Down Expand Up @@ -1011,12 +1005,39 @@ function runApp() {
return powerSaveBlocker.start('prevent-display-sleep')
})

ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, (_e, { windowStartupUrl = null, searchQueryText = null } = { }) => {
ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, (event, path, query, searchQueryText) => {
if (!isFreeTubeUrl(event.senderFrame.url)) {
return
}

if (path == null && query == null && searchQueryText == null) {
createWindow({ replaceMainWindow: false, showWindowNow: true })
return
}

if (
typeof path !== 'string' ||
(typeof query !== 'object' && query != null) ||
(typeof searchQueryText !== 'string' && searchQueryText != null)
) {
return
}

if (path.charAt(0) !== '/') {
path = `/${path}`
}

let windowStartupUrl = `${ROOT_APP_URL}#${path}`

if (query) {
windowStartupUrl += '?' + new URLSearchParams(query).toString()
}

createWindow({
replaceMainWindow: false,
showWindowNow: true,
windowStartupUrl: windowStartupUrl,
searchQueryText: searchQueryText
windowStartupUrl,
searchQueryText
})
})

Expand Down
11 changes: 2 additions & 9 deletions src/renderer/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,11 @@ export async function openExternalLink(url) {
* @param {object} params.query the query params to use (optional)
* @param {string} params.searchQueryText the text to show in the search bar in the new window (optional)
*/
export function openInternalPath({ path, query = {}, doCreateNewWindow, searchQueryText = null }) {
export function openInternalPath({ path, query = undefined, doCreateNewWindow, searchQueryText = null }) {
if (process.env.IS_ELECTRON && doCreateNewWindow) {
const { ipcRenderer } = require('electron')

// Combine current document path and new "hash" as new window startup URL
const newWindowStartupURL = new URL(window.location.href)
newWindowStartupURL.hash = `${path}?${(new URLSearchParams(query)).toString()}`

ipcRenderer.send(IpcChannels.CREATE_NEW_WINDOW, {
windowStartupUrl: newWindowStartupURL.toString(),
searchQueryText
})
ipcRenderer.send(IpcChannels.CREATE_NEW_WINDOW, path, query, searchQueryText)
} else {
router.push({
path,
Expand Down

0 comments on commit aff18dd

Please sign in to comment.