Skip to content

Commit 5f67562

Browse files
committed
Search + UI fixes
1 parent 1632cb5 commit 5f67562

File tree

2 files changed

+92
-54
lines changed

2 files changed

+92
-54
lines changed

ColorSet/Classes/MainWindowController.swift

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ import Cocoa
2626

2727
class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableViewDataSource, NSTextFieldDelegate, NSMenuDelegate
2828
{
29-
@objc private dynamic var selectedColor: ColorItem?
30-
@objc private dynamic var hasVariant = false
29+
@objc private dynamic var selectedColor: ColorItem?
30+
@objc private dynamic var hasVariant: Bool = false
31+
@objc public private( set ) dynamic var colors: [ ColorItem ] = []
3132

32-
public var url: URL?
33-
public private( set ) var colors: [ ColorItem ] = []
34-
private var observations: [ NSKeyValueObservation ] = []
33+
public var url: URL?
34+
private var observations: [ NSKeyValueObservation ] = []
35+
private var tableView: NSTableView?
36+
private var colorNameTextField: NSTextField?
37+
private var searchField: NSSearchField?
3538

36-
@IBOutlet public var tableView: NSTableView?
3739
@IBOutlet public var arrayController: NSArrayController?
3840

3941
convenience init( colors: [ ColorItem ] )
@@ -70,13 +72,20 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
7072
return
7173
}
7274

73-
self.tableView = tableView
75+
guard let colorNameTextField = self.window?.contentView?.subviewWithIdentifier( "ColorNameTextField" ) as? NSTextField else
76+
{
77+
return
78+
}
7479

75-
for color in self.colors
80+
guard let searchField = self.window?.contentView?.subviewWithIdentifier( "SearchField" ) as? NSSearchField else
7681
{
77-
controller.addObject( color )
82+
return
7883
}
7984

85+
self.tableView = tableView
86+
self.colorNameTextField = colorNameTextField
87+
self.searchField = searchField
88+
8089
controller.sortDescriptors = [ NSSortDescriptor( key: "name", ascending: true ) ]
8190
controller.selectsInsertedObjects = true
8291

@@ -123,6 +132,11 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
123132
self.observations.append( contentsOf: [ o1, o2 ] )
124133
}
125134

135+
@IBAction public func performFindPanelAction( _ sender: Any? )
136+
{
137+
self.window?.makeFirstResponder( self.searchField )
138+
}
139+
126140
@IBAction public func saveDocument( _ sender: Any? )
127141
{
128142
guard let url = self.url else
@@ -169,14 +183,9 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
169183

170184
public func save( to url: URL )
171185
{
172-
guard let colors = self.arrayController?.arrangedObjects as? [ ColorItem ] else
173-
{
174-
return
175-
}
176-
177186
let set = ColorSet()
178187

179-
for color in colors
188+
for color in self.colors
180189
{
181190
set.addColor( color.color, variant: color.variant, forName: color.name )
182191
}
@@ -205,12 +214,7 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
205214
var i = 0
206215
let color = ColorItem()
207216

208-
guard let items = self.arrayController?.arrangedObjects as? [ ColorItem ] else
209-
{
210-
return
211-
}
212-
213-
for item in items
217+
for item in self.colors
214218
{
215219
if( item.name.hasPrefix( "Untitled" ) )
216220
{
@@ -221,6 +225,7 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
221225
color.name = ( i > 0 ) ? "Untitled-" + String( describing: i ) : "Untitled"
222226

223227
self.arrayController?.addObject( color )
228+
self.window?.makeFirstResponder( self.colorNameTextField )
224229
}
225230

226231
@IBAction public func removeColor( _ sender: Any? )
@@ -259,26 +264,13 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
259264
return view
260265
}
261266

262-
func tableView( _ tableView: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int )
263-
{
264-
if( self.window?.isVisible ?? false )
265-
{
266-
tableView.editColumn( 0, row: row, with: nil, select: true )
267-
}
268-
}
269-
270267
func control( _ control: NSControl, textShouldBeginEditing fieldEditor: NSText ) -> Bool
271268
{
272269
return true
273270
}
274271

275272
func control( _ control: NSControl, textShouldEndEditing fieldEditor: NSText ) -> Bool
276273
{
277-
guard let items = self.arrayController?.arrangedObjects as? [ ColorItem ] else
278-
{
279-
return false
280-
}
281-
282274
guard let textField = control as? NSTextField else
283275
{
284276
return false
@@ -294,7 +286,7 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
294286
return false
295287
}
296288

297-
for item in items
289+
for item in self.colors
298290
{
299291
if( item == color )
300292
{

0 commit comments

Comments
 (0)