collin/browser-terminal-extension · 5974c60c
Rank the showing tab last rather than dropping it
Collin Richards · 2026-08-19 06:44 UTC · 5974c60cbc8a8cafaa8b7880d711b1634cb28578 · parent 233a77a4 · browse files
modifiedextension/sidebar.js+22 −8
| ⋯ 3551 unchanged lines | |||
| 3552 | 3552 | * Asked for when the box takes focus, and on every tab event while the list is | |
| 3553 | 3553 | * showing — which is what keeps a row from offering a tab that closed under it. | |
| 3554 | 3554 | * Not asked for otherwise: with the list down there is nothing to be stale. | |
| 3555 | + | * | |
| 3556 | + | * `panelWindowId` is the window the panel belongs to, and it is the answer | |
| 3557 | + | * everywhere else in this file — but it arrives from a `windows.getCurrent()` | |
| 3558 | + | * that resolves some time after load, and a box focused before then would get | |
| 3559 | + | * no rows and no second chance. `currentWindow` is the same window asked for a | |
| 3560 | + | * different way, and it is available immediately, so it stands in until the id | |
| 3561 | + | * is known rather than the question going unasked. | |
| 3555 | 3562 | */ | |
| 3556 | 3563 | function syncOmniTabs() { | |
| 3557 | - | if (panelWindowId == null) return; | |
| 3558 | 3564 | api.tabs | |
| 3559 | - | .query({ windowId: panelWindowId }) | |
| 3565 | + | .query(panelWindowId == null ? { currentWindow: true } : { windowId: panelWindowId }) | |
| 3560 | 3566 | .then((tabs) => { | |
| 3561 | 3567 | lastTabs = tabs; | |
| 3562 | 3568 | if (!$("omni-list").hidden) refreshOmni(); | |
| ⋯ 8 unchanged lines | |||
| 3571 | 3577 | api.tabs.onRemoved.addListener(repaintOmniTabs); | |
| 3572 | 3578 | api.tabs.onUpdated.addListener(repaintOmniTabs); | |
| 3573 | 3579 | api.tabs.onActivated.addListener(repaintOmniTabs); | |
| 3580 | + | // Asked once at load as well, so the first thing typed into the box has tabs to | |
| 3581 | + | // match against without waiting on a focus event that may already have happened. | |
| 3582 | + | syncOmniTabs(); | |
| 3574 | 3583 | ||
| 3575 | 3584 | /** | |
| 3576 | 3585 | * A URL as the box should match and show it: the scheme, a leading `www.` and a | |
| ⋯ 45 unchanged lines | |||
| 3622 | 3631 | * would leave the terminal behind in this one, which is a strange thing for the | |
| 3623 | 3632 | * terminal's own box to offer. | |
| 3624 | 3633 | * | |
| 3625 | - | * Titles and URLs are page-controlled, which is the same footing tmux names are | |
| 3626 | - | * on: both reach the DOM through textContent and nothing else. | |
| 3634 | + | * Every tab in the window, the showing one included — see the score. Titles and | |
| 3635 | + | * URLs are page-controlled, which is the same footing tmux names are on: both | |
| 3636 | + | * reach the DOM through textContent and nothing else. | |
| 3627 | 3637 | * | |
| 3628 | 3638 | * @param {string} q already lowercased and trimmed | |
| 3629 | 3639 | * @returns {TbOmniItem[]} | |
| ⋯ 3 unchanged lines | |||
| 3633 | 3643 | const out = []; | |
| 3634 | 3644 | for (const tab of lastTabs) { | |
| 3635 | 3645 | const id = tab.id; | |
| 3636 | - | // The showing tab is left out for the reason the current session is: going | |
| 3637 | - | // there is where you already are. | |
| 3638 | - | if (id == null || tab.active) continue; | |
| 3646 | + | if (id == null) continue; | |
| 3639 | 3647 | const where = tabAddress(tab.url ?? ""); | |
| 3640 | 3648 | const score = bestScore(omniScore(tab.title ?? "", q), omniScore(where, q)); | |
| 3641 | 3649 | if (score < 0) continue; | |
| ⋯ 1 unchanged line | |||
| 3643 | 3651 | kind: "tab", | |
| 3644 | 3652 | label: tab.title || where, | |
| 3645 | 3653 | meta: where, | |
| 3646 | - | score: score + OMNI_TAB_PENALTY, | |
| 3654 | + | // The showing tab is a worse answer than any other equally good match — | |
| 3655 | + | // it is the one place you can already see — but it is still an answer, | |
| 3656 | + | // and the same +100 the current window takes rather than the omission the | |
| 3657 | + | // current session takes. A session you are on cannot be typed at by | |
| 3658 | + | // accident; the address of the page in front of you very much can, and | |
| 3659 | + | // being told there is no such tab is the box looking broken. | |
| 3660 | + | score: score + OMNI_TAB_PENALTY + (tab.active ? 100 : 0), | |
| 3647 | 3661 | // Showing the tab is the whole of it. The keyboard stays where `runOmni` | |
| 3648 | 3662 | // puts it — a panel cannot hand focus to the page — and if the tab is | |
| 3649 | 3663 | // pinned, showing it moves the terminal too: the same forward pin that a | |
| ⋯ 2450 unchanged lines | |||