Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 8, 2022
1 parent 7bf146f commit 587ab25
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Color Picker/ColorPickerScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private struct BarView: View {
}
if showInMenuBar {
Divider()
Button("Preferences…") {
Button(OS.isMacOS13OrLater ? "Settings…" : "Preferences…") {
SSApp.showSettingsWindow()
}
.keyboardShortcut(",")
Expand Down
2 changes: 1 addition & 1 deletion Color Picker/SettingsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private struct GeneralSettings: View {
.help("Make the color picker window stay on top of all other windows.")
.padding(.bottom, 8)
Defaults.Toggle("Show in menu bar instead of Dock", key: .showInMenuBar)
.help("If you have “Keep in Dock” enabled when activating this preference, you should disable that since the Dock icon will no longer be functional.")
.help("If you have “Keep in Dock” enabled when activating this setting, you should disable that since the Dock icon will no longer be functional.")
Group {
LaunchAtLogin.Toggle()
.help(showInMenuBar ? "" : "There is really no point in launching the app at login if it is not in the menu bar. You can instead just put it in the Dock and launch it when needed.")
Expand Down
72 changes: 65 additions & 7 deletions Color Picker/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ enum SSApp {
extension SSApp {
@MainActor
static var swiftUIMainWindow: NSWindow? {
NSApp.windows.first { $0.simpleClassName == "SwiftUIWindow" }
NSApp.windows.first { $0.simpleClassName == (OS.isMacOS13OrLater ? "AppKitWindow" : "SwiftUIWindow") }
}
}

Expand All @@ -195,13 +195,17 @@ extension SSApp {
*/
@MainActor
static func showSettingsWindow() {
if NSApp.activationPolicy() == .accessory {
NSApp.activate(ignoringOtherApps: true)
}

// Run in the next runloop so it doesn't conflict with SwiftUI if run at startup.
DispatchQueue.main.async {
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
if NSApp.activationPolicy() == .accessory {
NSApp.activate(ignoringOtherApps: true)
}

if #available(macOS 13, *) {
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
} else {
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
}
}
}

Expand Down Expand Up @@ -1438,7 +1442,7 @@ extension NSMenu {

@discardableResult
func addSettingsItem() -> NSMenuItem {
addCallbackItem("Preferences…", key: ",") {
addCallbackItem(OS.isMacOS13OrLater ? "Settings…" : "Preferences…", key: ",") {
Task {
await SSApp.showSettingsWindow()
}
Expand Down Expand Up @@ -2960,3 +2964,57 @@ extension Button where Label == SwiftUI.Label<Text, Image> {
}
}
}


enum OperatingSystem {
case macOS
case iOS
case tvOS
case watchOS

#if os(macOS)
static let current = macOS
#elseif os(iOS)
static let current = iOS
#elseif os(tvOS)
static let current = tvOS
#elseif os(watchOS)
static let current = watchOS
#else
#error("Unsupported platform")
#endif
}

extension OperatingSystem {
/**
- Note: Only use this when you cannot use an `if #available` check. For example, inline in function calls.
*/
static let isMacOS14OrLater: Bool = {
#if os(macOS)
if #available(macOS 14, *) {
return true
} else {
return false
}
#else
return false
#endif
}()

/**
- Note: Only use this when you cannot use an `if #available` check. For example, inline in function calls.
*/
static let isMacOS13OrLater: Bool = {
#if os(macOS)
if #available(macOS 13, *) {
return true
} else {
return false
}
#else
return false
#endif
}()
}

typealias OS = OperatingSystem

0 comments on commit 587ab25

Please sign in to comment.