-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathOSSlider.m
369 lines (247 loc) · 10.1 KB
/
OSSlider.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
#import "OSSlider.h"
#import "OSPreferences.h"
#define marginSize [prefs PANE_SEPARATOR_SIZE]
#define scrollDuration [prefs SCROLL_TO_PANE_DURATION]
#define mcScrollDuration 0.40
@implementation OSSlider
@synthesize startingOffset = _startingOffset;
@synthesize currentOrientation = _currentOrientation;
@synthesize pageIndexPlaceholder = _pageIndexPlaceholder;
+ (id)sharedInstance
{
static OSSlider *_slider;
if (_slider == nil)
{
_slider = [[self alloc] init];
}
return _slider;
}
- (id)init{
CGRect frame = [[UIScreen mainScreen] bounds];
frame.size.width += marginSize;
if(![super initWithFrame:frame]){
return nil;
}
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.pagingEnabled = true;
self.clipsToBounds = false;
self.showsHorizontalScrollIndicator = false;
self.backgroundColor = [UIColor blackColor];
[self setDelegate:self];
[self removeGestureRecognizer:self.panGestureRecognizer];
return self;
}
- (void)handleUpSwitcherGesture:(UISwipeGestureRecognizer *)gesture{
if([[self currentPane] isKindOfClass:[OSAppPane class]]){
if([(OSAppPane*)[self currentPane] windowBarIsOpen]){
[(OSAppPane*)[self currentPane] setWindowBarHidden];
return;
}
}
[[OSViewController sharedInstance] setMissionControlActive:true animated:true];
}
- (void)handleDownSwitcherGesture:(UISwipeGestureRecognizer *)gesture{
if([[OSViewController sharedInstance] missionControlIsActive]){
[[OSViewController sharedInstance] setMissionControlActive:false animated:true];
return;
}
if([[self currentPane] isKindOfClass:[OSAppPane class]]){
if(![(OSAppPane*)[self currentPane] windowBarIsOpen]){
[(OSAppPane*)[self currentPane] setWindowBarVisible];
}
}
}
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration{
int appViewDegrees;
switch(orientation){
case UIInterfaceOrientationPortrait:
appViewDegrees = 0;
break;
case UIInterfaceOrientationPortraitUpsideDown:
appViewDegrees = 180;
break;
case UIInterfaceOrientationLandscapeLeft:
appViewDegrees = 90;
break;
case UIInterfaceOrientationLandscapeRight:
appViewDegrees = 270;
break;
}
for(OSPane *pane in [[OSPaneModel sharedInstance] panes]){
if([pane isKindOfClass:[OSAppPane class]]){
UIView *appView = [(OSAppPane*)pane appView];
appView.transform = CGAffineTransformMakeRotation(DegreesToRadians(appViewDegrees));
CGRect frame = [appView frame];
frame.origin = CGPointMake(0, 0);
[appView setFrame:frame];
}else if([pane isKindOfClass:[OSDesktopPane class]]){
//[[(OSDesktopPane*)pane wallpaperView] setOrientation:orientation duration:duration];
}
}
[self alignPanes];
self.contentOffset = CGPointMake(self.pageIndexPlaceholder * self.bounds.size.width, 0);
[self updateDockPosition];
}
- (void)addPane:(OSPane*)pane{
CGSize contentSize = self.contentSize;
contentSize.width = (marginSize + pane.frame.size.width) * [[OSPaneModel sharedInstance] count];
[self setContentSize:contentSize];
[pane setOriginX: (pane.frame.size.width + marginSize) * ([[OSPaneModel sharedInstance] count] - 1)];
if([[OSViewController sharedInstance] missionControlIsActive])
pane.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.5, 0.5);
if([[OSViewController sharedInstance] missionControlIsActive]){
CGRect frame = [pane frame];
frame.origin.y = 0;
[pane setFrame:frame];
}
[self addSubview:pane];
[self alignPanes];
}
- (void)removePane:(OSPane*)pane{
if([pane isKindOfClass:[OSDesktopPane class]]){
NSArray *windows = [[(OSDesktopPane*)pane windows] copy];
for(OSWindow *window in windows){
OSDesktopPane *toPane = nil;
for(OSDesktopPane *desktopPane in [[OSPaneModel sharedInstance] panes]){
if(![desktopPane isKindOfClass:[OSDesktopPane class]] || desktopPane == pane)
continue;
toPane = desktopPane;
break;
}
CGRect frame = window.frame;
frame.origin = [OSMCWindowLayoutManager convertPointFromSlider:frame.origin toPane:pane];
frame.origin = [OSMCWindowLayoutManager convertPointToSlider:frame.origin fromPane:toPane];
[window setFrame:frame];
[window switchToDesktopPane:toPane];
[self bringSubviewToFront:window];
}
[windows release];
}
OSPane *destination = nil;
if(pane != [self currentPane]){
destination = [self currentPane];
}else{
int index = [[[OSPaneModel sharedInstance] panes] indexOfObject:pane];
if(index > 0){
destination = [[OSPaneModel sharedInstance] paneAtIndex:index - 1];
}else{
destination = [[OSPaneModel sharedInstance] paneAtIndex:index + 1];
}
}
pane.hidden = true;
[self scrollToPane:destination animated:true];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, mcScrollDuration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[pane removeFromSuperview];
[self alignPanes];
[[OSThumbnailView sharedInstance] updateSelectedThumbnail];
});
}
- (void)alignPanes{
self.contentSize = CGSizeMake([[OSPaneModel sharedInstance] count] * self.bounds.size.width, self.bounds.size.height);
for(OSPane *pane in [[OSPaneModel sharedInstance] panes]){
[pane paneIndexWillChange];
CGRect bounds = CGRectMake(0, 0, self.bounds.size.width - marginSize, self.bounds.size.height);
pane.bounds = bounds;
[pane setCenter:CGPointMake((self.bounds.size.width * [[OSPaneModel sharedInstance] indexOfPane:pane]) - (marginSize / 2) + (self.bounds.size.width / 2), pane.center.y)];
pane.layer.shadowPath = [UIBezierPath bezierPathWithRect:pane.bounds].CGPath;
[pane paneIndexDidChange];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self updateDockPosition];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[[OSThumbnailView sharedInstance] updateSelectedThumbnail];
}
- (void)updateDockPosition{
if([[objc_getClass("SBUIController") sharedInstance] scaleGestureInProgress]){
return;
}
OSPane *intrudingPane;
CGRect currentPaneRect = CGRectIntersection(self.currentPane.frame, self.bounds);
CGRect intrudingPaneRect = CGRectZero;
if(self.contentOffset.x >= self.currentPane.frame.origin.x){
intrudingPane = [[OSPaneModel sharedInstance] paneAtIndex:self.currentPageIndex + 1];
}else{
intrudingPane = [[OSPaneModel sharedInstance] paneAtIndex:self.currentPageIndex - 1];
}
if(!intrudingPane && self.currentPane.showsDock){
[[OSViewController sharedInstance] setDockPercentage:0.0];
return;
}else if(!intrudingPane && !self.currentPane.showsDock){
[[OSViewController sharedInstance] setDockPercentage:1.0];
return;
}
intrudingPaneRect = CGRectIntersection(intrudingPane.frame, self.bounds);
float currentPanePercentage = (currentPaneRect.size.width * 100) / self.frame.size.width;
float intrudingPanePercentage = (intrudingPaneRect.size.width * 100) / self.frame.size.width;
float shownPercentage = 0;
if(!self.currentPane.showsDock)
shownPercentage += currentPanePercentage;
if(!intrudingPane.showsDock)
shownPercentage += intrudingPanePercentage;
shownPercentage = shownPercentage * 0.01;
[[OSViewController sharedInstance] setDockPercentage:shownPercentage];
}
- (BOOL)isPortrait{
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown){
return true;
}
return false;
}
- (int)currentPageIndex{
return nearbyint(self.contentOffset.x / self.bounds.size.width);
}
- (OSPane*)currentPane{
return [[OSPaneModel sharedInstance] paneAtIndex:self.currentPageIndex];
}
- (void)scrollToPane:(OSPane*)pane animated:(BOOL)animated{
if(!animated){
self.contentOffset = CGPointMake([[OSPaneModel sharedInstance] indexOfPane:pane] * self.bounds.size.width, 0);
[self updateDockPosition];
[[OSThumbnailView sharedInstance] updateSelectedThumbnail];
}else{
[UIView animateWithDuration:([[OSViewController sharedInstance] missionControlIsActive] ? mcScrollDuration : scrollDuration)
delay:([[OSViewController sharedInstance] missionControlIsActive] ? 0 : 0.25)
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect bounds = [self bounds];
bounds.origin.x = [[OSPaneModel sharedInstance] indexOfPane:pane] * self.bounds.size.width;
[self setBounds:bounds];
[self updateDockPosition];
[[OSThumbnailView sharedInstance] updateSelectedThumbnail];
}completion:^(BOOL completed){
}];
}
}
- (void)beginPaging{
self.pageOffsetBefore = self.contentOffset.x;
}
- (void)updatePaging:(float)percentage{
CGPoint velocity = [self.swipeGestureRecognizer movementVelocityInPointsPerSecond];
Ivar horizontalVelocity_ivar = class_getInstanceVariable(object_getClass(self), "_horizontalVelocity");
Ivar previousHorizontalVelocity_ivar = class_getInstanceVariable(object_getClass(self), "_previousHorizontalVelocity");
if (!horizontalVelocity_ivar || !previousHorizontalVelocity_ivar) return;
double *horizontalVelocity = ((double*)((uint8_t*)self + ivar_getOffset(horizontalVelocity_ivar)));
double *previousHorizontalVelocity = ((double*)((uint8_t*)self + ivar_getOffset(previousHorizontalVelocity_ivar)));
*previousHorizontalVelocity = *horizontalVelocity;
*horizontalVelocity = -(velocity.x / 300);
float pageWidth = self.frame.size.width;
float newContentOffset = 0;
newContentOffset = (-percentage * pageWidth) + self.pageOffsetBefore;
#ifdef __LP64__ /* This doesn't seem to work on 32 bit devices. Crashes with a bus error. */
BOOL outsideX;
self.contentOffset = [self _rubberBandContentOffsetForOffset:CGPointMake(newContentOffset, self.contentOffset.y) outsideX:&outsideX outsideY:NULL];
#endif
}
- (void)swipeGestureEndedWithCompletionType:(long long)arg1 cumulativePercentage:(double)arg2{
Ivar horizontalVelocity_ivar = class_getInstanceVariable(object_getClass(self), "_horizontalVelocity");
if (!horizontalVelocity_ivar) return;
double *horizontalVelocity = ((double*)((uint8_t*)self + ivar_getOffset(horizontalVelocity_ivar)));
[self _prepareToPageWithHorizontalVelocity:*horizontalVelocity verticalVelocity:0];
[self _endPanNormal:true];
}
- (void)dealloc{
[super dealloc];
}
@end