forked from johndbritton/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPLayoutView.m
612 lines (471 loc) · 17 KB
/
TPLayoutView.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
//
// TPLayoutView.m
// teleport
//
// Created by JuL on Thu Feb 26 2004.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPLayoutView.h"
#import "TPRemoteHost.h"
#import "TPLocalHost.h"
#import "TPLayoutRemoteHostView.h"
#import "TPLayoutLocalHostView.h"
#import "TPAnimationManager.h"
#import "TPHostAnimationController.h"
#import "TPPreferencePane.h"
#import "TPHostsManager.h"
#import "TPHostSnapping.h"
#define DEFAULT_SCALE 0.1
#define DEFAULT_HEIGHT 100
#define HOST_MARGIN 8
#define LINE_MARGIN 0
#define LINE_THICKNESS 1.0
#define DEBUG_SNAP 0
#define SNAP_DURATION 2.0
#define BOTTOM_MARGIN 20
NSRect TPScaledRect(NSRect rect, float scale)
{
NSRect scaledRect = rect;
scaledRect.origin.x *= scale;
scaledRect.origin.y *= scale;
scaledRect.size.width *= scale;
scaledRect.size.height *= scale;
return scaledRect;
}
@implementation TPLayoutView
- (instancetype)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if(self) {
_scaleFactor = DEFAULT_SCALE;
self.wantsLayer = YES;
_localHostView = [[TPLayoutLocalHostView alloc] initWithHost:[TPLocalHost localHost] layoutView:self];
[self addSubview:_localHostView];
_remoteHostsViews = [[NSMutableDictionary alloc] init];
// _layoutHosts = [[NSMutableDictionary alloc] init];
// _layoutLocalHost = [[TPLayoutLocalHostView alloc] initWithHost:[TPLocalHost localHost] layoutView:self];
// [self addSubview:_layoutLocalHost];
// _scrollerDeltaX = 0.0;
// _scroller = nil;
}
return self;
}
- (void)awakeFromNib
{
[self setWantsLayer:YES];
CALayer *layer = [CALayer layer];
self.layer = layer;
CGColorRef backgroundColor = CGColorCreateGenericGray(1.0, 0.5);
layer.backgroundColor = backgroundColor;
CFRelease(backgroundColor);
layer.borderWidth = 1.0;
CGColorRef borderColor = CGColorCreateGenericGray(0.5, 1.0);
layer.borderColor = borderColor;
layer.cornerRadius = 4.0;
CFRelease(borderColor);
// [self performSelector:@selector(setWantsLayer:) withObject:[NSNumber numberWithBool:YES]];
[self updateLayout];
}
#pragma mark -
#pragma mark Updating
- (NSArray*)remoteHosts
{
return [[TPHostsManager defaultManager] hostsWithState:(TPHostAllStates & ~TPHostUndefState)];
}
- (TPLayoutRemoteHostView*)remoteHostViewForHost:(TPRemoteHost*)host
{
return _remoteHostsViews[[host identifier]];
}
#pragma mark -
#pragma mark Layouting
- (void)_addSubviewWithFadeIn:(NSView*)subview
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.0];
[subview setAlphaValue:0.0];
[NSAnimationContext endGrouping];
[self addSubview:subview];
[[subview animator] setAlphaValue:1.0];
}
- (void)_updateViews
{
NSArray * hosts = [[self remoteHosts] hostsWithState:(TPHostSharedState|TPHostIncompatibleState|TPHostPeeredState)];
NSEnumerator * hostsEnum = [hosts objectEnumerator];
TPRemoteHost * host;
NSMutableSet * removedHosts = [[NSMutableSet alloc] initWithArray:[_remoteHostsViews allKeys]];
while((host = [hostsEnum nextObject])) {
NSString * remoteHostIdentifier = [host identifier];
TPLayoutRemoteHostView * remoteHostView = _remoteHostsViews[remoteHostIdentifier];
if(remoteHostView == nil) {
remoteHostView = [[TPLayoutRemoteHostView alloc] initWithHost:host layoutView:self];
[remoteHostView update];
_remoteHostsViews[remoteHostIdentifier] = remoteHostView;
[self _addSubviewWithFadeIn:remoteHostView];
}
else {
[remoteHostView setHostIdentifier:remoteHostIdentifier];
[removedHosts removeObject:remoteHostIdentifier];
}
}
NSEnumerator * removedHostsEnum = [removedHosts objectEnumerator];
NSString * removedHostKey;
while((removedHostKey = [removedHostsEnum nextObject])) {
TPLayoutRemoteHostView * remoteHostView = _remoteHostsViews[removedHostKey];
[remoteHostView removeFromSuperview];
[_remoteHostsViews removeObjectForKey:removedHostKey];
}
}
- (void)_updateScaleFactor
{
NSRect bounds = [self bounds];
NSRect localHostRect = [_localHostView totalFrame];
NSSize maxHostSize = NSZeroSize;
NSSize maxOnlineHostSize = NSZeroSize;
NSEnumerator * hostViewsEnum = [_remoteHostsViews objectEnumerator];
TPLayoutRemoteHostView * hostView;
while((hostView = [hostViewsEnum nextObject])) {
TPRemoteHost * host = (TPRemoteHost*)[hostView host];
NSRect hostRect = [hostView totalFrame];
maxHostSize.width = MAX(maxHostSize.width, NSWidth(hostRect));
maxHostSize.height = MAX(maxHostSize.height, NSHeight(hostRect));
if([host isInState:TPHostOnlineState|TPHostPeeredState]) {
maxOnlineHostSize.width = MAX(maxOnlineHostSize.width, NSWidth(hostRect));
maxOnlineHostSize.height = MAX(maxOnlineHostSize.height, NSHeight(hostRect));
}
}
if(!NSEqualSizes(maxHostSize, NSZeroSize)) {
float scaleFactorV = ((NSHeight(bounds) - BOTTOM_MARGIN) - 4*HOST_MARGIN - LINE_THICKNESS)/(3*maxHostSize.height + NSHeight(localHostRect));
float scaleFactorH = (NSWidth(bounds) - 2*HOST_MARGIN)/(2*maxHostSize.width + NSWidth(localHostRect));
_scaleFactor = MIN(scaleFactorH, scaleFactorV);
}
else {
_scaleFactor = DEFAULT_SCALE;
}
//NSLog(@"scaleFactor: %f", _scaleFactor);
if(!NSEqualSizes(maxOnlineHostSize, NSZeroSize)) {
_layoutHeight = round(NSHeight(bounds) - (maxOnlineHostSize.height*_scaleFactor + 2*HOST_MARGIN));
}
else {
_layoutHeight = NSHeight(bounds);
}
}
- (void)_updateScroller
{
NSArray * hosts = [[self remoteHosts] hostsWithState:(TPHostSharedState|TPHostIncompatibleState)];
NSRect fullFrame = [self frame];
if([hosts count] == 0) {
if(_scroller != nil) {
[_scroller removeFromSuperview];
_scroller = nil;
}
}
else {
/* Calculate width */
float width = HOST_MARGIN;
NSEnumerator * hostsEnum = [hosts objectEnumerator];
TPRemoteHost * host;
while(host = [hostsEnum nextObject]) {
NSRect hostRect = [host fullHostRect];
width += (floor(hostRect.size.width*_scaleFactor) + HOST_MARGIN);
}
float proportion = NSWidth(fullFrame)/width;
if(proportion < 1.0) {
NSRect scrollerFrame = NSMakeRect(LINE_THICKNESS, _layoutHeight - [NSScroller scrollerWidth], NSWidth(fullFrame) - 2*LINE_THICKNESS, [NSScroller scrollerWidth]);
if(_scroller == nil) {
_scroller = [[NSScroller alloc] initWithFrame:scrollerFrame];
[self addSubview:_scroller];
[_scroller setTarget:self];
[_scroller setAction:@selector(scroll:)];
}
[_scroller setFloatValue:_scrollerDeltaX/(NSWidth(fullFrame) - width) knobProportion:proportion];
[_scroller setFrame:scrollerFrame];
[_scroller setEnabled:YES];
[_scroller setHidden:NO];
}
else {
[_scroller removeFromSuperview];
_scroller = nil;
}
}
}
- (void)_updateLayout
{
NSRect bounds = [self bounds];
// Local host
NSRect localHostRect = [_localHostView totalFrame];
localHostRect = TPScaledRect(localHostRect, _scaleFactor);
NSPoint origin = NSMakePoint(floor((NSWidth(bounds) - NSWidth(localHostRect))/2.0),
floor((_layoutHeight + BOTTOM_MARGIN - NSHeight(localHostRect))/2.0));
[_localHostView updateLayoutWithScaleFactor:_scaleFactor];
[_localHostView setFrameOrigin:origin];
// Remote hosts
NSEnumerator * hostViewsEnum = [_remoteHostsViews objectEnumerator];
TPLayoutRemoteHostView * hostView;
//float hostOrigin = _layoutHeight + HOST_MARGIN;
NSPoint sharedHostPoint = NSMakePoint(HOST_MARGIN+_scrollerDeltaX, _layoutHeight + HOST_MARGIN);
while((hostView = [hostViewsEnum nextObject])) {
TPRemoteHost * host = (TPRemoteHost*)[hostView host];
NSRect totalFrame = [hostView totalFrame];
NSRect frame;
TPSide side = TPUndefSide;
if([host isInState:TPHostPeeredState]) {
NSRect localScreenFrame = [_localHostView screenFrameAtIndex:0];
//NSLog(@"localHostView: %@, localScreenFrame: %@",localHostView,NSStringFromRect(localScreenFrame));
NSRect hostRect = [host hostRect];
frame = TPScaledRect(hostRect, _scaleFactor);
frame.origin.x += NSMinX(localScreenFrame);
frame.origin.y += NSMinY(localScreenFrame);
frame.size.width = floor(NSWidth(frame));
frame.size.height = floor(NSHeight(frame));
frame = [hostView hostFrameFromScreenFrame:frame atIndex:[host sharedScreenIndex]];
NSRect representedRect = [host adjustedHostRect];
NSRect parentRect = [[host localScreen] frame];
TPGluedRect(NULL, &side, parentRect, representedRect, TPUndefSide);
}
else {
frame = TPScaledRect(totalFrame, _scaleFactor);
frame.size.width = floor(NSWidth(frame));
frame.size.height = floor(NSHeight(frame));
frame.origin = sharedHostPoint;
sharedHostPoint.x += NSWidth(frame) + HOST_MARGIN;
}
double (*xFunc)(double) = NULL;
double (*yFunc)(double) = NULL;
if(side & TPRightSide) {
xFunc = floor;
}
else if(side & TPLeftSide) {
xFunc = ceil;
}
else {
xFunc = floor;
}
if(side & TPTopSide) {
yFunc = floor;
}
else if(side & TPBottomSide) {
yFunc = ceil;
}
else {
yFunc = floor;
}
frame.origin.x = xFunc(NSMinX(frame));
frame.origin.y = yFunc(NSMinY(frame));
[hostView updateLayoutWithScaleFactor:_scaleFactor];
[hostView setFrameOrigin:frame.origin];
}
[CATransaction begin];
[CATransaction setValue:@YES forKey:kCATransactionDisableActions];
// Separation line
NSArray * onlineHosts = [[self remoteHosts] hostsWithState:TPHostSharedState|TPHostIncompatibleState|TPHostPeeringState];
if(([onlineHosts count] > 0) && (_scroller == nil || [_scroller isHidden])) {
if(_separationLine == nil) {
_separationLine = [CALayer layer];
[[self layer] insertSublayer:_separationLine atIndex:0];
CGColorRef grayColor = CGColorCreateGenericGray(0.5, 1.0);
_separationLine.backgroundColor = grayColor;
CFRelease(grayColor);
}
_separationLine.frame = CGRectMake(LINE_MARGIN, _layoutHeight, NSWidth(bounds) - 2*LINE_MARGIN, LINE_THICKNESS);
}
else if(_separationLine != nil) {
[_separationLine removeFromSuperlayer];
_separationLine = nil;
}
NSArray *peeredHosts = [[self remoteHosts] hostsWithState:TPHostPeeredState|TPHostOnlineState];
if (peeredHosts.count == 0) {
infoLabel.stringValue = NSLocalizedString(@"No shared Mac found on local network. Please check \\U201CShare this Mac\\U201D on the Macs you want to control.", nil); }
else {
infoLabel.stringValue = NSLocalizedString(@"Arrange the shared Macs around your screen to control them. Configure options on each one.", nil);
}
[CATransaction commit];
}
- (void)updateLayout
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0];
[self _updateViews];
[self _updateScaleFactor];
[self _updateScroller];
[self _updateLayout];
[NSAnimationContext endGrouping];
//[self setNeedsDisplay:YES];
}
- (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize
{
[self updateLayout];
}
#pragma mark -
#pragma mark Events
- (void)_dragHostView:(TPLayoutRemoteHostView*)hostView withInitialEvent:(NSEvent*)initialEvent
{
NSPoint point = [initialEvent locationInWindow];
point = [hostView convertPoint:point fromView:nil];
unsigned draggingScreenIndex = [hostView indexOfScreenAtPoint:point];
if(draggingScreenIndex == -1)
return;
// put the view at the top
[hostView removeFromSuperview];
[self addSubview:hostView];
[hostView setDraggingScreenIndex:draggingScreenIndex];
TPRemoteHost * host = (TPRemoteHost*)[hostView host];
NSEvent * event = initialEvent;
//NSRect viewFrame = [hostView frame];
NSRect frame = [hostView screenFrameAtIndex:draggingScreenIndex];
NSRect snappedFrame;
BOOL snapped = NO;
unsigned nearestScreenIndex = 0;
float minDist;
NSRect minGluedRect = NSZeroRect;
TPSide minSide = TPUndefSide;
NSPoint hostPosition;
[hostView mouseExited:initialEvent];
TPHostPlacementIndicator * indicator = [[TPHostPlacementIndicator alloc] initWithHost:host];
#if DEBUG_SNAP
CALayer * debugSnapLayer = [CALayer layer];
debugSnapLayer.frame = NSRectToCGRect([hostView screenFrameAtIndex:draggingScreenIndex]);
debugSnapLayer.backgroundColor = CGColorCreateGenericRGB(1.0, 0.0, 0.0, 0.5);
debugSnapLayer.zPosition = 10.0;
[[self layer] addSublayer:debugSnapLayer];
#endif
while(1) {
frame.origin.x += [event deltaX];
frame.origin.y -= [event deltaY];
#if DEBUG_SNAP
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithBool:YES] forKey:kCATransactionDisableActions];
debugSnapLayer.frame = NSRectToCGRect(frame);
[CATransaction commit];
#endif
snappedFrame = frame;
#define ANIMATE_SNAP 0
#define DISTANCE_TO_SNAP 30
#define SQUARED_DISTANCE_TO_SNAP (DISTANCE_TO_SNAP*DISTANCE_TO_SNAP)
nearestScreenIndex = [_localHostView nearestScreenIndexForFrame:frame distance:&minDist position:&(minGluedRect.origin) side:&minSide];
hostPosition = minGluedRect.origin;
NSPoint localHostPosition = [_localHostView screenFrameAtIndex:0].origin;
hostPosition.x = (hostPosition.x - localHostPosition.x)/_scaleFactor;
hostPosition.y = (hostPosition.y - localHostPosition.y)/_scaleFactor;
//NSLog(@"hostPosition: %@", NSStringFromPoint(hostPosition));
hostPosition = [hostView adjustedScreenPositionFromPosition:hostPosition localScreenIndex:nearestScreenIndex side:minSide];
//NSLog(@"hostPosition2: %@", NSStringFromPoint(hostPosition));
NSRect hostRect = [host hostRect];
hostRect.origin = hostPosition;
BOOL animate = NO;
snappedFrame.origin = minGluedRect.origin;
[indicator setHostRect:hostRect localScreenIndex:nearestScreenIndex];
// DebugLog(@"minDist=%f", minDist);
if(minDist < SQUARED_DISTANCE_TO_SNAP) {
if(!snapped) {
[indicator show];
animate = ANIMATE_SNAP;
_snapTime = 0.0;
}
snapped = YES;
// hostPosition.x = (hostPosition.x/_scaleFactor - localHostPosition.x);
// hostPosition.y = (hostPosition.y/_scaleFactor - localHostPosition.y);
//[self snapToScreenIndex:nearestScreenIndex atPosition:hostPosition ofSide:minSide];
}
else {
if(snapped) {
[indicator close];
//animate = YES;
_snapTime = 0.0;
}
snapped = NO;
//[self unsnap];
}
NSTimeInterval duration = 0.0;
#if ANIMATE_SNAP
NSTimeInterval timeIntervalSinceReferenceDate = [NSDate timeIntervalSinceReferenceDate];
BOOL hasRunningAnimation = (_snapTime > timeIntervalSinceReferenceDate);
if(animate && !hasRunningAnimation) {
_snapTime = timeIntervalSinceReferenceDate + SNAP_DURATION;
}
duration = (_snapTime - timeIntervalSinceReferenceDate);
if(hasRunningAnimation) {
CGFloat percent = (SNAP_DURATION - duration)/SNAP_DURATION;
// if(!snapped)
// percent = 1.0 - percent;
NSRect intermediateFrame = frame;
intermediateFrame.origin.x += percent * (NSMinX(snappedFrame) - NSMinX(frame));
intermediateFrame.origin.y += percent * (NSMinY(snappedFrame) - NSMinY(frame));
[NSAnimationContext beginGrouping];
[hostView setFrame:intermediateFrame];
[NSAnimationContext endGrouping];
}
//NSLog(@"duration: %f", duration);
#endif
NSRect hostFrame = snapped ? snappedFrame : frame;
hostFrame = [hostView hostFrameFromScreenFrame:hostFrame atIndex:draggingScreenIndex];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:duration];
if(duration > 0.0)
[[hostView animator] setFrame:hostFrame];
else
[hostView setFrame:hostFrame];
[NSAnimationContext endGrouping];
event = [NSApp nextEventMatchingMask:NSLeftMouseUpMask|NSLeftMouseDraggedMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
if([event type] == NSLeftMouseUp) {
break;
}
}
#if DEBUG_SNAP
[debugSnapLayer removeFromSuperlayer];
#endif
[indicator close];
if(snapped) {
//[[TPHostAnimationController controller] showAppearanceAnimationForHost:host];
[hostView pairToScreenIndex:nearestScreenIndex atPosition:hostPosition ofSide:minSide];
}
else if([host isInState:TPHostPeeredState]) {
[hostView unpair];
}
else {
[self updateLayout];
}
[hostView setDraggingScreenIndex:-1];
}
- (void)mouseDown:(NSEvent*)event
{
NSPoint point = [[self superview] convertPoint:[event locationInWindow] fromView:nil];
NSView * view = [self hitTest:point];
if([view isKindOfClass:[TPLayoutRemoteScreenView class]]) {
TPLayoutRemoteHostView * remoteHostView = (TPLayoutRemoteHostView*)[(TPLayoutRemoteScreenView*)view hostView];
[self _dragHostView:remoteHostView withInitialEvent:event];
}
}
- (IBAction)scroll:(id)sender
{
NSRect fullFrame = [self bounds];
float value = [_scroller floatValue];
_scrollerDeltaX = round(- value*(NSWidth(fullFrame)/[_scroller knobProportion]-NSWidth(fullFrame)));
[self _updateLayout];
}
- (void)scrollWheel:(NSEvent *)theEvent
{
if(_scroller == nil) return;
NSRect fullFrame = [self bounds];
CGFloat deltaX = - _scrollerDeltaX - [theEvent deltaX];
_scrollerDeltaX = round(- MAX(0.0, MIN((NSWidth(fullFrame)/[_scroller knobProportion]-NSWidth(fullFrame)), deltaX)));
[self _updateScroller];
[self _updateLayout];
}
//+ (id)defaultAnimationForKey:(NSString *)key
//{
// return nil;
//}
//
//- (id)animationForKey:(NSString*)key
//{
// NSLog(@"KEY: %@", key);
// return [CABasicAnimation animation];
//}
#pragma mark -
#pragma mark Animation
//- (id<CAAction>)actionForKey:(NSString*)key
//{
// NSLog(@"pouet");
// return [CABasicAnimation animation];
//}
@end