Skip to content

fix(titlebar): read the window title in child views, not WindowContentView.body - #548

Merged
umputun merged 2 commits into
masterfrom
title-off-body
Sep 4, 2026
Merged

umputun merged 2 commits into
masterfrom
title-off-body

Conversation

@umputun

@umputun umputun commented Sep 4, 2026

Copy link
Copy Markdown
Owner

PR B for #516, after #547. WindowContentView.body read the active session's displayName twice, for the OS title token and for the visible title row, so every OSC title tick invalidated the whole body and re-ran SwiftUI's graph and layout for the window. WindowAccessor.updateNSView then ran the full WindowAppearance.sync when only NSWindow.title had changed.

two child views now own those reads. WindowTitleSync resolves the OS title in its own body and hosts WindowAccessor; TitlebarLabel resolves TitlebarComposition for the visible row, and the zoom titlebar reuses it. The parent passes the store and ids and no title. WindowAccessor writes window.title on a token change and keeps the blend on attach, key, main, fullscreen, appearance and accessibility changes, which already carry every other blend input. The custom window name lookup moved into WindowLibrary next to windowName(for:), and the dashboard title uses it.

tests: hosted, a TitleProbeView title write leaves a separator sentinel the full blend would reset, a title set before attach survives attachment, and WindowTitleSync hosted in NSHostingView follows an OSC title, a session rename and a window rename to NSWindow.title. Host-free, the customWindowName(for:) cases.

profile, Time Profiler, Debug builds of a8bbaa5 and this branch, isolated state, sidebar hidden, one session printing numbered OSC 2 titles at about 10 Hz, two recordings of about 20 s each, main thread only. Values are inclusive main-thread sample weight in ms:

symbol a8bbaa5 this branch
WindowContentView.body.getter 212 0
WindowTitleSync.body / TitlebarLabel.body absent 6 / 28
WindowAppearance.sync 70 0
-[NSWindow _dosetTitle:andDefeatWrap:] 187 213
GraphHost.flushTransactions 1086 539
main thread, all samples 1774 978

these are sample weights over two separately timed recordings, not update counts and not a per-tick or total-CPU saving: the baseline tick count was not recorded, so the two workloads are not matched. The SwiftUI Instruments template hung on finalization here, so there is no SwiftUI-lane update count; the parent body had zero samples in the patched recording while the two child bodies and the title write were present. @dimonb, could you rerun your harness against this patch to measure the per-tick change?

Fix #516

…tView.body

WindowContentView.body read the active session's displayName twice, for the
OS title token and for the visible title row, so every OSC title tick
invalidated the whole body and re-ran SwiftUI's graph and layout for the
window. WindowAccessor.updateNSView then ran the full WindowAppearance.sync
when only the title had changed.

WindowTitleSync and TitlebarLabel read the session and window names in their
own bodies; the parent passes the store and ids and no title, so a title
tick reaches those two views and NSWindow.title alone. WindowAccessor writes
the title on a token change and keeps the blend on attach, key, main,
fullscreen, appearance and accessibility changes, which already carry every
other blend input.

In a 20 s Time Profiler recording of a 10 Hz title spinner, sidebar hidden,
WindowContentView.body had zero main-thread samples after this change against
212 before it; the two child bodies and the NSWindow title write carry the
tick, and WindowAppearance.sync no longer appears in it.

Fix #516
…tale notes

the custom-name lookup was an extension of an agtermCore type declared in
an app-target view file; it now sits beside windowName(for:) with a test,
and the dashboard title uses it instead of its own copy.

the WindowContentView comment on the title accessor said the parent body
reads the title, the inverse of what it does. windows.md said TitleProbeView
sets frameAutosaveName; it persists the frame under the window UUID in
UserDefaults and restores it once on attach.

Related to #516
Copilot AI lite review requested due to automatic review settings September 4, 2026 23:09
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying agterm with  Cloudflare Pages  Cloudflare Pages

Latest commit: c91cf97
Status: ✅  Deploy successful!
Preview URL: https://f2415b4a.agterm.pages.dev
Branch Preview URL: https://title-off-body.agterm.pages.dev

View logs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

TitlebarLabel.body currently contains let composition = composition, which should not compile due to self-referential initialization and needs self.composition (or a rename).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR reduces SwiftUI invalidation/layout work caused by frequent OSC title updates by moving window-title reads out of WindowContentView.body into dedicated child views, and by splitting “OS window title write” from “full titlebar blend” behavior in WindowAccessor. It also centralizes custom window-name lookup in agtermCore’s WindowLibrary and adds hosted + host-free tests for the new behavior.

Changes:

  • Introduce WindowTitleSync and TitlebarLabel to localize session/window-title reads to child views (avoiding re-running WindowContentView.body on OSC title ticks).
  • Change WindowAccessor.updateNSView to write NSWindow.title on token changes without re-running the full WindowAppearance.sync blend.
  • Add new WindowLibrary.customWindowName(for:) in agtermCore and update dashboard/window title composition plus tests.
File summaries
File Description
agtermTests/WindowContentView+TitlebarTests.swift Hosted tests ensuring OS window title follows OSC/session/window rename updates without re-rooting the view.
agtermTests/WindowAccessorTests.swift Hosted tests asserting title writes don’t trigger full blend, and pre-attach title survives attachment.
agtermCore/Tests/agtermCoreTests/WindowLibraryTests.swift Unit tests for customWindowName(for:) behavior (auto-name/unknown id handling).
agtermCore/Sources/agtermCore/WindowLibrary.swift Adds customWindowName(for:) API to centralize “custom name” lookup.
agterm/Views/WindowContentView+Titlebar.swift Refactors titlebar label/title sync into child views (TitlebarLabel, WindowTitleSync).
agterm/Views/WindowContentView+Dashboard.swift Uses customWindowName(for:) for dashboard window title suffix.
agterm/Views/WindowContentView.swift Switches .background(WindowAccessor(...)) to .background(windowTitleSync) to isolate title reads.
agterm/Views/WindowAppearance.swift Updates comment to reflect new re-application triggers (key/main/fullscreen + appearance).
agterm/Views/WindowAccessor.swift Updates representable to write title on token changes and keep blend on attach/notifications.
.claude/rules/windows.md Updates documentation of the window frame persistence/restore approach for TitleProbeView.
Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +232 to +234
var body: some View {
let composition = composition
VStack(alignment: .leading, spacing: 1) {
@umputun
umputun merged commit c3860d1 into master Sep 4, 2026
9 checks passed
@umputun
umputun deleted the title-off-body branch September 4, 2026 23:16
dimonb added a commit to dimonb/agterm that referenced this pull request Oct 26, 2026
umputun#548 moved the title write off the full blend, but NSWindow.title is still assigned on
every OSC title tick. WindowAppearance.sync hides it and the custom header draws the
visible title, so it only feeds the Window menu, Mission Control and the window
switcher. Each assignment runs AppKit's title machinery (NSWindowTitleController,
NSTitledFrame.setTitle), about 1.3 ms, which an agent's spinner pays ~10 times a second;
umputun#548's own profile still shows _dosetTitle at 213 ms.

The first write after a quiet interval lands at once, later ones within 0.5 s collapse
into one trailing write carrying the final value. Blend-pass writes stay immediate and
do not restart the interval, so a title set right after attach is not deferred.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sidebar: an animated OSC title on an un-renamed session costs three full tree walks per tick, ~3x agterm CPU

2 participants