Skip to content

Commit 49a6bac

Browse files
feat: add sb omnibox handler, wire it up to reopening in container
1 parent bfef24d commit 49a6bac

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/bg/background.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InstanceType } from 'src/types'
1+
import { Containers, InstanceType } from 'src/types'
22
import { NOID } from 'src/defaults'
33
import * as IPC from 'src/services/ipc'
44
import * as Logs from 'src/services/logs'
@@ -116,6 +116,66 @@ void (async function main() {
116116
if (newVersion <= currentVersion) browser.runtime.reload()
117117
})
118118

119+
browser.omnibox.setDefaultSuggestion({ description: 'Type the name of the container you want for this tab…' })
120+
121+
function matchContainers(input: string): Container[] {
122+
// TODO: order by score of some sort?
123+
// TODO: At the very least, put an exact match first.
124+
// TODO: Validate we want Sidebery records, not Firefox records.
125+
return Object.values(Containers.reactive.byId).filter(container =>
126+
container.name.toLowerCase().includes(input.toLowerCase())
127+
)
128+
}
129+
130+
browser.omnibox.onInputChanged.addListener(async (input, suggest) => {
131+
const suggestions = matchContainers(input).map(ctx => ({
132+
content: ctx.name,
133+
description: ctx.name,
134+
deletable: false,
135+
}))
136+
suggest(suggestions)
137+
})
138+
139+
browser.omnibox.onInputEntered.addListener(async (input, _disposition) => {
140+
// NOTE: We're semantically _re-opening_ tabs, which conflicts with a disposition. Ignore it.
141+
142+
if (!Windows.lastFocusedWinId) {
143+
Logs.err('omnibox: no last focused window ID found')
144+
return
145+
}
146+
147+
const matchingContainers = matchContainers(input)
148+
if (matchingContainers.length <= 0) {
149+
Logs.warn('omnibox: no matching containers found')
150+
return
151+
}
152+
const firstMatchingContainer = matchingContainers[0]
153+
154+
const sidebarTabs = await Tabs.getSidebarTabs(Windows.lastFocusedWinId)
155+
if (!sidebarTabs) {
156+
Logs.err('omnibox: no sidebar tabs found for last focused window ID')
157+
return
158+
}
159+
160+
const con = IPC.getConnection(InstanceType.sidebar, Windows.lastFocusedWinId)
161+
if ((con?.localPort && con.localPort.error) || (con?.remotePort && con.remotePort.error)) {
162+
Logs.err('need to fall back to creating tabs by hand')
163+
return
164+
}
165+
166+
const activeTabs = sidebarTabs.filter(tab => tab.active)
167+
try {
168+
await IPC.sidebar(
169+
Windows.lastFocusedWinId,
170+
'reopenInContainer',
171+
activeTabs.map(tab => tab.id),
172+
firstMatchingContainer.id
173+
)
174+
} catch {
175+
console.warn('failed to re-open tabs', activeTabs, 'in container', firstMatchingContainer)
176+
}
177+
})
178+
119179
Logs.info(`Init end: ${performance.now() - ts}ms`)
120180
})()
121181

src/manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,5 +465,8 @@
465465
},
466466
"background": {
467467
"page": "bg/background.html"
468+
},
469+
"omnibox": {
470+
"keyword": "sb"
468471
}
469472
}

src/sidebar/sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ async function main(): Promise<void> {
4343
getTabsTreeData: Tabs.getTabsTreeData,
4444
moveTabsToThisWin: Tabs.moveToThisWin,
4545
openTabs: Tabs.open,
46+
reopenInContainer: Tabs.reopenInContainer,
4647
handleReopening: Tabs.handleReopening,
4748
getActivePanelConfig: Sidebar.getActivePanelConfig,
4849
stopDrag: DnD.reset,

src/types/ipc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export type SidebarActions = {
111111

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

115116
notify: (config: Notification, timeout?: number) => void
116117
notifyAboutNewSnapshot: () => void

0 commit comments

Comments
 (0)