Skip to content

Commit

Permalink
WIP: add sb omnibox handler, wire it up to reopening in container
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Sep 7, 2024
1 parent d74c2f7 commit 3f89a75
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/bg/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InstanceType, SavedGroup, Stored, TabSessionData, UpgradeMsg } from 'src/types'
import { Container, InstanceType, SavedGroup, Stored, TabSessionData, UpgradeMsg } from 'src/types'
import { UpgradingState } from 'src/types'
import { DEFAULT_SETTINGS, GROUP_URL, NOID, URL_URL, V4_GROUP_URL_LEN } from 'src/defaults'
import { V4_URL_URL_LEN } from 'src/defaults'
Expand Down Expand Up @@ -123,6 +123,66 @@ void (async function main() {
if (newVersion <= currentVersion) browser.runtime.reload()
})

browser.omnibox.setDefaultSuggestion({ description: 'brooo' })

function matchContainers(input: string): Container[] {
// TODO: order by score of some sort?
// TODO: At the very least, put an exact match first.
// TODO: Validate we want Sidebery records, not Firefox records.
return Object.values(Containers.reactive.byId).filter(container =>
container.name.toLowerCase().includes(input.toLowerCase())
)
}

browser.omnibox.onInputChanged.addListener(async (input, suggest) => {
const suggestions = matchContainers(input).map(ctx => ({
content: ctx.name,
description: ctx.name,
deletable: false,
}))
suggest(suggestions)
})

browser.omnibox.onInputEntered.addListener(async (input, _disposition) => {
// NOTE: We're semantically _re-opening_ tabs, which conflicts with a disposition. Ignore it.

if (!Windows.lastFocusedWinId) {
Logs.err('omnibox: no last focused window ID found')
return
}

const matchingContainers = matchContainers(input)
if (matchingContainers.length <= 0) {
Logs.warn('omnibox: no matching containers found')
return
}
const firstMatchingContainer = matchingContainers[0]

const sidebarTabs = await Tabs.getSidebarTabs(Windows.lastFocusedWinId)
if (!sidebarTabs) {
Logs.err('omnibox: no sidebar tabs found for last focused window ID')
return
}

const con = IPC.getConnection(InstanceType.sidebar, Windows.lastFocusedWinId)
if ((con?.localPort && con.localPort.error) || (con?.remotePort && con.remotePort.error)) {
Logs.err('need to fall back to creating tabs by hand')
return
}

const activeTabs = sidebarTabs.filter(tab => tab.active)
try {
await IPC.sidebar(
Windows.lastFocusedWinId,
'reopenInContainer',
activeTabs.map(tab => tab.id),
firstMatchingContainer.id
)
} catch {
console.warn('failed to re-open tabs', activeTabs, 'in container', firstMatchingContainer)
}
})

Logs.info(`Init end: ${performance.now() - ts}ms`)
})()

Expand Down
3 changes: 3 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,8 @@
},
"background": {
"page": "bg/background.html"
},
"omnibox": {
"keyword": "sb"
}
}
1 change: 1 addition & 0 deletions src/sidebar/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function main(): Promise<void> {
getTabsTreeData: Tabs.getTabsTreeData,
moveTabsToThisWin: Tabs.moveToThisWin,
openTabs: Tabs.open,
reopenInContainer: Tabs.reopenInContainer,
handleReopening: Tabs.handleReopening,
getActivePanelConfig: Sidebar.getActivePanelConfig,
stopDrag: DnD.reset,
Expand Down
1 change: 1 addition & 0 deletions src/types/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type SidebarActions = {

moveTabsToThisWin: (tabs: Tab[], dst?: DstPlaceInfo) => Promise<boolean>
openTabs: (items: ItemInfo[], dst: DstPlaceInfo) => Promise<boolean>
reopenInContainer: (ids: ID[], containerId: string) => Promise<void>

notify: (config: Notification, timeout?: number) => void
notifyAboutNewSnapshot: () => void
Expand Down

0 comments on commit 3f89a75

Please sign in to comment.