From 77e82b4d2a26ebedd86802d6e0e7166b9ef5e5be Mon Sep 17 00:00:00 2001 From: Pete Schaffner Date: Fri, 9 Feb 2024 22:46:49 +0100 Subject: [PATCH] Improve window buttons backdrop with window opacity --- macos/Ghostty.xcodeproj/project.pbxproj | 11 +++++++ .../App/macOS/Ghostty-Bridging-Header.h | 5 +++ .../Features/Terminal/TerminalWindow.swift | 31 +++++++++---------- ...erminalWindowButtonsBackdropOverlayLayer.h | 4 +++ ...erminalWindowButtonsBackdropOverlayLayer.m | 9 ++++++ 5 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 macos/Sources/App/macOS/Ghostty-Bridging-Header.h create mode 100644 macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.h create mode 100644 macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.m diff --git a/macos/Ghostty.xcodeproj/project.pbxproj b/macos/Ghostty.xcodeproj/project.pbxproj index b7e0134c8..583313b92 100644 --- a/macos/Ghostty.xcodeproj/project.pbxproj +++ b/macos/Ghostty.xcodeproj/project.pbxproj @@ -69,6 +69,7 @@ C159E81D2B66A06B00FDFE9C /* OSColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C159E81C2B66A06B00FDFE9C /* OSColor+Extension.swift */; }; C159E89D2B69A2EF00FDFE9C /* OSColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C159E81C2B66A06B00FDFE9C /* OSColor+Extension.swift */; }; C1F26EA72B738B9900404083 /* NSView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1F26EA62B738B9900404083 /* NSView+Extension.swift */; }; + C1F26EE92B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = C1F26EE82B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -130,6 +131,9 @@ AEF9CE232B6AD07A0017E195 /* TerminalToolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalToolbar.swift; sourceTree = ""; }; C159E81C2B66A06B00FDFE9C /* OSColor+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OSColor+Extension.swift"; sourceTree = ""; }; C1F26EA62B738B9900404083 /* NSView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSView+Extension.swift"; sourceTree = ""; }; + C1F26EE72B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TerminalWindowButtonsBackdropOverlayLayer.h; sourceTree = ""; }; + C1F26EE82B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TerminalWindowButtonsBackdropOverlayLayer.m; sourceTree = ""; }; + C1F26EEA2B76CC2400404083 /* Ghostty-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Ghostty-Bridging-Header.h"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -235,6 +239,7 @@ A5FEB2FF2ABB69450068369E /* main.swift */, A53426342A7DA53D00EBB7A2 /* AppDelegate.swift */, 857F63802A5E64F200CA4815 /* MainMenu.xib */, + C1F26EEA2B76CC2400404083 /* Ghostty-Bridging-Header.h */, ); path = macOS; sourceTree = ""; @@ -286,6 +291,8 @@ A5D0AF3A2B36A1DE00D21823 /* TerminalRestorable.swift */, A596309D2AEE1D6C00D64628 /* TerminalView.swift */, A51B78462AF4B58B00F3EDB9 /* TerminalWindow.swift */, + C1F26EE72B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.h */, + C1F26EE82B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.m */, AEF9CE232B6AD07A0017E195 /* TerminalToolbar.swift */, A535B9D9299C569B0017E2E4 /* ErrorView.swift */, ); @@ -477,6 +484,7 @@ A56D58892ACDE6CA00508D2C /* ServiceProvider.swift in Sources */, A51BFC222B2FB6B400E92F16 /* AboutView.swift in Sources */, A5278A9B2AA05B2600CD3039 /* Ghostty.Input.swift in Sources */, + C1F26EE92B76CBFC00404083 /* TerminalWindowButtonsBackdropOverlayLayer.m in Sources */, A59630972AEE163600D64628 /* HostingWindow.swift in Sources */, A59630A02AEF6AEB00D64628 /* TerminalManager.swift in Sources */, A51BFC2B2B30F6BE00E92F16 /* UpdateDelegate.swift in Sources */, @@ -614,6 +622,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.mitchellh.ghostty; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "Sources/App/macOS/Ghostty-Bridging-Header.h"; SWIFT_VERSION = 5.0; }; name = ReleaseLocal; @@ -763,6 +772,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.mitchellh.ghostty; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "Sources/App/macOS/Ghostty-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; }; @@ -801,6 +811,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.mitchellh.ghostty; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_OBJC_BRIDGING_HEADER = "Sources/App/macOS/Ghostty-Bridging-Header.h"; SWIFT_VERSION = 5.0; }; name = Release; diff --git a/macos/Sources/App/macOS/Ghostty-Bridging-Header.h b/macos/Sources/App/macOS/Ghostty-Bridging-Header.h new file mode 100644 index 000000000..0b15a518e --- /dev/null +++ b/macos/Sources/App/macOS/Ghostty-Bridging-Header.h @@ -0,0 +1,5 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + +#import "TerminalWindowButtonsBackdropOverlayLayer.h" diff --git a/macos/Sources/Features/Terminal/TerminalWindow.swift b/macos/Sources/Features/Terminal/TerminalWindow.swift index d42a72de5..4cb75bd99 100644 --- a/macos/Sources/Features/Terminal/TerminalWindow.swift +++ b/macos/Sources/Features/Terminal/TerminalWindow.swift @@ -348,12 +348,18 @@ fileprivate class WindowDragView: NSView { // A view that matches the color of selected and unselected tabs in the adjacent tab bar. fileprivate class WindowButtonsBackdropView: NSView { - private let backgroundColor: NSColor + private let overlayLayer = TerminalWindowButtonsBackdropOverlayLayer() private let isLightTheme: Bool var isHighlighted: Bool = true { didSet { - setNeedsDisplay(self.bounds) + if isLightTheme { + overlayLayer.isHidden = isHighlighted + layer?.backgroundColor = .clear + } else { + overlayLayer.isHidden = true + layer?.backgroundColor = isHighlighted ? .clear : CGColor(genericGrayGamma2_2Gray: 0.0, alpha: 0.45) + } } } @@ -362,25 +368,16 @@ fileprivate class WindowButtonsBackdropView: NSView { } init(backgroundColor: CGColor) { - self.backgroundColor = NSColor(cgColor: backgroundColor)! self.isLightTheme = NSColor(cgColor: backgroundColor)!.isLightColor super.init(frame: .zero) - } - override func draw(_ dirtyRect: NSRect) { - if isLightTheme { - backgroundColor.setFill() - bounds.fill() + wantsLayer = true - if !isHighlighted { - let overlayColor = NSColor(cgColor: CGColor(genericGrayGamma2_2Gray: 0.95, alpha: 1))! - overlayColor.setFill() - bounds.fill(using: .plusDarker) - } - } else { - (isHighlighted ? backgroundColor : NSColor(cgColor: CGColor(genericGrayGamma2_2Gray: 0.0, alpha: 0.45))!).setFill() - bounds.fill() - } + overlayLayer.frame = layer!.bounds + overlayLayer.autoresizingMask = [.layerWidthSizable, .layerHeightSizable] + overlayLayer.backgroundColor = CGColor(genericGrayGamma2_2Gray: 0.95, alpha: 1) + + layer?.addSublayer(overlayLayer) } } diff --git a/macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.h b/macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.h new file mode 100644 index 000000000..2f5d0169a --- /dev/null +++ b/macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.h @@ -0,0 +1,4 @@ +#import + +@interface TerminalWindowButtonsBackdropOverlayLayer: CALayer +@end diff --git a/macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.m b/macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.m new file mode 100644 index 000000000..9ce1250ec --- /dev/null +++ b/macos/Sources/Features/Terminal/TerminalWindowButtonsBackdropOverlayLayer.m @@ -0,0 +1,9 @@ +#import "TerminalWindowButtonsBackdropOverlayLayer.h" + +@implementation TerminalWindowButtonsBackdropOverlayLayer + +// A private compositing filter ("plus darker") that is used in titlebar +// tab bars to create the effect of recessed, unselected tabs. +- (id)compositingFilter { return @"plusD"; } + +@end