Skip to content

Commit 3f89a75

Browse files
WIP: add sb omnibox handler, wire it up to reopening in container
1 parent d74c2f7 commit 3f89a75

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, SavedGroup, Stored, TabSessionData, UpgradeMsg } from 'src/types'
1+
import { Container, InstanceType, SavedGroup, Stored, TabSessionData, UpgradeMsg } from 'src/types'
22
import { UpgradingState } from 'src/types'
33
import { DEFAULT_SETTINGS, GROUP_URL, NOID, URL_URL, V4_GROUP_URL_LEN } from 'src/defaults'
44
import { V4_URL_URL_LEN } from 'src/defaults'
@@ -123,6 +123,66 @@ void (async function main() {
123123
if (newVersion <= currentVersion) browser.runtime.reload()
124124
})
125125

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

src/manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,5 +443,8 @@
443443
},
444444
"background": {
445445
"page": "bg/background.html"
446+
},
447+
"omnibox": {
448+
"keyword": "sb"
446449
}
447450
}

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
@@ -95,6 +95,7 @@ export type SidebarActions = {
9595

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

99100
notify: (config: Notification, timeout?: number) => void
100101
notifyAboutNewSnapshot: () => void

0 commit comments

Comments
 (0)