From 7e112229c87665e521139699350047d64db96b78 Mon Sep 17 00:00:00 2001 From: trickypr <23250792+trickypr@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:43:26 +1100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Right=20click=20->=20Open=20in=20ne?= =?UTF-8?q?w=20window?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/content/browser/browser.ts | 2 +- .../menus/HamburgerMenu/HamburgerMenu.svelte | 10 ++----- src/content/browser/lib/window/api.ts | 16 +++++++++++- src/content/browser/lib/window/initialize.ts | 10 +++++++ src/content/shared/contextMenus/menuItems.ts | 26 +++++++++---------- src/content/types.d.ts | 2 +- 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/content/browser/browser.ts b/src/content/browser/browser.ts index ac3d85d..49cd7d2 100644 --- a/src/content/browser/browser.ts +++ b/src/content/browser/browser.ts @@ -18,7 +18,7 @@ import './lib/window/api' // .getInterface(Ci.nsIAppWindow).XULBrowserWindow = window.XULBrowserWindow //window.browserDOMWindow = new nsBrowserAccess() -initializeWindow(window.arguments) +initializeWindow(window.arguments && window.arguments[0]) new App({ target: document.body, diff --git a/src/content/browser/components/menus/HamburgerMenu/HamburgerMenu.svelte b/src/content/browser/components/menus/HamburgerMenu/HamburgerMenu.svelte index 08321b1..59c638b 100644 --- a/src/content/browser/components/menus/HamburgerMenu/HamburgerMenu.svelte +++ b/src/content/browser/components/menus/HamburgerMenu/HamburgerMenu.svelte @@ -8,21 +8,15 @@ import HamburgerMenuItem from './HamburgerMenuItem.svelte' import { openTab } from '@browser/lib/window/tabs' + import { windowApi } from '@browser/lib/window/api' const openDialogWindowAction = (url: string) => () => Services.ww.openWindow(window, url, '_blank', 'chrome,dialog=yes,all', null) - - const openChromeWindowAction = (url: string) => () => - Services.ww.openWindow(null, url, '_blank', 'chrome,dialog=no,all', null)
- + windowApi.window.new()}> New Window diff --git a/src/content/browser/lib/window/api.ts b/src/content/browser/lib/window/api.ts index ffb2085..396b95e 100644 --- a/src/content/browser/lib/window/api.ts +++ b/src/content/browser/lib/window/api.ts @@ -3,12 +3,13 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import mitt from 'mitt' +import type { WindowArguments } from '.' import type { ContextMenuInfo } from '../../../../actors/ContextMenu.types' import { browserContextMenuInfo, setContextMenuParentActor, } from './contextMenu' -import { closeTab, openTab, tabs } from './tabs' +import { closeTab, openTab, runOnCurrentTab, setCurrentTab, tabs } from './tabs' export type WindowTriggers = { bookmarkCurrentPage: undefined @@ -16,9 +17,22 @@ export type WindowTriggers = { export const windowApi = { windowTriggers: mitt(), + window: { + new: (args?: WindowArguments) => + Services.ww.openWindow( + // @ts-expect-error Incorrect type generation + null, + Services.prefs.getStringPref('app.content'), + '_blank', + 'chrome,dialog=no,all', + args, + ), + }, tabs: { closeTab, openTab, + runOnCurrentTab, + setCurrentTab, get tabs() { return tabs.readOnce() }, diff --git a/src/content/browser/lib/window/initialize.ts b/src/content/browser/lib/window/initialize.ts index a6ce21d..bda29a1 100644 --- a/src/content/browser/lib/window/initialize.ts +++ b/src/content/browser/lib/window/initialize.ts @@ -6,6 +6,16 @@ import { type WindowArguments, getFullWindowConfiguration } from './arguments' import { openTab } from './tabs' export function initializeWindow(args: WindowArguments | undefined) { + // When opened via nsIWindowWatcher.openWindow, the arguments are + // passed through C++, and they arrive to us wrapped as an XPCOM + // object. We use wrappedJSObject to get at the underlying JS + // object. + // @ts-expect-error Incorrect type generation + if (args instanceof Ci.nsISupports) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + args = (args as any).wrappedJSObject as WindowArguments + } + const configuration = getFullWindowConfiguration(args || {}) // Setup tabs diff --git a/src/content/shared/contextMenus/menuItems.ts b/src/content/shared/contextMenus/menuItems.ts index be56e10..3b5054a 100644 --- a/src/content/shared/contextMenus/menuItems.ts +++ b/src/content/shared/contextMenus/menuItems.ts @@ -8,11 +8,6 @@ import { type ContextMenuInfo, contextMenuParentActor, } from '@browser/lib/window/contextMenu' -import { - openTab, - runOnCurrentTab, - setCurrentTab, -} from '@browser/lib/window/tabs' import { getClipboardHelper } from '@browser/lib/xul/ccWrapper' import type { MenuItemAction, VisibilityCheck } from '.' @@ -41,15 +36,17 @@ const copyProp = onStringValue((value) => { }) const openInNewTab = onStringValue((value) => { - const tab = openTab(resource.NetUtil.newURI(value)) + const tab = window.windowApi.tabs.openTab(resource.NetUtil.newURI(value)) if (Services.prefs.getBoolPref('browser.tabs.newTabFocus')) { - queueMicrotask(() => setCurrentTab(tab)) + queueMicrotask(() => window.windowApi.tabs.setCurrentTab(tab)) } }) -const openInNewWindow = onStringValue(() => { - // TODO -}) +const openInNewWindow = onStringValue((initialUrl) => + window.windowApi.window.new({ + initialUrl: initialUrl, + }), +) const saveImageUrl = onStringValue((value, info) => { if (!info.context.principal) @@ -114,21 +111,24 @@ export const MENU_ITEM_ACTIONS: MenuItemAction[] = ( title: 'Back', visible: ALWAYS, - action: () => runOnCurrentTab((tab) => tab.goBack()), + action: () => + window.windowApi.tabs.runOnCurrentTab((tab) => tab.goBack()), }, { id: 'navigation__forward', title: 'Forward', visible: ALWAYS, - action: () => runOnCurrentTab((tab) => tab.goForward()), + action: () => + window.windowApi.tabs.runOnCurrentTab((tab) => tab.goForward()), }, { id: 'navigation__reload', title: 'Reload', visible: ALWAYS, - action: () => runOnCurrentTab((tab) => tab.reload()), + action: () => + window.windowApi.tabs.runOnCurrentTab((tab) => tab.reload()), }, { id: 'navigation__bookmark', diff --git a/src/content/types.d.ts b/src/content/types.d.ts index 38c3e65..fdf1dc8 100644 --- a/src/content/types.d.ts +++ b/src/content/types.d.ts @@ -20,7 +20,7 @@ declare interface Window { * * @see {@link file://./browser/lib/window/arguments.ts} */ - arguments?: import('./browser/lib/window/arguments').WindowArguments + arguments?: [import('./browser/lib/window/arguments').WindowArguments] } declare interface NodeModule {