-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathABSManager.xm
189 lines (157 loc) · 6.38 KB
/
ABSManager.xm
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
#import "ABSManager.h"
#import "ReduceWhitePointLevel.h"
@interface UIDevice (Category)
@property float _backlightLevel;
@end
// used in iOS 14+
@interface SBDisplayBrightnessController : NSObject
-(void)setBrightnessLevel:(float)arg1;
-(void)setBrightnessLevel:(float)arg1 animated:(BOOL)animated; // iOS 15+
@end
// used in iOS 13
@interface SBBrightnessController
+(id)sharedBrightnessController;
-(void)setBrightnessLevel:(float)arg1;
@end
@interface AXSettings
+(id)sharedInstance;
-(BOOL)reduceWhitePointEnabled;
-(void)setReduceWhitePointEnabled:(BOOL)arg1;
// -(void)setReduceWhitePointLevel:(float)arg1; this method does not work, see below for an alternative
@end
NSArray<NSString*> *glyphStates = @[@"min", @"mid", @"full", @"max"];
@implementation ABSManager {
float _halfDistance;
int _glyphState;
CCUIContinuousSliderView* _nativeSliderView;
SCDisplaySliderModuleViewController* _bigSurSliderController;
Boolean _autoBrightnessShouldBeEnabled;
SBDisplayBrightnessController* _brightnessController;
}
+(ABSManager*)shared {
static ABSManager *sharedABSManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedABSManager = [self alloc];
});
return sharedABSManager;
}
+(float)clampZeroOne:(float)value {
if (value > 1) return 1.0f;
else if (value < 0) return 0.0f;
else return value;
}
-(void)initWithAutoBrightnessEnabled:(BOOL)modifyAutoBrightness andIosVersion:(int)iosVersion andThreshold:(float)threshold {
_modifyAutoBrightness = modifyAutoBrightness;
_iosVersion = iosVersion;
_threshold = threshold;
_distance = 1 - threshold;
_halfDistance = (1-threshold) / 2 + threshold;
if (iosVersion >= 14) _brightnessController = [%c(SBDisplayBrightnessController) new];
[self reCalculateCurrentSliderLevel];
[self calculateGlyphState];
}
-(void)reCalculateCurrentSliderLevel {
if ([self whitePointEnabled]) {
_currentSliderLevel = _threshold - (([self whitePointLevel] - 0.25) / 0.75) * _threshold;
} else {
_currentSliderLevel = [self brightness] * _distance + _threshold;
}
}
-(void)setThreshold:(float)threshold {
_threshold = threshold;
_distance = 1-threshold;
_halfDistance = (1-threshold) / 2 + threshold;
[self reCalculateCurrentSliderLevel];
[_nativeSliderView setValue:-_currentSliderLevel];
if (_bigSurSliderController != nil) [_bigSurSliderController updateSliderValue];
}
-(void)setBrightness:(float)amount {
if (_iosVersion >= 15) [_brightnessController setBrightnessLevel:amount animated:NO];
else if (_iosVersion >= 14) [_brightnessController setBrightnessLevel:amount];
else [[%c(SBBrightnessController) sharedBrightnessController] setBrightnessLevel:amount];
}
-(float)brightness {
return [[%c(UIDevice) currentDevice] _backlightLevel];
}
-(BOOL)whitePointEnabled {
_whitePointShouldBeEnabled = [[AXSettings sharedInstance] reduceWhitePointEnabled];
return _whitePointShouldBeEnabled;
}
-(void)setWhitePointEnabled:(BOOL)enabled {
if (enabled && _whitePointShouldBeEnabled) return;
if (!enabled && !_whitePointShouldBeEnabled) return;
if (enabled) [self setBrightness:0.0f];
[[AXSettings sharedInstance] setReduceWhitePointEnabled: enabled];
_whitePointShouldBeEnabled = enabled;
}
-(void)setWhitePointLevel:(float)amount {
// credits: https://github.com/opa334/WhitePointModule/blob/944087e67d1bed03282240e16d7e78294f59e865/WhitePointModule.m#L105
MADisplayFilterPrefSetReduceWhitePointIntensity(amount);
}
-(float)whitePointLevel {
// credits: https://github.com/opa334/WhitePointModule/blob/944087e67d1bed03282240e16d7e78294f59e865/WhitePointModule.m#L79
return MADisplayFilterPrefGetReduceWhitePointIntensity();
}
-(void)setAutoBrightnessEnabled:(BOOL)enabled {
if (!_modifyAutoBrightness) return;
if (enabled && _autoBrightnessShouldBeEnabled) return;
if (!enabled && !_autoBrightnessShouldBeEnabled) return;
// credits: https://github.com/a3tweaks/Flipswitch/blob/c1fe70e25d843c1c55b1df954b256ca5850910af/Switches/AutoBrightness.x#L28-L30
CFPreferencesSetAppValue(kABSAutoBrightnessKey, enabled ? kCFBooleanTrue : kCFBooleanFalse, kABSBackboard);
CFPreferencesAppSynchronize(kABSBackboard);
_autoBrightnessShouldBeEnabled = enabled;
}
-(void)calculateGlyphState {
// possible glyphState values: 0: min, 1: mid, 2: full, 3: max
if (_currentSliderLevel < _threshold) {
_glyphState = 0;
} else {
if (_currentSliderLevel == 1.0f) {
_glyphState = 3;
} else if (_currentSliderLevel > _halfDistance) {
_glyphState = 2;
} else {
_glyphState = 1;
}
}
}
-(NSString*)glyphState {
return glyphStates[_glyphState];
}
-(BOOL)moveWithGestureRecognizer:(UIPanGestureRecognizer*)recognizer withOldSliderLevel:(float)oldSliderLevel withView:(UIView*)view withYDirection:(BOOL)isY {
CGPoint translationPoint = [recognizer translationInView:view];
float translation = (float) isY ? translationPoint.y / [view frame].size.height : -translationPoint.x / [view frame].size.width;
_currentSliderLevel = [ABSManager clampZeroOne:oldSliderLevel-translation];
[self calculateGlyphState];
if (_currentSliderLevel >= _threshold) { // brightness
float upperSectionSliderLevel = _currentSliderLevel - _threshold; // 0.7..0
float newBrightnessLevel = upperSectionSliderLevel / _distance; // 1..0
[self setWhitePointEnabled:NO];
[self setBrightness:newBrightnessLevel];
[self setAutoBrightnessEnabled:YES];
[_nativeSliderView setValue:-_currentSliderLevel];
return YES;
} else { // whitepoint
float lowerSectionSliderLevel = _currentSliderLevel; // 0..0.3
float newWhitePointLevel = lowerSectionSliderLevel / _threshold; // 0..1
float newAdjustedWhitePointLevel = 1 - (newWhitePointLevel * 0.75f); // 1..0.25
[self setWhitePointEnabled:YES];
[self setWhitePointLevel:newAdjustedWhitePointLevel];
[self setAutoBrightnessEnabled:NO];
[_nativeSliderView setValue:-_currentSliderLevel];
if (_bigSurSliderController != nil) [_bigSurSliderController updateSliderValue];
return NO;
}
}
-(void)updateCurrentSliderLevelWithSystemBrightness:(float)brightnessLevel {
// brightnessLevel 0..1 system brightness
_currentSliderLevel = brightnessLevel * _distance + _threshold; // 1..0.3
}
-(void)setNativeSliderView:(CCUIContinuousSliderView*)view {
if (_nativeSliderView == nil) _nativeSliderView = view;
}
-(void)setBigSurSliderController:(SCDisplaySliderModuleViewController*)controller {
if (_bigSurSliderController == nil) _bigSurSliderController = controller;
}
@end