Skip to content

Commit

Permalink
Add ability to set the system color panel's mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Dec 18, 2023
1 parent b4035a6 commit 986bdea
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ``ColorWellKit/ColorPanelMode``

## Topics

### Color panel mode types

- ``GrayscaleColorPanelMode``
- ``RGBColorPanelMode``
- ``CMYKColorPanelMode``
- ``HSBColorPanelMode``
- ``CustomPaletteColorPanelMode``
- ``ColorListColorPanelMode``
- ``ColorWheelColorPanelMode``
- ``CrayonPickerColorPanelMode``

### Creating a color panel mode

- ``ColorPanelMode/gray``
- ``ColorPanelMode/rgb``
- ``ColorPanelMode/cmyk``
- ``ColorPanelMode/hsb``
- ``ColorPanelMode/customPalette``
- ``ColorPanelMode/colorList``
- ``ColorPanelMode/wheel``
- ``ColorPanelMode/crayon``
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ The example above will print the text "color well was pressed" to the console in
- ``colorWellStyle(_:)``
- ``colorWellSwatchColors(_:)``
- ``colorWellSecondaryAction(_:)``
- ``colorPanelMode(_:)``

### Getting a color well's content view

Expand All @@ -93,3 +94,4 @@ The example above will print the text "color well was pressed" to the console in
### Supporting Types

- ``ColorWellStyle``
- ``ColorPanelMode``
6 changes: 6 additions & 0 deletions Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public class CWColorWell: _CWColorWellBaseControl {
/// the color area execute your custom action method instead.
public var secondaryTarget: AnyObject?

/// The mode to switch the color panel to when the color well activates.
public var colorPanelMode: NSColorPanel.Mode?

/// The color well's color.
///
/// Setting this value immediately updates the visual state of the color
Expand Down Expand Up @@ -133,6 +136,9 @@ public class CWColorWell: _CWColorWellBaseControl {
segment.updateForCurrentActiveState(shouldActivate)
}
if shouldActivate {
if let colorPanelMode {
NSColorPanel.shared.mode = colorPanelMode
}
NSColorPanel.shared.orderFront(self)
}
}
Expand Down
178 changes: 178 additions & 0 deletions Sources/ColorWellKit/Views/SwiftUI/ColorPanelMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//
// ColorPanelMode.swift
// ColorWellKit
//

#if canImport(SwiftUI)
import SwiftUI

// MARK: - ColorPanelModeConfiguration

/// Values that configure the system color panel's mode.
@available(macOS 10.15, *)
public struct _ColorPanelModeConfiguration {
/// The underlying color panel mode.
let mode: NSColorPanel.Mode
}

// MARK: - ColorPanelMode

/// A type that specifies a mode for the system color panel.
@available(macOS 10.15, *)
public protocol ColorPanelMode {
/// Values that configure the system color panel's mode.
var _configuration: _ColorPanelModeConfiguration { get }
}

// MARK: - GrayscaleColorPanelMode

/// The grayscale color panel mode.
@available(macOS 10.15, *)
public struct GrayscaleColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .gray)

/// Creates an instance of the grayscale color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == GrayscaleColorPanelMode {
/// The grayscale color panel mode.
public static var gray: GrayscaleColorPanelMode {
GrayscaleColorPanelMode()
}
}

// MARK: - RGBColorPanelMode

/// The red-green-blue color panel mode.
@available(macOS 10.15, *)
public struct RGBColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .RGB)

/// Creates an instance of the red-green-blue color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == RGBColorPanelMode {
/// The red-green-blue color panel mode.
public static var rgb: RGBColorPanelMode {
RGBColorPanelMode()
}
}

// MARK: - CMYKColorPanelMode

/// The cyan-magenta-yellow-black color panel mode.
@available(macOS 10.15, *)
public struct CMYKColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .CMYK)

/// Creates an instance of the cyan-magenta-yellow-black color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == CMYKColorPanelMode {
/// The cyan-magenta-yellow-black color panel mode.
public static var cmyk: CMYKColorPanelMode {
CMYKColorPanelMode()
}
}

// MARK: - HSBColorPanelMode

/// The hue-saturation-brightness color panel mode.
@available(macOS 10.15, *)
public struct HSBColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .HSB)

/// Creates an instance of the hue-saturation-brightness color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == HSBColorPanelMode {
/// The hue-saturation-brightness color panel mode.
public static var hsb: HSBColorPanelMode {
HSBColorPanelMode()
}
}

// MARK: - CustomPaletteColorPanelMode

/// The custom palette color panel mode.
@available(macOS 10.15, *)
public struct CustomPaletteColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .customPalette)

/// Creates an instance of the custom palette color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == CustomPaletteColorPanelMode {
/// The custom palette color panel mode.
public static var customPalette: CustomPaletteColorPanelMode {
CustomPaletteColorPanelMode()
}
}

// MARK: - ColorListColorPanelMode

/// The color list color panel mode.
@available(macOS 10.15, *)
public struct ColorListColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .colorList)

/// Creates an instance of the color list color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == ColorListColorPanelMode {
/// The color list color panel mode.
public static var colorList: ColorListColorPanelMode {
ColorListColorPanelMode()
}
}

// MARK: - ColorWheelColorPanelMode

/// The color wheel color panel mode.
@available(macOS 10.15, *)
public struct ColorWheelColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .wheel)

/// Creates an instance of the color wheel color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == ColorWheelColorPanelMode {
/// The color wheel color panel mode.
public static var wheel: ColorWheelColorPanelMode {
ColorWheelColorPanelMode()
}
}

// MARK: - CrayonPickerColorPanelMode

/// The crayon picker color panel mode.
@available(macOS 10.15, *)
public struct CrayonPickerColorPanelMode: ColorPanelMode {
public let _configuration = _ColorPanelModeConfiguration(mode: .crayon)

/// Creates an instance of the crayon picker color panel mode.
public init() { }
}

@available(macOS 10.15, *)
extension ColorPanelMode where Self == CrayonPickerColorPanelMode {
/// The crayon picker color panel mode.
public static var crayon: CrayonPickerColorPanelMode {
CrayonPickerColorPanelMode()
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ struct ColorWellRepresentable: NSViewRepresentable {
{
colorWell.swatchColors = swatchColors
}
if colorWell.colorPanelMode != context.environment.colorPanelModeConfiguration?.mode {
colorWell.colorPanelMode = context.environment.colorPanelModeConfiguration?.mode
}
if let secondaryActionDelegate = context.environment.colorWellSecondaryActionDelegate {
colorWell.secondaryAction = #selector(secondaryActionDelegate.performAction)
colorWell.secondaryTarget = secondaryActionDelegate
Expand Down
13 changes: 13 additions & 0 deletions Sources/ColorWellKit/Views/SwiftUI/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ private struct ColorWellSecondaryActionDelegateKey: EnvironmentKey {
static let defaultValue: ColorWellSecondaryActionDelegate? = nil
}

@available(macOS 10.15, *)
private struct ColorPanelModeConfigurationKey: EnvironmentKey {
static var defaultValue: _ColorPanelModeConfiguration?
}

@available(macOS 10.15, *)
extension EnvironmentValues {
var colorWellStyleConfiguration: _ColorWellStyleConfiguration {
Expand All @@ -44,4 +49,12 @@ extension EnvironmentValues {
set { self[ColorWellSecondaryActionDelegateKey.self] = newValue }
}
}

@available(macOS 10.15, *)
extension EnvironmentValues {
var colorPanelModeConfiguration: _ColorPanelModeConfiguration? {
get { self[ColorPanelModeConfigurationKey.self] }
set { self[ColorPanelModeConfigurationKey.self] = newValue }
}
}
#endif
11 changes: 11 additions & 0 deletions Sources/ColorWellKit/Views/SwiftUI/ViewModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,16 @@ extension View {
delegate = ColorWellSecondaryActionDelegate(action: action)
}
}

/// Sets the color panel mode for color wells in this view.
///
/// When a color well with this modifier applied activates,
/// the system color panel will switch to the provided mode.
///
/// - Parameter mode: The color panel mode to apply to color
/// wells in this view.
public func colorPanelMode<M: ColorPanelMode>(_ mode: M) -> some View {
environment(\.colorPanelModeConfiguration, mode._configuration)
}
}
#endif

0 comments on commit 986bdea

Please sign in to comment.