diff --git a/Sources/ColorWellKit/Documentation.docc/Cocoa/Extensions/ColorWell/ColorWell.md b/Sources/ColorWellKit/Documentation.docc/Cocoa/Extensions/ColorWell/CWColorWell.md similarity index 100% rename from Sources/ColorWellKit/Documentation.docc/Cocoa/Extensions/ColorWell/ColorWell.md rename to Sources/ColorWellKit/Documentation.docc/Cocoa/Extensions/ColorWell/CWColorWell.md diff --git a/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorPanelMode/ColorPanelMode.md b/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorPanelMode/ColorPanelMode.md new file mode 100644 index 0000000..3623862 --- /dev/null +++ b/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorPanelMode/ColorPanelMode.md @@ -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`` diff --git a/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorWellView.md b/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorWell.md similarity index 98% rename from Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorWellView.md rename to Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorWell.md index d3a17e2..dd7c840 100644 --- a/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorWellView.md +++ b/Sources/ColorWellKit/Documentation.docc/SwiftUI/Extensions/ColorWell.md @@ -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 @@ -93,3 +94,4 @@ The example above will print the text "color well was pressed" to the console in ### Supporting Types - ``ColorWellStyle`` +- ``ColorPanelMode`` diff --git a/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift b/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift index 2f7ebde..2d804cc 100644 --- a/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift +++ b/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift @@ -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 @@ -133,6 +136,9 @@ public class CWColorWell: _CWColorWellBaseControl { segment.updateForCurrentActiveState(shouldActivate) } if shouldActivate { + if let colorPanelMode { + NSColorPanel.shared.mode = colorPanelMode + } NSColorPanel.shared.orderFront(self) } } diff --git a/Sources/ColorWellKit/Views/SwiftUI/ColorPanelMode.swift b/Sources/ColorWellKit/Views/SwiftUI/ColorPanelMode.swift new file mode 100644 index 0000000..34128c3 --- /dev/null +++ b/Sources/ColorWellKit/Views/SwiftUI/ColorPanelMode.swift @@ -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 diff --git a/Sources/ColorWellKit/Views/SwiftUI/ColorWellRepresentable.swift b/Sources/ColorWellKit/Views/SwiftUI/ColorWellRepresentable.swift index cad452c..b98f16f 100644 --- a/Sources/ColorWellKit/Views/SwiftUI/ColorWellRepresentable.swift +++ b/Sources/ColorWellKit/Views/SwiftUI/ColorWellRepresentable.swift @@ -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 diff --git a/Sources/ColorWellKit/Views/SwiftUI/EnvironmentValues.swift b/Sources/ColorWellKit/Views/SwiftUI/EnvironmentValues.swift index 5e338ed..e8f4469 100644 --- a/Sources/ColorWellKit/Views/SwiftUI/EnvironmentValues.swift +++ b/Sources/ColorWellKit/Views/SwiftUI/EnvironmentValues.swift @@ -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 { @@ -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 diff --git a/Sources/ColorWellKit/Views/SwiftUI/ViewModifiers.swift b/Sources/ColorWellKit/Views/SwiftUI/ViewModifiers.swift index 08bb534..9f53ed4 100644 --- a/Sources/ColorWellKit/Views/SwiftUI/ViewModifiers.swift +++ b/Sources/ColorWellKit/Views/SwiftUI/ViewModifiers.swift @@ -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(_ mode: M) -> some View { + environment(\.colorPanelModeConfiguration, mode._configuration) + } } #endif