-
Notifications
You must be signed in to change notification settings - Fork 22
/
AppController.m
160 lines (128 loc) · 4.71 KB
/
AppController.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
//
// AppController.m
// Textus
//
// Created by Jjgod Jiang on 3/16/09.
//
#import "AppController.h"
#import "TTTextView.h"
#import "TTDocument.h"
int hexNum(char ch) {
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
return 0;
}
NSColor* decodeColor(NSString* colorString) {
const char* cstr = [colorString UTF8String];
if (strlen(cstr) != 7)
return [NSColor whiteColor];
CGFloat red = (hexNum(cstr[1]) * 16 + hexNum(cstr[2])) / 255.0;
CGFloat green = (hexNum(cstr[3]) * 16 + hexNum(cstr[4])) / 255.0;
CGFloat blue = (hexNum(cstr[5]) * 16 + hexNum(cstr[6])) / 255.0;
return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0];
}
NSString* encodeColor(NSColor* color) {
return [NSString stringWithFormat:@"#%02X%02X%02X",
(int)([color redComponent] * 255.0),
(int)([color greenComponent] * 255.0),
(int)([color blueComponent] * 255.0)];
}
@implementation AppController {
NSMutableDictionary* _bookmarksDictionary;
}
+ (void)initialize {
NSMutableDictionary* appDefaults = [NSMutableDictionary dictionary];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[appDefaults setValue:@"#000000" forKey:@"foregroundColor"];
[appDefaults setValue:@"#FFFFFF" forKey:@"backgroundColor"];
[appDefaults setValue:@"STKaiti-SC-Regular" forKey:@"fontName"];
[appDefaults setValue:@24.0 forKey:@"fontSize"];
[appDefaults setValue:@1.1 forKey:@"lineHeight"];
[appDefaults setValue:[NSMutableDictionary dictionaryWithCapacity:20]
forKey:@"bookmarks"];
[defaults registerDefaults:appDefaults];
[[NSUserDefaultsController sharedUserDefaultsController]
setInitialValues:appDefaults];
[[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
}
- (void)awakeFromNib {
[[NSFontManager sharedFontManager] setSelectedFont:[self font] isMultiple:NO];
// load bookmarks into dictionary and insert them into menu
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
_bookmarksDictionary = [[defaults dictionaryForKey:@"bookmarks"] mutableCopy];
if (!_bookmarksDictionary)
_bookmarksDictionary = [[NSMutableDictionary alloc] init];
}
- (NSFont*)font {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
return [NSFont fontWithName:[defaults stringForKey:@"fontName"]
size:[defaults doubleForKey:@"fontSize"]];
}
- (NSColor*)foregroundColor {
NSString* colorString =
[[NSUserDefaults standardUserDefaults] stringForKey:@"foregroundColor"];
return decodeColor(colorString);
}
- (void)setForegroundColor:(NSColor*)color {
[[NSUserDefaults standardUserDefaults] setValue:encodeColor(color)
forKey:@"foregroundColor"];
}
- (NSColor*)backgroundColor {
NSString* colorString =
[[NSUserDefaults standardUserDefaults] stringForKey:@"backgroundColor"];
return decodeColor(colorString);
}
- (void)setBackgroundColor:(NSColor*)color {
[[NSUserDefaults standardUserDefaults] setValue:encodeColor(color)
forKey:@"backgroundColor"];
}
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication*)sender {
return NO;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)changeFont:(id)sender {
NSFont* oldFont = [self font];
NSFont* newFont = [sender convertFont:oldFont];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([oldFont fontName] != [newFont fontName])
[defaults setValue:[newFont fontName] forKey:@"fontName"];
if ([oldFont pointSize] != [newFont pointSize])
[defaults setValue:@([newFont pointSize]) forKey:@"fontSize"];
}
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem {
SEL theAction = [anItem action];
if (theAction == @selector(addBookmark:)) {
if ([NSApp keyWindow])
return YES;
return NO;
}
return YES;
}
- (IBAction)gotoBookmark:(id)sender {
}
- (IBAction)addBookmark:(id)sender {
if ([NSApp keyWindow]) {
TTTextView* textView =
[[[NSApp keyWindow].contentView subviews][0] documentView];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* indexes =
_bookmarksDictionary[textView.document.fileURL.path];
if (!indexes) {
indexes = [[NSMutableArray alloc] init];
_bookmarksDictionary[textView.document.fileURL.path] = indexes;
[indexes addObject:@(textView.document.fileContents.length)];
}
NSNumber* location = @(textView.document.lastReadLocation);
if (![indexes containsObject:location]) {
[indexes addObject:location];
[defaults setObject:_bookmarksDictionary forKey:@"bookmarks"];
}
}
}
@end