Skip to content

Commit 98bc686

Browse files
arvsrnAarav Sareen
andauthored
feat(app): browser address bar and recent pages in the side panel New tab (#66818)
Co-authored-by: Aarav Sareen <arvsrn@Aaravs-MacBook-Pro.local>
1 parent 31a291b commit 98bc686

15 files changed

Lines changed: 967 additions & 553 deletions

File tree

‎packages/app/component-tests/browser-pane-restore.spec.ts‎

Lines changed: 80 additions & 64 deletions
Large diffs are not rendered by default.

‎packages/app/src/runtime/plugin/new-tab-machine.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { SessionPane } from "@/shell/state/session-tabs"
33
import {
44
createNewTabsState,
55
selectHasNewTabs,
6+
selectNewTabFocus,
67
selectPaneNewTabs,
78
selectShownNewTab,
89
transitionNewTabs,
@@ -198,6 +199,33 @@ describe("new tab machine", () => {
198199
expect(last.emptied).toBe(0)
199200
})
200201

202+
test("a New tab the person opens takes the keyboard once, and keeps the request while it shows", () => {
203+
const region: Region = { regionActive: "file://a.ts", side: { active: "file://a.ts", keys: ["file://a.ts"] } }
204+
const opened = run([sync(region), { type: "open", pane: "side" }, sync(region)]).state
205+
206+
expect(selectNewTabFocus(opened, "side")).toBe(true)
207+
expect(selectNewTabFocus(transitionNewTabs(opened, { type: "focus.taken" }).state, "side")).toBe(false)
208+
})
209+
210+
test("opening the side region takes the keyboard into the New tab it opens on, not into a tab", () => {
211+
const empty = run([sync({}), { type: "focus.requested", pane: "side" }, sync({ sideOpen: true })]).state
212+
213+
expect(shown(empty, { sideOpen: true }, "side")).toBe("new:1")
214+
expect(selectNewTabFocus(empty, "side")).toBe(true)
215+
216+
const tab: Region = { sideOpen: true, regionActive: "file://a.ts", side: { active: "file://a.ts", keys: ["file://a.ts"] } }
217+
const onTab = run([sync({ ...tab, sideOpen: false }), { type: "focus.requested", pane: "side" }, sync(tab)]).state
218+
219+
expect(selectNewTabFocus(onTab, "side")).toBe(false)
220+
})
221+
222+
test("a New tab that opens by itself, as another session routes into an open side region, takes no keyboard", () => {
223+
const routed = run([sync({ sideOpen: true }), sync({ session: "s2", sideOpen: true })]).state
224+
225+
expect(shown(routed, { session: "s2", sideOpen: true }, "side")).toBe("new:2")
226+
expect(selectNewTabFocus(routed, "side")).toBe(false)
227+
})
228+
201229
test("a sync that changes nothing returns the same state", () => {
202230
const region: Region = { side: { active: "a", keys: ["a"] }, regionActive: "a" }
203231
const state = run([sync(region)]).state

‎packages/app/src/runtime/plugin/new-tab-machine.ts‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export type NewTabsState = {
3636
/** The current session is in an open-and-empty span of the side region, which opened its New tab once. */
3737
readonly openedEmpty: boolean
3838
readonly panes: Readonly<Record<SessionPane, PaneNewTabs>>
39+
/**
40+
* The pane whose New tab the person just asked for, with + or ⌘T or by opening the side region: it gives the keyboard
41+
* to its first field once that renders. A New tab that opens by itself, as when another session routes into an open
42+
* side region, leaves the keyboard where it is.
43+
*/
44+
readonly focus: SessionPane | undefined
3945
/** The region as the last `sync` saw it, which the next one compares against. */
4046
readonly snapshot: RegionTabSnapshot | undefined
4147
}
@@ -53,6 +59,10 @@ export type NewTabsEvent =
5359
| { readonly type: "open.selected"; readonly pane: SessionPane }
5460
/** `empty`: the strip holds no tab, so closing the last New tab empties the region. */
5561
| { readonly type: "dismiss"; readonly id: string; readonly empty: boolean }
62+
/** The person opens the side region: the New tab it shows, if any, takes the keyboard. */
63+
| { readonly type: "focus.requested"; readonly pane: SessionPane }
64+
/** The New tab gave the keyboard to its field. */
65+
| { readonly type: "focus.taken" }
5666

5767
/** The last New tab of a strip with no tabs closed: the region closes, as closing its last tab does. */
5868
export type NewTabsEffect = { readonly type: "emptied" }
@@ -63,13 +73,18 @@ export type NewTabsTransition = {
6373
}
6474

6575
export function createNewTabsState(): NewTabsState {
66-
return { serial: 0, session: "", openedEmpty: false, panes: emptyPanes(), snapshot: undefined }
76+
return { serial: 0, session: "", openedEmpty: false, panes: emptyPanes(), focus: undefined, snapshot: undefined }
6777
}
6878

6979
export function transitionNewTabs(state: NewTabsState, event: NewTabsEvent): NewTabsTransition {
7080
if (event.type === "sync") return { state: sync(state, event.snapshot), effects: [] }
7181

72-
if (event.type === "open") return { state: open(state, event.pane), effects: [] }
82+
if (event.type === "open") return { state: { ...open(state, event.pane), focus: event.pane }, effects: [] }
83+
84+
if (event.type === "focus.requested")
85+
return { state: state.focus === event.pane ? state : { ...state, focus: event.pane }, effects: [] }
86+
87+
if (event.type === "focus.taken") return { state: state.focus ? { ...state, focus: undefined } : state, effects: [] }
7388

7489
if (event.type === "select.new") {
7590
const pane = state.panes[event.pane]
@@ -122,14 +137,19 @@ export function selectShownNewTab(state: NewTabsState, pane: SessionPane, paneAc
122137
return shown(state.panes[pane], paneActive)
123138
}
124139

140+
/** The pane's New tab should take the keyboard now. */
141+
export function selectNewTabFocus(state: NewTabsState, pane: SessionPane) {
142+
return state.focus === pane
143+
}
144+
125145
export function selectHasNewTabs(state: NewTabsState) {
126146
return SESSION_PANES.some((pane) => state.panes[pane].ids.length > 0)
127147
}
128148

129149
function sync(state: NewTabsState, snapshot: RegionTabSnapshot): NewTabsState {
130150
// Another session starts with none, and opens one when it routes into an open, empty side region.
131151
if (snapshot.session !== state.session) {
132-
const reset = { ...state, session: snapshot.session, panes: emptyPanes(), snapshot }
152+
const reset = { ...state, session: snapshot.session, panes: emptyPanes(), focus: undefined, snapshot }
133153
const openedEmpty = snapshot.sideOpen && snapshot.empty
134154

135155
return openedEmpty ? open({ ...reset, openedEmpty }, "side") : { ...reset, openedEmpty }
@@ -169,12 +189,28 @@ function sync(state: NewTabsState, snapshot: RegionTabSnapshot): NewTabsState {
169189
const want = snapshot.sideOpen && snapshot.empty
170190
const opened = want && !state.openedEmpty
171191

172-
const result =
192+
const opening =
173193
opened && selectShownNewTab(synced, "side", snapshot.panes.side.active) === undefined
174194
? open({ ...synced, openedEmpty: true }, "side")
175195
: { ...synced, openedEmpty: want }
176196

177-
if (result.panes === state.panes && result.openedEmpty === state.openedEmpty && result.snapshot === previous)
197+
// A request whose pane shows a tab rather than a New tab is over: the person opened the region onto a tab, or picked
198+
// one. The side pane's waits while the region is closed, as the request comes just before it opens.
199+
const focus = opening.focus
200+
201+
const stale =
202+
focus !== undefined &&
203+
(focus !== "side" || snapshot.sideOpen) &&
204+
selectShownNewTab(opening, focus, snapshot.panes[focus].active) === undefined
205+
206+
const result = stale ? { ...opening, focus: undefined } : opening
207+
208+
if (
209+
result.panes === state.panes &&
210+
result.openedEmpty === state.openedEmpty &&
211+
result.focus === state.focus &&
212+
result.snapshot === previous
213+
)
178214
return state
179215

180216
return result

‎packages/app/src/runtime/plugin/panels.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { legacyKeys, panelKey } from "./panel-keys"
3434
import { closedEntry, createSideFocus, type ClosedTabs } from "./closed-tabs"
3535
import {
3636
createNewTabsState,
37+
selectNewTabFocus,
3738
selectPaneNewTabs,
3839
selectShownNewTab,
3940
transitionNewTabs,
@@ -445,6 +446,12 @@ export function createRegion(input: {
445446
dispatchNewTabs({ type: "open", pane })
446447
},
447448
dismissNewTab,
449+
/** The pane's New tab view should give the keyboard to its first field: the person just asked for it. */
450+
newTabFocus: (pane: SessionPane) => selectNewTabFocus(newTabsState(), pane),
451+
/** The New tab view gave the keyboard to its field. */
452+
newTabFocused: () => dispatchNewTabs({ type: "focus.taken" }),
453+
/** Runs once the side region stops moving, at once while it is still; hidden contents cannot take focus. */
454+
settled,
448455
/** Closes the shown new tab view in the given or active pane; returns whether one was showing. */
449456
closeNewTab(pane: SessionPane = activePane()) {
450457
const id = shownNewTab(pane)
@@ -472,6 +479,8 @@ export function createRegion(input: {
472479
focused instanceof HTMLElement && (focused.isContentEditable || /^(INPUT|TEXTAREA)$/.test(focused.tagName))
473480
? focused
474481
: undefined
482+
// A New tab the region opens on takes the keyboard in its field.
483+
dispatchNewTabs({ type: "focus.requested", pane: "side" })
475484
side.toggle()
476485
const key = active()
477486
const entry = key ? byKey().get(key) : undefined

‎packages/app/src/runtime/plugin/side-region.tsx‎

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { For, Show, createMemo, createRenderEffect, createSignal, onCleanup, type Accessor, type JSX } from "solid-js"
1+
import { For, Show, createEffect, createMemo, createRenderEffect, createSignal, onCleanup, type Accessor, type JSX } from "solid-js"
22
import { createStore } from "solid-js/store"
33
import { createMediaQuery } from "@solid-primitives/media"
44
import { createEventListener } from "@solid-primitives/event-listener"
@@ -242,6 +242,39 @@ export function PaneTabs(props: {
242242

243243
// Without a new tab item, "+" opens a host-owned new tab view listing the launchers, as ⌘T does inside the region.
244244
const current = createMemo(() => props.region.shownNewTab(props.pane.id))
245+
246+
// The control a plugin draws in the New tab view to take the keyboard, such as the browser's address field.
247+
const [newTabField, setNewTabField] = createSignal<{ focus(): void }>()
248+
249+
// A New tab the person asked for gives the keyboard to that control once it renders and the region stops moving: a
250+
// region that slides open takes no focus until it lands. Without such a control, focus stays.
251+
createEffect(() => {
252+
const field = newTabField()
253+
254+
if (!field || current() === undefined || !props.region.newTabFocus(props.pane.id)) return
255+
props.region.settled(() => {
256+
if (newTabField() !== field || !props.region.newTabFocus(props.pane.id)) return
257+
field.focus()
258+
props.region.newTabFocused()
259+
})
260+
})
261+
262+
// What the New tab view's plugin slots get: a tab a plugin opens from there lands in this pane.
263+
const newTab = {
264+
get session() {
265+
return props.view
266+
},
267+
get screen() {
268+
return props.screen
269+
},
270+
target: (plugin: string) => tabs().target(props.pane.id, plugin),
271+
autofocus(control: { focus(): void }) {
272+
setNewTabField(() => control)
273+
274+
return () => setNewTabField((field) => (field === control ? undefined : field))
275+
},
276+
}
277+
245278
const add = () => props.region.openNewTab(props.pane.id)
246279

247280
let selectionEvent: Event | undefined
@@ -439,7 +472,13 @@ export function PaneTabs(props: {
439472
<Show when={current()}>
440473
{(id) => (
441474
<Tabs.Content value={id()} class="flex flex-col h-full overflow-hidden contain-strict">
442-
<Launchers items={launchers()} onChoose={target} closed={closed} />
475+
<Launchers
476+
items={launchers()}
477+
onChoose={target}
478+
closed={closed}
479+
top={<PluginSlot at="session.panel.new" input={newTab} />}
480+
list={<PluginSlot at="session.panel.new.list" input={newTab} />}
481+
/>
443482
</Tabs.Content>
444483
)}
445484
</Show>
@@ -579,28 +618,37 @@ function AddMenu(props: { items: readonly SessionPanelMenuEntry[]; onChoose: (ke
579618
)
580619
}
581620

582-
/** The + menu items as rows, then the session's recently closed tabs, in the new tab view. */
621+
/**
622+
* The new tab view: what plugins draw across its top, the + menu items as rows, what plugins list under them, then the
623+
* session's recently closed tabs.
624+
*/
583625
function Launchers(props: {
584626
items: readonly SessionPanelMenuEntry[]
585627
onChoose: (key: string) => void
586628
closed: { entries: readonly ClosedSideTab[]; onReopen: (key: string) => void }
629+
top: JSX.Element
630+
list: JSX.Element
587631
}): JSX.Element {
588632
const keys = createMemo(() => props.items.map((entry) => entry.key), [], { equals: same })
589633
const byKey = createMemo(() => new Map(props.items.map((entry) => [entry.key, entry.item])))
590634

591635
return (
592-
<div class="flex-1 min-h-0 overflow-y-auto px-6">
593-
<div class="mx-auto flex max-w-[560px] flex-col gap-3 pt-12 pb-10">
594-
<For each={keys()}>
595-
{(key) => (
596-
<Show when={byKey().get(key)}>
597-
{(item) => <LauncherRow item={item()} onChoose={() => props.onChoose(key)} />}
598-
</Show>
599-
)}
600-
</For>
601-
<ClosedTabsSection entries={props.closed.entries} onReopen={props.closed.onReopen} />
636+
<>
637+
{props.top}
638+
<div data-scrollable class="flex-1 min-h-0 overflow-y-auto px-6">
639+
<div class="mx-auto flex max-w-[560px] flex-col gap-3 pt-12 pb-10">
640+
<For each={keys()}>
641+
{(key) => (
642+
<Show when={byKey().get(key)}>
643+
{(item) => <LauncherRow item={item()} onChoose={() => props.onChoose(key)} />}
644+
</Show>
645+
)}
646+
</For>
647+
{props.list}
648+
<ClosedTabsSection entries={props.closed.entries} onReopen={props.closed.onReopen} />
649+
</div>
602650
</div>
603-
</div>
651+
</>
604652
)
605653
}
606654

0 commit comments

Comments
 (0)