Skip to content

Commit ea8ef1f

Browse files
feat: add sb omnibox handler, wire it up to reopening in container
1 parent 85e2b00 commit ea8ef1f

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'
@@ -119,6 +119,66 @@ void (async function main() {
119119
if (newVersion <= currentVersion) browser.runtime.reload()
120120
})
121121

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

src/manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,5 +492,8 @@
492492
},
493493
"background": {
494494
"page": "bg/background.html"
495+
},
496+
"omnibox": {
497+
"keyword": "sb"
495498
}
496499
}

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.onExternalStop,

src/types/ipc.ts

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

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

118119
notify: (config: Notification, timeout?: number) => void
119120
notifyAboutNewSnapshot: () => void

0 commit comments

Comments
 (0)