Skip to content

Commit 418a1f1

Browse files
committed
Editing of lightness pairs.
1 parent dc6195f commit 418a1f1

File tree

6 files changed

+73
-6
lines changed

6 files changed

+73
-6
lines changed

ColorSet/Classes/LightnessPairCollectionViewItem.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ public class LightnessPairCollectionViewItem: NSCollectionViewItem
3434
self.color1.bind( NSBindingName( "color" ), to: self, withKeyPath: "representedObject.lightness1.color", options: nil )
3535
self.color2.bind( NSBindingName( "color" ), to: self, withKeyPath: "representedObject.lightness2.color", options: nil )
3636
}
37+
38+
@IBAction func edit( _ sender: Any? )
39+
{
40+
if let item = self.representedObject as? LightnessPairItem
41+
{
42+
item.onEdit?( item )
43+
}
44+
}
45+
46+
@IBAction func delete( _ sender: Any? )
47+
{
48+
if let item = self.representedObject as? LightnessPairItem
49+
{
50+
item.onDelete?( item )
51+
}
52+
}
3753
}

ColorSet/Classes/LightnessPairItem.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ import Cocoa
6363
@objc public private( set ) dynamic var lightness1: LightnessVariant
6464
@objc public private( set ) dynamic var lightness2: LightnessVariant
6565

66+
public var onEdit: ( ( LightnessPairItem ) -> Void )?
67+
public var onDelete: ( ( LightnessPairItem ) -> Void )?
68+
6669
init( base: ColorItem? )
6770
{
6871
self.lightness1 = LightnessVariant( base: base )

ColorSet/Classes/LightnessPairWindowController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class LightnessPairWindowController: NSWindowController
2828
{
2929
@objc public private( set ) dynamic var item = LightnessPairItem( base: nil )
3030

31-
convenience init( base: ColorItem )
31+
convenience init( base: ColorItem, item: LightnessPairItem )
3232
{
3333
self.init()
3434

35-
self.item = LightnessPairItem( base: base )
35+
self.item = item
3636
}
3737

3838
override var windowNibName: NSNib.Name?

ColorSet/Classes/MainWindowController.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
172172
{
173173
l.lightness1.base = self.selectedColor
174174
l.lightness2.base = self.selectedColor
175+
176+
l.onEdit =
177+
{
178+
[ weak self ] item in self?.editLightnessPair( item )
179+
}
180+
181+
l.onDelete =
182+
{
183+
[ weak self ] item in self?.lightnessPairsArrayController.removeObject( item )
184+
}
175185
}
176186
}
177187

@@ -383,6 +393,19 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
383393
}
384394

385395
@IBAction func addLightnessPair( _ sender: Any? )
396+
{
397+
self.editLightnessPair( nil )
398+
{
399+
[ weak self ] c, r in
400+
401+
if r == .OK
402+
{
403+
self?.lightnessPairsArrayController.addObject( c.item )
404+
}
405+
}
406+
}
407+
408+
func editLightnessPair( _ item: LightnessPairItem?, completion: ( ( LightnessPairWindowController, NSApplication.ModalResponse ) -> Void )? = nil )
386409
{
387410
guard let selectedColor = self.selectedColor else
388411
{
@@ -405,7 +428,9 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
405428
return
406429
}
407430

408-
let sheetController = LightnessPairWindowController( base: selectedColor )
431+
let o = item ?? LightnessPairItem( base: selectedColor )
432+
433+
let sheetController = LightnessPairWindowController( base: selectedColor, item: o )
409434

410435
guard let sheet = sheetController.window else
411436
{
@@ -424,10 +449,13 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
424449

425450
if r != .OK
426451
{
452+
completion?( sheetController, r )
453+
427454
return
428455
}
429456

430-
self.lightnessPairsArrayController.addObject( sheetController.item )
457+
completion?( sheetController, r )
458+
self.updateLightnesses()
431459
}
432460
}
433461
}

ColorSet/Classes/PaletteColorItem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ import ColorSetKit
101101
lightnesses.append( ( l2, c2 ) )
102102
}
103103

104-
if let color = info.colorPair.color
104+
if let color = info.colorPair.color?.usingColorSpace( .sRGB )
105105
{
106106
lightnesses.append( ( color.hsl().lightness, color ) )
107107
}
108108

109-
if let variant = info.colorPair.variant
109+
if let variant = info.colorPair.variant?.usingColorSpace( .sRGB )
110110
{
111111
lightnesses.append( ( variant.hsl().lightness, variant ) )
112112
}

ColorSet/Interface/Base.lproj/ColorSet.LightnessPairCollectionViewItem.xib

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,27 @@
121121
<constraint firstItem="nc5-o0-Gmi" firstAttribute="centerY" secondItem="2Zs-NC-huo" secondAttribute="centerY" id="vjc-bo-W29"/>
122122
<constraint firstAttribute="trailing" secondItem="tQ1-DE-98J" secondAttribute="trailing" id="yOg-WX-fDY"/>
123123
</constraints>
124+
<connections>
125+
<outlet property="menu" destination="8VB-Yx-MB9" id="MSF-8c-Y2e"/>
126+
</connections>
124127
<point key="canvasLocation" x="28" y="112"/>
125128
</customView>
129+
<menu id="8VB-Yx-MB9">
130+
<items>
131+
<menuItem title="Edit..." id="UIj-Rw-Vni">
132+
<modifierMask key="keyEquivalentModifierMask"/>
133+
<connections>
134+
<action selector="edit:" target="-2" id="GH8-c8-DsD"/>
135+
</connections>
136+
</menuItem>
137+
<menuItem title="Delete" id="qRQ-V5-aS8">
138+
<modifierMask key="keyEquivalentModifierMask"/>
139+
<connections>
140+
<action selector="delete:" target="-2" id="VaS-JE-ToR"/>
141+
</connections>
142+
</menuItem>
143+
</items>
144+
<point key="canvasLocation" x="139" y="238"/>
145+
</menu>
126146
</objects>
127147
</document>

0 commit comments

Comments
 (0)