forked from johndbritton/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPLayoutHostView.m
306 lines (240 loc) · 7.93 KB
/
TPLayoutHostView.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
//
// TPLayoutHost.m
// teleport
//
// Created by JuL on Sun Feb 29 2004.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPLayoutHostView.h"
#import "TPHost.h"
#import "TPHostsManager.h"
#define TEXT_MARGIN 6
@implementation NSBezierPath (TeleportAdditions)
+ (void)drawRect:(NSRect)rect withGradientFrom:(NSColor*)colorStart to:(NSColor*)colorEnd
{
float fraction = 0;
float height = rect.size.height - 1;
float width = rect.size.width;
float step = 1/height;
int i;
NSRect gradientRect = NSMakeRect(rect.origin.x, rect.origin.y, width, 1.0);
[colorEnd set];
[NSBezierPath fillRect:gradientRect];
for(i = 0; i < height; i++)
{
gradientRect.origin.y++;
NSColor * gradientColor = [colorStart blendedColorWithFraction:fraction ofColor:colorEnd];
[gradientColor set];
[NSBezierPath fillRect:gradientRect];
fraction += step;
}
}
@end
@implementation TPLayoutScreenView
- (instancetype) initWithHostView:(TPLayoutHostView*)hostView screenIndex:(unsigned)screenIndex
{
self = [super initWithFrame:NSZeroRect];
_hostView = hostView;
_screenIndex = screenIndex;
return self;
}
- (TPLayoutHostView*)hostView
{
return _hostView;
}
- (NSScreen*)screen
{
NSArray * screens = [[_hostView host] screens];
if(_screenIndex < [screens count]) {
return screens[_screenIndex];
}
else {
return nil;
}
}
- (unsigned)screenIndex
{
return _screenIndex;
}
- (BOOL)isMainScreen
{
return ([self screenIndex] == 0);
}
- (void)update
{
}
- (void)drawHostTitleInRect:(NSRect)rect dimmed:(BOOL)dimmed
{
if(NSWidth(rect) < 1.0 || NSHeight(rect) < 1.0)
return;
NSImage * image = [[NSImage alloc] initWithSize:rect.size];
[image setFlipped:YES];
[image lockFocus];
/* Create attributed string */
NSColor * fontColor;
if(dimmed)
fontColor = [[NSColor whiteColor] colorWithAlphaComponent:0.5];
else
fontColor = [NSColor whiteColor];
NSShadow * shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:3.0];
[shadow setShadowColor:[NSColor blackColor]];
[shadow setShadowOffset:NSMakeSize(1.0,-1.0)];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSCenterTextAlignment];
// [paragraphStyle setLineSpacing:-20.0];
NSDictionary * attributes = @{NSFontAttributeName: [NSFont boldSystemFontOfSize:[NSFont systemFontSize]],
NSShadowAttributeName: shadow,
NSForegroundColorAttributeName: fontColor,
NSParagraphStyleAttributeName: paragraphStyle};
NSAttributedString * attString = [[NSAttributedString alloc] initWithString:[[_hostView host] computerName] attributes:attributes];
/* Layout */
NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer * textContainer = [[NSTextContainer alloc] initWithContainerSize:rect.size];
NSTextStorage * textStorage = [[NSTextStorage alloc] initWithAttributedString:attString];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
NSRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:NSMakePoint(1.0, roundf((NSHeight(rect) - NSHeight(usedRect))/2.0) - 4.0)];
[image unlockFocus];
[[NSGraphicsContext currentContext] saveGraphicsState];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.5]];
[shadow set];
[image drawAtPoint:rect.origin fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
@end
@implementation TPLayoutHostView
+ (Class)screenViewClass
{
return [TPLayoutScreenView class];
}
+ (id)defaultAnimationForKey:(NSString *)key
{
if([key isEqualToString:NSAnimationTriggerOrderIn] || [key isEqualToString:NSAnimationTriggerOrderOut]) {
return nil;
}
else if([key isEqualToString:@"frameOrigin"]) {
CAAnimation * animation = [CABasicAnimation animation];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return animation;
}
else {
return [super defaultAnimationForKey:key];
}
}
- (instancetype) initWithHost:(TPHost*)host layoutView:(TPLayoutView*)layoutView
{
self = [super initWithFrame:NSZeroRect];
_hostIdentifier = [[host identifier] copy];
_layoutView = layoutView;
NSArray * screens = [host screens];
NSEnumerator * screensEnum = [screens objectEnumerator];
TPScreen * screen;
Class screenViewClass = [[self class] screenViewClass];
unsigned screenIndex = 0;
while((screen = [screensEnum nextObject])) {
NSView * screenView = [(TPLayoutScreenView*)[screenViewClass alloc] initWithHostView:self screenIndex:screenIndex];
[self addSubview:screenView];
screenIndex++;
}
NSShadow * shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:2.0];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.5]];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[self setShadow:shadow];
return self;
}
- (void)setHostIdentifier:(NSString*)hostIdentifier
{
if(hostIdentifier != _hostIdentifier) {
_hostIdentifier = hostIdentifier;
}
}
- (NSString*)hostIdentifier
{
return _hostIdentifier;
}
- (TPHost*)host
{
return [[TPHostsManager defaultManager] hostWithIdentifier:_hostIdentifier];
}
- (NSRect)totalFrame
{
NSArray * screens = [[self host] screens];
NSRect enclosingRect = NSZeroRect;
NSEnumerator * screensEnum = [screens objectEnumerator];
TPScreen * screen;
while((screen = [screensEnum nextObject])) {
NSRect screenRect = [screen frame];
enclosingRect = NSUnionRect(enclosingRect, screenRect);
}
return enclosingRect;
}
- (NSArray*)screenViews
{
return [self subviews];
}
- (unsigned)indexOfScreenAtPoint:(NSPoint)point
{
NSEnumerator * screenViewsEnum = [[self screenViews] objectEnumerator];
TPLayoutScreenView * screenView;
while((screenView = [screenViewsEnum nextObject]) != nil) {
if(NSPointInRect(point, [screenView frame])) {
return [screenView screenIndex];
}
}
return -1;
}
- (TPLayoutScreenView*)screenViewAtIndex:(unsigned)index
{
NSScreen * screen = [[self host] screens][index];
NSEnumerator * screenViewsEnum = [[self screenViews] objectEnumerator];
TPLayoutScreenView * screenView;
while((screenView = [screenViewsEnum nextObject]) != nil) {
if([[screenView screen] isEqual:screen]) {
return screenView;
}
}
return nil;
}
- (NSRect)screenFrameAtIndex:(unsigned)index
{
TPLayoutScreenView * screenView = [self screenViewAtIndex:index];
NSRect frame = [screenView frame];
frame = [self convertRect:frame toView:[self superview]];
return frame;
}
- (NSRect)hostFrameFromScreenFrame:(NSRect)frame atIndex:(unsigned)index
{
TPLayoutScreenView * screenView = [self screenViewAtIndex:index];
NSRect hostFrame = [self frame];
NSRect screenFrame = [screenView frame];
hostFrame.origin.x = NSMinX(frame) - NSMinX(screenFrame);
hostFrame.origin.y = NSMinY(frame) - NSMinY(screenFrame);
return hostFrame;
}
- (void)updateLayoutWithScaleFactor:(float)scaleFactor
{
NSRect totalFrame = [self totalFrame];
NSRect scaledTotalFrame = TPScaledRect(totalFrame, scaleFactor);
[self setFrameSize:scaledTotalFrame.size];
[self layer].masksToBounds = NO;
NSArray * screenViews = [self screenViews];
NSEnumerator * screensEnum = [screenViews objectEnumerator];
TPLayoutScreenView * screenView;
while((screenView = [screensEnum nextObject])) {
NSScreen * screen = [screenView screen];
NSRect screenFrame = [screen frame];
NSRect scaledScreenFrame = TPScaledRect(screenFrame, scaleFactor);
scaledScreenFrame.origin.x -= NSMinX(scaledTotalFrame);
scaledScreenFrame.origin.y -= NSMinY(scaledTotalFrame);
scaledScreenFrame.origin.x = floor(NSMinX(scaledScreenFrame));
scaledScreenFrame.origin.y = floor(NSMinY(scaledScreenFrame));
scaledScreenFrame.size.width = floor(NSWidth(scaledScreenFrame));
scaledScreenFrame.size.height = floor(NSHeight(scaledScreenFrame));
[screenView setFrame:scaledScreenFrame];
}
}
@end