Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix ext-browser for new window api #58

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions apps/extensions/lib/parent/ext-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ const lazy = lazyESModuleGetters({

class TabTracker extends TabTrackerBase {
get activeTab() {
/** @type {any | null} */
const window = lazy.WindowTracker.getActiveWindow()
return window?.windowApi?.tabs?.getCurrentTab()
return window?.activeTab()
}

init() {
Expand All @@ -30,30 +29,34 @@ class TabTracker extends TabTrackerBase {
}

/**
* @param {*} nativeTab
* @param {import('@browser/tabs').WindowTab} nativeTab
*/
getId(nativeTab) {
return nativeTab.getTabId()
return nativeTab.view.browserId || -1
}

/**
* @param {number} tabId
* @param {import('@browser/tabs').WindowTab} default_
*/
getTab(tabId, default_) {
const { tab } = lazy.WindowTracker.getWindowWithBrowser(tabId) || {
const { tab } = lazy.WindowTracker.getWindowWithBrowserId(tabId) || {
tab: default_,
}

return tab
}

/**
* @param {XULBrowserElement} browser
*/
getBrowserData(browser) {
const data = lazy.WindowTracker.getWindowWithBrowser(browser)
if (!data) return { windowId: -1, tabId: -1 }

return {
/** @type {number} */
// @ts-expect-error bad imported types
windowId: data.window.windowApi.id,
/** @type {number} */
tabId: data.tab.getTabId(),
windowId: data.window.windowId,
tabId: data.tab.view.browserId || -1,
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions apps/modules/lib/BrowserWindowTracker.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ export const WindowTracker = {
return this.registeredWindows.get(wid)
},

getWindowWithBrowser(browser) {
getWindowWithBrowserId(browserId) {
for (const window of this.registeredWindows.values()) {
const tab = window
.windowTabs()
.find((t) => t.view.browserId === browser.browserId)
.find((t) => t.view.browserId === browserId)
if (tab) return { window, tab }
}
return null
},

getWindowWithBrowser(browser) {
return this.getWindowWithBrowserId(browser.browserId)
},

focusWindow(id) {
this.activeWindow = id
this.events.emit('focus', this.getActiveWindow())
Expand Down
8 changes: 5 additions & 3 deletions libs/link/types/modules/BrowserWindowTracker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
declare module 'resource://app/modules/BrowserWindowTracker.sys.mjs' {
import type { Emitter } from 'resource://app/modules/mitt.sys.mjs'

// TODO: Replace this with the correct tab type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Tab = any
type Tab = import('@browser/tabs').WindowTab

export type WindowTrackerEvents = {
windowCreated: Window & typeof globalThis
Expand Down Expand Up @@ -35,6 +33,10 @@ declare module 'resource://app/modules/BrowserWindowTracker.sys.mjs' {

getWindowById(wid: number): typeof window | undefined

getWindowWithBrowserId(
browserId: number,
): { window: Window & typeof globalThis; tab: Tab } | null

getWindowWithBrowser(
browser: XULBrowserElement,
): { window: Window & typeof globalThis; tab: Tab } | null
Expand Down
Loading