Repository navigation
Expand file tree
/
Copy pathSCSafetyCheckWindowController.m
More file actions
265 lines (222 loc) · 8.83 KB
/
Copy pathSCSafetyCheckWindowController.m
File metadata and controls
265 lines (222 loc) · 8.83 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// SCSafetyCheckWindowController.m
// SelfControl
//
// Window controller for the startup safety check UI.
// Creates UI programmatically (no XIB needed).
//
#import "SCSafetyCheckWindowController.h"
#import "SCStartupSafetyCheck.h"
#import "SCVersionTracker.h"
@interface SCSafetyCheckWindowController ()
@property (nonatomic, strong) SCStartupSafetyCheck* safetyCheck;
@property (nonatomic, assign) BOOL checkInProgress;
@property (nonatomic, strong) SCSafetyCheckResult* lastResult;
// Programmatic UI elements
@property (nonatomic, strong) NSProgressIndicator* progressIndicator;
@property (nonatomic, strong) NSTextField* statusLabel;
@property (nonatomic, strong) NSButton* okButton;
@property (nonatomic, strong) NSView* resultsView;
@property (nonatomic, strong) NSTextField* resultTitleLabel;
@property (nonatomic, strong) NSArray<NSTextField*>* resultLabels;
@end
@implementation SCSafetyCheckWindowController
- (instancetype)init {
// Create window programmatically
NSRect frame = NSMakeRect(0, 0, 420, 365);
NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:NO];
window.title = @"Fence Safety Check";
self = [super initWithWindow:window];
if (self) {
_safetyCheck = nil;
_checkInProgress = NO;
[self setupUI];
}
return self;
}
- (void)setupUI {
NSView* contentView = self.window.contentView;
CGFloat width = contentView.frame.size.width;
CGFloat y = contentView.frame.size.height - 40;
// Title label
NSTextField* titleLabel = [self createLabelWithText:@"Safety Check" fontSize:18 bold:YES];
titleLabel.frame = NSMakeRect(20, y, width - 40, 24);
[contentView addSubview:titleLabel];
y -= 30;
// Subtitle
NSTextField* subtitleLabel = [self createLabelWithText:@"Verifying blocking mechanisms work correctly..." fontSize:12 bold:NO];
subtitleLabel.textColor = [NSColor secondaryLabelColor];
subtitleLabel.frame = NSMakeRect(20, y, width - 40, 18);
[contentView addSubview:subtitleLabel];
y -= 35;
// Progress indicator
self.progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(20, y, width - 40, 20)];
self.progressIndicator.style = NSProgressIndicatorStyleBar;
self.progressIndicator.indeterminate = NO; // Must be NO for determinate progress bar
self.progressIndicator.minValue = 0;
self.progressIndicator.maxValue = 100;
self.progressIndicator.doubleValue = 0;
[contentView addSubview:self.progressIndicator];
y -= 25;
// Status label
self.statusLabel = [self createLabelWithText:@"Ready to start..." fontSize:13 bold:NO];
self.statusLabel.frame = NSMakeRect(20, y, width - 40, 18);
[contentView addSubview:self.statusLabel];
y -= 40;
// Results view (initially hidden)
self.resultsView = [[NSView alloc] initWithFrame:NSMakeRect(20, 60, width - 40, y - 60)];
self.resultsView.hidden = YES;
[contentView addSubview:self.resultsView];
// Result title
self.resultTitleLabel = [self createLabelWithText:@"Results" fontSize:16 bold:YES];
self.resultTitleLabel.frame = NSMakeRect(0, self.resultsView.frame.size.height - 24, width - 40, 24);
[self.resultsView addSubview:self.resultTitleLabel];
// Result labels
NSArray* resultNames = @[
@"Hosts file blocking",
@"Packet filter blocking",
@"App blocking (Calculator)",
@"Hosts file cleanup",
@"Packet filter cleanup",
@"App unblocking",
@"Emergency script (emergency.sh)"
];
NSMutableArray* labels = [NSMutableArray array];
CGFloat ry = self.resultsView.frame.size.height - 50;
for (NSString* name in resultNames) {
NSTextField* label = [self createLabelWithText:[NSString stringWithFormat:@"\u2022 %@", name] fontSize:13 bold:NO];
label.frame = NSMakeRect(10, ry, width - 60, 18);
[self.resultsView addSubview:label];
[labels addObject:label];
ry -= 22;
}
self.resultLabels = labels;
// Buttons at bottom
CGFloat buttonY = 15;
CGFloat buttonWidth = 90;
// OK button (initially hidden)
self.okButton = [[NSButton alloc] initWithFrame:NSMakeRect(width - buttonWidth - 20, buttonY, buttonWidth, 32)];
self.okButton.bezelStyle = NSBezelStyleRounded;
self.okButton.title = @"OK";
self.okButton.target = self;
self.okButton.action = @selector(okClicked:);
self.okButton.keyEquivalent = @"\r";
self.okButton.hidden = YES;
[contentView addSubview:self.okButton];
[self.window center];
}
- (NSTextField*)createLabelWithText:(NSString*)text fontSize:(CGFloat)fontSize bold:(BOOL)bold {
NSTextField* label = [[NSTextField alloc] init];
label.stringValue = text;
label.editable = NO;
label.bordered = NO;
label.drawsBackground = NO;
label.selectable = NO;
if (bold) {
label.font = [NSFont boldSystemFontOfSize:fontSize];
} else {
label.font = [NSFont systemFontOfSize:fontSize];
}
return label;
}
- (void)showWindow:(id)sender {
[super showWindow:sender];
[self.window center];
[self.window makeKeyAndOrderFront:nil];
[self.window setLevel:NSFloatingWindowLevel];
[NSApp activateIgnoringOtherApps:YES];
}
- (void)runSafetyCheck {
if (self.checkInProgress) {
NSLog(@"SCSafetyCheckWindowController: Check already in progress");
return;
}
// Reset UI state for fresh run (fixes display bug on re-run)
self.progressIndicator.doubleValue = 0;
self.statusLabel.stringValue = @"Starting safety check...";
for (NSTextField* label in self.resultLabels) {
label.textColor = [NSColor labelColor];
}
self.resultTitleLabel.textColor = [NSColor labelColor];
self.checkInProgress = YES;
self.resultsView.hidden = YES;
self.okButton.hidden = YES;
self.safetyCheck = [[SCStartupSafetyCheck alloc] init];
__weak typeof(self) weakSelf = self;
[self.safetyCheck runWithProgressHandler:^(NSString* status, CGFloat progress) {
[weakSelf updateProgress:progress status:status];
} completionHandler:^(SCSafetyCheckResult* result) {
weakSelf.checkInProgress = NO;
weakSelf.lastResult = result;
[weakSelf showResults:result];
// completionHandler called when user clicks OK
}];
}
- (void)updateProgress:(CGFloat)progress status:(NSString*)status {
dispatch_async(dispatch_get_main_queue(), ^{
self.progressIndicator.doubleValue = progress * 100;
self.statusLabel.stringValue = status;
});
}
- (void)showResults:(SCSafetyCheckResult*)result {
dispatch_async(dispatch_get_main_queue(), ^{
self.resultsView.hidden = NO;
self.okButton.hidden = NO;
if (result.passed) {
self.resultTitleLabel.stringValue = @"Safety Check Passed";
self.resultTitleLabel.textColor = [NSColor systemGreenColor];
self.statusLabel.stringValue = @"All blocking mechanisms verified!";
} else {
self.resultTitleLabel.stringValue = @"Safety Check Failed";
self.resultTitleLabel.textColor = [NSColor systemRedColor];
self.statusLabel.stringValue = @"Some mechanisms may not work.";
}
// Update result labels
BOOL results[] = {
result.hostsBlockWorked,
result.pfBlockWorked,
result.appBlockWorked,
result.hostsUnblockWorked,
result.pfUnblockWorked,
result.appUnblockWorked,
result.emergencyScriptWorked
};
NSArray* names = @[
@"Hosts file blocking",
@"Packet filter blocking",
@"App blocking (Calculator)",
@"Hosts file cleanup",
@"Packet filter cleanup",
@"App unblocking",
@"Emergency script (emergency.sh)"
];
for (NSUInteger i = 0; i < self.resultLabels.count && i < 7; i++) {
NSTextField* label = self.resultLabels[i];
BOOL passed = results[i];
if (passed) {
label.stringValue = [NSString stringWithFormat:@"\u2705 %@", names[i]];
label.textColor = [NSColor labelColor];
} else {
label.stringValue = [NSString stringWithFormat:@"\u274C %@", names[i]];
label.textColor = [NSColor systemRedColor];
}
}
self.progressIndicator.doubleValue = 100;
});
}
- (IBAction)okClicked:(id)sender {
[self.window close];
if (self.completionHandler && self.lastResult) {
self.completionHandler(self.lastResult);
}
}
- (void)cancelCheck {
if (self.checkInProgress) {
[self.safetyCheck cancel];
self.checkInProgress = NO;
}
}
@end