diff --git a/Sources/WeiBei/Views/ContentView.swift b/Sources/WeiBei/Views/ContentView.swift index 5fe494789..0df8f7bfe 100644 --- a/Sources/WeiBei/Views/ContentView.swift +++ b/Sources/WeiBei/Views/ContentView.swift @@ -24,7 +24,7 @@ struct ContentView: View { ZStack(alignment: .top) { LayoutContentView() .frame(maxWidth: .infinity, maxHeight: .infinity) - .background(WeiBeiTheme.paper) + .background(Color(nsColor: WeiBeiNativePalette.paper(for: store.appearanceMode))) // Only cross-fade immersive ↔ document families. Pane show/hide inside // the document family is owned by AppKit StableDocumentWorkspace animation // — a second SwiftUI layout animation here made toggles feel split/janky. @@ -614,6 +614,7 @@ private struct ThreePaneWorkspaceChrome: View { visibleOrder: visibleOrder, draggedRole: paneReorder.drag?.role, expansionRequest: expansionRequest, + appearanceMode: store.appearanceMode, onFramesChange: onFramesChange, onExpansionRequestHandled: onExpansionRequestHandled ) diff --git a/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift b/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift index e3b6be418..a1de4477e 100644 --- a/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift +++ b/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift @@ -18,6 +18,10 @@ struct EmptyWorkspaceLauncherView: View { @Environment(\.accessibilityReduceMotion) private var reduceMotion @State private var selectedInspirationID: String? + /// Bumped on theme change so a long-lived NSHostingView cannot keep a stale paper snapshot. + @State private var appearanceEpoch = 0 + + private var liveAppearanceMode: WeiBeiAppearanceMode { store.appearanceMode } var body: some View { TimelineView(.periodic(from: .now, by: 60)) { timeline in @@ -30,9 +34,10 @@ struct EmptyWorkspaceLauncherView: View { ? EmptyWorkspaceLayoutMetrics.compactInspirationSlotHeight : EmptyWorkspaceLayoutMetrics.inspirationSlotHeight let currentInspiration = inspiration(at: timeline.date) + let mode = liveAppearanceMode ZStack { - EmptyWorkspacePaperField(compact: compact) + EmptyWorkspacePaperField(mode: mode, compact: compact) workspaceContent( at: timeline.date, @@ -44,8 +49,18 @@ struct EmptyWorkspaceLauncherView: View { inspirationSlotHeight: inspirationSlotHeight ) } + // Rebuild the board when theme changes — long-lived NSHostingView + // does not always re-resolve ambient WeiBeiTheme Color snapshots. + .id("\(mode.rawValue)-\(appearanceEpoch)") } } + .background(EmptyWorkspaceResolvedColor.paper(liveAppearanceMode)) + .onChange(of: store.appearanceMode) { _, _ in + appearanceEpoch &+= 1 + } + .onReceive(NotificationCenter.default.publisher(for: WeiBeiThemeRuntime.didChangeNotification)) { _ in + appearanceEpoch &+= 1 + } .accessibilityElement(children: .contain) .accessibilityIdentifier("empty-workspace-launcher") } @@ -140,7 +155,7 @@ struct EmptyWorkspaceLauncherView: View { Text(EmptyWorkspaceDayPeriod.current(at: date).greeting(language: store.interfaceLanguage)) .font(WeiBeiTypography.brandFont(language: store.interfaceLanguage, size: compact ? 14.5 : 16, weight: .regular)) .tracking(store.interfaceLanguage == .chinese ? 0.8 : 0.35) - .foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.92)) + .foregroundStyle(EmptyWorkspaceResolvedColor.secondaryInk(liveAppearanceMode).opacity(0.92)) .multilineTextAlignment(.center) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("empty-workspace-greeting") @@ -186,17 +201,57 @@ struct EmptyWorkspaceLauncherView: View { } } +/// sRGB SwiftUI colors resolved from an explicit mode (not ambient WeiBeiTheme). +/// `Color(nsColor:)` can stick to the wrong snapshot inside a long-lived NSHostingView. +private enum EmptyWorkspaceResolvedColor { + static func paper(_ mode: WeiBeiAppearanceMode) -> Color { + color(WeiBeiNativePalette.paper(for: mode)) + } + + static func paperRaised(_ mode: WeiBeiAppearanceMode) -> Color { + color(WeiBeiNativePalette.paperRaised(for: mode)) + } + + static func ink(_ mode: WeiBeiAppearanceMode) -> Color { + color(WeiBeiNativePalette.ink(for: mode)) + } + + static func secondaryInk(_ mode: WeiBeiAppearanceMode) -> Color { + color(WeiBeiNativePalette.secondaryInk(for: mode)) + } + + static func hairline(_ mode: WeiBeiAppearanceMode) -> Color { + color(WeiBeiNativePalette.hairline(for: mode)) + } + + private static func color(_ nsColor: NSColor) -> Color { + let converted = nsColor.usingColorSpace(.sRGB) ?? nsColor + var red: CGFloat = 0 + var green: CGFloat = 0 + var blue: CGFloat = 0 + var alpha: CGFloat = 0 + converted.getRed(&red, green: &green, blue: &blue, alpha: &alpha) + return Color(.sRGB, red: red, green: green, blue: blue, opacity: alpha) + } +} + private struct EmptyWorkspacePaperField: View { + let mode: WeiBeiAppearanceMode let compact: Bool var body: some View { - ZStack { - WeiBeiTheme.paper + let paper = EmptyWorkspaceResolvedColor.paper(mode) + let raised = EmptyWorkspaceResolvedColor.paperRaised(mode) + let ink = EmptyWorkspaceResolvedColor.ink(mode) + let hairline = EmptyWorkspaceResolvedColor.hairline(mode) + + return ZStack { + paper RadialGradient( colors: [ - WeiBeiTheme.paperRaised.opacity(0.72), - WeiBeiTheme.paper.opacity(0), + raised.opacity(mode.isDark ? 0.45 : 0.72), + paper.opacity(0), ], center: UnitPoint(x: 0.5, y: 0.42), startRadius: 8, @@ -205,16 +260,16 @@ private struct EmptyWorkspacePaperField: View { LinearGradient( colors: [ - WeiBeiTheme.ink.opacity(0.025), + ink.opacity(mode.isDark ? 0.04 : 0.025), Color.clear, - WeiBeiTheme.ink.opacity(0.018), + ink.opacity(mode.isDark ? 0.03 : 0.018), ], startPoint: .topLeading, endPoint: .bottomTrailing ) Rectangle() - .fill(WeiBeiTheme.hairline.opacity(0.30)) + .fill(hairline.opacity(mode.isDark ? 0.40 : 0.30)) .frame(width: 1, height: compact ? 24 : 48) .frame(maxHeight: .infinity, alignment: .top) .padding(.top, compact ? 14 : 24) diff --git a/Sources/WeiBei/Views/ReaderView.swift b/Sources/WeiBei/Views/ReaderView.swift index 84a01b71f..25d86db6f 100644 --- a/Sources/WeiBei/Views/ReaderView.swift +++ b/Sources/WeiBei/Views/ReaderView.swift @@ -220,11 +220,13 @@ struct ReaderView: View { } } } - .background(WeiBeiTheme.paper) - .foregroundStyle(WeiBeiTheme.ink) + // Bind paper fill to the live mode so empty reader / page chrome tracks theme switches. + .background(Color(nsColor: WeiBeiNativePalette.paper(for: store.appearanceMode))) + .foregroundStyle(Color(nsColor: WeiBeiNativePalette.ink(for: store.appearanceMode))) .animation(WeiBeiMotion.panel, value: pdfBrowseMode) .animation(WeiBeiMotion.panel, value: store.showReaderSearch) .animation(WeiBeiMotion.panel, value: pdfHasSelectableText) + .animation(WeiBeiMotion.appearance, value: store.appearanceMode) .onAppear { syncReaderLocationTitle() capturePendingPDFPageRequest() diff --git a/Sources/WeiBei/Views/StableDocumentWorkspace.swift b/Sources/WeiBei/Views/StableDocumentWorkspace.swift index 9f31170f3..08b7a46a6 100644 --- a/Sources/WeiBei/Views/StableDocumentWorkspace.swift +++ b/Sources/WeiBei/Views/StableDocumentWorkspace.swift @@ -13,6 +13,9 @@ struct StableDocumentWorkspace: NSViewRepresentable { let visibleOrder: [WorkspacePaneRole] let draggedRole: WorkspacePaneRole? let expansionRequest: PaneExpansionRequest? + /// Explicit input so theme changes always re-enter `updateNSView` and repaint the empty board layer. + /// Do not rely only on `@EnvironmentObject` for long-lived NSHostingView paper sync. + let appearanceMode: WeiBeiAppearanceMode let onFramesChange: ([WorkspacePaneRole], [CGRect]) -> Void let onExpansionRequestHandled: (UUID) -> Void @@ -39,12 +42,26 @@ struct StableDocumentWorkspace: NSViewRepresentable { ) splitView.install(roleHosts: roleHosts, emptyHost: emptyHost) context.coordinator.install(in: splitView) + applyEmptyBoardPaper(to: splitView, mode: appearanceMode) update(splitView, coordinator: context.coordinator) return splitView } func updateNSView(_ splitView: StableDocumentSplitView, context: Context) { update(splitView, coordinator: context.coordinator) + // Empty board is a long-lived NSHostingView. Never reassign `rootView` here + // (pane continuity / SelfCheck). Sync AppKit paper under the SwiftUI board instead; + // EmptyWorkspaceLauncherView rebuilds its own colors from store + theme notification. + applyEmptyBoardPaper(to: splitView, mode: appearanceMode) + } + + private func applyEmptyBoardPaper(to splitView: StableDocumentSplitView, mode: WeiBeiAppearanceMode) { + let paper = WeiBeiNativePalette.paper(for: mode) + let cgPaper = paper.cgColor + splitView.wantsLayer = true + splitView.layer?.backgroundColor = cgPaper + splitView.emptyHost?.wantsLayer = true + splitView.emptyHost?.layer?.backgroundColor = cgPaper } static func dismantleNSView(_ splitView: StableDocumentSplitView, coordinator: StableDocumentSplitCoordinator) { diff --git a/Sources/WeiBeiSelfCheck/main.swift b/Sources/WeiBeiSelfCheck/main.swift index e41d8a8ba..948d240a0 100644 --- a/Sources/WeiBeiSelfCheck/main.swift +++ b/Sources/WeiBeiSelfCheck/main.swift @@ -1796,8 +1796,11 @@ expect(stableDocumentSource.contains("WorkspacePaneRole.allCases.map") && stableDocumentSource.contains("func assertStableOwnership()") && stableDocumentSource.contains("roleHosts.values.allSatisfy { $0.superview === self }") && stableDocumentSource.contains("EmptyWorkspaceLauncherView().environmentObject(store)") + && stableDocumentSource.contains("let appearanceMode: WeiBeiAppearanceMode") + && stableDocumentSource.contains("applyEmptyBoardPaper(to: splitView, mode: appearanceMode)") + && stableDocumentSource.contains("emptyHost?.layer?.backgroundColor = cgPaper") && !stableDocumentSource.contains("removeFromSuperview()") - && !stableDocumentSource.contains("rootView ="), "document pane hosts remain under one AppKit parent while frames animate") + && !stableDocumentSource.contains("rootView ="), "document pane hosts remain under one AppKit parent while frames animate; empty board paper syncs via layer + explicit appearanceMode") expect(stableDocumentSource.contains("recordContinuityTransition(duration: layoutAnimationDuration)") && stableDocumentSource.contains("host.layer?.presentation()?.frame ?? host.frame") && paneContinuityRecorderSource.contains("WEIBEI_VERIFY_PANE_TRACE_DIR") @@ -1855,7 +1858,11 @@ expect(emptyWorkspaceSource.contains("TimelineView(.periodic(from: .now, by: 60) && emptyWorkspaceSource.contains("static let compactHeightThreshold: CGFloat = 680") && emptyWorkspaceSource.contains("geometry.size.width < EmptyWorkspaceLayoutMetrics.compactWidthThreshold") && emptyWorkspaceSource.contains("min(116, max(76") - && emptyWorkspaceSource.contains("minimumScaleFactor(0.78)"), "empty workspace greeting updates with time and its entry and inspiration typography adapt to compact windows") + && emptyWorkspaceSource.contains("minimumScaleFactor(0.78)") + && emptyWorkspaceSource.contains("EmptyWorkspacePaperField(mode: mode, compact: compact)") + && emptyWorkspaceSource.contains("EmptyWorkspaceResolvedColor.paper") + && emptyWorkspaceSource.contains("WeiBeiThemeRuntime.didChangeNotification") + && emptyWorkspaceSource.contains("appearanceEpoch"), "empty workspace greeting updates with time; paper field resolves explicit theme colors and rebuilds on appearance changes") expect(emptyWorkspaceSource.contains("selectedInspirationID") && emptyWorkspaceSource.contains("randomItem(excludingID: currentID") && emptyWorkspaceSource.contains("static let entryCenterRatio: CGFloat = 0.402") @@ -3947,6 +3954,7 @@ expect(contentViewSource.contains("documentPaneLayoutView()") && contentViewSource.contains("ThreePaneWorkspaceChrome(") && contentViewSource.contains("PaneReorderPreviewView(role: drag.role)") && contentViewSource.contains(".opacity(0.11)") + && contentViewSource.contains("appearanceMode: store.appearanceMode") && stableDocumentSource.contains("PersistentPaneHost(role: role, registry: registry)") && contentViewSource.contains("private struct PersistentPaneRoot: View") && contentViewSource.contains("AgentPaneView(showsPaneHeader: false, reorderRole: reorderRole)")