Skip to content

Commit

Permalink
Automatically update color text fields when changed
Browse files Browse the repository at this point in the history
This closes #5
Move files into groups, and general refactoring
  • Loading branch information
viktorstrate committed Jun 27, 2018
1 parent f3ec307 commit 9d9bbc9
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 40 deletions.
32 changes: 26 additions & 6 deletions Color Picker Plus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
3426013B20E159BA00767696 /* ColorUndoManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3426013A20E159BA00767696 /* ColorUndoManager.swift */; };
34414FA220E3C7E200E0E67A /* ColorRepresentingTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34414FA120E3C7E200E0E67A /* ColorRepresentingTextField.swift */; };
344AA57720DFEBEA0045C7B2 /* Color Picker Plus.colorPicker in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3491077020D13DDC0058E50D /* Color Picker Plus.colorPicker */; };
34509CE420D328C400CF78C0 /* RGB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34509CE320D328C400CF78C0 /* RGB.swift */; };
34509CE620D3293500CF78C0 /* HSV.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34509CE520D3293500CF78C0 /* HSV.swift */; };
Expand Down Expand Up @@ -37,6 +38,7 @@

/* Begin PBXFileReference section */
3426013A20E159BA00767696 /* ColorUndoManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorUndoManager.swift; sourceTree = "<group>"; };
34414FA120E3C7E200E0E67A /* ColorRepresentingTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorRepresentingTextField.swift; sourceTree = "<group>"; };
34509CE320D328C400CF78C0 /* RGB.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RGB.swift; sourceTree = "<group>"; };
34509CE520D3293500CF78C0 /* HSV.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HSV.swift; sourceTree = "<group>"; };
34509CE920D3ADDF00CF78C0 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
Expand All @@ -63,6 +65,26 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
34414FA320E3CC7A00E0E67A /* Views */ = {
isa = PBXGroup;
children = (
3491078520D164BF0058E50D /* ColorGraphicsView.swift */,
34BC620E20D27DA90093CCFC /* CurrentColorView.swift */,
347FCA7A20E13FE000C6C34A /* ContainerView.swift */,
);
path = Views;
sourceTree = "<group>";
};
34414FA420E3CCA200E0E67A /* Utils */ = {
isa = PBXGroup;
children = (
3426013A20E159BA00767696 /* ColorUndoManager.swift */,
34509CE920D3ADDF00CF78C0 /* Logger.swift */,
3491078720D1771D0058E50D /* HSBGen.swift */,
);
path = Utils;
sourceTree = "<group>";
};
34509CE220D328B000CF78C0 /* Models */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -91,18 +113,15 @@
3491077220D13DDC0058E50D /* Color Picker Plus */ = {
isa = PBXGroup;
children = (
34414FA420E3CCA200E0E67A /* Utils */,
34509CE220D328B000CF78C0 /* Models */,
34414FA320E3CC7A00E0E67A /* Views */,
3491077F20D149740058E50D /* ToolbarIcon.tiff */,
3491077320D13DDC0058E50D /* Info.plist */,
3491077920D13DF80058E50D /* ColorPickerPlus.swift */,
3491078120D14A080058E50D /* ColorPickerPlus.xib */,
3491078520D164BF0058E50D /* ColorGraphicsView.swift */,
34BC620E20D27DA90093CCFC /* CurrentColorView.swift */,
3491078720D1771D0058E50D /* HSBGen.swift */,
34509CE920D3ADDF00CF78C0 /* Logger.swift */,
347FCA7A20E13FE000C6C34A /* ContainerView.swift */,
3426013A20E159BA00767696 /* ColorUndoManager.swift */,
34CEE9C720E267CC00D56DF9 /* ScrollingTextField.swift */,
34414FA120E3C7E200E0E67A /* ColorRepresentingTextField.swift */,
);
path = "Color Picker Plus";
sourceTree = "<group>";
Expand Down Expand Up @@ -187,6 +206,7 @@
34509CE620D3293500CF78C0 /* HSV.swift in Sources */,
34BC620F20D27DA90093CCFC /* CurrentColorView.swift in Sources */,
3491078820D1771D0058E50D /* HSBGen.swift in Sources */,
34414FA220E3C7E200E0E67A /* ColorRepresentingTextField.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
34 changes: 20 additions & 14 deletions Color Picker Plus/ColorPickerPlus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import AppKit

public class ColorPickerPlus: NSColorPicker, NSColorPickingCustom {

static var shared: ColorPickerPlus!

@IBOutlet var pickerView: ContainerView!
@IBOutlet weak var colorGraphicsView: ColorGraphicsView!
@IBOutlet weak var currentColorView: CurrentColorView!
Expand All @@ -18,15 +20,15 @@ public class ColorPickerPlus: NSColorPicker, NSColorPickingCustom {
@IBOutlet weak var radioSaturation: NSButton!
@IBOutlet weak var radioBrightness: NSButton!

@IBOutlet weak var txtHue: NSTextField!
@IBOutlet weak var txtSaturation: NSTextField!
@IBOutlet weak var txtBrightness: NSTextField!
@IBOutlet weak var txtHex: NSTextField!
@IBOutlet weak var txtHue: ColorRepresentingTextField!
@IBOutlet weak var txtSaturation: ColorRepresentingTextField!
@IBOutlet weak var txtBrightness: ColorRepresentingTextField!
@IBOutlet weak var txtHex: ColorRepresentingTextField!

@IBOutlet weak var txtRed: NSTextField!
@IBOutlet weak var txtGreen: NSTextField!
@IBOutlet weak var txtBlue: NSTextField!
@IBOutlet weak var txtAlpha: NSTextField!
@IBOutlet weak var txtRed: ColorRepresentingTextField!
@IBOutlet weak var txtGreen: ColorRepresentingTextField!
@IBOutlet weak var txtBlue: ColorRepresentingTextField!
@IBOutlet weak var txtAlpha: ColorRepresentingTextField!

@IBOutlet weak var labelRed: ScrollingTextField!
@IBOutlet weak var labelGreen: ScrollingTextField!
Expand Down Expand Up @@ -56,6 +58,9 @@ public class ColorPickerPlus: NSColorPicker, NSColorPickingCustom {

public func provideNewView(_ initialRequest: Bool) -> NSView {
if (initialRequest) {

ColorPickerPlus.shared = self

let pickerNibName = "ColorPickerPlus"
guard bundle.loadNibNamed(NSNib.Name(rawValue: pickerNibName), owner: self, topLevelObjects: nil) else {
Logger.error(message: "Could not find nib named \(pickerNibName)")
Expand All @@ -66,17 +71,18 @@ public class ColorPickerPlus: NSColorPicker, NSColorPickingCustom {
radioHue.state = NSControl.StateValue.on

colorGraphicsView.delegate = self
pickerView.colorPickerPlus = self


let formatter = NumberFormatter()
formatter.numberStyle = .none

self.textFieldsNumberFormatter = formatter

labelRed.setup(inputField: txtRed, formatter: formatter, colorPickerPlus: self, min: 0, max: 255)
labelGreen.setup(inputField: txtGreen, formatter: formatter, colorPickerPlus: self, min: 0, max: 255)
labelBlue.setup(inputField: txtBlue, formatter: formatter, colorPickerPlus: self, min: 0, max: 255)
labelAlpha.setup(inputField: txtAlpha, formatter: formatter, colorPickerPlus: self)
labelRed.setup(inputField: txtRed, formatter: formatter, min: 0, max: 255)
labelGreen.setup(inputField: txtGreen, formatter: formatter, min: 0, max: 255)
labelBlue.setup(inputField: txtBlue, formatter: formatter, min: 0, max: 255)
labelAlpha.setup(inputField: txtAlpha, formatter: formatter)

txtHex.isHexField = true

}

Expand Down
16 changes: 8 additions & 8 deletions Color Picker Plus/ColorPickerPlus.xib
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<stackView distribution="fill" orientation="vertical" alignment="leading" spacing="10" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wJR-BT-YT1">
<rect key="frame" x="94" y="0.0" width="48" height="86"/>
<subviews>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Dmi-eZ-E7y">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Dmi-eZ-E7y" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="0.0" y="64" width="48" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="dgw-DR-OQv">
<font key="font" metaFont="system"/>
Expand All @@ -146,7 +146,7 @@
<action selector="colorFieldChanged:" target="-2" id="ln4-BJ-Mlu"/>
</connections>
</textField>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="RpC-Cw-ELb">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="RpC-Cw-ELb" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="0.0" y="32" width="48" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="aed-92-IxK">
<font key="font" metaFont="system"/>
Expand All @@ -157,7 +157,7 @@
<action selector="colorFieldChanged:" target="-2" id="auZ-Oo-gd4"/>
</connections>
</textField>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="lhq-oV-Ebz">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="lhq-oV-Ebz" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="48" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="VxX-KQ-TLf">
<font key="font" metaFont="system"/>
Expand Down Expand Up @@ -282,7 +282,7 @@
<stackView distribution="fill" orientation="vertical" alignment="leading" spacing="10" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ZDk-K3-R4f">
<rect key="frame" x="47" y="0.0" width="48" height="86"/>
<subviews>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="JEv-fu-S9J">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="JEv-fu-S9J" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="0.0" y="64" width="48" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="L3j-0q-75F">
<font key="font" metaFont="system"/>
Expand All @@ -293,7 +293,7 @@
<action selector="colorFieldChanged:" target="-2" id="e6P-gD-kXm"/>
</connections>
</textField>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="BVS-R3-iSJ">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="BVS-R3-iSJ" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="0.0" y="32" width="48" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="2gy-Q6-hgb">
<font key="font" metaFont="system"/>
Expand All @@ -304,7 +304,7 @@
<action selector="colorFieldChanged:" target="-2" id="ze9-ES-Bob"/>
</connections>
</textField>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="OFF-dh-jID">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="OFF-dh-jID" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="48" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="4lb-H6-cEE">
<font key="font" metaFont="system"/>
Expand Down Expand Up @@ -374,7 +374,7 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="8PU-Py-5OS">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="8PU-Py-5OS" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="14" y="0.0" width="72" height="22"/>
<constraints>
<constraint firstAttribute="width" constant="72" id="K3F-vH-G6A"/>
Expand Down Expand Up @@ -412,7 +412,7 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="b4l-9j-wMk">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="b4l-9j-wMk" customClass="ColorRepresentingTextField" customModule="Color_Picker_Plus" customModuleProvider="target">
<rect key="frame" x="44" y="0.0" width="48" height="22"/>
<constraints>
<constraint firstAttribute="width" constant="48" id="r48-Uw-2CM"/>
Expand Down
38 changes: 38 additions & 0 deletions Color Picker Plus/ColorRepresentingTextField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ColorRepresentingTextField.swift
// Color Picker Plus
//
// Created by Viktor Hundahl Strate on 27/06/2018.
// Copyright © 2018 Viktor Hundahl Strate. All rights reserved.
//

import Cocoa

class ColorRepresentingTextField: NSTextField, NSTextFieldDelegate {

var isHexField: Bool = false

override func awakeFromNib() {
super.awakeFromNib()
self.delegate = self
}

override func controlTextDidChange(_ obj: Notification) {
Logger.debug(message: "Text field changed")

if isHexField {
if self.stringValue.count != 6 {
Logger.debug(message: "Hex field is not 6 characters long, skipping color update")
return
}

if RGB.fromHEX(NSString(string: self.stringValue)) == nil {
Logger.debug(message: "Hex field does not contain a valid hex value, skipping color update")
return
}
}

ColorPickerPlus.shared.colorFieldChanged(self)
}

}
8 changes: 3 additions & 5 deletions Color Picker Plus/ScrollingTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ class ScrollingTextField: NSTextField {
var minValue: CGFloat! = 0
var maxValue: CGFloat! = 100
var numberFormatter: NumberFormatter!
var colorPickerPlus: ColorPickerPlus!


/// Mouse sensitivity
let sensitivity: CGFloat = 1

var startX: CGFloat = 0
var startValue: CGFloat = 0

func setup(inputField: NSTextField, formatter: NumberFormatter, colorPickerPlus: ColorPickerPlus, min: CGFloat = 0, max: CGFloat = 100) {
func setup(inputField: NSTextField, formatter: NumberFormatter, min: CGFloat = 0, max: CGFloat = 100) {
self.inputField = inputField
self.numberFormatter = formatter
self.colorPickerPlus = colorPickerPlus
self.minValue = min
self.maxValue = max
}
Expand All @@ -54,7 +52,7 @@ class ScrollingTextField: NSTextField {
newValue = max(newValue, minValue)

inputField.stringValue = numberFormatter.string(from: NSNumber(value: Float(newValue))) ?? "ERR"
colorPickerPlus.colorFieldChanged(self.inputField)
ColorPickerPlus.shared.colorFieldChanged(self.inputField)
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import Cocoa

class ContainerView: NSView {

var colorPickerPlus: ColorPickerPlus?
var colorPickerPlus: ColorPickerPlus {
get {
return ColorPickerPlus.shared
}
}

override func awakeFromNib() {

Expand All @@ -27,11 +31,6 @@ class ContainerView: NSView {

override func keyDown(with event: NSEvent) {

guard let colorPickerPlus = colorPickerPlus else {
Logger.error(message: "ContainerView could not find ColorPickerPlus")
return
}

Logger.debug(message: "Check for undo/redo shortcut")

guard let undoManager = colorPickerPlus.undoManager else {
Expand All @@ -55,7 +54,7 @@ class ContainerView: NSView {

override func performKeyEquivalent(with event: NSEvent) -> Bool {

guard let undoManager = colorPickerPlus?.undoManager else {
guard let undoManager = colorPickerPlus.undoManager else {
return false
}

Expand Down
File renamed without changes.

0 comments on commit 9d9bbc9

Please sign in to comment.