Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 9, 2021
1 parent ffdb873 commit 95f3068
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Color Picker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@
repositoryURL = "https://github.com/sindresorhus/Defaults";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.1.0;
minimumVersion = 5.0.0;
};
};
E3E9F9AB2642B75100AE6450 /* XCRemoteSwiftPackageReference "appcenter-sdk-apple" */ = {
Expand Down
4 changes: 4 additions & 0 deletions Color Picker/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ final class AppState: ObservableObject {
]
)

// TODO: Remove in 2022.
Defaults.migrate(.shownColorFormats, to: .v5)
Defaults.migrate(.colorFormatToCopyAfterPicking, to: .v5)

DispatchQueue.main.async { [self] in
didLaunch()
}
Expand Down
37 changes: 35 additions & 2 deletions Color Picker/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension KeyboardShortcuts.Name {
static let toggleWindow = Self("toggleWindow")
}

enum CopyColorFormat: String, Codable, CaseIterable {
enum CopyColorFormat: String, CaseIterable, Defaults.Serializable {
case none
case hex
case hsl
Expand All @@ -40,7 +40,24 @@ enum CopyColorFormat: String, Codable, CaseIterable {
}
}

enum ColorFormat: String, Codable, CaseIterable {
// TODO: Remove in 2022.
enum CodableCopyColorFormat: String {
case none
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
case rgb
Expand All @@ -63,3 +80,19 @@ enum ColorFormat: String, Codable, CaseIterable {
extension ColorFormat: Identifiable {
var id: Self { self }
}

// TODO: Remove in 2022.
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
}
2 changes: 1 addition & 1 deletion Color Picker/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension AppState {
NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)
.sink { [self] _ in
DispatchQueue.main.async {
if colorPanel.isVisible {
if colorPanel.isVisible, SSApp.settingsWindow?.isVisible != true {
colorPanel.makeKeyAndOrderFront(nil)
}
}
Expand Down
7 changes: 6 additions & 1 deletion Color Picker/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ private struct CopyColorFormatSetting: View {
@Default(.colorFormatToCopyAfterPicking) private var colorFormat

var body: some View {
EnumPicker("Copy color after picking:", enumBinding: $colorFormat) { element, _ in
EnumPicker(
enumBinding: $colorFormat,
label: Text("Copy color after picking:")
.fixedSize()
) { element, _ in
Text(element.title)
if element == .none {
Divider()
}
}
.fixedSize()
}
}

Expand Down
78 changes: 16 additions & 62 deletions Color Picker/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ extension SSApp {
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
}
}

/// The SwiftUI settings window.
static var settingsWindow: NSWindow? {
NSApp.windows.first { $0.frameAutosaveName == "com_apple_SwiftUI_Settings_window" }
}
}


Expand Down Expand Up @@ -782,9 +787,9 @@ extension DispatchQueue {


extension Defaults {
final class Observable<Value: Codable>: ObservableObject {
final class Observable<Value: Serializable>: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
private var observation: DefaultsObservation?
private var cancellable: AnyCancellable?
private let key: Defaults.Key<Value>

var value: Value {
Expand All @@ -798,15 +803,16 @@ extension Defaults {
init(_ key: Key<Value>) {
self.key = key

self.observation = Defaults.observe(key, options: [.prior]) { [weak self] change in
guard change.isPrior else {
return
}
self.cancellable = Defaults.publisher(key, options: [.prior])
.sink { [weak self] change in
guard change.isPrior else {
return
}

DispatchQueue.mainSafeAsync {
self?.objectWillChange.send()
DispatchQueue.mainSafeAsync {
self?.objectWillChange.send()
}
}
}
}

/// Reset the key back to its default value.
Expand All @@ -817,58 +823,6 @@ extension Defaults {
}


extension Defaults {
/**
Creates a SwiftUI `Toggle` view that is connected to a Bool `Defaults` key.
```
struct ShowAllDayEventsSetting: View {
var body: some View {
Defaults.Toggle("Show All-Day Events", key: .showAllDayEvents)
}
}
```
*/
struct Toggle<Label, Key>: View where Label: View, Key: Defaults.Key<Bool> {
// TODO: Find a way to store the handler without using an embedded class.
private final class OnChangeHolder {
var onChange: ((Bool) -> Void)?
}

private let label: () -> Label
@ObservedObject private var observable: Defaults.Observable<Bool>
private let onChangeHolder = OnChangeHolder()

init(key: Key, @ViewBuilder label: @escaping () -> Label) {
self.label = label
self.observable = Defaults.Observable(key)
}

var body: some View {
SwiftUI.Toggle(isOn: $observable.value, label: label)
.onChange(of: observable.value) {
onChangeHolder.onChange?($0)
}
}
}
}

extension Defaults.Toggle where Label == Text {
init<S>(_ title: S, key: Defaults.Key<Bool>) where S: StringProtocol {
self.label = { Text(title) }
self.observable = Defaults.Observable(key)
}
}

extension Defaults.Toggle {
/// Do something when the value changes to a different value.
func onChange(_ action: @escaping (Bool) -> Void) -> Self {
onChangeHolder.onChange = action
return self
}
}


struct NativeTextField: NSViewRepresentable {
typealias NSViewType = InternalTextField

Expand Down Expand Up @@ -1568,7 +1522,7 @@ extension NSPasteboard {
}
.store(in: &cancellables)

if NSApp.isActive {
if NSApp?.isActive == true {
start()
}
} else {
Expand Down

0 comments on commit 95f3068

Please sign in to comment.