-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTweak.xm
237 lines (190 loc) · 10.9 KB
/
Tweak.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
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
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <rootless.h>
#define PLIST_PATH ROOT_PATH_NS(@"/var/mobile/Library/Preferences/com.gilshahar7.volumepercentprefs.plist")
@interface _UILegibilityLabel
@property (nonatomic, retain) NSString *string;
@end
@interface SBElasticSliderView : UIView
@end
@interface SBElasticVolumeViewController : UIViewController
@property (nonatomic, retain) UILabel *percentLabel;
@property (nonatomic, retain) NSLayoutConstraint *centerConstraint;
@property (nonatomic, retain) NSArray<NSLayoutConstraint *> *outsideConstraint;
@property (nonatomic, retain) NSArray<NSLayoutConstraint *> *topConstraint;
@property (nonatomic,readonly) SBElasticSliderView * sliderView;
@property BOOL enlarged;
@property BOOL orientationFlippedToLandscape;
@property int axis;
-(float)currentVolume;
-(void)layoutVolumePercent;
@end
int fontSizeMinimized = 10;
int fontSizeEnlarged = 15;
float horizontalPaddingMinimized = 0.0;
float verticalPaddingMinimized = -20.0;
float verticalPaddingEnlarged = 10.0;
static void loadPrefs() {
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:PLIST_PATH];
fontSizeMinimized = (int)[[prefs objectForKey:@"fontSizeMinimized"]?:@(10) intValue];
fontSizeEnlarged = (int)[[prefs objectForKey:@"fontSizeEnlarged"]?:@(15) intValue];
horizontalPaddingMinimized = (int)[[prefs objectForKey:@"horizontalPaddingMinimized"]?:@(0) intValue];
verticalPaddingMinimized = (int)[[prefs objectForKey:@"verticalPaddingMinimized"]?:@(-20) intValue];
verticalPaddingEnlarged = (int)[[prefs objectForKey:@"verticalPaddingEnlarged"]?:@(10) intValue];
}
static UIColor *colorWithHexString(NSString *hex) {
NSString *reductedString = [hex stringByReplacingOccurrencesOfString:@"#" withString:@""];
if ([reductedString length] == 3) {
reductedString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
[reductedString substringWithRange:NSMakeRange(0, 1)],[reductedString substringWithRange:NSMakeRange(0, 1)],
[reductedString substringWithRange:NSMakeRange(1, 1)],[reductedString substringWithRange:NSMakeRange(1, 1)],
[reductedString substringWithRange:NSMakeRange(2, 1)],[reductedString substringWithRange:NSMakeRange(2, 1)]];
}
if ([reductedString length] == 6) {
reductedString = [reductedString stringByAppendingString:@"ff"];
}
unsigned int baseValue;
[[NSScanner scannerWithString:reductedString] scanHexInt:&baseValue];
float red = ((baseValue >> 24) & 0xFF)/255.0f;
float green = ((baseValue >> 16) & 0xFF)/255.0f;
float blue = ((baseValue >> 8) & 0xFF)/255.0f;
float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
%hook SBElasticVolumeViewController
%property (assign) BOOL enlarged;
%property (assign) BOOL orientationFlippedToLandscape;
%property (nonatomic, retain) UILabel *percentLabel;
%property (nonatomic, retain) NSLayoutConstraint *centerConstraint;
%property (nonatomic, retain) NSArray *outsideConstraint;
%property (nonatomic, retain) NSArray *topConstraint;
-(void)viewDidLoad{
%orig;
self.enlarged = true;
self.orientationFlippedToLandscape = false;
if(!self.percentLabel){
UILabel *percentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 47, 20)];
float currentVolume = [self currentVolume];
currentVolume = currentVolume*100;
NSString *percentstr = [NSString stringWithFormat:@"%.0f%%", currentVolume];
[percentLabel setText:percentstr];//Set text in label.
[percentLabel setFont:[UIFont systemFontOfSize:fontSizeEnlarged]];
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:PLIST_PATH];
[percentLabel setTextColor:colorWithHexString([prefs objectForKey:@"enlargedColor"])];
[percentLabel setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
[percentLabel setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];//Set line adjustment.
percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.sliderView addSubview:percentLabel];
// center percentLabel horizontally in self.sliderView
self.centerConstraint = [NSLayoutConstraint constraintWithItem:percentLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.sliderView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self.sliderView addConstraint:self.centerConstraint];
// align percentLabel from the top
NSNumber *verticalPaddingEnlarged2 = [NSNumber numberWithFloat:verticalPaddingEnlarged];
NSDictionary *metricsVPE = [NSDictionary dictionaryWithObjectsAndKeys:verticalPaddingEnlarged2, @"verticalPaddingEnlarged", nil];
self.topConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-verticalPaddingEnlarged-[percentLabel]" options:0 metrics:metricsVPE views:NSDictionaryOfVariableBindings(percentLabel)];
[self.sliderView addConstraints:self.topConstraint];
// height constraint
NSArray *heightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[percentLabel(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(percentLabel)];
[self.sliderView addConstraints:heightConstraint];
self.percentLabel = percentLabel;
}
}
-(void)updateValue:(float)arg1{
%orig;
if(self.percentLabel){
arg1 = arg1*100;
NSString *percentstr = [NSString stringWithFormat:@"%.0f%%", arg1];
[self.percentLabel setText:percentstr];
}
}
-(void)transitionToState:(long long)state animated:(BOOL)arg2 completion:(/*^block*/id)arg3{
%orig;
[self layoutVolumePercent];
}
-(void)_updateForAxisChange:(int)arg1{
%orig;
[self layoutVolumePercent];
}
%new
-(void)layoutVolumePercent{
NSInteger state = MSHookIvar<NSInteger>(self, "_state");
UILabel *percentLabel = self.percentLabel;
if(percentLabel){
if (self.axis == 1){
//landscape
if(self.orientationFlippedToLandscape == false){
if(self.centerConstraint == nil){
self.centerConstraint = [NSLayoutConstraint constraintWithItem:percentLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.sliderView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self.sliderView addConstraint:self.centerConstraint];
}
[self.sliderView removeConstraints:self.outsideConstraint];
self.outsideConstraint = nil;
[self.sliderView removeConstraints:self.topConstraint];
self.topConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[percentLabel]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(percentLabel)];
[self.sliderView addConstraints:self.topConstraint];
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:PLIST_PATH];
[percentLabel setTextColor:colorWithHexString([prefs objectForKey:@"minimizedColor"])];
[percentLabel setFont:[UIFont systemFontOfSize:fontSizeMinimized]];
[self.view layoutIfNeeded];
[self.percentLabel sizeToFit];
self.orientationFlippedToLandscape = true;
}
}else if(self.axis == 2){
//portrait
if(state == 2){ //minimized
if(self.enlarged == true || self.orientationFlippedToLandscape == true){
self.enlarged = false;
if(self.outsideConstraint == nil){
NSNumber *horizontalPaddingMinimized2 = [NSNumber numberWithFloat:horizontalPaddingMinimized];
NSDictionary *metricsHPM = [NSDictionary dictionaryWithObjectsAndKeys:horizontalPaddingMinimized2, @"horizontalPaddingMinimized", nil];
self.outsideConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-horizontalPaddingMinimized-[percentLabel]" options:NSLayoutFormatDirectionLeftToRight metrics:metricsHPM views:NSDictionaryOfVariableBindings(percentLabel)];
[self.sliderView addConstraints:self.outsideConstraint];
}
[self.sliderView removeConstraints:self.topConstraint];
NSNumber *verticalPaddingMinimized2 = [NSNumber numberWithFloat:verticalPaddingMinimized];
NSDictionary *metricsVPM = [NSDictionary dictionaryWithObjectsAndKeys:verticalPaddingMinimized2, @"verticalPaddingMinimized", nil];
self.topConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-verticalPaddingMinimized-[percentLabel]" options:0 metrics:metricsVPM views:NSDictionaryOfVariableBindings(percentLabel)];
[self.sliderView addConstraints:self.topConstraint];
self.orientationFlippedToLandscape = false;
[self.sliderView removeConstraint:self.centerConstraint];
self.centerConstraint = nil;
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:PLIST_PATH];
[percentLabel setTextColor:colorWithHexString([prefs objectForKey:@"minimizedColor"])];
[UIView animateWithDuration:0.2 animations:^{
[percentLabel setFont:[UIFont systemFontOfSize:fontSizeMinimized]];
[self.view layoutIfNeeded];
[self.percentLabel sizeToFit];
}];
}
}else if(state == 3 || state == 1){
if(self.enlarged == false || self.orientationFlippedToLandscape == true){
self.enlarged = true;
if(self.centerConstraint == nil){
self.centerConstraint = [NSLayoutConstraint constraintWithItem:percentLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.sliderView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self.sliderView addConstraint:self.centerConstraint];
}
[self.sliderView removeConstraints:self.outsideConstraint];
self.outsideConstraint = nil;
[self.sliderView removeConstraints:self.topConstraint];
NSNumber *verticalPaddingEnlarged2 = [NSNumber numberWithFloat:verticalPaddingEnlarged];
NSDictionary *metricsVPE = [NSDictionary dictionaryWithObjectsAndKeys:verticalPaddingEnlarged2, @"verticalPaddingEnlarged", nil];
self.topConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-verticalPaddingEnlarged-[percentLabel]" options:0 metrics:metricsVPE views:NSDictionaryOfVariableBindings(percentLabel)];
[self.sliderView addConstraints:self.topConstraint];
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:PLIST_PATH];
[percentLabel setTextColor:colorWithHexString([prefs objectForKey:@"enlargedColor"])];
self.orientationFlippedToLandscape = false;
[UIView animateWithDuration:0.2 animations:^{
[percentLabel setFont:[UIFont systemFontOfSize:fontSizeEnlarged]];
[self.view layoutIfNeeded];
[self.percentLabel sizeToFit];
}];
}
}
}
}
}
%end
%ctor{
loadPrefs();
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.gilshahar7.volumepercentprefs.settingschanged"), NULL, CFNotificationSuspensionBehaviorCoalesce);
}