forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 11
/
UserPreferences.m
213 lines (185 loc) · 7.05 KB
/
UserPreferences.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// UserPreferences.m
// iSH
//
// Created by Charlie Melbye on 11/12/18.
//
#import <UIKit/UIKit.h>
#import "UserPreferences.h"
static NSString *const kPreferenceCapsLockMappingKey = @"Caps Lock Mapping";
static NSString *const kPreferenceOptionMappingKey = @"Option Mapping";
static NSString *const kPreferenceBacktickEscapeKey = @"Backtick Mapping Escape";
static NSString *const kPreferenceFontFamilyKey = @"Font Family";
static NSString *const kPreferenceFontSizeKey = @"Font Size";
static NSString *const kPreferenceThemeKey = @"Theme";
static NSString *const kPreferenceDisableDimmingKey = @"Disable Dimming";
NSString *const kPreferenceLaunchCommandKey = @"Init Command";
NSString *const kPreferenceBootCommandKey = @"Boot Command";
@implementation UserPreferences {
NSUserDefaults *_defaults;
}
+ (instancetype)shared {
static UserPreferences *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});
return shared;
}
- (instancetype)init {
self = [super init];
if (self) {
_defaults = [NSUserDefaults standardUserDefaults];
Theme *defaultTheme = [Theme presetThemeNamed:@"Light"];
[_defaults registerDefaults:@{
kPreferenceFontFamilyKey: @"Menlo",
kPreferenceFontSizeKey: @(12),
kPreferenceThemeKey: defaultTheme.properties,
kPreferenceCapsLockMappingKey: @(CapsLockMapControl),
kPreferenceOptionMappingKey: @(OptionMapNone),
kPreferenceBacktickEscapeKey: @(NO),
kPreferenceDisableDimmingKey: @(NO),
kPreferenceLaunchCommandKey: @[@"/bin/login", @"-f", @"root"],
kPreferenceBootCommandKey: @[@"/sbin/init"],
}];
_theme = [[Theme alloc] initWithProperties:[_defaults objectForKey:kPreferenceThemeKey]];
}
return self;
}
- (CapsLockMapping)capsLockMapping {
return [_defaults integerForKey:kPreferenceCapsLockMappingKey];
}
- (void)setCapsLockMapping:(CapsLockMapping)capsLockMapping {
[_defaults setInteger:capsLockMapping forKey:kPreferenceCapsLockMappingKey];
}
- (OptionMapping)optionMapping {
return [_defaults integerForKey:kPreferenceOptionMappingKey];
}
- (void)setOptionMapping:(OptionMapping)optionMapping {
[_defaults setInteger:optionMapping forKey:kPreferenceOptionMappingKey];
}
- (BOOL)backtickMapEscape {
return [_defaults boolForKey:kPreferenceBacktickEscapeKey];
}
- (void)setBacktickMapEscape:(BOOL)backtickMapEscape {
[_defaults setBool:backtickMapEscape forKey:kPreferenceBacktickEscapeKey];
}
- (NSNumber *)fontSize {
return [_defaults objectForKey:kPreferenceFontSizeKey];
}
- (void)setFontSize:(NSNumber *)fontSize {
[_defaults setObject:fontSize forKey:kPreferenceFontSizeKey];
}
- (NSString *)fontFamily {
return [_defaults objectForKey:kPreferenceFontFamilyKey];
}
- (void)setFontFamily:(NSString *)fontFamily {
[_defaults setObject:fontFamily forKey:kPreferenceFontFamilyKey];
}
- (UIColor *)foregroundColor {
return self.theme.foregroundColor;
}
- (UIColor *)backgroundColor {
return self.theme.backgroundColor;
}
- (void)setTheme:(Theme *)theme {
_theme = theme;
[_defaults setObject:theme.properties forKey:kPreferenceThemeKey];
}
- (BOOL)shouldDisableDimming {
return [_defaults boolForKey:kPreferenceDisableDimmingKey];
}
- (void)setShouldDisableDimming:(BOOL)dim {
[_defaults setBool:dim forKey:kPreferenceDisableDimmingKey];
}
- (NSArray<NSString *> *)launchCommand {
return [_defaults stringArrayForKey:kPreferenceLaunchCommandKey];
}
- (void)setLaunchCommand:(NSArray<NSString *> *)launchCommand {
[_defaults setObject:launchCommand forKey:kPreferenceLaunchCommandKey];
}
- (BOOL)hasChangedLaunchCommand {
NSArray *defaultLaunchCommand = [[[NSUserDefaults alloc] initWithSuiteName:NSRegistrationDomain] stringArrayForKey:kPreferenceLaunchCommandKey];
return ![self.launchCommand isEqual:defaultLaunchCommand];
}
- (NSArray<NSString *> *)bootCommand {
return [_defaults stringArrayForKey:kPreferenceBootCommandKey];
}
- (void)setBootCommand:(NSArray<NSString *> *)bootCommand {
[_defaults setObject:bootCommand forKey:kPreferenceBootCommandKey];
}
@end
static id ArchiveColor(UIColor *color) {
CGFloat r, g, b;
[color getRed:&r green:&g blue:&b alpha:nil];
return [NSString stringWithFormat:@"%f %f %f", r, g, b];
}
static UIColor *UnarchiveColor(id data) {
NSArray<NSString *> *components = [data componentsSeparatedByString:@" "];
CGFloat r = components[0].doubleValue;
CGFloat g = components[1].doubleValue;
CGFloat b = components[2].doubleValue;
return [UIColor colorWithRed:r green:g blue:b alpha:1];
}
@implementation Theme
- (instancetype)initWithProperties:(NSDictionary<NSString *,id> *)props {
if (self = [super init]) {
_foregroundColor = UnarchiveColor(props[kThemeForegroundColor]);
_backgroundColor = UnarchiveColor(props[kThemeBackgroundColor]);
}
return self;
}
+ (instancetype)_themeWithForegroundColor:(UIColor *)foreground backgroundColor:(UIColor *)background {
return [[self alloc] initWithProperties:@{kThemeForegroundColor: ArchiveColor(foreground),
kThemeBackgroundColor: ArchiveColor(background)}];
}
- (UIStatusBarStyle)statusBarStyle {
CGFloat lightness;
[self.backgroundColor getWhite:&lightness alpha:nil];
if (lightness > 0.5)
return UIStatusBarStyleDefault;
else
return UIStatusBarStyleLightContent;
}
- (UIKeyboardAppearance)keyboardAppearance {
CGFloat lightness;
[self.backgroundColor getWhite:&lightness alpha:nil];
if (lightness > 0.5)
return UIKeyboardAppearanceLight;
else
return UIKeyboardAppearanceDark;
}
- (NSDictionary<NSString *,id> *)properties {
return @{kThemeForegroundColor: ArchiveColor(self.foregroundColor),
kThemeBackgroundColor: ArchiveColor(self.backgroundColor)};
}
- (BOOL)isEqual:(id)object {
if ([self class] != [object class])
return NO;
return [self.properties isEqualToDictionary:[object properties]];
}
NSDictionary<NSString *, Theme *> *presetThemes;
+ (void)initialize {
presetThemes = @{@"Light": [self _themeWithForegroundColor:UIColor.blackColor
backgroundColor:UIColor.whiteColor],
@"Dark": [self _themeWithForegroundColor:UIColor.whiteColor
backgroundColor:UIColor.blackColor],
@"1337": [self _themeWithForegroundColor:UIColor.greenColor
backgroundColor:UIColor.blackColor]};
}
+ (NSArray<NSString *> *)presetNames {
return @[@"Light", @"Dark", @"1337"];
}
+ (instancetype)presetThemeNamed:(NSString *)name {
return presetThemes[name];
}
- (NSString *)presetName {
for (NSString *name in presetThemes) {
if ([self isEqual:presetThemes[name]])
return name;
}
return nil;
}
@end
NSString *const kThemeForegroundColor = @"ForegroundColor";
NSString *const kThemeBackgroundColor = @"BackgroundColor";