-
Notifications
You must be signed in to change notification settings - Fork 4
/
STKGroupController.m
751 lines (677 loc) · 28.4 KB
/
STKGroupController.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#import "STKGroupController.h"
#import "STKConstants.h"
#import "Asphaleia.h"
#import <dlfcn.h>
#define kFullDimStrength 0.3f
@interface SBSearchGesture (ApexAdditions)
- (void)stk_setEnabled:(BOOL)enabled;
@end
NSString * NSStringFromSTKClosingEvent(STKClosingEvent event) {
switch (event) {
case STKClosingEventHomeButtonPress: {
return @"STKClosingEventHomeButtonPress";
}
case STKClosingEventListViewScroll: {
return @"STKClosingEventListViewScroll";
}
case STKClosingEventSwitcherActivation: {
return @"STKClosingEventSwitcherActivation";
}
case STKClosingEventLock: {
return @"STKClosingEventLock";
}
}
}
@implementation STKGroupController
{
STKGroupView *_openGroupView;
UIView *_listDimmingView;
UISwipeGestureRecognizer *_closeSwipeRecognizer;
UITapGestureRecognizer *_closeTapRecognizer;
STKGroupSelectionAnimator *_selectionAnimator;
STKSelectionView *_selectionView;
STKGroupSlot _selectionSlot;
NSMutableArray *_iconsToShow;
NSMutableArray *_iconsToHide;
BOOL _openGroupViewWasModified;
BOOL _wasLongPressed;
BOOL _hasInfiniBoard;
BOOL _hasInfinidock;
BOOL _hasCylinder;
STKIconViewRecycler *_recycler;
NSCache *_groupCache;
}
+ (instancetype)sharedController
{
static dispatch_once_t pred;
static id _si;
dispatch_once(&pred, ^{
_si = [[self alloc] init];
});
return _si;
}
- (instancetype)init
{
if ((self = [super init])) {
_closeTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_closeOpenGroupOrSelectionView)];
_closeSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(_closeOpenGroupOrSelectionView)];
_closeSwipeRecognizer.direction = (UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown);
_closeSwipeRecognizer.delegate = self;
_closeTapRecognizer.delegate = self;
_recycler = [[STKIconViewRecycler alloc] init];
_groupCache = [[NSCache alloc] init];
[_groupCache setCountLimit:200];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_prefsChanged)
name:(NSString *)STKPrefsChangedNotificationName
object:nil];
void *handle = dlopen("/Library/MobileSubstrate/DynamicLibraries/Infiniboard.dylib", RTLD_NOW);
_hasInfiniBoard = !!handle;
handle = dlopen("/Library/MobileSubstrate/DynamicLibraries/Infinidock.dylib", RTLD_NOW);
_hasInfinidock = !!handle;
handle = dlopen("/Library/MobileSubstrate/DynamicLibraries/Cylinder.dylib", RTLD_NOW);
_hasCylinder = !!handle;
}
return self;
}
- (void)addOrUpdateGroupViewForIconView:(SBIconView *)iconView
{
SBIcon *icon = iconView.icon;
if (icon == [[CLASS(SBIconController) sharedInstance] grabbedIcon]) {
return;
}
STKGroupView *groupView = [iconView groupView];
STKPreferences *preferences = [STKPreferences sharedPreferences];
SBIconCoordinate currentCoordinate = [STKGroupLayoutHandler coordinateForIcon:icon];
if (!groupView) {
STKGroup *group = [preferences groupForCentralIcon:icon];
if (!group) {
group = [_groupCache objectForKey:icon.leafIdentifier] ?: [self _groupWithEmptySlotsForIcon:icon];
group.lastKnownCoordinate = currentCoordinate;
[_groupCache setObject:group forKey:icon.leafIdentifier];
}
groupView = [[[STKGroupView alloc] initWithGroup:group iconViewSource:_recycler] autorelease];
groupView.delegate = self;
groupView.showPreview = preferences.shouldShowPreviews;
groupView.showGrabbers = !(preferences.shouldHideGrabbers);
groupView.activationMode = preferences.activationMode;
[icon noteBadgeDidChange];
}
[groupView.group relayoutForNewCoordinate:currentCoordinate];
groupView.group.lastKnownCoordinate = currentCoordinate;
}
- (void)removeGroupViewFromIconView:(SBIconView *)iconView
{
iconView.groupView = nil;
}
- (void)performRotationWithDuration:(NSTimeInterval)duration
{
STKGroupView *groupView = [STKGroupController sharedController].openGroupView;
[groupView closeWithCompletionHandler:^{
[groupView.group forceRelayout];
}];
EXECUTE_BLOCK_AFTER_DELAY((duration + 0.01), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:STKEditingEndedNotificationName object:nil];
});
}
- (BOOL)handleClosingEvent:(STKClosingEvent)event
{
BOOL handled = NO;
if (event == STKClosingEventHomeButtonPress) {
BOOL sbIsFrontMost = ![(SpringBoard *)[UIApplication sharedApplication] _accessibilityFrontMostApplication];
if (sbIsFrontMost && ([self _activeGroupView] || _selectionView)) {
handled = [self _closeOpenGroupOrSelectionView];
}
}
else if (!_selectionView) {
// scroll, switcher open, or lock
handled = [self _closeOpenGroupOrSelectionView];
}
if (handled) {
CLog(@"Handling closing event: %@", NSStringFromSTKClosingEvent(event));
}
return handled;
}
- (void)handleStatusBarTap
{
if ([STKPreferences sharedPreferences].shouldOpenSpotlightFromStatusBarTap && !_selectionView) {
[[CLASS(SBSearchGesture) sharedInstance] revealAnimated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (NSEC_PER_SEC * 0.15)), dispatch_get_main_queue(), ^{
if ([CLASS(SBSearchViewController) instancesRespondToSelector:@selector(_setShowingKeyboard:)]) {
[[CLASS(SBSearchViewController) sharedInstance] _setShowingKeyboard:YES];
}
});
}
}
- (void)handleIconRemoval:(SBIcon *)removedIcon
{
if (!removedIcon || [removedIcon isDownloadingIcon]) {
return;
}
SBIconModel *model = [(SBIconController *)[CLASS(SBIconController) sharedInstance] model];
STKGroup *group = nil;
if ((group = [[[[STKPreferences sharedPreferences] groupForCentralIcon:removedIcon] retain] autorelease])) {
if (group.empty) {
return;
}
NSArray *icons = [group.layout allIcons];
for (SBIcon *icon in icons) {
STKGroupSlot slot = [group.layout slotForIcon:icon];
[group removeIconInSlot:slot];
}
[group finalizeState];
[[STKPreferences sharedPreferences] removeGroup:group];
[model _postIconVisibilityChangedNotificationShowing:icons hiding:nil];
}
else if ((group = [[STKPreferences sharedPreferences] groupForSubappIcon:removedIcon])) {
if (group.empty) {
return;
}
STKGroupSlot slot = [group.layout slotForIcon:removedIcon];
[group removeIconInSlot:slot];
[group finalizeState];
[model _postIconVisibilityChangedNotificationShowing:@[removedIcon] hiding:nil];
}
}
- (STKGroupView *)activeGroupView
{
return [self _activeGroupView];
}
- (STKIconViewRecycler *)iconViewRecycler
{
return _recycler;
}
- (STKGroup *)_groupWithEmptySlotsForIcon:(SBIcon *)icon
{
STKGroupLayout *slotLayout = [STKGroupLayoutHandler emptyLayoutForIconAtLocation:[STKGroupLayoutHandler locationForIcon:icon]];
STKGroup *group = [[STKGroup alloc] initWithCentralIcon:icon layout:slotLayout];
group.state = STKGroupStateEmpty;
[group addObserver:[STKPreferences sharedPreferences]];
return [group autorelease];
}
- (STKGroupView *)_activeGroupView
{
return [[(_openGroupView ?: _openingGroupView) retain] autorelease];
}
- (void)_setAllowScrolling:(BOOL)allow
{
SBIconController *controller = [CLASS(SBIconController) sharedInstance];
[self _currentScrollView].scrollEnabled = allow;
UIScrollView *scrollView = [[controller currentRootIconList].subviews firstObject];
if ([scrollView isKindOfClass:CLASS(IFInfiniboardScrollView)]) {
// Infiniboard
scrollView.scrollEnabled = allow;
}
scrollView = [((UIView *)[[CLASS(SBIconController) sharedInstance] dockListView]).subviews firstObject];
if ([scrollView isKindOfClass:CLASS(UIScrollView)]) {
// Infinidock
scrollView.scrollEnabled = allow;
}
SBIconListPageControl *pageControl = [[controller _currentFolderController].contentView valueForKey:@"pageControl"];
pageControl.userInteractionEnabled = allow;
}
- (UIScrollView *)_currentScrollView
{
SBFolderController *currentFolderController = [[CLASS(SBIconController) sharedInstance] _currentFolderController];
return [currentFolderController.contentView scrollView];
}
- (void)_setupDimmingView
{
if (_hasCylinder) {
return;
}
[self _removeDimmingView];
SBIconController *controller = [CLASS(SBIconController) sharedInstance];
if ([controller hasOpenFolder]) {
// Don't add the dimming view
return;
}
SBIconListView *listView = STKCurrentListView();
// The list dimming view should cover itself, the list view before, and the one after it.
CGRect frame = [UIScreen mainScreen].bounds;
frame.origin.x -= frame.size.width;
frame.origin.y -= 40.f;
frame.size.width *= 3.f;
frame.size.height += 40.f;
_listDimmingView = [[UIView alloc] initWithFrame:frame];
_listDimmingView.backgroundColor = [UIColor colorWithWhite:0.f alpha:1.f];
_listDimmingView.alpha = 0.f;
_listDimmingView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[listView addSubview:_listDimmingView];
STKGroupView *activeGroupView = [self _activeGroupView];
[activeGroupView.superview.superview bringSubviewToFront:activeGroupView.superview];
}
- (void)_removeDimmingView
{
if (_listDimmingView) {
[_listDimmingView removeFromSuperview];
[_listDimmingView release];
_listDimmingView = nil;
[STKCurrentListView() stk_reorderIconViews];
}
}
- (void)_setDimStrength:(CGFloat)strength
{
if (_hasCylinder) {
return;
}
if (!_listDimmingView) {
[self _setupDimmingView];
}
_listDimmingView.alpha = strength;
}
- (void)_addCloseGestureRecognizers
{
UIView *view = [[CLASS(SBIconController) sharedInstance] contentView];
[view addGestureRecognizer:_closeSwipeRecognizer];
[view addGestureRecognizer:_closeTapRecognizer];
}
- (void)_removeCloseGestureRecognizers
{
[_closeTapRecognizer.view removeGestureRecognizer:_closeTapRecognizer];
[_closeSwipeRecognizer.view removeGestureRecognizer:_closeSwipeRecognizer];
}
- (BOOL)_closeOpenGroupOrSelectionView
{
BOOL closed = NO;
if (_selectionView) {
if (_selectionView.isKeyboardVisible) {
[_selectionView dismissKeyboard];
}
else {
[self _closeSelectionView];
closed = YES;
}
}
else if (_openGroupView.group.hasPlaceholders && (_openGroupViewWasModified == NO)) {
[_openGroupView.group removePlaceholders];
}
else if (!_closingGroupView) {
_closingGroupView = [self _activeGroupView];
[_closingGroupView close];
closed = (_closingGroupView != nil);
}
return closed;
}
- (void)_showSelectionViewForIconView:(SBIconView *)selectedIconView
{
// This method can be called before the open animation on the group has finished
// So, ensure that _openGroupView is not nil
_openGroupView = [self _activeGroupView];
_selectionSlot = [_openGroupView.subappLayout slotForIcon:selectedIconView];
_selectionView = [[[STKSelectionView alloc] initWithFrame:CGRectZero
selectedIcon:([selectedIconView.icon isLeafIcon] ? selectedIconView.icon : nil)
centralIcon:_openGroupView.group.centralIcon] autorelease];
SBIconModel *model = [(SBIconController *)[CLASS(SBIconController) sharedInstance] model];
NSMutableSet *visibleIconIdentifiers = [[[model visibleIconIdentifiers] mutableCopy] autorelease];
[visibleIconIdentifiers addObjectsFromArray:[STKPreferences sharedPreferences].identifiersForSubappIcons];
[visibleIconIdentifiers removeObject:_openGroupView.group.centralIcon.leafIdentifier];
// User a mutable set to prevent duplicates, if any.
NSMutableSet *availableIcons = [NSMutableSet setWithCapacity:visibleIconIdentifiers.count];
for (NSString *identifier in visibleIconIdentifiers) {
SBIcon *icon = [model expectedIconForDisplayIdentifier:identifier];
BOOL iconIsWithoutGroup = ![[STKPreferences sharedPreferences] groupForCentralIcon:icon];
if (icon && iconIsWithoutGroup && ![icon isDownloadingIcon]) {
[availableIcons addObject:icon];
}
}
_selectionView.iconsForSelection = [availableIcons allObjects];
_selectionView.selectionHandler = ^{
if (![selectedIconView.icon isLeafIcon]) {
[self _closeSelectionView];
}
};
_selectionAnimator = [[STKGroupSelectionAnimator alloc] initWithSelectionView:_selectionView iconView:selectedIconView];
[_selectionAnimator openSelectionViewAnimatedWithCompletion:nil];
[self _setAllowScrolling:NO];
}
- (void)_selectIconForCurrentSlot:(SBIcon *)iconToSelect
{
_openGroupViewWasModified = YES;
Class emptyReplacementClass = (_openGroupView.group.state == STKGroupStateEmpty ? CLASS(STKEmptyIcon) : CLASS(STKPlaceholderIcon));
if (!iconToSelect) {
NSUInteger emptyCount = 0;
for (SBIcon *icon in _openGroupView.group.layout) {
if (![icon isLeafIcon]) {
emptyCount++;
}
}
if (emptyCount >= 3) {
emptyReplacementClass = CLASS(STKEmptyIcon);
}
}
SBIcon *iconInSelectedSlot = [_openGroupView.group.layout iconInSlot:_selectionSlot];
if (!_iconsToHide) _iconsToHide = [NSMutableArray new];
if (!_iconsToShow) _iconsToShow = [NSMutableArray new];
if ([iconInSelectedSlot isLeafIcon]) {
// Show the icon that is being replaced
[_iconsToShow addObject:iconInSelectedSlot];
}
if (iconToSelect && (iconToSelect != iconInSelectedSlot)) {
// The selected icon needs to be hidden from the home screen
[_iconsToHide addObject:iconToSelect];
STKGroupSlot slotForIconIfAlreadyInGroup = [_openGroupView.group.layout slotForIcon:iconToSelect];
if (slotForIconIfAlreadyInGroup.index != NSNotFound) {
// the group already contains this icon, so replace it with an empty icon
[_openGroupView.group replaceIconInSlot:slotForIconIfAlreadyInGroup withIcon:[[emptyReplacementClass new] autorelease]];
}
}
else if (!iconToSelect) {
iconToSelect = [[emptyReplacementClass new] autorelease];
}
[_openGroupView.group replaceIconInSlot:_selectionSlot withIcon:iconToSelect];
if ([iconToSelect isKindOfClass:CLASS(STKPlaceholderIcon)]) {
[_openGroupView.group.placeholderLayout addIcon:iconToSelect toIconsAtPosition:_selectionSlot.position];
}
if (_openGroupView.group.state != STKGroupStateEmpty) {
[_openGroupView.group addPlaceholders];
}
}
- (void)_closeSelectionView
{
[self _selectIconForCurrentSlot:_selectionView.selectedIcon];
[_selectionAnimator closeSelectionViewAnimatedWithCompletion:^{
[_selectionView removeFromSuperview];
[_selectionView release];
_selectionView = nil;
[_selectionAnimator release];
_selectionAnimator = nil;
[self _setAllowScrolling:YES];
}];
}
- (void)_prefsChanged
{
SBIconController *iconController = [CLASS(SBIconController) sharedInstance];
STKPreferences *preferences = [STKPreferences sharedPreferences];
NSMutableArray *listViews = [NSMutableArray array];
if ([iconController dockListView]) [listViews addObject:[iconController dockListView]];
[listViews addObjectsFromArray:[iconController _rootFolderController].iconListViews];
if ([iconController _currentFolderController] != [iconController _rootFolderController]) {
[listViews addObjectsFromArray:[iconController _currentFolderController].iconListViews];
}
for (SBIconListView *listView in listViews) {
[listView enumerateIconViewsUsingBlock:^(SBIconView *iconView) {
STKGroupView *groupView = iconView.groupView;
groupView.activationMode = preferences.activationMode;
groupView.showGrabbers = !(preferences.shouldHideGrabbers);
groupView.showPreview = preferences.shouldShowPreviews;
[iconView.icon noteBadgeDidChange];
}];
}
[[CLASS(SBSearchGesture) sharedInstance] stk_setEnabled:!preferences.shouldDisableSearchGesture];
}
#pragma mark - Group View Delegate
- (BOOL)shouldGroupViewOpen:(STKGroupView *)groupView
{
BOOL shouldOpen = YES;
if (([STKPreferences sharedPreferences].activationMode == STKActivationModeNone)
|| ((groupView.group.state == STKGroupStateEmpty) && [STKPreferences sharedPreferences].shouldLockLayouts)) {
shouldOpen = NO;
}
else {
shouldOpen = (_openGroupView == nil);
}
SBIconController *controller = [CLASS(SBIconController) sharedInstance];
BOOL presentingShortcutMenu = (([controller respondsToSelector:@selector(presentedShortcutMenu)]) && (controller.presentedShortcutMenu != nil));
return (shouldOpen && !presentingShortcutMenu);
}
- (BOOL)groupView:(STKGroupView *)groupView shouldRecognizeGesturesSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)recognizer
{
BOOL allow = YES;
STKActivationMode activationMode = [STKPreferences sharedPreferences].activationMode;
if (recognizer == _closeSwipeRecognizer || recognizer == _closeTapRecognizer) {
allow = NO;
}
else if ([recognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
UISwipeGestureRecognizerDirection recognizerDirection = [(UISwipeGestureRecognizer *)recognizer direction];
if ((activationMode & STKActivationModeSwipeUp) && !(activationMode & STKActivationModeSwipeDown)) {
// Allow all directions other than Up
allow = !(recognizerDirection & UISwipeGestureRecognizerDirectionUp);
}
else if ((activationMode & STKActivationModeSwipeDown) && !(activationMode & STKActivationModeSwipeUp)) {
// Allow all directions other than Down
allow = !(recognizerDirection & UISwipeGestureRecognizerDirectionDown);
}
else if (STKActivationModeIsUpAndDown(activationMode)) {
allow = NO;
}
}
else if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
NSArray *targets = [recognizer valueForKey:@"_targets"];
id target = [targets firstObject];
target = [target valueForKey:@"_target"];
BOOL activationModeConflictsWithSearch = ((activationMode & STKActivationModeSwipeUp) || (activationMode & STKActivationModeSwipeDown));
allow = (!([target isKindOfClass:CLASS(SBSearchScrollView)] && activationModeConflictsWithSearch)
&& [recognizer.view isKindOfClass:[UIScrollView class]]);
}
return allow;
}
- (void)groupViewWillOpen:(STKGroupView *)groupView
{
_openingGroupView = groupView;
[self _setAllowScrolling:NO];
[groupView.group.centralIcon noteBadgeDidChange];
for (SBIcon *icon in groupView.group.layout) {
[icon noteBadgeDidChange];
}
[self _addCloseGestureRecognizers];
[[CLASS(SBSearchGesture) sharedInstance] stk_setEnabled:NO];
}
- (void)groupView:(STKGroupView *)groupView didMoveToOffset:(CGFloat)offset
{
[self _setDimStrength:fminf((offset * kFullDimStrength), kFullDimStrength)];
}
- (void)groupViewDidOpen:(STKGroupView *)groupView
{
_openingGroupView = nil;
if (groupView.isOpen) {
_openGroupView = groupView;
[self _setAllowScrolling:YES];
}
}
- (void)groupViewWillClose:(STKGroupView *)groupView
{
[self _setAllowScrolling:YES];
_openingGroupView = nil;
[groupView.group.centralIcon noteBadgeDidChange];
for (SBIcon *icon in groupView.group.layout) {
[icon noteBadgeDidChange];
}
[[CLASS(SBSearchGesture) sharedInstance] stk_setEnabled:YES];
}
- (void)groupViewDidClose:(STKGroupView *)groupView
{
if (_openGroupViewWasModified) {
_openGroupView.group.state = STKGroupStateDirty;
[_openGroupView.group finalizeState];
if (_iconsToHide.count > 0 || _iconsToShow.count > 0) {
SBIconModel *model = [(SBIconController *)[CLASS(SBIconController) sharedInstance] model];
NSDictionary *userInfo = @{@"SBIconModelIconsToShowKey": _iconsToShow, @"SBIconModelIconsToHideKey": _iconsToHide};
[[NSNotificationCenter defaultCenter] postNotificationName:@"SBIconModelVisibilityDidChangeNotification" object:model userInfo:userInfo];
[_iconsToShow release];
[_iconsToHide release];
_iconsToShow = nil;
_iconsToHide = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:STKEditingEndedNotificationName object:nil];
[groupView resetLayouts];
}
}
[self _removeDimmingView];
[self _removeCloseGestureRecognizers];
_openGroupView = nil;
_closingGroupView = nil;
_openGroupViewWasModified = NO;
}
- (void)groupViewWillBeDestroyed:(STKGroupView *)groupView
{
if (groupView == _openGroupView) {
[self groupViewDidClose:groupView];
[[CLASS(SBSearchGesture) sharedInstance] stk_setEnabled:YES];
}
else if (groupView == _openingGroupView) {
_openingGroupView = nil;
}
}
- (void)iconTapped:(SBIconView *)iconView
{
STKGroupView *activeGroupView = [self _activeGroupView];
SBIconController *controller = [CLASS(SBIconController) sharedInstance];
if (!activeGroupView) {
[controller iconTapped:iconView];
return;
}
EXECUTE_BLOCK_AFTER_DELAY(0.2, ^{
[iconView setHighlighted:NO];
});
BOOL presentingShortcutMenu = ([controller respondsToSelector:@selector(presentedShortcutMenu)]) && (controller.presentedShortcutMenu != nil);
if (presentingShortcutMenu) {
return;
}
if ((iconView.groupView == activeGroupView) && activeGroupView.group.hasPlaceholders) {
[self _closeOpenGroupOrSelectionView];
return;
}
if (iconView.apexOverlayView) {
[self _showSelectionViewForIconView:iconView];
}
else {
BOOL protectedByAsphaleia = [(asphaleiaMainClass *)[objc_getClass("asphaleiaMainClass") sharedInstance] possiblyProtectApp:iconView.icon.leafIdentifier inView:iconView];
if (!protectedByAsphaleia) {
if ([iconView.icon respondsToSelector:@selector(launchFromLocation:)]) {
[iconView.icon launchFromLocation:SBIconLocationHomeScreen];
}
else if ([iconView.icon respondsToSelector:@selector(launchFromLocation:context:)]) {
[iconView.icon launchFromLocation:SBIconLocationHomeScreen context:nil];
}
if ([STKPreferences sharedPreferences].shouldCloseOnLaunch) {
[activeGroupView close];
}
}
}
}
- (BOOL)iconShouldAllowTap:(SBIconView *)iconView
{
SBIconController *controller = [CLASS(SBIconController) sharedInstance];
if (!_openGroupView) {
return [controller iconShouldAllowTap:iconView];
}
if (_wasLongPressed) {
_wasLongPressed = NO;
return NO;
}
BOOL presentingShortcutMenu = (([controller respondsToSelector:@selector(presentedShortcutMenu)]) && (controller.presentedShortcutMenu != nil));
return (!_selectionView && !presentingShortcutMenu);
}
/// If this method returns YES, then the delegate call does not needed to be forwarded to the icon controller
- (BOOL)handleLongPressOnIconView:(SBIconView *)iconView {
SBIconController *controller = [CLASS(SBIconController) sharedInstance];
BOOL presentingShortcutMenu = (([controller respondsToSelector:@selector(presentedShortcutMenu)]) && (controller.presentedShortcutMenu != nil));
if (![self _activeGroupView] || ![iconView.icon isLeafIcon] || presentingShortcutMenu) {
return NO;
}
_wasLongPressed = YES;
[iconView setHighlighted:NO];
STKGroup *group = ([iconView containerGroupView] ?: [iconView groupView]).group;
if (!group.empty) {
[group addPlaceholders];
}
return YES;
}
- (void)iconHandleLongPress:(SBIconView *)iconView
{
if (![self handleLongPressOnIconView:iconView]) {
[[CLASS(SBIconController) sharedInstance] iconHandleLongPress:iconView];
}
}
// iOS 10
- (void)iconHandleLongPress:(SBIconView *)iconView withFeedbackBehavior:(id)feedbackBehavior
{
if (![self handleLongPressOnIconView:iconView]) {
[[CLASS(SBIconController) sharedInstance] iconHandleLongPress:iconView withFeedbackBehavior:feedbackBehavior];
}
}
- (void)iconTouchBegan:(SBIconView *)iconView
{
if (![self _activeGroupView]) {
[[CLASS(SBIconController) sharedInstance] iconTouchBegan:iconView];
return;
}
[iconView setHighlighted:YES];
[iconView setNeedsLayout];
}
- (void)icon:(SBIconView *)iconView touchMoved:(UITouch *)touch
{
if (!_openGroupView) {
[[CLASS(SBIconController) sharedInstance] icon:iconView touchMoved:touch];
return;
}
if (_wasLongPressed) {
CGPoint location = [touch locationInView:[iconView _iconImageView]];
_wasLongPressed = [[iconView _iconImageView] pointInside:location withEvent:nil];
}
}
- (void)icon:(SBIconView *)iconView touchEnded:(BOOL)ended
{
if (_openGroupView) {
// I don't care about this method on open group views.
return;
}
[[CLASS(SBIconController) sharedInstance] icon:iconView touchEnded:ended];
}
#pragma mark - Gesture Recognizer Delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
{
STKGroupView *activeGroupView = [self _activeGroupView];
BOOL shouldReceiveTouch = NO;
if (_selectionView) {
// If a selection view is active, then we only need to ensure that the touch is not on the it.
BOOL touchIsOutsideSelectionView = !([_selectionView.contentView hitTest:[touch locationInView:_selectionView.contentView]
withEvent:nil]);
shouldReceiveTouch = touchIsOutsideSelectionView;
}
else {
// Since there's not selection view, we can receive the touch as long as it isn't on the active group view
BOOL touchIsOutsideActiveGroupView = !([activeGroupView hitTest:[touch locationInView:activeGroupView] withEvent:nil]);
shouldReceiveTouch = touchIsOutsideActiveGroupView;
}
return shouldReceiveTouch;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return (otherGestureRecognizer.view == [[[CLASS(SBIconController) sharedInstance] _currentFolderController].contentView scrollView]);
}
#pragma mark - SUPER HAXXX
/*
Instead of implementing (and forwarding) each method of SBIconViewDelegate to SBIconController, we only implement those which we require.
We then implement -forwardInvocation and forward the requisite methods to SBIconController.
However, SBIconView checks (using -respondsToSelector:) whether we implement any given method in SBIconViewDelegate.
SO, we override -respondsToSelector: too! H4XX
*/
- (BOOL)__selectorIsPartOfIconViewDelegateProtocol:(SEL)selector
{
Protocol *iconViewDelegateProtocol = @protocol(SBIconViewDelegate);
struct objc_method_description methodDescription = protocol_getMethodDescription(iconViewDelegateProtocol,
selector,
NO,
YES);
return (methodDescription.name != NULL && methodDescription.types != NULL);
}
- (BOOL)respondsToSelector:(SEL)selector
{
if ([self __selectorIsPartOfIconViewDelegateProtocol:selector]) {
return YES;
}
return [super respondsToSelector:selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
SEL selector = [invocation selector];
if ([self __selectorIsPartOfIconViewDelegateProtocol:selector]
&& [[CLASS(SBIconController) sharedInstance] respondsToSelector:selector]) {
[invocation invokeWithTarget:[CLASS(SBIconController) sharedInstance]];
}
else {
[super forwardInvocation:invocation];
}
}
@end