forked from rentzsch/markdownlive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PreferencesManager.m
122 lines (97 loc) · 3.53 KB
/
PreferencesManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// PreferencesManager.m
// MarkdownLive
//
// Created by Akihiro Noguchi on 7/05/11.
// Copyright 2011 Aki. All rights reserved.
//
#import "PreferencesManager.h"
@interface PreferencesManager (Private)
+ (NSColor *)colorForKey:(NSString *)key;
+ (void)setColor:(NSColor *)col forKey:(NSString *)key;
+ (NSDictionary *)editPaneDefaults;
@end
@implementation PreferencesManager
+ (void)initialize {
NSMutableDictionary *defVals = [NSMutableDictionary dictionary];
[defVals addEntriesFromDictionary:[PreferencesManager editPaneDefaults]];
[[NSUserDefaults standardUserDefaults] registerDefaults:defVals];
}
+ (NSDictionary *)editPaneDefaults {
return [NSDictionary dictionaryWithObjectsAndKeys:
@"Monaco", kEditPaneFontName,
[NSNumber numberWithFloat:9.0f], kEditPaneFontSize,
[NSArchiver archivedDataWithRootObject:[NSColor blackColor]], kEditPaneForegroundColor,
[NSArchiver archivedDataWithRootObject:[NSColor whiteColor]], kEditPaneBackgroundColor,
[NSArchiver archivedDataWithRootObject:[NSColor selectedTextBackgroundColor]], kEditPaneSelectionColor,
[NSArchiver archivedDataWithRootObject:[NSColor blackColor]], kEditPaneCaretColor,
nil];
}
+ (void)resetEditPanePreferences {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSDictionary *defs = [PreferencesManager editPaneDefaults];
for (NSString *key in defs) {
[prefs setObject:[defs objectForKey:key] forKey:key];
}
}
+ (NSString *)editPaneFontName {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
return [prefs stringForKey:kEditPaneFontName];
}
+ (void)setEditPaneFontName:(NSString *)value {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:value forKey:kEditPaneFontName];
}
+ (float)editPaneFontSize {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
return [prefs floatForKey:kEditPaneFontSize];
}
+ (void)setEditPaneFontSize:(float)value {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:value forKey:kEditPaneFontSize];
}
+ (NSFont *)editPaneFont {
return [NSFont fontWithName:[PreferencesManager editPaneFontName]
size:[PreferencesManager editPaneFontSize]];
}
+ (NSColor *)colorForKey:(NSString *)key {
if (key) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *data = [prefs dataForKey:key];
if (data) {
return (NSColor *)[NSUnarchiver unarchiveObjectWithData:data];
}
}
return nil;
}
+ (void)setColor:(NSColor *)col forKey:(NSString *)key {
if (col && key) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setValue:[NSArchiver archivedDataWithRootObject:col] forKey:key];
}
}
+ (NSColor *)editPaneForegroundColor {
return [PreferencesManager colorForKey:kEditPaneForegroundColor];
}
+ (void)setEditPaneForegroundColor:(NSColor *)value {
[PreferencesManager setColor:value forKey:kEditPaneForegroundColor];
}
+ (NSColor *)editPaneBackgroundColor {
return [PreferencesManager colorForKey:kEditPaneBackgroundColor];
}
+ (void)setEditPaneBackgroundColor:(NSColor *)value {
[PreferencesManager setColor:value forKey:kEditPaneBackgroundColor];
}
+ (NSColor *)editPaneSelectionColor {
return [PreferencesManager colorForKey:kEditPaneSelectionColor];
}
+ (void)setEditPaneSelectionColor:(NSColor *)value {
[PreferencesManager setColor:value forKey:kEditPaneSelectionColor];
}
+ (NSColor *)editPaneCaretColor {
return [PreferencesManager colorForKey:kEditPaneCaretColor];
}
+ (void)setEditPaneCaretColor:(NSColor *)value {
[PreferencesManager setColor:value forKey:kEditPaneCaretColor];
}
@end