-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWhitePointModule.m
137 lines (103 loc) · 4.62 KB
/
WhitePointModule.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
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "WhitePointModule.h"
#import "MediaAccessibility.h"
#import "AccessibilityUtilities.h"
#import <ControlCenterUIKit/CCUIModuleSliderView.h>
#import <ControlCenterUI/CCUIModuleInstance.h>
#import <ControlCenterUI/CCUIModuleInstanceManager.h>
@interface CCUIModuleInstanceManager (CCSupport)
- (CCUIModuleInstance*)instanceForModuleIdentifier:(NSString*)moduleIdentifier;
@end
#define kWhitePointIntensityMin 0.25
#define kWhitePointIntensityMax 1.0
//Inside display settings, the intensity can only be changed between 25% and 100%
//Anything under 25% is simply not applied, therefore we have to translate the module percent (0% - 100%) to the actual percent (25%-100%) and back
//There are also constants inside ControlCenterUIKit: kMADisplayFilterPrefReduceWhitePointIntensity(Max/Min), but they both appear to be 0 for some reason so I made my own
CGFloat whitePointIntensityValueForModuleValue(CGFloat moduleValue)
{
return kWhitePointIntensityMin + moduleValue * (kWhitePointIntensityMax - kWhitePointIntensityMin);
}
CGFloat moduleValueForWhitePointIntensityValue(CGFloat whitePointIntensityValue)
{
return (whitePointIntensityValue - kWhitePointIntensityMin) / (kWhitePointIntensityMax - kWhitePointIntensityMin);
}
CGFloat reversedModuleValueForValue(CGFloat moduleValue)
{
return -moduleValue + 1.0;
}
@implementation WhitePointModule
- (instancetype)init
{
self = [super init];
_contentViewController = [[WhitePointModuleContentViewController alloc] init];
_contentViewController.module = self;
_backgroundViewController = [[WhitePointModuleBackgroundViewController alloc] init];
_backgroundViewController.module = self;
_ignoreUpdates = NO;
_preferences = [[NSUserDefaults alloc] initWithSuiteName:@"com.opa334.whitepointmodule"];
[self updatePrefs];
return self;
}
- (UIViewController*)contentViewController
{
return _contentViewController;
}
- (UIViewController*)backgroundViewController
{
return _backgroundViewController;
}
- (void)updatePrefs
{
_invertPercententageEnabled = [_preferences boolForKey:@"invertSliderEnabled"];
[self updateState];
}
- (void)updateState
{
if(!_ignoreUpdates)
{
BOOL currentState = [AXSettings sharedInstance].reduceWhitePointEnabled;
[_contentViewController setSelected:currentState];
CGFloat moduleValue = moduleValueForWhitePointIntensityValue(MADisplayFilterPrefGetReduceWhitePointIntensity());
if(_invertPercententageEnabled)
{
moduleValue = reversedModuleValueForValue(moduleValue);
}
_contentViewController.sliderView.value = moduleValue;
}
}
- (void)applyState
{
_ignoreUpdates = YES;
BOOL newState = [_contentViewController isSelected];
[AXSettings sharedInstance].reduceWhitePointEnabled = newState;
CGFloat moduleValue = _contentViewController.sliderView.value;
if(_invertPercententageEnabled)
{
moduleValue = reversedModuleValueForValue(moduleValue);
}
CGFloat newIntensity = whitePointIntensityValueForModuleValue(moduleValue);
MADisplayFilterPrefSetReduceWhitePointIntensity(newIntensity);
//RE: How did you find this function?
//I reversed the PreferenceBundle of the accessibility settings and saw that it was calling it (probably... I don't know for sure)
_ignoreUpdates = NO;
}
@end
void prefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
CCUIModuleInstance* wpModuleInstance = [[NSClassFromString(@"CCUIModuleInstanceManager") sharedInstance] instanceForModuleIdentifier:@"com.opa334.whitepointmodule"];
[(WhitePointModule*)wpModuleInstance.module updatePrefs];
}
void displayFilterSettingsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
CCUIModuleInstance* wpModuleInstance = [[NSClassFromString(@"CCUIModuleInstanceManager") sharedInstance] instanceForModuleIdentifier:@"com.opa334.whitepointmodule"];
[(WhitePointModule*)wpModuleInstance.module updateState];
}
__attribute__((constructor))
static void init(void)
{
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayFilterSettingsChanged, CFSTR("com.apple.mediaaccessibility.displayFilterSettingsChanged"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
//RE: How did you figure this key out?
//I hooked CFNotificationCenterPostNotification in Preferences.app with a log and toggled the option inside settings, this should work for any other option too
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, prefsChanged, CFSTR("com.opa334.whitepointmodule/ReloadPrefs"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
}