Repository Public License
1# OrbitEngine23Workspaces, profiles, tab organisation, Split, Blink (tab suspension), Focus and session orchestration.4`Packages/NavigatorKit/Sources/OrbitEngine`. Pure logic: imports `NavigatorCore`, `Persistence`, `Foundation` and5`CoreGraphics` (for `CGPoint` / `CGSize` / `CGRect` only). No WebKit, no SwiftUI. The UI layer draws and releases6web views; this module decides and keeps the books.78| File | What it is |9|---|---|10| `OrbitalLayout.swift` | Geometry of the orbital view (pure) |11| `TabLayout.swift`, `TabOperations.swift` | The five tab layouts and tab rules (pure) |12| `SplitEngine.swift` | Split layouts, geometry, device limits (pure) |13| `BlinkPolicy.swift`, `BlinkCoordinator.swift` | Suspension policy (pure) and its bookkeeping (actor) |14| `FocusSession.swift` | Focus state machine (pure) and `FocusController` (actor) |15| `SessionOrchestration.swift` | Coalescing, debounced session writes, launch restoration |16| `FuzzySearch.swift` | Fuzzy matcher and search across spaces and tabs (pure) |17| `OrbitService.swift` | The actor the UI calls for anything that is stored |1819## Orbital view2021Spaces are satellites on concentric rings around a core. `OrbitSpace.ring` and `OrbitSpace.angle` are the stored22position; everything else is computed.2324- **Projection.** Rings are circles seen under a tilt: the point at angle `a` of a ring of radius `r` is drawn at25 `(cx + r·cos a, cy + r·sin a·cos tilt)`. Angles are radians, 0 at the right, growing clockwise on screen;26 `-π/2` is the top. `depth = sin a` (−1 at the back/top, 1 at the front/bottom) scales the satellite by27 `1 + depthScale·depth` and gives the painter's order (`OrbitalLayout.satellites` returns back to front).28- **Metrics** (`OrbitalMetrics.standard`): first ring radius 150, ring spacing 110, satellite diameter 56, minimum29 gap 18, tilt 0.45 rad, depth scale 0.12, rings 0…7. Zoom (`OrbitalViewport`, 0.25…4) scales everything together,30 so placement does not depend on zoom or on the window size.31- **No overlap.** Two satellites of a ring separated by a chord `c` are at least `c·cos tilt` apart on screen, so32 the minimum angular separation of a ring is `2·asin((largestDiameter + gap) / (2·r·cos tilt))` and the ring33 capacity is `⌊2π / separation⌋`. With the standard metrics the capacities are 10, 18, 25, … and the total is34 `OrbitalLayout.totalCapacity()`. Adjacent rings are `ringSpacing·cos tilt` ≈ 99 points apart at their closest35 point, more than the largest satellite plus the gap.36- **Automatic placement** (`placement(forNewSpaceAmong:)`): innermost ring with room, in the middle of its widest37 gap; the top of the ring when it is empty. Existing satellites are never moved, so a ring can be left before38 its nominal capacity (see the function's comment); `evenSlots` / `OrbitService.tidySpaces` is the arrangement39 that reaches it.40- **Hit-testing** (`hitTest`): front-most satellite containing the point, optional slop for touch.41- **Drag** (`drop`): nearest ring; the angle snaps to the ring's regular grid when the pointer is within a42 quarter of a grid step of a grid position; if the place is taken, the nearest free angle of the ring, then the43 nearest ring with room. The result says whether it snapped or was displaced.44- **Accessible equivalents.** `listOrder` (the user's order: `order`, creation date, id) is what VoiceOver, the45 sidebar and the grid use. `spatialOrder` (ring, then clockwise from the top) is for arrow keys inside the46 orbital view. `gridFrames` lays the same list out as a grid. Archived spaces are in none of them.4748## Tab layouts4950`TabLayout.presentation(mode:tabs:groups:selected:size:)` returns what each `TabLayoutMode` displays:5152| Mode | Result |53|---|---|54| `vertical` | Sections: pinned (flat), each group in order (a collapsed group keeps its header, a hidden count, and the selected tab if it is inside), ungrouped tabs. Rows carry a tree depth from `parentID` (capped at 6; orphans and cycles become roots; subtrees can be collapsed). |55| `horizontal` | The same flat order fitted in the strip width: pinned tabs have a fixed width, the others share the rest between a minimum and a maximum; what does not fit goes to the overflow menu, and the selected tab is always swapped in. |56| `compact` | The current tab, the count, and the most-recently-used list for the switcher. |57| `grid` | Flat order and thumbnail frames. |58| `orbit` | Pinned tabs evenly on ring 0, the others on the following rings; tabs beyond the capacity have no slot and are shown in the list. |5960Display order everywhere: open tabs only, pinned first, then `order`, creation date, id — the same rule as61storage.6263## Split6465`SplitEngine` works on `SplitLayout` values and never touches storage: `adding`, `removing` (returns `nil` when66fewer than two panes remain), `replacing` (swaps when the tab is already shown), `moving`, `changingAxis`,67`equalized`, `focusingNext` / `focusingPrevious`, `resizing`, `restored`, `paneRects`, `dividers`, `pane(at:)`.6869- `fractions` are widths (`horizontal`), heights (`vertical`), or — for `grid` — weights inside a row. Grid rows70 are 2 → [2], 3 → [2, 1], 4 → [2, 2], 5 → [3, 2], 6 → [3, 3], 7 → [4, 3], 8 → [4, 4], with equal row heights; in a71 container more than 1.2× taller than wide the lines are columns instead.72- Resizing a divider changes only its two neighbours and keeps each at least `minimumLength` points (240 by73 default); when the pair is smaller than twice the minimum it splits evenly.74- `restored` validates a stored layout: panes whose tab is gone and duplicates are dropped, the device limit is75 applied, unusable fractions fall back to equal sizes.7677**Pane limits** (`SplitCapacity.maximumPanes`). Product limits, not measurements:7879| Device | Panes |80|---|---|81| iPhone | 1 (no Split; the UI offers a quick switch between recent tabs) |82| iPad, compact width | 2 |83| iPad, regular width | 3, or 2 below 6 GB of physical memory |84| Mac | 8 from 24 GB, 6 from 16 GB, 4 below |8586## Blink8788`BlinkPolicy.evaluate(tabs:budget:pressure:now:)` is a pure function returning suspend / resume decisions with89reasons:90911. A visible tab without a web view is resumed (`becameVisible`).922. Protected tabs are never suspended at any pressure level: visible (including every pane of a split), capturing93 camera / microphone / screen, playing audio, holding unsubmitted form input, excluded by the user (per tab for94 the run, or per host — stored). They still count against the limit; the plan reports `overLimitBy`.953. With an idle limit, an unprotected, unpinned tab idle for longer is suspended (`idle`).964. While live web views exceed the limit, unprotected live tabs are suspended, unpinned before pinned, least97 recently active first. Under normal pressure a tab used in the last 30 s is spared; under memory pressure98 nothing unprotected is.99100Limit by pressure: the budget (normal), half of it (warning), zero beyond the protected tabs (critical).101102Automatic budget (`BlinkBudget.automatic`), a heuristic derived from physical memory, overridable by the user103(`BlinkCoordinator.setMaximumLiveWebViews`, setting `blink.maximum_live_web_views`, 0 = automatic):104105| Physical memory | Mac | iPad | iPhone |106|---|---|---|---|107| 4 GB | 4 | 3 | 3 |108| 8 GB | 6 | 4 | 4 |109| 16 GB | 12 | 8 | 6 |110| 32 GB | 24 | 8 | 6 |111| 64 GB and more | 40 | 8 | 6 |112113`BlinkCoordinator` (actor) holds the live signals reported by the web layer (`report(_:for:)`,114`setVisibleTabs`), the exclusions and the settings, builds the plan (`plan(for:pressure:)`) and records each115transition the UI carried out (`confirm`, `record`) in the tab's `lifecycle` through `database.tabs`. Private116tabs are tracked in memory (`trackGhostTab`).117118What "unsubmitted form input" means is defined by the page script of the web layer, not here: this module only119receives the flag. A page that keeps its state in JavaScript without form fields is not detected and loses that120state when suspended, like in other browsers.121122## Focus123124`FocusSession` is a value-type state machine: `idle → running ⇄ paused → finished` (`start`, `pause`, `resume`,125`end`), with an optional timer. Every transition takes `now`; `elapsed`, `remaining` and `deadline` are computed,126pauses do not count, and `refresh(now:)` finishes a session whose timer ran out *at the time it ran out*.127`FocusController` (actor, injectable clock) keeps one session per space, stores `FocusSettings` with the space128(in memory for a private space), captures a `FocusRestoration` per window at `start` (sidebar, agent panel, tab129layout) and returns them at `end` so the UI puts the chrome back. Sessions are in memory: they do not survive a130relaunch.131132## Session orchestration133134- `SessionChangeCoalescer` shortens a sequence of `SessionChange`s while guaranteeing the same stored result135 (rules in its comment). A test applies random sequences both ways to two databases and compares them.136- `SessionOrchestrator` (actor): `record(_:)` queues changes, a flush happens 400 ms after the last change and at137 most 2 s after the first unflushed one; `flush()` and `shutdown()` (flush + clean-shutdown mark) are explicit.138 Changes of private windows, spaces and tabs are dropped before they are queued. If storage refuses the atomic139 batch, changes are retried one by one and the refusals reported without page data.140- `SessionRestorer.launch(database:policy:paneLimit:)` runs `beginSession()` + `restore()` and returns a141 `LaunchModel`: `firstLaunch`, `restored` (only the selected tab and split panes of each window are in142 `tabsToLoad`; the rest stays dormant), `crashRecovery` (windows are provided but nothing loads until143 `confirmingRestore`, so a page that crashed the app is not reloaded automatically) or `fresh`. Repairs: missing144 selected tab, invalid split, windows without tabs (closed in storage), windows of archived spaces (left alone).145146## OrbitService147148Actor over the stores; holds no state. Private (ghost) profiles, spaces, windows and tabs are refused with149`OrbitError.ghostNotPersisted`: they live in memory in `PrivacyEngine.GhostSession`.150151Rules worth knowing:152153- `bootstrap()` is idempotent and guarantees one profile and one active space.154- A profile keeps at least one active space; the last profile cannot be deleted.155- Pinned tabs are flat: pinning removes the tab from its group and from the tree.156- Closing or moving a tab away fixes the window it leaves: selection (next tab, else previous), split (the pane is157 removed; the split ends below two panes), children (adopted by the tab's parent).158- Moving a tab to another space drops its group. Moving it to another **profile** also drops its WebKit session159 state: the back/forward list and form state of one profile never enter another; the page reloads from its URL160 in the other profile's data store.161- "Reopen closed tab" reopens in the original window when it is still open, else in an open window of the space,162 else in a new one.163164## Not verified165166- Nothing here was exercised with real web views: the Blink budgets and pane limits are heuristics.167- `SplitCapacity.currentPlatform` on a physical iPad / iPhone (model-name detection) was only compiled.168- The read-modify-write in `BlinkCoordinator.setLifecycle` can overwrite a concurrent `touchTab` of the same169 tab; a lifecycle-only update in `TabStore` would remove the race.170