forked from irlabs/TransparentWebView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferenceController.m
executable file
·184 lines (149 loc) · 5.78 KB
/
PreferenceController.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
//
// PreferenceController.m
// TransparentWebView
//
// Created by Dirk van Oosterbosch on 24-02-11.
// Copyright 2011 IR labs. All rights reserved.
//
#import "PreferenceController.h"
NSString *const TWVShouldAutomaticReloadKey = @"shouldAutomaticRelaod";
NSString *const TWVAutomaticReloadIntervalKey = @"automaticReloadInterval";
NSString *const TWVAutomaticReloadChangedNotification = @"TWVautomaticReloadChanged";
NSString *const TWVOpacityKey = @"opacity";
NSString *const TWVOpacityChangedNotification = @"TWVopacityChanged";
@implementation PreferenceController
- (id)init {
if (![super initWithWindowNibName:@"Preferences"]) {
return nil;
}
lastSliderValue = 0.0f;
sliderDeltas = [[NSMutableArray alloc] init]; // Measure the last 5 deltas
return self;
}
- (BOOL)shouldAutomaticReload {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:TWVShouldAutomaticReloadKey];
}
- (int)automaticReloadInterval {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults integerForKey:TWVAutomaticReloadIntervalKey];
}
- (double)opacity {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults doubleForKey:TWVOpacityKey];
}
- (void)windowDidLoad {
BOOL shouldReloadState = [self shouldAutomaticReload];
[autoRefreshCheckBox setState:shouldReloadState];
[self setAutomaticReloadEnabled:shouldReloadState];
int intervalSeconds = [self automaticReloadInterval];
lastSliderValue = powf(intervalSeconds / (180.0 * 60.0), (1 / 2.0));
[autoRefreshIntervalSlider setFloatValue:lastSliderValue];
[self setAutomaticIntervalLabelValue:intervalSeconds];
[self setOpacitySliderValue:self.opacity];
[self setOpacityLabelValue:self.opacity];
}
- (void)setAutomaticReloadEnabled:(BOOL)enabledState {
if (enabledState) {
// On
[autoRefreshIntervalSlider setEnabled:YES];
[autoRefreshIntervalValueLabel setAlphaValue:1.0f];
[autoRefreshInfoLabel setAlphaValue:1.0f];
[autoRefreshRightMark setAlphaValue:1.0f];
[autoRefreshMiddleMark setAlphaValue:1.0f];
[autoRefreshLeftMark setAlphaValue:1.0f];
} else {
// Off
[autoRefreshIntervalSlider setEnabled:NO];
[autoRefreshIntervalValueLabel setAlphaValue:0.4f];
[autoRefreshInfoLabel setAlphaValue:0.4f];
[autoRefreshRightMark setAlphaValue:0.4f];
[autoRefreshMiddleMark setAlphaValue:0.4f];
[autoRefreshLeftMark setAlphaValue:0.4f];
}
}
- (void)setAutomaticIntervalLabelValue:(int)secondsValue {
int minutes = secondsValue / 60;
int seconds = secondsValue - (minutes * 60);
[autoRefreshIntervalValueLabel setStringValue:[NSString stringWithFormat:@"%d'%02d\" minutes", minutes, seconds]];
}
- (IBAction)changeAutomaticRefresh:(id)sender {
int state = [autoRefreshCheckBox state];
[self setAutomaticReloadEnabled:state];
// Set the value in the Defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:state forKey:TWVShouldAutomaticReloadKey];
// Send a notification
[[NSNotificationCenter defaultCenter] postNotificationName:TWVAutomaticReloadChangedNotification object:self];
}
- (IBAction)changeAutoRefreshValue:(id)sender {
float sliderValue = [autoRefreshIntervalSlider floatValue];
float exponentialSeconds = (powf(sliderValue, 2.0)) * (180.0 * 60.0);
float deltaSlider = fabsf(sliderValue - lastSliderValue);
// Remembering the last 5 deltas
float formerDeltaSlider = deltaSlider;
if ([sliderDeltas count] > 0) {
formerDeltaSlider = [[sliderDeltas lastObject] floatValue];
}
//NSLog(@"deltaSlider: %f formerDeltaSlider: %f %d deltas", deltaSlider, formerDeltaSlider, [sliderDeltas count]);
if ([sliderDeltas count] > 6) {
[sliderDeltas removeLastObject];
}
[sliderDeltas insertObject:[NSNumber numberWithFloat:deltaSlider] atIndex:0];
int totalSeconds;
if (formerDeltaSlider > 0.01) {
// Round on 10 minutes
totalSeconds = (roundf(exponentialSeconds / 600.0)) * 600;
} else if (formerDeltaSlider > 0.002) {
// Round on minutes
totalSeconds = (roundf(exponentialSeconds / 60.0)) * 60;
// } else if (formerDeltaSlider > 0.0004) {
} else {
// Round on 5 seconds
totalSeconds = (roundf(exponentialSeconds / 5.0)) * 5;
// // Round on single seconds
// totalSeconds = roundf(exponentialSeconds);
}
[self setAutomaticIntervalLabelValue:totalSeconds];
lastSliderValue = sliderValue;
// Set the value in the Defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:totalSeconds] forKey:TWVAutomaticReloadIntervalKey];
// Send a notification
[[NSNotificationCenter defaultCenter] postNotificationName:TWVAutomaticReloadChangedNotification object:self];
}
#pragma mark - Opacity
- (void)changeOpacityValue:(id)sender
{
double opacity = [opacitySlider doubleValue] / 100.0;
if (fabs(opacity - self.opacity) > 0.1) {
[self setOpacityLabelValue:opacity];
[self setOpacityPreference:opacity];
}
}
- (void)setOpacityValue:(double)opacity setPreference:(BOOL)setPreference {
[self setOpacitySliderValue:opacity];
[self setOpacityLabelValue:opacity];
if (setPreference) {
[self setOpacityPreference:opacity];
}
}
- (void)setOpacityLabelValue:(double)opacity {
[opacityValueLabel setStringValue:[NSString stringWithFormat:@"%.f%%", round(opacity * 100.0), nil]];
}
- (void)setOpacitySliderValue:(double)opacity {
[opacitySlider setDoubleValue:opacity * 100.0];
}
- (void)setOpacityPreference:(double)opacity {
// Set the value in the Defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithDouble:opacity] forKey:TWVOpacityKey];
[defaults synchronize];
// Send a notification
[[NSNotificationCenter defaultCenter] postNotificationName:TWVOpacityChangedNotification object:self];
}
- (void)dealloc {
[sliderDeltas release];
[super dealloc];
}
@end