-
Notifications
You must be signed in to change notification settings - Fork 3
/
SwitchApp.xmi
270 lines (204 loc) · 10 KB
/
SwitchApp.xmi
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
/*
* Copyright (c) 2011-2013, Xuzz Productions, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "SwitchApp.h"
#import "Common.h"
#import "Preferences.h"
@implementation ZephyrSwitchAppGesture
- (BOOL)shouldActivateAtEdge:(SBOffscreenEdge)edge {
SBApplication *topApplication = [SBApp _accessibilityFrontMostApplication];
if (topApplication != nil) {
BOOL disableIdentifier = [(NSNumber *) ZephyrPreferencesGet([NSString stringWithFormat:@"SideDisable-%@", [topApplication displayIdentifier]], (NSNumber *) kCFBooleanFalse) boolValue];
if (disableIdentifier) return NO;
}
// Put this after the above check so you can quickly page through disabled apps.
if (topApplication == nil) topApplication = MSHookIvar<SBApplication *>([objc_getClass("SBUIController") sharedInstance], "_pendingAppActivatedByGesture");
return topApplication != nil
&& [self currentOrientationIsSupported]
&& ![SBApp _accessibilityIsSystemGestureActive]
&& ![[objc_getClass("SBUIController") sharedInstance] isSwitcherShowing]
&& ![[objc_getClass("SBBulletinWindowController") sharedInstance] isBusy]
&& !ZephyrAssistantIsVisible()
&& ![[objc_getClass("SBBulletinListController") sharedInstanceIfExists] listViewIsActive]
&& [(NSNumber *) ZephyrPreferencesGet(@"SwipeSideEnabled", [NSNumber numberWithBool:YES]) boolValue];
}
- (BOOL)shouldActivateAtEdge:(SBOffscreenEdge)edge atPoint:(CGPoint)point {
CGFloat area = [ZephyrPreferencesGet(@"SideScreenArea", [NSNumber numberWithFloat:1.0f]) floatValue];
UIInterfaceOrientation orientation = ZephyrCurrentInterfaceOrientation();
CGFloat height = ZephyrHeightForOrientation(orientation);
if (area > 0 && point.y > height * area) {
return NO;
} else if (area < 0 && point.y < height + height * area) {
return NO;
}
if ([UIKeyboard isOnScreen]) {
CGSize size = [UIKeyboard defaultSizeForInterfaceOrientation:orientation];
if (point.y > height - size.height) {
return NO;
}
}
return YES;
}
- (BOOL)shouldUseGrabberAtEdge:(SBOffscreenEdge)edge {
BOOL enabled = [(NSNumber *) ZephyrPreferencesGet(@"SideGrabberEnabled", [NSNumber numberWithBool:NO]) boolValue];
if (enabled) {
SBApplication *topApplication = [SBApp _accessibilityFrontMostApplication];
SBApplication *pendingApplication = MSHookIvar<SBApplication *>([objc_getClass("SBUIController") sharedInstance], "_pendingAppActivatedByGesture");
// When quickly paging between apps, don't require the grabber again until an app launches.
if (topApplication == nil && pendingApplication != nil) {
return NO;
} else {
return YES;
}
} else {
return NO;
}
}
- (BOOL)currentOrientationIsSupported {
return YES;
}
- (void)handleGestureBegan:(SBGestureRecognizer *)recognizer withLocation:(CGFloat)location {
SBUIController *controller = [objc_getClass("SBUIController") sharedInstance];
[controller handleFluidHorizontalSystemGesture:recognizer];
}
- (void)handleGestureChanged:(SBGestureRecognizer *)recognizer withLocation:(CGFloat)location velocity:(CGPoint)velocity {
SBUIController *controller = [objc_getClass("SBUIController") sharedInstance];
[controller handleFluidHorizontalSystemGesture:recognizer];
}
- (void)handleGestureEnded:(SBGestureRecognizer *)recognizer withLocation:(CGFloat)location velocity:(CGPoint)velocity completionType:(int)type {
SBUIController *controller = [objc_getClass("SBUIController") sharedInstance];
[controller handleFluidHorizontalSystemGesture:recognizer];
}
- (void)handleGestureCanceled:(SBGestureRecognizer *)recognizer {
SBUIController *controller = [objc_getClass("SBUIController") sharedInstance];
[controller handleFluidHorizontalSystemGesture:recognizer];
}
@end
%group SwitchApp
%hook SBUIController
static ZephyrSwitchAppGesture *gesture = nil;
- (void)finishLaunching {
%orig;
NSInteger touches = [(NSNumber *) ZephyrPreferencesGet(@"SideMinimumTouchCount", ZephyrPreferencesGet(@"MinimumTouchCount", [NSNumber numberWithInt:1])) intValue];
CGFloat edgeMargin = [(NSNumber *) ZephyrPreferencesGet(@"SideSensitivityDistance", [NSNumber numberWithFloat:30.0f]) floatValue];
gesture = [[ZephyrSwitchAppGesture alloc] init];
[gesture addOffscreenEdge:kSBOffscreenEdgeRight minimumTouchCount:touches edgeMargin:edgeMargin];
[gesture addOffscreenEdge:kSBOffscreenEdgeLeft minimumTouchCount:touches edgeMargin:edgeMargin];
ZephyrPreferencesApplyActionRegister(^{
NSInteger touches = [(NSNumber *) ZephyrPreferencesGet(@"SideMinimumTouchCount", ZephyrPreferencesGet(@"MinimumTouchCount", [NSNumber numberWithInt:1])) intValue];
CGFloat edgeMargin = [(NSNumber *) ZephyrPreferencesGet(@"SideSensitivityDistance", [NSNumber numberWithFloat:30.0f]) floatValue];
for (SBOffscreenSwipeGestureRecognizer *recognizer in [gesture gestureRecognizers]) {
[recognizer setMinTouches:touches];
[recognizer setEdgeMargin:edgeMargin];
}
});
}
static SBGestureRecognizer *activeHorizontalRecognizer = nil;
// avoid conflict between native gesture and custom gestures
- (void)handleFluidHorizontalSystemGesture:(SBGestureRecognizer *)recognizer {
// This setting only controls the default gesture recognizer, not ours (offscreen swipe recognizers).
if (![recognizer isKindOfClass:objc_getClass("SBOffscreenSwipeGestureRecognizer")]) {
BOOL enabled = [ZephyrPreferencesGet(@"SystemSwitchAppEnabled", [NSNumber numberWithBool:YES]) boolValue];
if (!enabled) return;
}
if (recognizer == activeHorizontalRecognizer) {
if ([recognizer state] == SBGestureRecognizerStateEnded || [recognizer state] == SBGestureRecognizerStateCancelled) {
activeHorizontalRecognizer = nil;
}
%orig;
} else if (activeHorizontalRecognizer == nil && [recognizer state] == SBGestureRecognizerStateBegan) {
activeHorizontalRecognizer = recognizer;
%orig;
}
}
%end
// The gesture, when starting, tries to rotate the screen (since they are only designed
// for the iPad). Since the iPhone's home screen can't rotate, disable that part here.
static NSInteger switchAppGestureBeganDisableRotation = 0;
%hook SpringBoard
- (void)noteInterfaceOrientationChanged:(UIInterfaceOrientation)orientation {
if (switchAppGestureBeganDisableRotation) return;
%orig;
}
%end
%hook UIDevice
- (void)setOrientation:(UIDeviceOrientation)orientation animated:(BOOL)animated {
if (switchAppGestureBeganDisableRotation) return;
%orig;
}
%end
%hook SBUIController
- (void)_switchAppGestureBegan {
if (!ZephyrHomeShouldRotateToInterfaceOrientation(ZephyrCurrentInterfaceOrientation())) {
switchAppGestureBeganDisableRotation += 1;
}
%orig;
if (!ZephyrHomeShouldRotateToInterfaceOrientation(ZephyrCurrentInterfaceOrientation())) {
switchAppGestureBeganDisableRotation -= 1;
}
}
- (void)_switchAppGestureBegan:(CGFloat)location {
if (!ZephyrHomeShouldRotateToInterfaceOrientation(ZephyrCurrentInterfaceOrientation())) {
switchAppGestureBeganDisableRotation += 1;
}
%orig;
if (!ZephyrHomeShouldRotateToInterfaceOrientation(ZephyrCurrentInterfaceOrientation())) {
switchAppGestureBeganDisableRotation -= 1;
}
}
%end
%hook SBSwitchAppGestureView
- (id)initWithInterfaceOrientation:(UIInterfaceOrientation)orientation startingApp:(SBApplication *)startingApp leftwardApp:(SBApplication *)leftApp rightwardApp:(SBApplication *)rightApp {
if ((self = %orig) != nil) {
// iOS 5.0 doesn't set this by default, so make sure to ourselves.
[self setContentScaleFactor:[[UIScreen mainScreen] scale]];
return self;
}
return self;
}
// Rotation support. By default, we can't rotate the SBSwitchAppGestureView, and
// since we no longer replace the _switchAppGestureBegan method, we can't add a
// different container view there. Instead, just hack that support in here instead.
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (newSuperview == nil) {
UIView *superview = [self superview];
[superview removeFromSuperview];
}
}
- (void)didMoveToSuperview {
UIView *superview = [self superview];
UIView *contentView = MSHookIvar<UIView *>([objc_getClass("SBUIController") sharedInstance], "_contentView");
if (superview == contentView) {
UIView *containerView = [[UIView alloc] initWithFrame:[contentView bounds]];
ZephyrRotateViewFromOrientationToOrientation(containerView, ZephyrHomeInterfaceOrientation(), ZephyrCurrentInterfaceOrientation(), YES);
[containerView addSubview:self];
[superview addSubview:containerView];
[containerView release];
}
}
%end
%end
%ctor {
%init(SwitchApp);
}