Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,23 @@ enum AgentPaneTheme {
/// pane backs it with the window's own background, as it backs a terminal (cx-tcju).
@MainActor static func webTheme(_ tokens: ThemeTokens, surface: SurfaceKind,
backgrounds: SurfaceBackgrounds = ThemeScope.app.surfaceBackgrounds) -> WebTheme {
WebTheme(tokens, surface: surface, backgrounds: pageBackgrounds(tokens, surface: surface, backgrounds: backgrounds))
}

/// What the page of `surface` paints under its content, as ``webTheme(_:surface:backgrounds:)``
/// gives it (`--cmux-surface-background`), in the active scope. Opaque only when the page hides
/// everything under it, so a picture of it needs no alpha (`PageImageBackground`).
@MainActor static func pageBackground(_ tokens: ThemeTokens, surface: SurfaceKind,
backgrounds: SurfaceBackgrounds = ThemeScope.app.surfaceBackgrounds) -> ThemeRGB {
WebTheme.pageBackground(tokens, surface: surface, backgrounds: pageBackgrounds(tokens, surface: surface, backgrounds: backgrounds))
}

/// `backgrounds` as the page of `surface` gets them: the New Tab page is clear unless it has its own.
private static func pageBackgrounds(_ tokens: ThemeTokens, surface: SurfaceKind, backgrounds: SurfaceBackgrounds) -> SurfaceBackgrounds {
var backgrounds = backgrounds
if surface == .newTabPage, backgrounds[.newTabPage] == nil {
backgrounds.overrides[.newTabPage] = SurfaceBackground(color: tokens.surfaceBackground.withAlpha(0))
}
return WebTheme(tokens, surface: surface, backgrounds: backgrounds)
return backgrounds
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
public import AppKit
public import CmuxNextDesign

extension AgentPaneView {
/// An image of the page as shown (the next launch draws it until this
/// page paints again).
/// page paints again, and so does a released page built again,
/// `AgentPageHibernation`); nil when no picture can stand in for it
/// (``PageImageBackground/cover(_:color:pageIsOpaque:keepsClearAreas:)``).
public func pageImage() async -> NSImage? {
let color = performWithTheme { AgentPaneTheme.underPageColor(themeTokens, surface: surfaceKind) }
return await snapshotPage().map { PageImageBackground.fill($0, color) }
// Read before and after: the page can move to or from Chromium while its picture is taken.
guard snapshotKeepsClearAreas || Self.pageIsOpaque(surfaceKind, in: self) else { return nil }
let image = await snapshotPage()
let under = performWithTheme { AgentPaneTheme.underPageColor(themeTokens, surface: surfaceKind) }
return PageImageBackground.cover(image, color: under, pageIsOpaque: Self.pageIsOpaque(surfaceKind, in: self),
keepsClearAreas: snapshotKeepsClearAreas)
}

/// Whether a saved picture of a page (`AgentPaneLaunchImages`) may stand in for it under the
/// pane: one that keeps clear pixels always, one with no alpha only when the page paints an
/// opaque background. The draw side of ``pageImage()``'s rule, so a picture an older version saved
/// (a Chromium JPEG of a see-through page, black where the page is clear) is never drawn by the
/// first launch after an update.
///
/// - Parameters:
/// - image: The saved picture.
/// - newTab: Whether the page is a New Tab page (its tab's record, known before its view is
/// made: a launch that defers the tab draws its picture first).
/// - scope: The view whose theme scope the page shows in (its pane): a workspace's own
/// picture or glass makes its pages clear.
public static func savedPictureCovers(_ image: NSImage, newTab: Bool, in scope: NSView) -> Bool {
PageImageBackground.covers(image, pageIsOpaque: pageIsOpaque(newTab ? .newTabPage : .agentPane, in: scope))
}

/// Whether the page of `surface` paints an opaque background of its own in `scope`'s theme
/// scope (`performWithTheme`).
private static func pageIsOpaque(_ surface: SurfaceKind, in scope: NSView) -> Bool {
scope.performWithTheme { PageImageBackground.isOpaque(AgentPaneTheme.pageBackground(scope.themeTokens, surface: surface)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,43 @@ import CmuxNextDesign
/// shows through the image as it shows through the live page. Flattening it painted the solid
/// theme color over the art until the live page drew.
enum PageImageBackground {
/// Whether `color` hides what is under it (an opaque window), so a page drawn over it needs no alpha.
static func isOpaque(_ color: ThemeRGB) -> Bool { color.alpha >= 1 }

/// The picture a pane may draw for its page until the live page paints, or nil when there is none.
///
/// A picture that cannot keep the page's clear pixels (a Chromium capture is a JPEG with no
/// alpha, `PageWebView.snapshotKeepsClearAreas`) shows those pixels black. A page that paints an
/// opaque background of its own (`pageIsOpaque`: an opaque window, or a surface background that
/// hides the window) has no clear pixels, so its picture still covers. A page that is clear
/// anywhere (a glass, frosted or see-through window, or a background image) has them: its picture
/// is no cover at all, and the pane shows its loading state on the window's own backdrop instead.
/// Leo, 2026-10-26: "on restarting after an update/maybe in general, we dont wanna see a black
/// background on the agent chat".
///
/// - Parameters:
/// - page: The picture; nil when none was taken.
/// - color: The color the pane shows under the page, which an opaque one is flattened over.
/// - pageIsOpaque: Whether the page paints an opaque background of its own.
/// - keepsClearAreas: Whether the picture keeps the page's clear pixels (it has alpha).
/// Whether a saved `image` may stand in for its page: it keeps clear pixels (it has alpha), or
/// the page paints an opaque background so it has none to lose (``cover(_:color:pageIsOpaque:keepsClearAreas:)``).
@MainActor
static func covers(_ image: NSImage, pageIsOpaque: Bool) -> Bool {
pageIsOpaque || image.cgImage(forProposedRect: nil, context: nil, hints: nil)?.keepsClearPixels == true
}

@MainActor
static func cover(_ page: NSImage?, color: ThemeRGB, pageIsOpaque: Bool, keepsClearAreas: Bool) -> NSImage? {
guard let page, keepsClearAreas || pageIsOpaque else { return nil }
return fill(page, color)
}

/// `page` drawn over `color` at the snapshot's pixel size when `color` is opaque; `page`
/// itself, clear pixels kept, when the page is transparent (`color` with alpha below 1).
@MainActor
static func fill(_ page: NSImage, _ color: ThemeRGB) -> NSImage {
guard color.alpha >= 1 else { return page }
guard isOpaque(color) else { return page }
guard let image = page.cgImage(forProposedRect: nil, context: nil, hints: nil),
let context = CGContext(data: nil, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: 0,
space: CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ struct AgentTabContent {
private func drawLastPage(_ key: String, until view: AgentPaneView?) {
let paneView = pane.view
let images = pane.services.agentTabs.launchImages
// From the tab's record, so a tab its launch defers (no view yet) is judged as what it is.
let newTab = view.map { $0.model.newTab != nil } ?? pane.services.agentTabs.isNewTabPage(key)
// A New Tab page with no saved image of its own draws the last New Tab frame (cx-ec99), unless
// it shows in Chromium: that page draws over the frame as it comes in, both seen at once.
let newTabFrame = { view?.model.newTab == nil || view?.pageDrawsOverCovers == true ? nil : AgentPaneNewTabFrame.shared.image(size: paneView.contentHost.bounds.size, appearance: Self.appearance(of: paneView)) }
let image = { images.take(key, size: paneView.contentHost.bounds.size, appearance: Self.appearance(of: paneView)) ?? newTabFrame() }
// A saved picture with no alpha over a page that is clear now (an older version's Chromium JPEG of a
// see-through page) is black where the page is clear: the pane shows its loading state instead.
let saved = { images.take(key, size: paneView.contentHost.bounds.size, appearance: Self.appearance(of: paneView))
.flatMap { image in AgentPaneView.savedPictureCovers(image, newTab: newTab, in: paneView) ? image : nil } }
let image = { saved() ?? newTabFrame() }
if paneView.launchImageView == nil, let image = image() { paneView.showLaunchImage(image) }
guard let imageView = paneView.launchImageView, let view else { return }
view.showsLoadingState = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ public nonisolated struct WebTheme: Equatable, Sendable {
}
}

/// The page background (`--cmux-surface-background`) of a page made with these arguments, in the
/// active scope: the surface's own background (`appearance.surfaces`) when it has one, else the
/// surface token, opaque in an opaque window and clear over a material. A host asks it for what
/// the page paints under its content (an agent page's saved picture, `PageImageBackground`).
@MainActor
public static func pageBackground(_ tokens: ThemeTokens, reduceTransparency: Bool = false, surface surfaceKind: SurfaceKind? = nil,
backgrounds: SurfaceBackgrounds = ThemeScope.app.surfaceBackgrounds) -> ThemeRGB {
let pageOpaque = WindowBackdrop.current(tokens, reduceTransparency: reduceTransparency).panesPaintBackground
let surface = tokens.surfaceBackground
let windowTint = WindowBackdrop.currentTint(tokens)
return surfaceKind.flatMap { backgrounds.fill(for: $0, tokens: tokens, windowTint: windowTint) }
?? (pageOpaque ? surface.withAlpha(1) : surface.withAlpha(0))
}

/// - Parameter tokens: The theme tokens of the view's scope.
/// - Parameter reduceTransparency: The user's Reduce Transparency setting.
/// - Parameter surface: The surface the page is (agent chat, new tab
Expand All @@ -88,11 +102,9 @@ public nonisolated struct WebTheme: Equatable, Sendable {
public init(_ tokens: ThemeTokens, reduceTransparency: Bool = false, surface surfaceKind: SurfaceKind? = nil,
backgrounds: SurfaceBackgrounds = ThemeScope.app.surfaceBackgrounds, scrollers: String? = nil,
app: AppTheme? = nil, glass: GlassLensMode? = nil) {
let pageOpaque = WindowBackdrop.current(tokens, reduceTransparency: reduceTransparency).panesPaintBackground
let surface = tokens.surfaceBackground
let windowTint = WindowBackdrop.currentTint(tokens)
let page = surfaceKind.flatMap { backgrounds.fill(for: $0, tokens: tokens, windowTint: windowTint) }
?? (pageOpaque ? surface.withAlpha(1) : surface.withAlpha(0))
let page = Self.pageBackground(tokens, reduceTransparency: reduceTransparency, surface: surfaceKind, backgrounds: backgrounds)
let cardMaterial = CardMaterial(tokens, lift: ChromeTunables.opaqueOverlayLift.value,
minimumOpacity: ChromeTunables.cardOpacity.value)
let tooltip = TooltipSurface(tokens, lift: ChromeTunables.opaqueOverlayLift.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,52 @@ struct PageImageBackgroundTests {
let image = PageImageBackground.fill(try Self.page(), color)
#expect(try Self.alpha(image, x: 2, y: 2) == 1)
}

/// Leo 2026-10-26: "on restarting after an update/maybe in general, we dont wanna see a black
/// background on the agent chat". A Chromium picture has no alpha, so a see-through page's clear
/// pixels come out black; over a transparent window that picture was the relaunch's first frame.
@Test func aPictureWithNoAlphaIsNoCoverForATransparentPage() throws {
let clear = AgentPaneTheme.underPageColor(ThemeTokens.unresolved, surface: .newTabPage)
#expect(clear.alpha < 1)
#expect(PageImageBackground.cover(try Self.page(), color: clear, pageIsOpaque: false, keepsClearAreas: false) == nil)
}

@Test func aPictureWithNoAlphaStillCoversAnOpaquePage() throws {
let opaque = AgentPaneTheme.underPageColor(ThemeTokens.unresolved, surface: .newTabPage).withAlpha(1)
let image = try #require(PageImageBackground.cover(try Self.page(), color: opaque, pageIsOpaque: true, keepsClearAreas: false))
#expect(try Self.alpha(image, x: 2, y: 2) == 1)
}

/// A surface background (`appearance.surfaces`) leaves the pane's under-page color clear, while
/// the page itself paints opaque: its picture has no clear pixels to lose, so it still covers.
@Test func aPictureWithNoAlphaCoversAPageWithItsOwnOpaqueBackground() throws {
let clearUnder = AgentPaneTheme.underPageColor(ThemeTokens.unresolved, surface: .newTabPage)
#expect(PageImageBackground.cover(try Self.page(), color: clearUnder, pageIsOpaque: true, keepsClearAreas: false) != nil)
}

/// The page background the host reads is the one the page paints (`WebTheme`): a New Tab page
/// with no surface background of its own is clear in every window.
@Test func theNewTabPagePaintsNoOpaqueBackgroundOfItsOwn() {
#expect(AgentPaneTheme.pageBackground(ThemeTokens.unresolved, surface: .newTabPage).alpha < 1)
}

@Test func aPictureThatKeepsClearPixelsCoversATransparentPage() throws {
let clear = AgentPaneTheme.underPageColor(ThemeTokens.unresolved, surface: .newTabPage)
let image = try #require(PageImageBackground.cover(try Self.page(), color: clear, pageIsOpaque: false, keepsClearAreas: true))
#expect(try Self.alpha(image, x: 2, y: 2) == 0)
}

@Test func noPictureIsNoCover() {
#expect(PageImageBackground.cover(nil, color: ThemeTokens.unresolved.surfaceBackground.withAlpha(1), pageIsOpaque: true,
keepsClearAreas: true) == nil)
}

/// A picture an older version saved (`AgentPaneLaunchImages`) gets the same rule when it is drawn:
/// one with no alpha only over a page that paints opaque; one that keeps clear pixels always.
@Test func aSavedPictureWithNoAlphaIsDrawnOnlyOverAnOpaquePage() throws {
let flat = PageImageBackground.fill(try Self.page(), ThemeTokens.unresolved.surfaceBackground.withAlpha(1))
#expect(!PageImageBackground.covers(flat, pageIsOpaque: false))
#expect(PageImageBackground.covers(flat, pageIsOpaque: true))
#expect(PageImageBackground.covers(try Self.page(), pageIsOpaque: false))
}
}
Loading