@@ -18,15 +18,50 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
1818 var aboutWindow : NSWindow !
1919 var preferencesWindow : NSWindow !
2020 var eyedroppers : Eyedroppers !
21+ let colorHistoryManager = ColorHistoryManager ( )
2122
2223 var undoManager = UndoManager ( )
24+ private var hadColorHistory = false
25+
26+ override init ( ) {
27+ super. init ( )
28+ eyedroppers = Eyedroppers ( )
29+ eyedroppers. foreground. undoManager = undoManager
30+ eyedroppers. background. undoManager = undoManager
31+ eyedroppers. foreground. colorHistoryManager = colorHistoryManager
32+ eyedroppers. background. colorHistoryManager = colorHistoryManager
33+ }
2334
2435 var pikaTouchBarController : PikaTouchBarController !
2536 var splashTouchBarController : SplashTouchBarController !
2637 var aboutTouchBarController : SplashTouchBarController !
2738
2839 let notificationCenter = NotificationCenter . default
2940
41+ private func idealWindowContentHeight( ) -> CGFloat {
42+ SwatchLayout . totalHeight (
43+ base: 230 ,
44+ hasHistory: hadColorHistory,
45+ paletteCount: 0
46+ )
47+ }
48+
49+ func updateWindowSize( animate: Bool ) {
50+ guard pikaWindow != nil else { return }
51+ let contentHeight = idealWindowContentHeight ( )
52+ let targetContent = NSRect ( x: 0 , y: 0 , width: CGFloat ( pikaWindow. frame. width) , height: contentHeight)
53+ let targetFrame = pikaWindow. frameRect ( forContentRect: targetContent)
54+ // Pin the top edge of the window.
55+ let currentFrame = pikaWindow. frame
56+ let newFrame = NSRect (
57+ x: currentFrame. origin. x,
58+ y: currentFrame. origin. y + currentFrame. size. height - targetFrame. height,
59+ width: currentFrame. size. width,
60+ height: targetFrame. height
61+ )
62+ pikaWindow. setFrame ( newFrame, display: true , animate: animate && pikaWindow. isVisible)
63+ }
64+
3065 func setupAppMode( ) {
3166 var currentMode = Defaults [ . appMode] == . regular
3267 ? NSApplication . ActivationPolicy. regular
@@ -41,7 +76,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
4176 NSApp . setActivationPolicy ( newMode)
4277 NSApp . activate ( ignoringOtherApps: true )
4378 if change. newValue == . regular {
44- DispatchQueue . main. asyncAfter ( deadline : . now ( ) ) {
79+ DispatchQueue . main. async {
4580 NSApp . unhide ( self )
4681
4782 if let window = NSApp . windows. first {
@@ -99,11 +134,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
99134 setupAppMode ( )
100135 setupStatusBar ( )
101136
102- // Set up eyedroppers
103- eyedroppers = Eyedroppers ( )
104- eyedroppers. foreground. undoManager = undoManager
105- eyedroppers. background. undoManager = undoManager
106-
107137 // Define content view
108138 let contentView = ContentView ( )
109139 . environmentObject ( eyedroppers)
@@ -112,13 +142,26 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
112142 maxWidth: 650 ,
113143 minHeight: 230 ,
114144 idealHeight: 230 ,
115- maxHeight: 400 ,
145+ maxHeight: 550 ,
116146 alignment: . center)
117147
118148 pikaWindow = PikaWindow . createPrimaryWindow ( )
119149 pikaWindow. contentView = NSHostingView ( rootView: contentView)
120150 pikaTouchBarController = PikaTouchBarController ( window: pikaWindow)
121151
152+ hadColorHistory = !Defaults[ . colorHistory] . isEmpty
153+ updateWindowSize ( animate: false )
154+
155+ Defaults . observe ( . colorHistory) { [ weak self] _ in
156+ DispatchQueue . main. async {
157+ guard let self = self else { return }
158+ let hasHistory = !Defaults[ . colorHistory] . isEmpty
159+ guard hasHistory != self . hadColorHistory else { return }
160+ self . hadColorHistory = hasHistory
161+ self . updateWindowSize ( animate: true )
162+ }
163+ } . tieToLifetime ( of: self )
164+
122165 // Define global keyboard shortcuts
123166 KeyboardShortcuts . onKeyUp ( for: . togglePika) { [ ] in
124167 if Defaults [ . viewedSplash] {
@@ -157,25 +200,26 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
157200 // swiftlint:disable function_body_length
158201 @objc func handleGetURLEvent( _ event: NSAppleEventDescriptor , withReplyEvent _: NSAppleEventDescriptor ) {
159202 if let urlString = event. forKeyword ( AEKeyword ( keyDirectObject) ) ? . stringValue {
160- let url = URL ( string: urlString)
161- guard url != nil , let scheme = url!. scheme, let action = url!. host else {
162- // some error
203+ guard let url = URL ( string: urlString) ,
204+ let scheme = url. scheme,
205+ let action = url. host
206+ else {
163207 return
164208 }
165209
166- var list = url! . pathComponents. dropFirst ( )
210+ var list = url. pathComponents. dropFirst ( )
167211 let task = list. popFirst ( )
168212 let colorFormat = list. popFirst ( )
169213
170214 if scheme. caseInsensitiveCompare ( " pika " ) == . orderedSame {
171- if colorFormat != nil {
172- if let format = ColorFormat . withLabel ( colorFormat! ) {
173- Defaults [ . colorFormat ] = format
174- }
215+ if let colorFormat = colorFormat ,
216+ let format = ColorFormat . withLabel ( colorFormat)
217+ {
218+ Defaults [ . colorFormat ] = format
175219 }
176220
177221 if action == " format " {
178- if let format = ColorFormat . withLabel ( task! ) {
222+ if let task = task , let format = ColorFormat . withLabel ( task) {
179223 Defaults [ . colorFormat] = format
180224 }
181225 }
@@ -385,14 +429,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
385429 }
386430
387431 @IBAction func triggerSystemPickerForeground( _: Any ) {
432+ eyedroppers. foreground. togglePicker ( )
388433 notificationCenter. post ( name: Notification . Name ( PikaConstants . ncTriggerSystemPickerForeground) , object: self )
389434 }
390435
391436 @IBAction func triggerSystemPickerBackground( _: Any ) {
437+ eyedroppers. background. togglePicker ( )
392438 notificationCenter. post ( name: Notification . Name ( PikaConstants . ncTriggerSystemPickerBackground) , object: self )
393439 }
394440
395441 @IBAction func triggerSwap( _: Any ) {
442+ swap ( & eyedroppers. foreground. color, & eyedroppers. background. color)
396443 notificationCenter. post ( name: Notification . Name ( PikaConstants . ncTriggerSwap) , object: self )
397444 }
398445
0 commit comments