Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/_locales/dict.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,13 @@ export const commonTranslations: Translations = {
zh_CN: '扩展没有在隐私窗口中运行的权限',
zh_TW: '擴充套件沒有於隱私視窗中執行的權限',
},

// ---
// -- Omnibox
// -
'omnibox.container_switch.prompt': {
en: 'Type the name of the container you want for this tab…',
},
}

if (!window.translations) window.translations = commonTranslations
Expand Down
3 changes: 3 additions & 0 deletions src/bg/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Menu } from 'src/services/menu'
import { WebReq } from 'src/services/web-req'
import { Sync } from 'src/services/_services'
import { Styles } from 'src/services/styles'
import * as omnibox from 'src/services/omnibox'

void (async function main() {
Info.setInstanceType(InstanceType.bg)
Expand Down Expand Up @@ -119,6 +120,8 @@ void (async function main() {
if (newVersion <= currentVersion) browser.runtime.reload()
})

omnibox.setupListeners()

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 @@ -501,5 +501,8 @@
},
"background": {
"page": "bg/background.html"
},
"omnibox": {
"keyword": "="
}
}
74 changes: 74 additions & 0 deletions src/services/omnibox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Containers } from 'src/services/containers'
import { translate } from 'src/dict'
import * as IPC from 'src/services/ipc'
import * as Logs from 'src/services/logs'
import { Tabs } from 'src/services/tabs.bg'
import { Windows } from 'src/services/windows'
import { Container, InstanceType } from 'src/types'

function setupListeners() {
browser.omnibox.setDefaultSuggestion({
description: translate('omnibox.container_switch.prompt'),
})

function matchContainers(input: string): Container[] {
// TODO: order by score of some sort?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Any suggestions on what to do here?

return Object.values(Containers.reactive.byId).filter(container =>
container.name.toLowerCase().includes(input.toLowerCase())
)
}

browser.omnibox.onInputChanged.addListener(async (input, suggest) => {
const suggestions =
input.length >= 3
? matchContainers(input).map(ctx => ({
content: ctx.name,

Check failure on line 25 in src/services/omnibox.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
description: ctx.name,

Check failure on line 26 in src/services/omnibox.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
deletable: false,

Check failure on line 27 in src/services/omnibox.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
}))

Check failure on line 28 in src/services/omnibox.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
: []
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 {
Logs.warn('failed to re-open tabs', activeTabs, 'in container', firstMatchingContainer)
}
})
}

export { setupListeners }
1 change: 1 addition & 0 deletions src/sidebar/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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.onExternalStop,
Expand Down
1 change: 1 addition & 0 deletions src/types/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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
Loading