Skip to content

Commit

Permalink
Target macOS 13
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 28, 2023
1 parent 77dba2d commit be6c837
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 991 deletions.
9 changes: 7 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
only_rules:
- accessibility_trait_for_button
- array_init
- blanket_disable_command
- block_based_kvo
- class_delegate_protocol
- closing_brace
Expand All @@ -21,13 +22,15 @@ only_rules:
- control_statement
- custom_rules
- deployment_target
- direct_return
- discarded_notification_center_observer
- discouraged_assert
- discouraged_direct_init
- discouraged_none_name
- discouraged_object_literal
- discouraged_optional_boolean
- discouraged_optional_collection
- duplicate_conditions
- duplicate_enum_cases
- duplicate_imports
- duplicated_key_in_dictionary_literal
Expand All @@ -53,7 +56,7 @@ only_rules:
- implicit_getter
- implicit_return
- inclusive_language
- inert_defer
- invalid_swiftlint_command
- is_disjoint
- joined_default_parameter
- last_where
Expand All @@ -80,6 +83,7 @@ only_rules:
- notification_center_detachment
- ns_number_init_as_function_reference
- nsobject_prefer_isequal
- number_separator
- opening_brace
- operator_usage_whitespace
- operator_whitespace
Expand Down Expand Up @@ -131,7 +135,6 @@ only_rules:
- unneeded_parentheses_in_closure_argument
- unowned_variable_capture
- untyped_error_in_catch
- unused_capture_list
- unused_closure_parameter
- unused_control_flow_label
- unused_enumerated
Expand All @@ -153,6 +156,8 @@ analyzer_rules:
- typesafe_array_init
for_where:
allow_for_as_filter: true
number_separator:
minimum_length: 5
identifier_name:
max_length:
warning: 100
Expand Down
371 changes: 166 additions & 205 deletions Color Picker.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

68 changes: 10 additions & 58 deletions Color Picker/App.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import SwiftUI
import LaunchAtLogin

// TODO: Remove the view menu

/**
NOTES:
- The "com.apple.security.files.user-selected.read-only" entitlement is required by the "Open" menu in the "Color Palettes" pane.
TODO when targeting macOS 13:
- Upload non-App Store version.
TODO shortcut action ideas;
- Convert color
- Toggle color panel
Expand All @@ -23,7 +17,7 @@ struct AppMain: App {
@StateObject private var pasteboardObserver = NSPasteboard.SimpleObservable(.general, onlyWhileAppIsActive: true)

init() {
migrate()
setUpConfig()
}

var body: some Scene {
Expand Down Expand Up @@ -85,67 +79,25 @@ struct AppMain: App {
}
}

private func migrate() {
LaunchAtLogin.migrateIfNeeded()

// TODO: Remove in 2023.
SSApp.runOnce(identifier: "migrateShownColorFormats") {
guard !SSApp.isFirstLaunch else {
return
}

Defaults.migrate(.shownColorFormats, to: .v5)
Defaults.migrate(.colorFormatToCopyAfterPicking, to: .v5)
}

// TODO: Remove in 2023.
SSApp.runOnce(identifier: "migrateToPreferredColorFormatSetting") {
guard !SSApp.isFirstLaunch else {
return
}

if Defaults[.colorFormatToCopyAfterPicking] != .none {
Defaults[.copyColorAfterPicking] = true
}

switch Defaults[.colorFormatToCopyAfterPicking] {
case .none:
break
case .hex:
Defaults[.preferredColorFormat] = .hex
case .hsl:
Defaults[.preferredColorFormat] = .hsl
case .rgb:
Defaults[.preferredColorFormat] = .rgb
case .lch:
Defaults[.preferredColorFormat] = .lch
}
}

// Preserve the old behavior for existing users.
SSApp.runOnce(identifier: "setDefaultsForMenuBarItemClickActionSetting") {
guard !SSApp.isFirstLaunch else {
return
}

Defaults[.menuBarItemClickAction] = .toggleWindow
private func setUpConfig() {
#if !DEBUG
SentrySDK.start {
$0.dsn = "https://[email protected]/6139060"
$0.enableSwizzling = false
$0.enableAppHangTracking = false // https://github.com/getsentry/sentry-cocoa/issues/2643
}
#endif
}
}

@MainActor
private final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
if #available(macOS 13, *) {
SSApp.swiftUIMainWindow?.close()
}
SSApp.swiftUIMainWindow?.close()
}

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if #available(macOS 13, *) {
AppState.shared.handleAppReopen()
}

AppState.shared.handleAppReopen()
return false
}
}
29 changes: 2 additions & 27 deletions Color Picker/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,13 @@ final class AppState: ObservableObject {
}
}

init() {
setUpConfig()

private init() {
DispatchQueue.main.async { [self] in
didLaunch()
}
}

private func didLaunch() {
fixStuff()
setUpEvents()
handleMenuBarIcon()
showWelcomeScreenIfNeeded()
Expand All @@ -155,30 +152,8 @@ final class AppState: ObservableObject {
#endif
}

private func setUpConfig() {
#if !DEBUG
SentrySDK.start {
$0.dsn = "https://[email protected]/6139060"
$0.enableSwizzling = false
}
#endif
}

private func fixStuff() {
if #available(macOS 13, *) {
SSApp.swiftUIMainWindow?.close()
} else {
// Make the invisible native SwitUI window not block access to the desktop. (macOS 12.0)
// https://github.com/feedback-assistant/reports/issues/253
SSApp.swiftUIMainWindow?.ignoresMouseEvents = true

// Make the invisible native SwiftUI window not show up in mission control when in menu bar mode. (macOS 11.6)
SSApp.swiftUIMainWindow?.collectionBehavior = .stationary
}
}

private func requestReview() {
SSApp.requestReviewAfterBeingCalledThisManyTimes([10, 100, 200, 1000])
SSApp.requestReviewAfterBeingCalledThisManyTimes([8, 100, 200, 1000])
}

private func addToRecentlyPickedColor(_ color: NSColor) {
Expand Down
12 changes: 3 additions & 9 deletions Color Picker/ColorPickerScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ struct ColorPickerScreen: View {
Text(colorPanel.color.accessibilityName)
.font(.system(largerText ? .title3 : .body))
.textSelection(.enabled)
.accessibilityHidden(true)
}
}
.padding(9)
Expand Down Expand Up @@ -291,7 +292,7 @@ private struct BarView: View {
actionButton
Spacer()
}
// Cannot do this as the `Menu` buttons don't respect it. (macOS 12.0.1)
// Cannot do this as the `Menu` buttons don't respect it. (macOS 13.2)
// https://github.com/feedback-assistant/reports/issues/249
// .font(.title3)
.background {
Expand All @@ -318,7 +319,7 @@ private struct BarView: View {
Divider()
Defaults.Toggle("Stay on Top", key: .stayOnTop)
Divider()
Button(OS.isMacOS13OrLater ? "Settings" : "Preferences") {
Button("Settings…") {
SSApp.showSettingsWindow()
}
.keyboardShortcut(",")
Expand All @@ -328,11 +329,8 @@ private struct BarView: View {
.labelStyle(.iconOnly)
// .padding(8) // Has no effect. (macOS 12.0.1)
}
// TODO: Remove when targeting macOS 13 where it's fixed.
.buttonStyle(.automatic) // Without, it becomes disabled: https://github.com/feedback-assistant/reports/issues/250 (macOS 12.0.1)
.padding(8)
.contentShape(.rectangle)
.fixedSize()
.opacity(0.6) // Try to match the other buttons.
.menuIndicator(.hidden)
}
Expand Down Expand Up @@ -370,9 +368,6 @@ private struct RecentlyPickedColorsButton: View {
recentlyPickedColors = []
}
}
// TODO: Remove when targeting macOS 13 where it's fixed.
// Without, it becomes disabled. (macOS 12.4)
.buttonStyle(.automatic)
} label: {
Image(systemName: "clock.fill")
.controlSize(.large)
Expand All @@ -381,7 +376,6 @@ private struct RecentlyPickedColorsButton: View {
}
.menuIndicator(.hidden)
.padding(8)
.fixedSize()
.opacity(0.6) // Try to match the other buttons.
.disabled(recentlyPickedColors.isEmpty)
.help(recentlyPickedColors.isEmpty ? "No recently picked colors" : "Recently picked colors")
Expand Down
60 changes: 0 additions & 60 deletions Color Picker/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,13 @@ extension Defaults.Keys {
static let largerText = Key<Bool>("largerText", default: false)
static let copyColorAfterPicking = Key<Bool>("copyColorAfterPicking", default: false)
static let showAccessibilityColorName = Key<Bool>("showAccessibilityColorName", default: false)

// Deprecated
static let colorFormatToCopyAfterPicking = Key<CopyColorFormat>("colorFormatToCopyAfterPicking", default: .none)
}

extension KeyboardShortcuts.Name {
static let pickColor = Self("pickColor")
static let toggleWindow = Self("toggleWindow")
}

// TODO: Remove in 2023.
enum CopyColorFormat: String, CaseIterable, Defaults.Serializable {
case none // swiftlint:disable:this discouraged_none_name
case hex
case hsl
case rgb
case lch

var title: String {
switch self {
case .none:
return "None"
case .hex:
return "Hex"
case .hsl:
return "HSL"
case .rgb:
return "RGB"
case .lch:
return "LCH"
}
}
}

// TODO: Remove in 2023.
enum CodableCopyColorFormat: String {
case none // swiftlint:disable:this discouraged_none_name
case hex
case hsl
case rgb
case lch
}

extension CodableCopyColorFormat: Defaults.CodableType {
typealias NativeForm = CopyColorFormat
}

extension CopyColorFormat: Defaults.NativeType {
typealias CodableForm = CodableCopyColorFormat
}

enum ColorFormat: String, CaseIterable, Defaults.Serializable {
case hex
case hsl
Expand All @@ -93,22 +49,6 @@ extension ColorFormat: Identifiable {
var id: Self { self }
}

// TODO: Remove in 2023.
enum CodableColorFormat: String {
case hex
case hsl
case rgb
case lch
}

extension CodableColorFormat: Defaults.CodableType {
typealias NativeForm = ColorFormat
}

extension ColorFormat: Defaults.NativeType {
typealias CodableForm = CodableColorFormat
}

enum MenuBarItemClickAction: String, CaseIterable, Defaults.Serializable {
case showMenu
case showColorSampler
Expand Down
10 changes: 0 additions & 10 deletions Color Picker/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ extension AppState {
colorPanel.toggle()
}

if #unavailable(macOS 13) {
// We use this instead of `applicationShouldHandleReopen` because of the macOS bug.
// https://github.com/feedback-assistant/reports/issues/246
NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)
.sink { [self] _ in
handleAppReopen()
}
.store(in: &cancellables)
}

// Workaround for the color picker window not becoming active after the settings window closes. (macOS 11.3)
NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)
.sink { [self] _ in
Expand Down
5 changes: 0 additions & 5 deletions Color Picker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,5 @@
<true/>
<key>MDItemKeywords</key>
<string>color,picker,system,pick,colour,colors,colours,sampler</string>
<key>NSUserActivityTypes</key>
<array>
<string>GetRandomColorIntent</string>
<string>SampleColorIntent</string>
</array>
</dict>
</plist>
Loading

0 comments on commit be6c837

Please sign in to comment.