-
Notifications
You must be signed in to change notification settings - Fork 4
/
MultiSwitcher.x
268 lines (202 loc) · 8.1 KB
/
MultiSwitcher.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#import "Headers.h"
#import <SpringBoard/VolumeControl.h>
#pragma mark external methods
extern void BKSDisplayBrightnessSet(float value);
extern float BKSDisplayBrightnessGetCurrent();
#pragma mark interface declaration
@interface DualSliderContainerView : UIView
@property (copy) UISlider *slider;
@end
typedef enum : int {
ModeBrightness,
ModeVolumn
} SlideMode;
@interface MultiCenter : UIView <UICollectionViewDelegate, UICollectionViewDataSource> {
VolumeControl *volumeControl;
UIView *_sliderContainer;
}
@property (assign) SlideMode mode;
@property UISlider *slider;
@property (nonatomic, readonly) UICollectionView *collectionView;
@end
@interface UIToggle : UIButton
@property BOOL toggled;
@property UICollectionViewCell *cell;
@property (nonatomic, copy) void (^action)(UIToggle *toggle);
@end
@interface MCCollectionViewLayout : UICollectionViewFlowLayout
@end
#pragma mark global variable
static NSArray *toggles;
static MultiCenter *center;
#pragma mark hooks
%hook SBDeckSwitcherViewController
- (void)viewDidLoad {
center = [MultiCenter new];
center.userInteractionEnabled = YES;
%orig;
}
- (void)viewDidLayoutSubviews {
%orig;
if ([KazeSwitcherController() isVisible]) {
[self.view addSubview:center];
[self.view bringSubviewToFront:center];
} else {
[center removeFromSuperview];
}
}
%end
%hook CCUIConnectivityModuleViewController
- (void)viewDidLayoutSubviews {
%orig;
if ([KazeSwitcherController() isVisible]) {
[self.view addSubview:center];
[self.view bringSubviewToFront:center];
} else {
[center removeFromSuperview];
}
}
%end
#pragma mark implementation for interfaces
@implementation MultiCenter
static NSString * const reuseIdentifier = @"Cell";
static CGFloat collectionViewHeight = 75;
- (MultiCenter *)init {
CGFloat height = kScreenHeight / 4;
self = [super initWithFrame:CGRectMake(0, height * 3, kScreenWidth, height)];
if (self) {
self.mode = ModeVolumn;
self.backgroundColor = UIColor.lightGrayColor;
MCCollectionViewLayout *layout = [MCCollectionViewLayout new];
_collectionView = [UICollectionView.alloc initWithFrame:CGRectMake(0, 0, kScreenWidth, collectionViewHeight) collectionViewLayout:layout];
_collectionView.backgroundColor = UIColor.whiteColor;
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self addSubview:_collectionView];
[self setupSlider:(CGSize){kScreenWidth, height - collectionViewHeight}];
volumeControl = [%c(VolumeControl) sharedVolumeControl];
}
return self;
}
- (void)setupSlider:(CGSize)size {
CGFloat sliderHeight = size.height;
_sliderContainer = [UIView.alloc initWithFrame:CGRectMake(0, collectionViewHeight, kScreenWidth, sliderHeight)];
CGFloat sliderX = 75;
self.slider = [UISlider.alloc initWithFrame:CGRectMake(sliderX, 0, kScreenWidth - 150, sliderHeight)];
[self.slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
[self.slider setMinimumTrackTintColor:[UIColor whiteColor]];
[_sliderContainer addSubview:_slider];
CGFloat buttonWidth = 18;
CGFloat buttonX = (sliderX - buttonWidth) / 2;
CGFloat yOffset = (sliderHeight - buttonWidth) / 2;
UIToggle *brightness = [self getButtonWith:@"LessBright" frame:CGRectMake(buttonX, yOffset, buttonWidth, buttonWidth) action:@selector(changeMode:)];
[_sliderContainer addSubview:brightness];
UIToggle *volume = [self getButtonWith:@"speaker" frame:CGRectMake(size.width - buttonX - buttonWidth, yOffset, buttonWidth, buttonWidth) action:@selector(changeMode:)];
[_sliderContainer addSubview:volume];
[self addSubview:_sliderContainer];
[self changeMode:brightness];
}
- (UIColor *)selectedBlueColor {
return [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
}
- (UIColor *)lightGrayColor {
return [UIColor colorWithRed:211.0/255.0 green:211.0/255.0 blue:211.0/255.0 alpha:0.7];
}
- (UIToggle *)getButtonWith:(NSString *)imgName frame:(CGRect)frame action:(SEL)selector {
UIToggle *button = [UIToggle buttonWithType:UIButtonTypeCustom];
button.frame = frame;
UIImage *image = [KazeImage(imgName) imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:image forState:UIControlStateNormal];
button.tintColor = UIColor.blackColor;
[button addTarget:center action:selector forControlEvents:UIControlEventTouchUpInside];
return button;
}
# pragma mark action
- (void)sliderChanged:(UISlider *)sender {
// NSLog(@"%@ %@", NSStringFromSelector(_cmd), sender);
CGFloat value = sender.value;
if (self.mode == ModeBrightness) {
BKSDisplayBrightnessSet(value);
} else if (self.mode == ModeVolumn) {
[volumeControl setMediaVolume:value];
[volumeControl hideVolumeHUDIfVisible];
}
}
- (void)changeMode:(UIToggle *)modeToggle {
if (self.mode == ModeBrightness) {
self.mode = ModeVolumn;
self.slider.value = [volumeControl volume];
} else {
self.mode = ModeBrightness;
self.slider.value = BKSDisplayBrightnessGetCurrent();
}
}
- (void)colorIfChosen:(UIToggle *)sender {
sender.toggled = !sender.toggled;
if (sender.toggled) {
sender.cell.contentView.backgroundColor = [self selectedBlueColor];
sender.tintColor = UIColor.whiteColor;
} else {
sender.cell.contentView.backgroundColor = [self lightGrayColor];
sender.tintColor = UIColor.blackColor;
}
if (sender.action)
sender.action(sender);
}
# pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSLog(@"%@", NSStringFromSelector(_cmd));
NSString *imgName = toggles[indexPath.row];
// UIImageView *imgView = [UIImageView.alloc initWithImage:KazeImage(imgName)];
UIToggle *toggle = [self getButtonWith:imgName frame:CGRectMake(9, 9, 30, 30) action:@selector(colorIfChosen:)];
toggle.cell = cell;
[cell.contentView addSubview:toggle];
cell.contentView.backgroundColor = [self lightGrayColor];
cell.contentView.layer.cornerRadius = 24;
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(48, 48);
}
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
@end
@implementation DualSliderContainerView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
return self;
}
@end
@implementation UIToggle
@end
@implementation MCCollectionViewLayout
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
MultiCenter* controller = (MultiCenter *)self.collectionView.delegate;
NSMutableArray *attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
CGSize size = [controller collectionView:self.collectionView layout:self sizeForItemAtIndexPath:[NSIndexPath indexPathWithIndex:0]];
CGFloat xOffset = (kScreenWidth - 17.5 * 4 - size.width * 5 ) / 2;
CGFloat yOffset = (CGRectGetHeight(self.collectionView.frame) - size.height ) / 2;
for (int i = 0; attributes.count > i; i++) {
UICollectionViewLayoutAttributes* curAttributes = attributes[i];
curAttributes.frame = CGRectOffset(curAttributes.frame, xOffset, yOffset);
}
return attributes;
}
@end
%ctor {
// controlBlueColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
// lightGrayColor = [UIColor colorWithRed:211.0f/255.0f green:211.0f/255.0f blue:211.0f/255.0f alpha:0.8f];
toggles = @[@"Airplane", @"Wifi", @"CCBluetooth", @"DND", @"locked"];
}