Repository navigation
Match in-app dialogs to the Ghostty theme colors #15515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import AppKit | ||
| import SwiftUI | ||
|
|
||
| /// Colors for in-app dialogs and cards drawn over terminal content. They use | ||
| /// the Ghostty theme's background and foreground, so a dark terminal theme on a | ||
| /// light macOS appearance (or the reverse) still gets a matching dialog. | ||
| struct GhosttyDialogTheme: Equatable { | ||
| /// Opaque terminal background. Translucent terminals keep readable dialogs. | ||
| let background: NSColor | ||
| /// Terminal foreground, adjusted only when it is unreadable on `background`. | ||
| let foreground: NSColor | ||
| /// The scheme system controls (buttons, progress, dynamic colors) resolve in. | ||
| let colorScheme: ColorScheme | ||
|
|
||
| var secondaryForeground: NSColor { foreground.withAlphaComponent(0.65) } | ||
| var border: NSColor { foreground.withAlphaComponent(0.2) } | ||
| var separator: NSColor { foreground.withAlphaComponent(0.12) } | ||
|
|
||
| static func resolved(background: NSColor, foreground: NSColor) -> GhosttyDialogTheme { | ||
| let opaqueBackground = (background.usingColorSpace(.sRGB) ?? background).withAlphaComponent(1) | ||
| let opaqueForeground = (foreground.usingColorSpace(.sRGB) ?? foreground).withAlphaComponent(1) | ||
| return GhosttyDialogTheme( | ||
| background: opaqueBackground, | ||
| foreground: cmuxReadableForegroundNSColor(preferred: opaqueForeground, on: opaqueBackground), | ||
| colorScheme: cmuxReadableColorScheme(for: opaqueBackground) | ||
| ) | ||
| } | ||
|
|
||
| static func current() -> GhosttyDialogTheme { | ||
| resolved( | ||
| background: GhosttyApp.shared.defaultBackgroundColor, | ||
| foreground: GhosttyApp.shared.defaultForegroundColor | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// Publishes the current ``GhosttyDialogTheme`` and refreshes it when the | ||
| /// Ghostty default appearance changes, so every themed dialog shares one | ||
| /// observer instead of each view subscribing to notifications. | ||
| @MainActor | ||
| @Observable | ||
| final class GhosttyDialogThemeObserver { | ||
| static let shared = GhosttyDialogThemeObserver() | ||
|
|
||
| private(set) var theme: GhosttyDialogTheme | ||
| @ObservationIgnored | ||
| private var observers: [NSObjectProtocol] = [] | ||
|
|
||
| init(notificationCenter: NotificationCenter = .default) { | ||
| theme = .current() | ||
| for name in [Notification.Name.ghosttyDefaultBackgroundDidChange, .ghosttyConfigDidReload] { | ||
| observers.append(notificationCenter.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in | ||
| MainActor.assumeIsolated { self?.refresh() } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func refresh() { | ||
| let next = GhosttyDialogTheme.current() | ||
| if next != theme { theme = next } | ||
| } | ||
| } | ||
|
|
||
| private struct GhosttyDialogThemeModifier: ViewModifier { | ||
| @State private var observer = GhosttyDialogThemeObserver.shared | ||
|
|
||
| func body(content: Content) -> some View { | ||
| let theme = observer.theme | ||
| content | ||
| .environment(\.colorScheme, theme.colorScheme) | ||
| .foregroundStyle(Color(nsColor: theme.foreground)) | ||
| } | ||
| } | ||
|
|
||
| private struct GhosttyDialogSurfaceModifier: ViewModifier { | ||
| let cornerRadius: CGFloat | ||
| @State private var observer = GhosttyDialogThemeObserver.shared | ||
|
|
||
| func body(content: Content) -> some View { | ||
| let theme = observer.theme | ||
| let shape = RoundedRectangle(cornerRadius: cornerRadius, style: .continuous) | ||
| content | ||
| .background(Color(nsColor: theme.background), in: shape) | ||
| .overlay(shape.strokeBorder(Color(nsColor: theme.border), lineWidth: 1)) | ||
| .ghosttyDialogTheme() | ||
| } | ||
| } | ||
|
|
||
| extension View { | ||
| /// Resolves this dialog subtree against the Ghostty theme: its color scheme | ||
| /// and default text color. Hierarchical styles such as `.secondary` derive | ||
| /// from the theme foreground. | ||
| func ghosttyDialogTheme() -> some View { | ||
| modifier(GhosttyDialogThemeModifier()) | ||
| } | ||
|
|
||
| /// Draws a themed dialog panel: Ghostty background, a foreground-tinted | ||
| /// border, and ``ghosttyDialogTheme()`` for the content. | ||
| func ghosttyDialogSurface(cornerRadius: CGFloat) -> some View { | ||
| modifier(GhosttyDialogSurfaceModifier(cornerRadius: cornerRadius)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import AppKit | ||
| import CmuxFoundation | ||
| import SwiftUI | ||
| import Testing | ||
|
|
||
| #if canImport(cmux_DEV) | ||
| @testable import cmux_DEV | ||
| #elseif canImport(cmux) | ||
| @testable import cmux | ||
| #endif | ||
|
|
||
| struct GhosttyDialogThemeTests { | ||
| private func hex(_ color: NSColor) -> String { color.hexString() } | ||
|
|
||
| @Test("A dark Ghostty theme gives a dark dialog even when macOS is light") | ||
| func darkThemeDrivesDialogColors() throws { | ||
| let background = try #require(NSColor(hex: "#1D1F21")) | ||
| let foreground = try #require(NSColor(hex: "#C5C8C6")) | ||
| let theme = GhosttyDialogTheme.resolved(background: background, foreground: foreground) | ||
|
|
||
| #expect(hex(theme.background) == "#1D1F21") | ||
| #expect(hex(theme.foreground) == "#C5C8C6") | ||
| #expect(theme.colorScheme == .dark) | ||
| } | ||
|
|
||
| @Test("A light Ghostty theme gives a light dialog") | ||
| func lightThemeDrivesDialogColors() throws { | ||
| let background = try #require(NSColor(hex: "#FDF6E3")) | ||
| let foreground = try #require(NSColor(hex: "#657B83")) | ||
| let theme = GhosttyDialogTheme.resolved(background: background, foreground: foreground) | ||
|
|
||
| #expect(hex(theme.background) == "#FDF6E3") | ||
| #expect(theme.colorScheme == .light) | ||
| } | ||
|
|
||
| @Test("A translucent terminal background still gives an opaque dialog") | ||
| func translucentBackgroundIsOpaque() throws { | ||
| let background = try #require(NSColor(hex: "#1D1F21")).withAlphaComponent(0.4) | ||
| let theme = GhosttyDialogTheme.resolved(background: background, foreground: .white) | ||
|
|
||
| #expect(theme.background.alphaComponent == 1) | ||
| #expect(hex(theme.background) == "#1D1F21") | ||
| } | ||
|
|
||
| @Test("An unreadable foreground is replaced by a readable one") | ||
| func unreadableForegroundIsAdjusted() throws { | ||
| let background = try #require(NSColor(hex: "#101010")) | ||
| let foreground = try #require(NSColor(hex: "#151515")) | ||
| let theme = GhosttyDialogTheme.resolved(background: background, foreground: foreground) | ||
|
|
||
| #expect(hex(theme.foreground) != "#151515") | ||
| #expect(cmuxContrastRatio(foreground: theme.foreground, background: theme.background) >= 4.5) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.