-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
// Copyright © 2024 Povio Inc. All rights reserved. | ||
// | ||
|
||
|
||
import SwiftUI | ||
|
||
@available(iOS 15.0, *) | ||
|
79 changes: 79 additions & 0 deletions
79
Sources/UI/SwiftUI/Views/SimpleColorPicker/SimpleColorPicker.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// SimpleColorPicker.swift | ||
// PovioKit | ||
// | ||
// Created by Borut Tomazin on 15/08/2024. | ||
// Copyright © 2024 Povio Inc. All rights reserved. | ||
// | ||
|
||
#if os(macOS) | ||
import SwiftUI | ||
import Combine | ||
|
||
/// SwiftUI wrapper for NSColorWell, allowing the selection of colors with a customizable size. | ||
public struct SimpleColorPicker: NSViewRepresentable { | ||
@Binding var selection: Color | ||
var size: CGSize? | ||
|
||
public init(selection: Binding<Color>, size: CGSize? = nil) { | ||
self._selection = selection | ||
self.size = size | ||
} | ||
|
||
public func makeNSView(context: Context) -> NSColorWell { | ||
let colorWell: NSColorWell | ||
if #available(macOS 13.0, *) { | ||
colorWell = NSColorWell(style: .minimal) | ||
} else { | ||
colorWell = NSColorWell() | ||
} | ||
colorWell.color = NSColor(selection) | ||
|
||
context.coordinator.startObservingColorChange(of: colorWell) | ||
|
||
// override the size | ||
if let size { | ||
colorWell.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
colorWell.widthAnchor.constraint(equalToConstant: size.width), | ||
colorWell.heightAnchor.constraint(equalToConstant: size.height) | ||
]) | ||
} | ||
|
||
return colorWell | ||
} | ||
|
||
public func updateNSView(_ nsView: NSColorWell, context: Context) { | ||
context.coordinator.colorDidChange = { | ||
selection = Color(nsColor: $0) | ||
} | ||
|
||
// update the size | ||
if let size { | ||
if let widthConstraint = nsView.constraints.first(where: { $0.firstAttribute == .width }) { | ||
widthConstraint.constant = size.width | ||
} | ||
if let heightConstraint = nsView.constraints.first(where: { $0.firstAttribute == .height }) { | ||
heightConstraint.constant = size.height | ||
} | ||
} | ||
} | ||
|
||
public func makeCoordinator() -> Coordinator { | ||
Coordinator() | ||
} | ||
|
||
@MainActor | ||
public class Coordinator: NSObject { | ||
var colorDidChange: ((NSColor) -> Void)? | ||
|
||
private var cancellable: AnyCancellable? | ||
|
||
func startObservingColorChange(of colorWell: NSColorWell) { | ||
cancellable = colorWell.publisher(for: \.color).sink { [weak self] in | ||
self?.colorDidChange?($0) | ||
} | ||
} | ||
} | ||
} | ||
#endif |