Skip to content

Commit 3a9be37

Browse files
committed
ColorSet - Added a shared instance.
1 parent 40bb771 commit 3a9be37

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

ColorSetKit/ColorSet.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ import Cocoa
2828
{
2929
private static let magic: UInt64 = 0x434F4C4F52534554
3030

31+
public static var shared: ColorSet =
32+
{
33+
var set: ColorSet?
34+
35+
if let path = Bundle.main.path( forResource: "Colors", ofType: "colorset" )
36+
{
37+
if FileManager.default.fileExists( atPath: path )
38+
{
39+
set = ColorSet( path: path )
40+
}
41+
}
42+
43+
#if DEBUG
44+
45+
if set == nil, let path = Bundle( identifier: "com.xs-labs.ColorSetKit-Test" )?.path( forResource: "Colors", ofType: "colorset" )
46+
{
47+
if FileManager.default.fileExists( atPath: path )
48+
{
49+
set = ColorSet( path: path )
50+
}
51+
}
52+
53+
#endif
54+
55+
return set ?? ColorSet()
56+
}()
57+
3158
@objc public private( set ) dynamic var colors = [ String : ColorPair ]()
3259

3360
@objc public override init()

ColorSetKit/NSColor+ColorSetKit.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,8 @@ import Cocoa
2626

2727
@objc public extension NSColor
2828
{
29-
private static var mainColorSet: ColorSet?
30-
private static var lock = NSLock()
31-
3229
@objc class func colorFrom( colorSet name: String ) -> NSColor?
3330
{
34-
lock.lock()
35-
36-
if mainColorSet == nil
37-
{
38-
if let path = Bundle.main.path( forResource: "Colors", ofType: "colorset" )
39-
{
40-
if FileManager.default.fileExists( atPath: path )
41-
{
42-
mainColorSet = ColorSet( path: path )
43-
}
44-
}
45-
}
46-
47-
lock.unlock()
48-
4931
if name.hasPrefix( "NS" )
5032
{
5133
var selectorName = String( name[ name.index( name.startIndex, offsetBy: 2 )... ] )
@@ -64,7 +46,7 @@ import Cocoa
6446

6547
}
6648

67-
let pair = mainColorSet?.colors[ name ]
49+
let pair = ColorSet.shared.colors[ name ]
6850

6951
if #available( macOS 10.14, * )
7052
{

0 commit comments

Comments
 (0)