anvilsign in

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
35523552 * Asked for when the box takes focus, and on every tab event while the list is
35533553 * showing — which is what keeps a row from offering a tab that closed under it.
35543554 * 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.
35553562 */
35563563 function syncOmniTabs() {
3557- if (panelWindowId == null) return;
35583564 api.tabs
3559- .query({ windowId: panelWindowId })
3565+ .query(panelWindowId == null ? { currentWindow: true } : { windowId: panelWindowId })
35603566 .then((tabs) => {
35613567 lastTabs = tabs;
35623568 if (!$("omni-list").hidden) refreshOmni();
⋯ 8 unchanged lines
35713577 api.tabs.onRemoved.addListener(repaintOmniTabs);
35723578 api.tabs.onUpdated.addListener(repaintOmniTabs);
35733579 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();
35743583
35753584 /**
35763585 * A URL as the box should match and show it: the scheme, a leading `www.` and a
⋯ 45 unchanged lines
36223631 * would leave the terminal behind in this one, which is a strange thing for the
36233632 * terminal's own box to offer.
36243633 *
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.
36273637 *
36283638 * @param {string} q already lowercased and trimmed
36293639 * @returns {TbOmniItem[]}
⋯ 3 unchanged lines
36333643 const out = [];
36343644 for (const tab of lastTabs) {
36353645 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;
36393647 const where = tabAddress(tab.url ?? "");
36403648 const score = bestScore(omniScore(tab.title ?? "", q), omniScore(where, q));
36413649 if (score < 0) continue;
⋯ 1 unchanged line
36433651 kind: "tab",
36443652 label: tab.title || where,
36453653 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),
36473661 // Showing the tab is the whole of it. The keyboard stays where `runOmni`
36483662 // puts it — a panel cannot hand focus to the page — and if the tab is
36493663 // pinned, showing it moves the terminal too: the same forward pin that a
⋯ 2450 unchanged lines