From 375df5715500df9a73c68ccec61c6baa5797186e Mon Sep 17 00:00:00 2001 From: Pete Schaffner Date: Mon, 29 Jan 2024 21:56:22 +0100 Subject: [PATCH] Tint split view divider based on theme background --- macos/Ghostty.xcodeproj/project.pbxproj | 4 ++++ macos/Sources/Ghostty/Ghostty.App.swift | 2 +- macos/Sources/Helpers/NSColor+Extension.swift | 21 +++++++++++++++++++ .../Helpers/SplitView/SplitView.Divider.swift | 14 +++++++++++-- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 macos/Sources/Helpers/NSColor+Extension.swift diff --git a/macos/Ghostty.xcodeproj/project.pbxproj b/macos/Ghostty.xcodeproj/project.pbxproj index 458feec34..08cc344a4 100644 --- a/macos/Ghostty.xcodeproj/project.pbxproj +++ b/macos/Ghostty.xcodeproj/project.pbxproj @@ -65,6 +65,7 @@ A5E112952AF73E8A00C6E0C2 /* ClipboardConfirmationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5E112942AF73E8A00C6E0C2 /* ClipboardConfirmationController.swift */; }; A5E112972AF7401B00C6E0C2 /* ClipboardConfirmationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5E112962AF7401B00C6E0C2 /* ClipboardConfirmationView.swift */; }; A5FEB3002ABB69450068369E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5FEB2FF2ABB69450068369E /* main.swift */; }; + C159E81D2B66A06B00FDFE9C /* NSColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C159E81C2B66A06B00FDFE9C /* NSColor+Extension.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -123,6 +124,7 @@ A5E112942AF73E8A00C6E0C2 /* ClipboardConfirmationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClipboardConfirmationController.swift; sourceTree = ""; }; A5E112962AF7401B00C6E0C2 /* ClipboardConfirmationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClipboardConfirmationView.swift; sourceTree = ""; }; A5FEB2FF2ABB69450068369E /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + C159E81C2B66A06B00FDFE9C /* NSColor+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSColor+Extension.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -187,6 +189,7 @@ 8503D7C62A549C66006CFF3D /* FullScreenHandler.swift */, A59630962AEE163600D64628 /* HostingWindow.swift */, A59FB5D02AE0DEA7009128F3 /* MetalView.swift */, + C159E81C2B66A06B00FDFE9C /* NSColor+Extension.swift */, A5CEAFDA29B8005900646FDA /* SplitView */, ); path = Helpers; @@ -489,6 +492,7 @@ A5E112952AF73E8A00C6E0C2 /* ClipboardConfirmationController.swift in Sources */, 8503D7C72A549C66006CFF3D /* FullScreenHandler.swift in Sources */, A596309E2AEE1D6C00D64628 /* TerminalView.swift in Sources */, + C159E81D2B66A06B00FDFE9C /* NSColor+Extension.swift in Sources */, A5CEAFDE29B8058B00646FDA /* SplitView.Divider.swift in Sources */, A5E112972AF7401B00C6E0C2 /* ClipboardConfirmationView.swift in Sources */, A514C8D82B54DC6800493A16 /* Ghostty.App.swift in Sources */, diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index f988c553c..1be89ea4e 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -30,7 +30,7 @@ extension Ghostty { /// The global app configuration. This defines the app level configuration plus any behavior /// for new windows, tabs, etc. Note that when creating a new window, it may inherit some /// configuration (i.e. font size) from the previously focused window. This would override this. - private(set) var config: Config + @Published private(set) var config: Config /// The ghostty app instance. We only have one of these for the entire app, although I guess /// in theory you can have multiple... I don't know why you would... diff --git a/macos/Sources/Helpers/NSColor+Extension.swift b/macos/Sources/Helpers/NSColor+Extension.swift new file mode 100644 index 000000000..26ab2c7e2 --- /dev/null +++ b/macos/Sources/Helpers/NSColor+Extension.swift @@ -0,0 +1,21 @@ +// +// NSColor+Extension.swift +// Ghostty +// +// Created by Pete Schaffner on 28/01/2024. +// + +import AppKit + +extension NSColor { + var isLightColor: Bool { + var r: CGFloat = 0 + var g: CGFloat = 0 + var b: CGFloat = 0 + var a: CGFloat = 0 + + self.getRed(&r, green: &g, blue: &b, alpha: &a) + let luminance = (0.299 * r) + (0.587 * g) + (0.114 * b) + return luminance > 0.5 + } +} diff --git a/macos/Sources/Helpers/SplitView/SplitView.Divider.swift b/macos/Sources/Helpers/SplitView/SplitView.Divider.swift index aba1c48f4..4d7a11a0e 100644 --- a/macos/Sources/Helpers/SplitView/SplitView.Divider.swift +++ b/macos/Sources/Helpers/SplitView/SplitView.Divider.swift @@ -3,6 +3,8 @@ import SwiftUI extension SplitView { /// The split divider that is rendered and can be used to resize a split view. struct Divider: View { + @EnvironmentObject var ghostty: Ghostty.App + let direction: SplitViewDirection let visibleSize: CGFloat let invisibleSize: CGFloat @@ -42,13 +44,21 @@ extension SplitView { return visibleSize + invisibleSize } } - + + private var color: Color { + let backgroundColor = NSColor(ghostty.config.backgroundColor) + let isLightBackground = backgroundColor.isLightColor + let newColor = isLightBackground ? backgroundColor.shadow(withLevel: 0.1) : backgroundColor.shadow(withLevel: 0.4) + + return Color(nsColor: newColor ?? .gray.withAlphaComponent(0.5)) + } + var body: some View { ZStack { Color.clear .frame(width: invisibleWidth, height: invisibleHeight) Rectangle() - .fill(Color.gray) + .fill(color) .frame(width: visibleWidth, height: visibleHeight) } .onHover { isHovered in