Skip to content

Commit

Permalink
Deprecate PopoverConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Dec 4, 2023
1 parent 27dbbf4 commit 032951c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 43 deletions.
39 changes: 24 additions & 15 deletions Sources/ColorWellKit/Views/Cocoa/ColorWell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ColorWell: _ColorWellBaseControl {

private var isExclusive = true

var _popoverConfiguration: _PopoverConfiguration? = .default

/// The color well's delegate object.
public weak var delegate: ColorWellDelegate?

Expand All @@ -32,20 +34,6 @@ public class ColorWell: _ColorWellBaseControl {
@objc dynamic
public var allowsMultipleSelection: Bool = true

/// A configuration that specifies the appearance of the user-selectable
/// swatches in the color well’s popover.
///
/// If the ``secondaryAction`` and ``secondaryTarget`` properties have
/// been set, the action specified by those properties will be invoked
/// instead of the popover being shown.
///
/// If this value is `nil`, and the secondary action and target properties
/// have not been set, the color well will not show its popover, and will
/// instead defer to opening the system color panel.
///
/// The default value of this property is ``PopoverConfiguration-swift.struct/default``.
public var popoverConfiguration: PopoverConfiguration? = .default

/// The action to perform when the color area of the color well is pressed.
///
/// By default, color wells with the ``Style-swift.enum/minimal`` or
Expand Down Expand Up @@ -215,7 +203,7 @@ public class ColorWell: _ColorWellBaseControl {
return false
}
guard
let popoverConfiguration,
let popoverConfiguration = _popoverConfiguration,
layoutView.segments.contains(segment)
else {
return false
Expand Down Expand Up @@ -289,3 +277,24 @@ public class ColorWell: _ColorWellBaseControl {
}
}
}

// MARK: Deprecated
extension ColorWell {
/// A configuration that specifies the appearance of the user-selectable
/// swatches in the color well’s popover.
///
/// If the ``secondaryAction`` and ``secondaryTarget`` properties have
/// been set, the action specified by those properties will be invoked
/// instead of the popover being shown.
///
/// If this value is `nil`, and the secondary action and target properties
/// have not been set, the color well will not show its popover, and will
/// instead defer to opening the system color panel.
///
/// The default value of this property is ``PopoverConfiguration-swift.struct/default``.
@available(*, deprecated, message: "Use the color well's 'secondaryAction' to create a custom popover.")
public var popoverConfiguration: PopoverConfiguration? {
get { _popoverConfiguration }
set { _popoverConfiguration = newValue }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ColorWellPullDownSwatchSegment: ColorWellSwatchSegment {
// perform further checks
return true
}
if let popoverConfiguration = colorWell.popoverConfiguration {
if let popoverConfiguration = colorWell._popoverConfiguration {
// we have a configuration; make sure it has colors
return !popoverConfiguration.colors.isEmpty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension ColorWellPopover.LayoutView {
class SwatchLayout: NSGridView {

// swiftlint:disable:next nesting
typealias Configuration = ColorWell.PopoverConfiguration
typealias Configuration = ColorWell._PopoverConfiguration

// swiftlint:disable:next nesting
typealias ColorSwatch = ColorWellPopover.ColorSwatch
Expand Down Expand Up @@ -255,7 +255,7 @@ extension ColorWellPopover.LayoutView.SwatchLayout {
class SelectionIndicator: NSView {

// swiftlint:disable:next nesting
typealias SwatchShape = ColorWell.PopoverConfiguration.SwatchShape
typealias SwatchShape = ColorWell._PopoverConfiguration.SwatchShape

fileprivate var borderWidth: CGFloat = 0 {
didSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AppKit

/// A popover that contains a grid of selectable color swatches.
class ColorWellPopover: NSPopover {
typealias Configuration = ColorWell.PopoverConfiguration
typealias Configuration = ColorWell._PopoverConfiguration

private weak var colorWell: ColorWell?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import CoreGraphics

extension ColorWell.PopoverConfiguration {
extension ColorWell._PopoverConfiguration {
/// A type that configures the layout of a popover's content view.
public struct ContentLayout {
// swiftlint:disable:next nesting
Expand Down Expand Up @@ -162,13 +162,13 @@ extension ColorWell.PopoverConfiguration {
}

// MARK: ContentLayout: Equatable
extension ColorWell.PopoverConfiguration.ContentLayout: Equatable { }
extension ColorWell._PopoverConfiguration.ContentLayout: Equatable { }

// MARK: ContentLayout: Hashable
extension ColorWell.PopoverConfiguration.ContentLayout: Hashable { }
extension ColorWell._PopoverConfiguration.ContentLayout: Hashable { }

// MARK: Deprecated
extension ColorWell.PopoverConfiguration {
extension ColorWell._PopoverConfiguration {
/// A type that configures the layout of a popover's content view.
@available(*, deprecated, renamed: "ContentLayout")
public typealias Layout = ContentLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import AppKit
extension ColorWell {
/// A type that specifies the appearance and behavior of
/// a color well's popover.
public struct PopoverConfiguration {
@available(*, deprecated, message: "Use the color well's 'secondaryAction' to create a custom popover.")
public typealias PopoverConfiguration = _PopoverConfiguration

public struct _PopoverConfiguration {
/// The colors that are displayed as swatches inside
/// the popover.
public var colors: [NSColor]
Expand Down Expand Up @@ -95,14 +98,14 @@ extension ColorWell {
swatchSize: NSSize? = nil,
swatchShape: SwatchShape = .rectangle,
borderEffect: SwatchBorderEffect = .default
) -> PopoverConfiguration {
) -> Self {
guard
let path = bundle.path(forResource: name, ofType: "clr"),
let colorList = NSColorList(name: name, fromFile: path)
else {
preconditionFailure("Failed to load color list resource.")
}
return PopoverConfiguration(
return Self(
colorList: colorList,
contentLayout: contentLayout,
swatchSize: swatchSize,
Expand All @@ -115,7 +118,7 @@ extension ColorWell {
///
/// This configuration specifies a small grid of color swatches with 6
/// columns of colors across the color spectrum.
public static let `default`: PopoverConfiguration = _loadColorListResource(
public static let `default`: Self = _loadColorListResource(
name: "DefaultColors",
bundle: .module,
contentLayout: .grid(columnCount: 6).padding(minLength: 7.5, maxLength: 10),
Expand All @@ -126,7 +129,7 @@ extension ColorWell {
/// A configuration that specifies a grid of color swatches with 12 columns
/// of colors across the color spectrum, and a row of common colors across
/// the top.
public static let standard: PopoverConfiguration = _loadColorListResource(
public static let standard: Self = _loadColorListResource(
name: "StandardColors",
bundle: .module,
contentLayout: .grid(columnCount: 12, topRowSpacing: 4),
Expand All @@ -135,7 +138,7 @@ extension ColorWell {

/// A configuration that specifies a single row of color swatches consisting
/// of some of the most common colors.
public static let simple: PopoverConfiguration = {
public static let simple: Self = {
let hexStrings = [
"FF0000", // red
"FF8000", // orange
Expand All @@ -153,7 +156,7 @@ extension ColorWell {
let colors = hexStrings.compactMap { string in
NSColor(hexString: string)
}
return PopoverConfiguration(
return Self(
colors: colors,
contentLayout: .grid(columnCount: 12, horizontalSpacing: 2.5),
swatchSize: NSSize(width: 20, height: 20),
Expand Down Expand Up @@ -221,7 +224,7 @@ extension ColorWell {
}

// MARK: PopoverConfiguration: Equatable
extension ColorWell.PopoverConfiguration: Equatable {
extension ColorWell._PopoverConfiguration: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.colors == rhs.colors &&
lhs.contentLayout == rhs.contentLayout &&
Expand All @@ -233,7 +236,7 @@ extension ColorWell.PopoverConfiguration: Equatable {
}

// MARK: PopoverConfiguration: Hashable
extension ColorWell.PopoverConfiguration: Hashable {
extension ColorWell._PopoverConfiguration: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(colors)
hasher.combine(contentLayout)
Expand All @@ -245,7 +248,7 @@ extension ColorWell.PopoverConfiguration: Hashable {
}

// MARK: Deprecated
extension ColorWell.PopoverConfiguration {
extension ColorWell._PopoverConfiguration {
/// The layout of the popover's content view.
@available(*, deprecated, renamed: "contentLayout")
public var layout: Layout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import AppKit

extension ColorWell.PopoverConfiguration {
extension ColorWell._PopoverConfiguration {
/// A type that applies an effect to the border color of a swatch.
public struct SwatchBorderEffect {

Expand Down Expand Up @@ -159,7 +159,7 @@ extension ColorWell.PopoverConfiguration {
}

// MARK: SwatchBorderEffect: Equatable
extension ColorWell.PopoverConfiguration.SwatchBorderEffect: Equatable { }
extension ColorWell._PopoverConfiguration.SwatchBorderEffect: Equatable { }

// MARK: SwatchBorderEffect: Hashable
extension ColorWell.PopoverConfiguration.SwatchBorderEffect: Hashable { }
extension ColorWell._PopoverConfiguration.SwatchBorderEffect: Hashable { }
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import CoreGraphics

extension ColorWell.PopoverConfiguration {
extension ColorWell._PopoverConfiguration {
/// An algorithm that specifies the shape for swatches displayed inside
/// a color well's popover.
public struct SwatchShape {
Expand Down Expand Up @@ -115,7 +115,7 @@ extension ColorWell.PopoverConfiguration {
}

// MARK: SwatchShape: Equatable
extension ColorWell.PopoverConfiguration.SwatchShape: Equatable { }
extension ColorWell._PopoverConfiguration.SwatchShape: Equatable { }

// MARK: SwatchShape: Hashable
extension ColorWell.PopoverConfiguration.SwatchShape: Hashable { }
extension ColorWell._PopoverConfiguration.SwatchShape: Hashable { }
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ struct ColorWellRepresentable: NSViewRepresentable {
if colorWell.style != context.environment.colorWellStyleConfiguration.style {
colorWell.style = context.environment.colorWellStyleConfiguration.style
}
if colorWell.popoverConfiguration != context.environment.colorWellPopoverConfiguration {
colorWell.popoverConfiguration = context.environment.colorWellPopoverConfiguration
if colorWell._popoverConfiguration != context.environment.colorWellPopoverConfiguration {
colorWell._popoverConfiguration = context.environment.colorWellPopoverConfiguration
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/ColorWellKit/Views/SwiftUI/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private struct ColorWellStyleConfigurationKey: EnvironmentKey {

@available(macOS 10.15, *)
private struct ColorWellPopoverConfigurationKey: EnvironmentKey {
static let defaultValue = ColorWell.PopoverConfiguration.default
static let defaultValue = ColorWell._PopoverConfiguration.default
}

@available(macOS 10.15, *)
Expand All @@ -26,7 +26,7 @@ extension EnvironmentValues {

@available(macOS 10.15, *)
extension EnvironmentValues {
var colorWellPopoverConfiguration: ColorWell.PopoverConfiguration {
var colorWellPopoverConfiguration: ColorWell._PopoverConfiguration {
get { self[ColorWellPopoverConfigurationKey.self] }
set { self[ColorWellPopoverConfigurationKey.self] = newValue }
}
Expand Down

0 comments on commit 032951c

Please sign in to comment.