Skip to content

Commit

Permalink
🐛 Fix ext-browser for new window api (#58)
Browse files Browse the repository at this point in the history
Small bug fix pr. It:
- Removes references to the old `windowApi` object
- Improves type inferance
- Fixes a bug not caught by old type inferance
  • Loading branch information
trickypr authored Apr 26, 2024
1 parent bda4e07 commit 7e02b35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
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

0 comments on commit 7e02b35

Please sign in to comment.