Skip to content

Commit c200247

Browse files
committed
Custom file format
1 parent 788ac1d commit c200247

File tree

6 files changed

+51
-73
lines changed

6 files changed

+51
-73
lines changed

ColorSet/Classes/ApplicationDelegate.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,42 @@ class ApplicationDelegate: NSResponder, NSApplicationDelegate
9191

9292
private func open( url: URL ) -> Bool
9393
{
94-
do
94+
guard let set = ColorSet( url: url ) else
9595
{
96-
let data = try Data( contentsOf: url )
96+
let alert = NSAlert()
97+
alert.messageText = "Load error"
98+
alert.informativeText = "Invalid data format"
9799

98-
guard let colors = NSKeyedUnarchiver.unarchiveObject( with: data ) as? [ ColorItem ] else
100+
alert.runModal()
101+
102+
return false
103+
}
104+
105+
var colors = [ ColorItem ]()
106+
107+
for p in set.colors
108+
{
109+
guard let color = p.value.color else
99110
{
100-
let alert = NSAlert()
101-
alert.messageText = "Load error"
102-
alert.informativeText = "Invalid data format"
103-
104-
alert.runModal()
105-
106-
return false
111+
continue
107112
}
108113

109-
let controller = MainWindowController( colors: colors )
110-
controller.url = url
111-
112-
controller.window?.center()
113-
controller.window?.makeKeyAndOrderFront( nil )
114+
let item = ColorItem()
115+
item.name = p.key
116+
item.color = color
117+
item.variant = p.value.variant
114118

115-
self.controllers.append( controller )
116-
}
117-
catch let error as NSError
118-
{
119-
NSAlert( error: error ).runModal()
119+
colors.append( item )
120120
}
121121

122+
let controller = MainWindowController( colors: colors )
123+
controller.url = url
124+
125+
controller.window?.center()
126+
controller.window?.makeKeyAndOrderFront( nil )
127+
128+
self.controllers.append( controller )
129+
122130
return true
123131
}
124132
}

ColorSet/Classes/ColorItem.swift

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Cocoa
2626

27-
class ColorItem: NSObject, NSCoding
27+
class ColorItem: NSObject
2828
{
2929
@objc public dynamic var name = "Untitled"
3030
@objc public dynamic var hasVariant = false
@@ -177,43 +177,4 @@ class ColorItem: NSObject, NSCoding
177177

178178
self.updating = false
179179
}
180-
181-
func encode( with coder: NSCoder )
182-
{
183-
coder.encode( self.name, forKey: "n" )
184-
coder.encode( self.hasVariant, forKey: "v" )
185-
186-
coder.encode( Double( self.red ), forKey: "r1" )
187-
coder.encode( Double( self.green ), forKey: "g1" )
188-
coder.encode( Double( self.blue ), forKey: "b1" )
189-
coder.encode( Double( self.alpha ), forKey: "a1" )
190-
191-
coder.encode( Double( self.red2 ), forKey: "r2" )
192-
coder.encode( Double( self.green2 ), forKey: "g2" )
193-
coder.encode( Double( self.blue2 ), forKey: "b2" )
194-
coder.encode( Double( self.alpha2 ), forKey: "a2" )
195-
}
196-
197-
required init?( coder: NSCoder )
198-
{
199-
guard let n = coder.decodeObject( forKey: "n" ) as? String else { return nil }
200-
201-
self.name = n
202-
self.hasVariant = coder.decodeBool( forKey: "v" )
203-
204-
self.red = CGFloat( coder.decodeDouble( forKey: "r1" ) )
205-
self.green = CGFloat( coder.decodeDouble( forKey: "g1" ) )
206-
self.blue = CGFloat( coder.decodeDouble( forKey: "b1" ) )
207-
self.alpha = CGFloat( coder.decodeDouble( forKey: "a1" ) )
208-
209-
self.red2 = CGFloat( coder.decodeDouble( forKey: "r2" ) )
210-
self.green2 = CGFloat( coder.decodeDouble( forKey: "g2" ) )
211-
self.blue2 = CGFloat( coder.decodeDouble( forKey: "b2" ) )
212-
self.alpha2 = CGFloat( coder.decodeDouble( forKey: "a2" ) )
213-
214-
super.init()
215-
self.observe()
216-
self.updateColorFromRGB()
217-
self.updateVariantFromRGB()
218-
}
219180
}

ColorSet/Classes/ColorSet-Bridging-Header.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323
******************************************************************************/
2424

2525
#import "Exception.h"
26+
#import "ColorPair.h"
27+
#import "ColorSet.h"

ColorSet/Classes/ColorSet.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ - ( nullable instancetype )initWithData: ( NSData * )data
8787

8888
[ stream readUInt32 ];
8989

90-
if( ( n = [ stream readUInt32 ] ) )
91-
{
92-
return 0;
93-
}
90+
n = [ stream readUInt64 ];
9491

9592
for( i = 0; i < n; i++ )
9693
{
@@ -100,19 +97,19 @@ - ( nullable instancetype )initWithData: ( NSData * )data
10097
NSColor * color;
10198
NSColor * variant;
10299

103-
if( ( name = [ stream readString ] ) )
100+
if( ( name = [ stream readString ] ) == nil )
104101
{
105102
return nil;
106103
}
107104

108105
hasVariant = [ stream readUInt8 ];
109106

110-
if( ( color = [ stream readColor ] ) )
107+
if( ( color = [ stream readColor ] ) == nil )
111108
{
112109
return nil;
113110
}
114111

115-
if( ( variant = [ stream readColor ] ) )
112+
if( ( variant = [ stream readColor ] ) == nil )
116113
{
117114
return nil;
118115
}
@@ -168,15 +165,19 @@ - ( void )addColor: ( NSColor * )color variant: ( nullable NSColor * )variant fo
168165
return;
169166
}
170167

168+
[ self willChangeValueForKey: NSStringFromSelector( @selector( mutableColors ) ) ];
171169
[ self.mutableColors setObject: [ [ ColorPair alloc ] initWithColor: color variant: variant ] forKey: name ];
170+
[ self didChangeValueForKey: NSStringFromSelector( @selector( mutableColors ) ) ];
172171
}
173172
}
174173

175174
- ( void )setColor: ( NSColor * )color variant: ( nullable NSColor * )variant forName: ( NSString * )name
176175
{
177176
@synchronized( self )
178177
{
178+
[ self willChangeValueForKey: NSStringFromSelector( @selector( mutableColors ) ) ];
179179
[ self.mutableColors setObject: [ [ ColorPair alloc ] initWithColor: color variant: variant ] forKey: name ];
180+
[ self didChangeValueForKey: NSStringFromSelector( @selector( mutableColors ) ) ];
180181
}
181182
}
182183

ColorSet/Classes/ColorSetStream.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ - ( BOOL )read: ( size_t )n in: ( void * )buf
282282
{
283283
if( self.pos < self.mutableData.length + n )
284284
{
285-
memcpy( buf, ( ( char * )( self.mutableData.bytes ) ) + n, n );
285+
memcpy( buf, ( ( char * )( self.mutableData.bytes ) ) + self.pos, n );
286286

287-
self.pos += n;
287+
self.pos = self.pos + n;
288288

289289
return YES;
290290
}

ColorSet/Classes/MainWindowController.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
6767
controller.addObject( color )
6868
}
6969

70-
self.arrayController?.selectsInsertedObjects = true
70+
controller.sortDescriptors = [ NSSortDescriptor( key: "name", ascending: true ) ]
71+
controller.selectsInsertedObjects = true
7172

72-
colorView.bind( NSBindingName( "color" ), to: self, withKeyPath: "selectedColor.color", options: nil )
73+
colorView.bind( NSBindingName( "color" ), to: self, withKeyPath: "selectedColor.color", options: nil )
7374
variantView.bind( NSBindingName( "color" ), to: self, withKeyPath: "selectedColor.variant", options: nil )
7475

7576
let o1 = controller.observe( \.selectionIndexes, options: .new )
@@ -163,11 +164,16 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
163164
return
164165
}
165166

166-
let data = NSKeyedArchiver.archivedData( withRootObject: colors )
167+
let set = ColorSet()
168+
169+
for color in colors
170+
{
171+
set.addColor( color.color, variant: color.variant, forName: color.name )
172+
}
167173

168174
do
169175
{
170-
try data.write( to: url )
176+
try set.write( to: url )
171177
}
172178
catch let error as NSError
173179
{

0 commit comments

Comments
 (0)