11import SwiftUI
22import AppKit
33
4+ enum Radius {
5+ static let control : CGFloat = 6
6+ static let group : CGFloat = 10
7+ static let surface : CGFloat = 14
8+
9+ static func shape ( _ radius : CGFloat ) -> RoundedRectangle {
10+ RoundedRectangle ( cornerRadius : radius , style : . continuous )
11+ }
12+ }
13+
14+ enum AppSurface {
15+ static let card = Color ( nsColor : NSColor ( name : nil , dynamicProvider : cardColor ) )
16+
17+ /// `controlBackgroundColor` is white above the window in light mode, but darker than it in dark mode.
18+ private static func cardColor ( _ appearance : NSAppearance ) -> NSColor {
19+ var resolved = NSColor . controlBackgroundColor
20+ appearance . performAsCurrentDrawingAppearance {
21+ let window = NSColor . windowBackgroundColor . usingColorSpace ( . sRGB ) ?? . windowBackgroundColor
22+ let control = NSColor . controlBackgroundColor . usingColorSpace ( . sRGB ) ?? . controlBackgroundColor
23+ if luminance ( control ) + 0.01 >= luminance ( window ) {
24+ resolved = control
25+ } else {
26+ resolved = window . blended ( withFraction : 0.08 , of : . white ) ?? window
27+ }
28+ }
29+ return resolved
30+ }
31+
32+ private static func luminance ( _ color : NSColor ) -> CGFloat {
33+ 0.2126 * color . redComponent + 0.7152 * color . greenComponent + 0.0722 * color . blueComponent
34+ }
35+ }
36+
437struct AppIcon : View {
538 let app : AppEntry
639 let size : CGFloat
@@ -12,47 +45,118 @@ struct AppIcon: View {
1245}
1346
1447struct IconButtonChrome : ViewModifier {
15- var tint : Color = . accentColor
48+ var tint : Color = . primary
1649 var isPressed = false
1750 @ State private var isHovering = false
1851 @ Environment ( \. isEnabled ) private var isEnabled
52+ @ Environment ( \. accessibilityReduceMotion ) private var reduceMotion
53+
54+ private var fillOpacity : Double {
55+ guard isEnabled else { return 0 }
56+ if isPressed { return 0.16 }
57+ if isHovering { return 0.10 }
58+ return 0.06
59+ }
60+
61+ private var strokeOpacity : Double {
62+ guard isEnabled , isHovering || isPressed else { return 0 }
63+ return isPressed ? 0.22 : 0.16
64+ }
1965
2066 func body ( content : Content ) -> some View {
2167 content
2268 . labelStyle ( . iconOnly )
2369 . font ( . system ( size : 13 , weight : . medium ) )
2470 . foregroundStyle ( isEnabled ? tint : Color ( nsColor : . tertiaryLabelColor ) )
2571 . frame ( width : 28 , height : 28 )
26- . background (
27- RoundedRectangle ( cornerRadius : 8 )
28- . fill ( tint . opacity ( isPressed ? 0.16 : isHovering ? 0.12 : 0.07 ) )
29- )
72+ . background ( Color . primary . opacity ( fillOpacity ) , in : Radius . shape ( Radius . control ) )
3073 . overlay {
31- RoundedRectangle ( cornerRadius : 8 )
32- . strokeBorder ( tint . opacity ( isPressed ? 0.28 : isHovering ? 0.22 : 0.12 ) , lineWidth : 1 )
74+ Radius . shape ( Radius . control )
75+ . strokeBorder ( Color . primary . opacity ( strokeOpacity ) , lineWidth : 1 )
3376 }
34- . contentShape ( RoundedRectangle ( cornerRadius : 8 ) )
77+ . contentShape ( Radius . shape ( Radius . control ) )
3578 . onHover { isHovering = $0 && isEnabled }
36- . animation ( . easeOut ( duration : 0.18 ) , value : isHovering )
37- . animation ( . easeOut ( duration : 0.12 ) , value : isPressed )
79+ . onChange ( of : isEnabled ) { _ , enabled in
80+ if ! enabled { isHovering = false }
81+ }
82+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : isHovering )
83+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : isPressed )
84+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : isEnabled )
3885 }
3986}
4087
4188extension View {
42- func iconButtonChrome ( tint : Color = . accentColor , isPressed : Bool = false ) -> some View {
89+ func iconButtonChrome ( tint : Color = . primary , isPressed : Bool = false ) -> some View {
4390 modifier ( IconButtonChrome ( tint : tint , isPressed : isPressed ) )
4491 }
4592}
4693
4794struct IconButtonStyle : ButtonStyle {
48- var tint : Color = . accentColor
95+ var tint : Color = . primary
4996
5097 func makeBody ( configuration : Configuration ) -> some View {
5198 configuration . label
5299 . iconButtonChrome ( tint : tint , isPressed : configuration . isPressed )
53100 }
54101}
55102
103+ /// Neutral hover and press for controls that otherwise look like static text.
104+ struct QuietAffordance : ViewModifier {
105+ var cornerRadius : CGFloat = Radius . control
106+ @ Environment ( \. accessibilityReduceMotion ) private var reduceMotion
107+ @ State private var hovering = false
108+ @ GestureState private var pressed = false
109+
110+ func body ( content : Content ) -> some View {
111+ content
112+ . background {
113+ Radius . shape ( cornerRadius )
114+ . fill ( Color . primary . opacity ( pressed ? 0.08 : hovering ? 0.04 : 0 ) )
115+ }
116+ . scaleEffect ( reduceMotion || ! pressed ? 1 : 0.985 )
117+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : hovering )
118+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : pressed )
119+ . onHover { hovering = $0 }
120+ . simultaneousGesture ( DragGesture ( minimumDistance : 0 ) . updating ( $pressed ) { _ , state , _ in
121+ state = true
122+ } )
123+ }
124+ }
125+
126+ extension View {
127+ func quietAffordance ( cornerRadius : CGFloat = Radius . control ) -> some View {
128+ modifier ( QuietAffordance ( cornerRadius : cornerRadius ) )
129+ }
130+ }
131+
132+ struct QuietPressButtonStyle : ButtonStyle {
133+ var cornerRadius : CGFloat = Radius . control
134+
135+ func makeBody ( configuration : Configuration ) -> some View {
136+ QuietPressButton ( cornerRadius : cornerRadius , isPressed : configuration . isPressed , label : configuration . label )
137+ }
138+ }
139+
140+ private struct QuietPressButton : View {
141+ @ Environment ( \. accessibilityReduceMotion ) private var reduceMotion
142+ @ State private var hovering = false
143+ var cornerRadius : CGFloat
144+ var isPressed : Bool
145+ var label : ButtonStyleConfiguration . Label
146+
147+ var body : some View {
148+ label
149+ . background {
150+ Radius . shape ( cornerRadius )
151+ . fill ( Color . primary . opacity ( isPressed ? 0.08 : hovering ? 0.04 : 0 ) )
152+ }
153+ . scaleEffect ( reduceMotion || ! isPressed ? 1 : 0.985 )
154+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : hovering )
155+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : isPressed )
156+ . onHover { hovering = $0 }
157+ }
158+ }
159+
56160struct EmptyState : View {
57161 let symbol : String
58162 let title : String
@@ -95,11 +199,62 @@ struct SearchField: View {
95199 }
96200 }
97201 . font ( . callout ) . padding ( . horizontal , 9 ) . padding ( . vertical , 7 )
98- . background ( . quaternary . opacity ( 0.6 ) , in : RoundedRectangle ( cornerRadius : 7 ) )
99- . overlay {
100- RoundedRectangle ( cornerRadius : 7 )
101- . strokeBorder ( isFocused ? Color . accentColor . opacity ( 0.7 ) : . clear , lineWidth : 2 )
202+ . fieldChrome ( focused : isFocused )
203+ }
204+ }
205+
206+ struct FieldChrome : ViewModifier {
207+ var focused : Bool
208+ var radius : CGFloat = Radius . control
209+ @ Environment ( \. accessibilityReduceMotion ) private var reduceMotion
210+
211+ func body ( content : Content ) -> some View {
212+ content
213+ . background ( Color ( nsColor : . controlBackgroundColor ) , in : Radius . shape ( radius ) )
214+ . overlay {
215+ FieldFocusRing ( focused : focused , radius : radius )
216+ . animation ( Motion . feedback ( reduced : reduceMotion ) , value : focused )
217+ }
218+ }
219+ }
220+
221+ private struct FieldFocusRing : View {
222+ var focused : Bool
223+ var radius : CGFloat
224+
225+ var body : some View {
226+ ZStack {
227+ Radius . shape ( radius )
228+ . strokeBorder ( focused ? Color . accentColor : Color . primary . opacity ( 0.12 ) , lineWidth : 1 )
229+ Radius . shape ( radius )
230+ . inset ( by : - 1.5 )
231+ . stroke ( Color . accentColor . opacity ( focused ? 0.28 : 0 ) , lineWidth : 3 )
102232 }
233+ . allowsHitTesting ( false )
234+ }
235+ }
236+
237+ struct ElevatedCard : ViewModifier {
238+ var radius : CGFloat = Radius . surface
239+
240+ func body ( content : Content ) -> some View {
241+ content
242+ . background ( AppSurface . card , in : Radius . shape ( radius ) )
243+ . overlay {
244+ Radius . shape ( radius )
245+ . strokeBorder ( Color . primary . opacity ( 0.08 ) , lineWidth : 1 )
246+ . allowsHitTesting ( false )
247+ }
248+ }
249+ }
250+
251+ extension View {
252+ func fieldChrome ( focused : Bool , radius : CGFloat = Radius . control ) -> some View {
253+ modifier ( FieldChrome ( focused : focused , radius : radius ) )
254+ }
255+
256+ func elevatedCard ( radius : CGFloat = Radius . surface ) -> some View {
257+ modifier ( ElevatedCard ( radius : radius ) )
103258 }
104259}
105260
@@ -110,7 +265,7 @@ struct ShortcutHint: View {
110265 HStack ( spacing : 5 ) {
111266 Text ( keys ) . font ( . system ( size : 11 , weight : . medium , design : . monospaced ) )
112267 . padding ( . horizontal , 5 ) . padding ( . vertical , 3 )
113- . background ( . quaternary , in : RoundedRectangle ( cornerRadius : 4 ) )
268+ . background ( . quaternary , in : Radius . shape ( Radius . control ) )
114269 Text ( title ) . font ( . caption )
115270 }
116271 . foregroundStyle ( . secondary )
0 commit comments