-
Notifications
You must be signed in to change notification settings - Fork 2
/
ControlCenter.x
230 lines (180 loc) · 7.86 KB
/
ControlCenter.x
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
#import "NRESettings.h"
#import <Cephei/HBPreferences.h>
#import <ControlCenterUIKit/ControlCenterUIKit.h>
#import <MaterialKit/MaterialKit.h>
%group Modules
// These must be in here in order to not hook too early or late;
#pragma mark - Toggle
%hook CCUIDisplayBackgroundViewController
%property (retain, nonatomic) CCUILabeledRoundButtonViewController *noireButton;
%property (retain, nonatomic) NRESettings *settings;
- (void)viewDidLoad {
%orig;
// Register for settings changes
self.settings = NRESettings.sharedSettings;
[self.settings addObserver:self];
// Add our button
NSBundle *bundle = [NSBundle bundleWithPath:@"/Library/Application Support/com.shade.noire.bundle"];
CCUICAPackageDescription *packageDescription = [CCUICAPackageDescription descriptionForPackageNamed:@"StyleMode" inBundle:bundle];
self.noireButton = [[CCUILabeledRoundButtonViewController alloc] initWithGlyphPackageDescription:packageDescription highlightColor:[UIColor whiteColor] useLightStyle:NO];
self.noireButton.title = [bundle localizedStringForKey:@"APPEARANCE" value:@"" table:nil];
self.noireButton.labelsVisible = YES;
[self.noireButton.button addTarget:self action:@selector(_noireButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.noireButton.view];
}
- (void)dealloc {
// Unregister observer
[self.settings removeObserver:self];
%orig;
}
- (void)_updateState {
%orig;
// Set button state
self.noireButton.enabled = !self.settings.enabled;
NSString *state = self.settings.enabled ? @"dark" : @"light";
self.noireButton.glyphState = state;
NSString *subtitleKey = self.settings.enabled ? @"DARK" : @"LIGHT";
NSBundle *bundle = [NSBundle bundleWithPath:@"/Library/Application Support/com.shade.noire.bundle"];
self.noireButton.subtitle = [bundle localizedStringForKey:subtitleKey value:@"" table:nil];
}
- (void)viewWillLayoutSubviews {
%orig;
// Update size
self.noireButton.view.frame = self.nightShiftButton.view.bounds;
if (CCUIIsPortrait()) {
CGFloat centerY = self.nightShiftButton.view.center.y;
CGFloat sliderInset = (CGRectGetHeight(self.view.bounds) - CCUISliderExpandedContentModuleHeight()) / 4.0;
CGFloat buttonInset = CGRectGetWidth(self.nightShiftButton.view.bounds) / 2.0;
CGFloat width = CGRectGetWidth(self.view.bounds);
if (self.trueToneButton) {
// Layout for 3 toggles
CGFloat desiredInset = sliderInset - buttonInset + 3.0;
// Put ours on the left side
self.noireButton.view.center = CGPointMake(desiredInset, centerY);
// Realign truetone and night shift
self.nightShiftButton.view.center = CGPointMake(width / 2.0, centerY);
self.trueToneButton.view.center = CGPointMake((width - desiredInset), centerY);
return;
}
// Layout for 2 toggles
CGFloat desiredInset = sliderInset + 3.0;
self.noireButton.view.center = CGPointMake(desiredInset, centerY);
self.nightShiftButton.view.center = CGPointMake((width - desiredInset), centerY);
} else {
// Landscape
CGFloat centerX = self.nightShiftButton.view.center.x;
CGFloat centerY = CGRectGetHeight(self.view.bounds) / 2.0;
CGFloat buttonInset = CGRectGetHeight(self.nightShiftButton.view.bounds) / 2.0;
if (self.trueToneButton) {
// Layout for 3
self.nightShiftButton.view.center = CGPointMake(centerX, centerY);
CGFloat sliderInset = (CGRectGetHeight(self.view.bounds) - CCUISliderExpandedContentModuleHeight()) / 2.0;
self.trueToneButton.view.center = CGPointMake(centerX, sliderInset + buttonInset + 15.0);
self.noireButton.view.center = CGPointMake(centerX, CGRectGetHeight(self.view.bounds) - (sliderInset + buttonInset + 15.0));
return;
}
// Layout for 2
self.noireButton.view.center = CGPointMake(centerX, centerY - (buttonInset + 15.0));
self.nightShiftButton.view.center = CGPointMake(centerX, centerY + buttonInset + 15.0);
}
}
%new
- (void)_noireButtonPressed:(UIControl *)button {
// Toggle noire
[self _toggleNoire];
}
%new
- (void)_toggleNoire {
// Change settings
BOOL enabled = self.settings.enabled;
HBPreferences *preferences = [HBPreferences preferencesForIdentifier:@"com.shade.noire"];
[preferences setBool:!enabled forKey:NREPreferencesEnabledKey];
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.shade.noire/ReloadPrefs"), NULL, NULL, YES);
// Update state
[self _updateState];
}
%new
- (void)settings:(NRESettings *)settings changedValueForKeyPath:(NSString *)keyPath {
if (![keyPath isEqualToString:@"enabled"]) {
return;
}
[self _updateState];
}
%end
#pragma mark - Control Center theming
%hook CCUIConnectivityModuleViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = %orig;
if (self) {
// Register observer
NRESettings *settings = NRESettings.sharedSettings;
[settings addObserver:self];
}
return self;
}
- (void)dealloc {
// Unregister observer
NRESettings *settings = NRESettings.sharedSettings;
[settings removeObserver:self];
%orig;
}
- (void)viewDidLoad {
%orig;
// Check if enabled
NRESettings *settings = NRESettings.sharedSettings;
BOOL enabled = settings.enabled && settings.controlCenter;
if (!enabled) {
return;
}
// Theme buttons
for (CCUILabeledRoundButtonViewController *buttonViewController in self.buttonViewControllers) {
CCUILabeledRoundButton *buttonContainer = buttonViewController.buttonContainer;
CCUIRoundButton *buttonView = buttonContainer.buttonView;
MTMaterialView *alternateStateBackgroundView = buttonView.alternateSelectedStateBackgroundView;
if (!alternateStateBackgroundView) {
// Nothing to configure
continue;
}
CGFloat initialAlpha = alternateStateBackgroundView.alpha;
[alternateStateBackgroundView transitionToRecipe:MTMaterialRecipeNotificationsDark options:MTMaterialOptionsSecondaryOverlay weighting:alternateStateBackgroundView.weighting];
// Fix alpha
alternateStateBackgroundView.alpha = initialAlpha;
}
}
%new
- (void)settings:(NRESettings *)settings changedValueForKeyPath:(NSString *)keyPath {
if (![keyPath isEqualToString:@"enabled"] && ![keyPath isEqualToString:@"controlcenter"]) {
return;
}
// Update theme
BOOL enabled = settings.enabled && settings.controlCenter;
for (CCUILabeledRoundButtonViewController *buttonViewController in self.buttonViewControllers) {
CCUILabeledRoundButton *buttonContainer = buttonViewController.buttonContainer;
CCUIRoundButton *buttonView = buttonContainer.buttonView;
MTMaterialView *alternateStateBackgroundView = buttonView.alternateSelectedStateBackgroundView;
if (!alternateStateBackgroundView) {
// Nothing to configure
continue;
}
CGFloat initialAlpha = alternateStateBackgroundView.alpha;
MTMaterialRecipe recipe = enabled ? MTMaterialRecipeNotificationsDark : MTMaterialRecipeControlCenterModules;
MTMaterialOptions options = enabled ? MTMaterialOptionsSecondaryOverlay : MTMaterialOptionsPrimaryOverlay;
[alternateStateBackgroundView transitionToRecipe:recipe options:options weighting:alternateStateBackgroundView.weighting];
// Fix alpha
alternateStateBackgroundView.alpha = initialAlpha;
}
}
%end
%end
#pragma mark - Initializer
%hook CCUIModuleInstanceManager
- (void)_updateModuleInstances {
%orig;
// Load hooks when modules are loaded
%init(Modules);
}
%end
%ctor {
// Init inital hook
%init;
}