-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTBCSAppSwitcherViewController.x
More file actions
382 lines (295 loc) · 14.2 KB
/
Copy pathTBCSAppSwitcherViewController.x
File metadata and controls
382 lines (295 loc) · 14.2 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#import "TBCSAppSwitcherViewController.h"
#import "TBCSAppSwitcherCollectionViewCell.h"
#import "TBCSAppSwitcherManager.h"
#import "TBCSDisplayItem.h"
#import "TBCSPreferencesManager.h"
#import <Cephei/CompactConstraint.h>
#import <SpringBoard/SpringBoard.h>
#import <UIKit/UIImage+Private.h>
static NSString *const kTBCSAppSwitcherCollectionViewCellIdentifier = @"ChrysalisAppSwitcherCell";
@implementation TBCSAppSwitcherViewController {
NSArray <TBCSDisplayItem *> *_displayItems;
UIVisualEffectView *_blurEffectView;
UICollectionView *_collectionView;
UIView *_slidingIndicatorView;
UILabel *_noAppsLabel;
UIView *_closeButtonContainerView;
UIView *_dividerView;
UIImageView *_closeAppsImageView;
CAShapeLayer *_chevronPathMaskLayer;
CAShapeLayer *_rectanglePathMaskLayer;
CAGradientLayer *_gradientLayer;
NSLayoutConstraint *_closeButtonConstraint;
BOOL _showHomeScreenButton;
}
#pragma mark - UIViewController
- (void)loadView {
[super loadView];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:[TBCSPreferencesManager sharedInstance].blurEffectStyle];
_blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
_blurEffectView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_blurEffectView];
CALayer *containerLayer = [CALayer layer];
// chevron
UIBezierPath *chevronBezierPath = [UIBezierPath bezierPath];
[chevronBezierPath moveToPoint: CGPointMake(1.5, 45.68)];
[chevronBezierPath addCurveToPoint: CGPointMake(29.5, 6.68) controlPoint1: CGPointMake(1.5, 35.98) controlPoint2: CGPointMake(29.5, 38.68)];
[chevronBezierPath addCurveToPoint: CGPointMake(29.5, 87.68) controlPoint1: CGPointMake(29.5, -25.32) controlPoint2: CGPointMake(29.5, 121.98)];
[chevronBezierPath addCurveToPoint: CGPointMake(1.5, 45.68) controlPoint1: CGPointMake(29.5, 53.38) controlPoint2: CGPointMake(1.5, 55.38)];
[chevronBezierPath closePath];
chevronBezierPath.miterLimit = 4;
[chevronBezierPath applyTransform:CGAffineTransformMakeScale(0.5, 0.5)];
[chevronBezierPath fill];
_chevronPathMaskLayer = [[CAShapeLayer alloc] init];
_chevronPathMaskLayer.path = chevronBezierPath.CGPath;
[containerLayer addSublayer:_chevronPathMaskLayer];
// rectangle
_rectanglePathMaskLayer = [[CAShapeLayer alloc] init];
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(14, 0, self.view.frame.size.width-14, self.view.frame.size.height) byRoundingCorners:( UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(9.0, 9.0)];
[rectanglePath fill];
_rectanglePathMaskLayer.path = rectanglePath.CGPath;
[containerLayer addSublayer:_rectanglePathMaskLayer];
_blurEffectView.layer.mask = containerLayer;
// other views
_slidingIndicatorView = [[UIView alloc] init];
_slidingIndicatorView.frame = CGRectMake(17.5, 0.0, 65.0, 65.0);
_slidingIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
_slidingIndicatorView.center = CGPointMake(_slidingIndicatorView.center.x, self.view.center.y);
_slidingIndicatorView.alpha = 0.4;
_slidingIndicatorView.backgroundColor = [UIColor whiteColor];
_slidingIndicatorView.layer.masksToBounds = YES;
_slidingIndicatorView.layer.cornerRadius = 18.0;
[self.view addSubview:_slidingIndicatorView];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(70.0, 70.0);
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
_collectionView.translatesAutoresizingMaskIntoConstraints = NO;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.scrollEnabled = NO;
[_collectionView registerClass:[TBCSAppSwitcherCollectionViewCell class] forCellWithReuseIdentifier:kTBCSAppSwitcherCollectionViewCellIdentifier];
[self.view addSubview:_collectionView];
_gradientLayer = [[CAGradientLayer alloc] init];
_gradientLayer.startPoint = CGPointMake(0.0, 0.5);
_gradientLayer.endPoint = CGPointMake(1.0, 0.5);
_gradientLayer.colors = @[(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor];
_gradientLayer.locations = @[@0.93, @1.0];
_collectionView.layer.mask = _gradientLayer;
_noAppsLabel = [[UILabel alloc] init];
_noAppsLabel.translatesAutoresizingMaskIntoConstraints = NO;
_noAppsLabel.text = @"no apps";
_noAppsLabel.textAlignment = NSTextAlignmentCenter;
_noAppsLabel.alpha = 0.6;
_noAppsLabel.font = [UIFont systemFontOfSize:30.0];
[_collectionView addSubview:_noAppsLabel];
_closeButtonContainerView = [[UIView alloc] init];
_closeButtonContainerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_closeButtonContainerView];
_dividerView = [[UIView alloc] init];
_dividerView.translatesAutoresizingMaskIntoConstraints = NO;
_dividerView.alpha = 0.45;
_dividerView.backgroundColor = [UIColor whiteColor];
[_closeButtonContainerView addSubview:_dividerView];
_closeAppsImageView = [[UIImageView alloc] init];
_closeAppsImageView.translatesAutoresizingMaskIntoConstraints = NO;
_closeAppsImageView.image = [UIImage imageNamed:@"x" inBundle:[NSBundle bundleWithPath:@"/Library/PreferenceBundles/ChrysalisPrefs.bundle"]];
_closeAppsImageView.alpha = 0.45;
_closeAppsImageView.contentMode = UIViewContentModeCenter;
[_closeButtonContainerView addSubview:_closeAppsImageView];
// auto layout
NSDictionary <NSString *, UIView *> *views = @{
@"self": self.view,
@"blurEffectView": _blurEffectView,
@"collectionView": _collectionView,
@"dividerView": _dividerView,
@"closeAppsImageView": _closeAppsImageView,
@"closeButtonContainerView": _closeButtonContainerView,
@"noAppsLabel": _noAppsLabel
};
[self.view hb_addCompactConstraints:@[
@"blurEffectView.left = self.left",
@"blurEffectView.right = self.right",
@"blurEffectView.top = self.top",
@"blurEffectView.bottom = self.bottom",
@"collectionView.top = self.top",
@"collectionView.bottom = self.bottom",
@"noAppsLabel.left = collectionView.left",
@"noAppsLabel.right = collectionView.right",
@"noAppsLabel.top = collectionView.top",
@"noAppsLabel.bottom = collectionView.bottom",
@"closeButtonContainerView.top = self.top",
@"closeButtonContainerView.bottom = self.bottom",
] metrics:nil views:views];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[collectionView][closeButtonContainerView]|" options:kNilOptions metrics:nil views:views]];
[_closeButtonContainerView hb_addCompactConstraints:@[
@"dividerView.top = closeButtonContainerView.top",
@"dividerView.bottom = closeButtonContainerView.bottom",
@"closeAppsImageView.top = closeButtonContainerView.top",
@"closeAppsImageView.bottom = closeButtonContainerView.bottom",
] metrics:nil views:views];
[_closeButtonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[dividerView(==0.5)][closeAppsImageView(==45)]|" options:kNilOptions metrics:nil views:views]];
// prefs
[self configurePreferences];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(configurePreferences) name:HBPreferencesDidChangeNotification object:[TBCSPreferencesManager sharedInstance].preferences];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self _updateAppsInSwitcher];
[self configurePreferences];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
_gradientLayer.frame = self.view.bounds;
_chevronPathMaskLayer.frame = CGRectMake(0, self.view.frame.size.height/2-25, 14, 34);
_rectanglePathMaskLayer.frame = self.view.bounds;
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(14, 0, self.view.frame.size.width-14, self.view.frame.size.height) byRoundingCorners:( UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(9.0, 9.0)];
[rectanglePath fill];
_rectanglePathMaskLayer.path = rectanglePath.CGPath;
}
#pragma mark - Collection View Delegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TBCSAppSwitcherCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTBCSAppSwitcherCollectionViewCellIdentifier forIndexPath:indexPath];
cell.displayItem = _displayItems[indexPath.row];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_displayItems count];
}
#pragma mark - Update
- (void)_updateAppsInSwitcher {
[_displayItems release];
NSMutableArray <TBCSDisplayItem *> *displayItems = [NSMutableArray array];
if (_useDemoApps) {
// fill in standard example apps to display in demo situations
for (NSString *identifier in @[ @"com.apple.mobilemail", @"com.apple.AppStore", @"com.apple.Music", @"com.apple.mobilenotes", @"com.apple.mobileslideshow", @"com.apple.mobilesafari", @"com.apple.Preferences" ]) {
[displayItems addObject:[TBCSDisplayItem displayItemWithType:@"App" displayIdentifier:identifier]];
}
} else {
// get the list of apps
displayItems = [TBCSAppSwitcherManager displayItems];
}
if (_showHomeScreenButton && (_useDemoApps || ((SpringBoard *)[UIApplication sharedApplication])._accessibilityFrontMostApplication)) {
[displayItems insertObject:[TBCSDisplayItem homeScreenDisplayItem] atIndex:0];
}
_displayItems = [displayItems copy];
// reload the collection view
[_collectionView reloadData];
// do we have any apps open?
if (_displayItems.count > 0) {
// if so, we can hide the label, show the sliding indicator
_noAppsLabel.hidden = YES;
_slidingIndicatorView.hidden = NO;
} else {
// otherwise, we want the opposite
_noAppsLabel.hidden = NO;
_slidingIndicatorView.hidden = YES;
}
}
#pragma mark - Gesture
- (BOOL)_isPointOutOfBounds:(CGPoint)point {
UIView *superview = self.view.superview;
CGFloat top = superview.frame.origin.y;
CGFloat bottom = top + superview.frame.size.height;
return point.y < top - 150.f || point.y > bottom + 200.f;
}
- (NSUInteger)_indexForPoint:(CGPoint)point contentOffset:(CGPoint)contentOffset {
// from the point of the finger, get the index of the icon
// the last 45pt are reserved for the close apps button
// TODO: support contentOffset
if (point.x > self.view.frame.size.width - 45.0 && !_closeButtonContainerView.hidden) {
return NSUIntegerMax;
} else {
return roundf((point.x - 50.0) / 70.0);
}
}
- (void)updateViewToNewPoint:(CGPoint)point {
NSUInteger index = [self _indexForPoint:point contentOffset:CGPointZero];
CGRect potentialFrame = _slidingIndicatorView.frame;
if ([self _isPointOutOfBounds:point]) {
potentialFrame.origin.x += potentialFrame.size.width / 2;
potentialFrame.origin.y += potentialFrame.size.height / 2;
potentialFrame.size.width = 0;
potentialFrame.size.height = 0;
} else if (index == NSUIntegerMax) {
potentialFrame = CGRectMake(self.view.frame.size.width - 40, 0, 35, 35);
} else if (index < _displayItems.count) {
potentialFrame = CGRectMake((index * 70.0) + 17.5, 0, 65, 65);
}
[UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:15.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
_slidingIndicatorView.frame = potentialFrame;
_slidingIndicatorView.center = CGPointMake(_slidingIndicatorView.center.x, self.view.center.y);
} completion:nil];
}
- (void)openAppAtPoint:(CGPoint)point {
if ([self _isPointOutOfBounds:point]) {
return;
}
NSUInteger index = [self _indexForPoint:point contentOffset:CGPointZero];
if (index == NSUIntegerMax) {
[TBCSAppSwitcherManager quitAllApps];
for (UICollectionViewCell *cell in _collectionView.visibleCells) {
UIView *view = cell.contentView;
[UIView animateWithDuration:0.2 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:15.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
view.transform = CGAffineTransformMakeScale(0.15, 0.15);
view.alpha = 0.0;
} completion:^(BOOL completion) {
view.transform = CGAffineTransformIdentity;
view.alpha = 1.0;
}];
}
return;
} else if (index == 0 && _showHomeScreenButton) {
[TBCSAppSwitcherManager suspend];
} else if (_displayItems.count > index) {
TBCSDisplayItem *displayItem = _displayItems[index];
[(SpringBoard *)[UIApplication sharedApplication] launchApplicationWithIdentifier:displayItem.displayIdentifier suspended:NO];
}
}
#pragma mark - Preferences
- (void)configurePreferences {
TBCSPreferencesManager *preferences = [TBCSPreferencesManager sharedInstance];
_showHomeScreenButton = preferences.showHomeScreenButton;
_closeButtonContainerView.hidden = !preferences.showQuitAppsButton;
if (_closeButtonContainerView.hidden) {
_closeButtonConstraint = [NSLayoutConstraint hb_compactConstraint:@"self.width = 0" metrics:nil views:nil self:_closeButtonContainerView];
[_closeButtonContainerView addConstraint:_closeButtonConstraint];
} else if (_closeButtonConstraint) {
[_closeButtonContainerView removeConstraint:_closeButtonConstraint];
[_closeButtonConstraint release];
_closeButtonConstraint = nil;
}
[UIView animateWithDuration:1 animations:^{
_blurEffectView.effect = [UIBlurEffect effectWithStyle:[TBCSPreferencesManager sharedInstance].blurEffectStyle];
_dividerView.alpha = 0.0;
_closeAppsImageView.alpha = 0.0;
}];
UIColor *selectedColor = preferences.darkMode ? [UIColor whiteColor] : [UIColor blackColor];
_dividerView.backgroundColor = selectedColor;
_closeAppsImageView.image = [_closeAppsImageView.image _flatImageWithColor:selectedColor];
[UIView animateWithDuration:0.5 animations:^{
_dividerView.alpha = 0.45;
_closeAppsImageView.alpha = 0.45;
}];
[self _updateAppsInSwitcher];
}
#pragma mark - Memory management
- (void)dealloc {
[_displayItems release];
[_blurEffectView release];
[_collectionView release];
[_slidingIndicatorView release];
[_noAppsLabel release];
[_closeButtonContainerView release];
[_dividerView release];
[_closeAppsImageView release];
[_chevronPathMaskLayer release];
[_rectanglePathMaskLayer release];
[_gradientLayer release];
[_closeButtonConstraint release];
[super dealloc];
}
@end