Skip to content

Commit ce6a253

Browse files
Refactor: Swift code quality improvements across Pika codebase
- Type-safe Notification.Name constants replacing raw string literals - Add typed notification helpers to Eyedropper.Types enum - Replace Combine timer subscriptions with Swift Concurrency (Task/async-await) in ContentView, SwapButtonStyle, EyedropperButton, KeyboardShortcutItem, Toast - Fix @State misuse: KeyboardShortcutItem properties converted to let constants - Deduplicate getBackgroundColor() via Color.pikaControlBackground extension - Deduplicate AppearanceButtonStyle overlay via shared @ViewBuilder function - Data-drive KeyboardShortcutGrid with ShortcutEntry structs (220 → ~60 lines) - Decompose PreferencesView into 6 private sub-view structs - Replace Any type erasure in ComplianceToggleGroup with enum ComplianceData - Consolidate WCAG/APCA conditional logic in Footer with computed properties - Split Cula.swift into NSColor+HSL.swift and NSColor+Lab.swift extensions - Remove dead ColorCompliance protocol - Register new extension files in Xcode project (both targets) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a8b1bd6 commit ce6a253

21 files changed

Lines changed: 877 additions & 1236 deletions

Pika.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
22FE80B325BA0F820063759E /* KeyboardShortcutItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22FE80B225BA0F820063759E /* KeyboardShortcutItem.swift */; };
1919
C49A11482DB394F500EE7E80 /* APCACompliance.swift in Sources */ = {isa = PBXBuildFile; fileRef = C49A11472DB394F500EE7E80 /* APCACompliance.swift */; };
2020
C49A11492DB394F500EE7E80 /* APCACompliance.swift in Sources */ = {isa = PBXBuildFile; fileRef = C49A11472DB394F500EE7E80 /* APCACompliance.swift */; };
21+
CC000001000000000000AAA1 /* Color+PikaControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000AAA0 /* Color+PikaControl.swift */; };
22+
CC000001000000000000AAA2 /* Color+PikaControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000AAA0 /* Color+PikaControl.swift */; };
23+
CC000001000000000000AAB1 /* NSColor+HSL.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000AAB0 /* NSColor+HSL.swift */; };
24+
CC000001000000000000AAB2 /* NSColor+HSL.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000AAB0 /* NSColor+HSL.swift */; };
25+
CC000001000000000000AAC1 /* NSColor+Lab.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000AAC0 /* NSColor+Lab.swift */; };
26+
CC000001000000000000AAC2 /* NSColor+Lab.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000AAC0 /* NSColor+Lab.swift */; };
2127
EA0C525025AA729300AFF716 /* Visualisation.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0C524F25AA729300AFF716 /* Visualisation.swift */; };
2228
EA0C526025AB5A2B00AFF716 /* NavigationMenuItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0C525F25AB5A2B00AFF716 /* NavigationMenuItems.swift */; };
2329
EA0C526425AB5D1700AFF716 /* PikaWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA0C526325AB5D1700AFF716 /* PikaWindow.swift */; };
@@ -152,6 +158,9 @@
152158
C1BF64202C1AE53C004D33DD /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/Localizable.strings"; sourceTree = "<group>"; };
153159
C1BF64212C1AE53C004D33DD /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/Main.strings"; sourceTree = "<group>"; };
154160
C49A11472DB394F500EE7E80 /* APCACompliance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APCACompliance.swift; sourceTree = "<group>"; };
161+
CC000001000000000000AAA0 /* Color+PikaControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+PikaControl.swift"; sourceTree = "<group>"; };
162+
CC000001000000000000AAB0 /* NSColor+HSL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSColor+HSL.swift"; sourceTree = "<group>"; };
163+
CC000001000000000000AAC0 /* NSColor+Lab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSColor+Lab.swift"; sourceTree = "<group>"; };
155164
EA0C524F25AA729300AFF716 /* Visualisation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Visualisation.swift; sourceTree = "<group>"; };
156165
EA0C525F25AB5A2B00AFF716 /* NavigationMenuItems.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationMenuItems.swift; sourceTree = "<group>"; };
157166
EA0C526325AB5D1700AFF716 /* PikaWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PikaWindow.swift; sourceTree = "<group>"; };
@@ -336,6 +345,9 @@
336345
EA635DCB25B3B42B0014D91A /* WCAGCompliance.swift */,
337346
22EF1D9A25B7AA18001102FA /* Sequence.swift */,
338347
C49A11472DB394F500EE7E80 /* APCACompliance.swift */,
348+
CC000001000000000000AAA0 /* Color+PikaControl.swift */,
349+
CC000001000000000000AAB0 /* NSColor+HSL.swift */,
350+
CC000001000000000000AAC0 /* NSColor+Lab.swift */,
339351
);
340352
path = Extensions;
341353
sourceTree = "<group>";
@@ -638,6 +650,9 @@
638650
F8ABAC5A2EAAD0DF008CD152 /* ColorPickOverlay.swift in Sources */,
639651
EAD0B6F8259CF29300FA2F67 /* AboutView.swift in Sources */,
640652
C49A11482DB394F500EE7E80 /* APCACompliance.swift in Sources */,
653+
CC000001000000000000AAA1 /* Color+PikaControl.swift in Sources */,
654+
CC000001000000000000AAB1 /* NSColor+HSL.swift in Sources */,
655+
CC000001000000000000AAC1 /* NSColor+Lab.swift in Sources */,
641656
EAA8AE1925B8EC070049299B /* KeyboardShortcutKey.swift in Sources */,
642657
EA0C526F25AB683400AFF716 /* EyedropperButton.swift in Sources */,
643658
EA72BB8425A5334B008205E7 /* MetalShader.metal in Sources */,
@@ -672,6 +687,9 @@
672687
EAE23DAC2D032A38005BB270 /* PikaTouchBar.swift in Sources */,
673688
EAE23DAD2D032A38005BB270 /* LoadColors.swift in Sources */,
674689
C49A11492DB394F500EE7E80 /* APCACompliance.swift in Sources */,
690+
CC000001000000000000AAA2 /* Color+PikaControl.swift in Sources */,
691+
CC000001000000000000AAB2 /* NSColor+HSL.swift in Sources */,
692+
CC000001000000000000AAC2 /* NSColor+Lab.swift in Sources */,
675693
EAE23DAE2D032A38005BB270 /* SplashTouchBar.swift in Sources */,
676694
EAE23DAF2D032A38005BB270 /* OverflowContentViewModifier.swift in Sources */,
677695
EAE23DB02D032A38005BB270 /* KeyboardShortcutGrid.swift in Sources */,

Pika/AppDelegate.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
351351
}
352352
preferencesWindow.makeKeyAndOrderFront(nil)
353353
preferencesWindow.makeFirstResponder(nil)
354-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerPreferences), object: self)
354+
notificationCenter.post(name: .triggerPreferences, object: self)
355355
}
356356

357357
@IBAction func openSplashWindow(_: Any?) {
@@ -369,77 +369,77 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
369369
}
370370

371371
@IBAction func triggerPickForeground(_: Any) {
372-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerPickForeground), object: self)
372+
notificationCenter.post(name: .triggerPickForeground, object: self)
373373
}
374374

375375
@IBAction func triggerPickBackground(_: Any) {
376-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerPickBackground), object: self)
376+
notificationCenter.post(name: .triggerPickBackground, object: self)
377377
}
378378

379379
@IBAction func triggerCopyForeground(_: Any) {
380-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerCopyForeground), object: self)
380+
notificationCenter.post(name: .triggerCopyForeground, object: self)
381381
}
382382

383383
@IBAction func triggerCopyBackground(_: Any) {
384-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerCopyBackground), object: self)
384+
notificationCenter.post(name: .triggerCopyBackground, object: self)
385385
}
386386

387387
@IBAction func triggerSystemPickerForeground(_: Any) {
388-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerSystemPickerForeground), object: self)
388+
notificationCenter.post(name: .triggerSystemPickerForeground, object: self)
389389
}
390390

391391
@IBAction func triggerSystemPickerBackground(_: Any) {
392-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerSystemPickerBackground), object: self)
392+
notificationCenter.post(name: .triggerSystemPickerBackground, object: self)
393393
}
394394

395395
@IBAction func triggerSwap(_: Any) {
396-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerSwap), object: self)
396+
notificationCenter.post(name: .triggerSwap, object: self)
397397
}
398398

399399
@IBAction func triggerUndo(_: Any) {
400-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerUndo), object: self)
400+
notificationCenter.post(name: .triggerUndo, object: self)
401401
undoManager.undo()
402402
}
403403

404404
@IBAction func triggerRedo(_: Any) {
405-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerRedo), object: self)
405+
notificationCenter.post(name: .triggerRedo, object: self)
406406
undoManager.redo()
407407
}
408408

409409
@IBAction func triggerCopyText(_: Any) {
410-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerCopyText), object: self)
410+
notificationCenter.post(name: .triggerCopyText, object: self)
411411
}
412412

413413
@IBAction func triggerCopyData(_: Any) {
414-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerCopyData), object: self)
414+
notificationCenter.post(name: .triggerCopyData, object: self)
415415
}
416416

417417
@IBAction func triggerFormatHex(_: Any) {
418-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatHex), object: self)
418+
notificationCenter.post(name: .triggerFormatHex, object: self)
419419
}
420420

421421
@IBAction func triggerFormatRGB(_: Any) {
422-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatRGB), object: self)
422+
notificationCenter.post(name: .triggerFormatRGB, object: self)
423423
}
424424

425425
@IBAction func triggerFormatHSB(_: Any) {
426-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatHSB), object: self)
426+
notificationCenter.post(name: .triggerFormatHSB, object: self)
427427
}
428428

429429
@IBAction func triggerFormatHSL(_: Any) {
430-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatHSL), object: self)
430+
notificationCenter.post(name: .triggerFormatHSL, object: self)
431431
}
432432

433433
@IBAction func triggerFormatOpenGL(_: Any) {
434-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatOpenGL), object: self)
434+
notificationCenter.post(name: .triggerFormatOpenGL, object: self)
435435
}
436436

437437
@IBAction func triggerFormatLAB(_: Any) {
438-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatLAB), object: self)
438+
notificationCenter.post(name: .triggerFormatLAB, object: self)
439439
}
440440

441441
@IBAction func triggerFormatOKLCH(_: Any) {
442-
notificationCenter.post(name: Notification.Name(PikaConstants.ncTriggerFormatOKLCH), object: self)
442+
notificationCenter.post(name: .triggerFormatOKLCH, object: self)
443443
}
444444

445445
@IBAction func hidePika(_: Any) {

Pika/Constants/Constants.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ enum PikaConstants {
6262
static let disabledFormats: [ColorFormat] = [.hex, .hsl, .opengl, .lab, .oklch]
6363
}
6464

65+
extension Notification.Name {
66+
static let triggerPickForeground = Notification.Name(PikaConstants.ncTriggerPickForeground)
67+
static let triggerPickBackground = Notification.Name(PikaConstants.ncTriggerPickBackground)
68+
static let triggerCopyForeground = Notification.Name(PikaConstants.ncTriggerCopyForeground)
69+
static let triggerCopyBackground = Notification.Name(PikaConstants.ncTriggerCopyBackground)
70+
static let triggerCopyText = Notification.Name(PikaConstants.ncTriggerCopyText)
71+
static let triggerCopyData = Notification.Name(PikaConstants.ncTriggerCopyData)
72+
static let triggerSystemPickerForeground = Notification.Name(PikaConstants.ncTriggerSystemPickerForeground)
73+
static let triggerSystemPickerBackground = Notification.Name(PikaConstants.ncTriggerSystemPickerBackground)
74+
static let triggerSwap = Notification.Name(PikaConstants.ncTriggerSwap)
75+
static let triggerUndo = Notification.Name(PikaConstants.ncTriggerUndo)
76+
static let triggerRedo = Notification.Name(PikaConstants.ncTriggerRedo)
77+
static let triggerPreferences = Notification.Name(PikaConstants.ncTriggerPreferences)
78+
static let triggerFormatHex = Notification.Name(PikaConstants.ncTriggerFormatHex)
79+
static let triggerFormatRGB = Notification.Name(PikaConstants.ncTriggerFormatRGB)
80+
static let triggerFormatHSB = Notification.Name(PikaConstants.ncTriggerFormatHSB)
81+
static let triggerFormatHSL = Notification.Name(PikaConstants.ncTriggerFormatHSL)
82+
static let triggerFormatOpenGL = Notification.Name(PikaConstants.ncTriggerFormatOpenGL)
83+
static let triggerFormatLAB = Notification.Name(PikaConstants.ncTriggerFormatLAB)
84+
static let triggerFormatOKLCH = Notification.Name(PikaConstants.ncTriggerFormatOKLCH)
85+
static let triggerQuit = Notification.Name(PikaConstants.ncTriggerQuit)
86+
}
87+
6588
enum PikaText {
6689
static let textAppName = NSLocalizedString("app.name", comment: "Pika")
6790

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import SwiftUI
2+
3+
extension Color {
4+
static func pikaControlBackground(for colorScheme: ColorScheme) -> Color {
5+
colorScheme == .dark
6+
? Color(red: 27 / 255, green: 27 / 255, blue: 27 / 255)
7+
: Color(red: 233 / 255, green: 233 / 255, blue: 233 / 255)
8+
}
9+
}

0 commit comments

Comments
 (0)