Skip to content

Commit

Permalink
✨ Right click -> Open in new window
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Dec 8, 2023
1 parent 69b7d78 commit 7e11222
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/content/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</script>

<xul:panel class="panel" id="hamburgerMenu">
<div class="panel__container">
<HamburgerMenuItem
on:click={openChromeWindowAction(
Services.prefs.getStringPref('app.content'),
)}
>
<HamburgerMenuItem on:click={() => windowApi.window.new()}>
New Window
</HamburgerMenuItem>

Expand Down
16 changes: 15 additions & 1 deletion src/content/browser/lib/window/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@
* 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
}

export const windowApi = {
windowTriggers: mitt<WindowTriggers>(),
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()
},
Expand Down
10 changes: 10 additions & 0 deletions src/content/browser/lib/window/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions src/content/shared/contextMenus/menuItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/content/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7e11222

Please sign in to comment.