This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenLayer.m
69 lines (54 loc) · 2.51 KB
/
ScreenLayer.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
//
// ScreenLayer.m
// Synergistic
//
// Created by siteworx on 9/24/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "ScreenLayer.h"
#import "NSImage-Extras.h"
#import "Screen.h"
#define IMAGE_WIDTH 32
#define IMAGE_HEIGHT 32
#define TEXT_OFFSET 20
@implementation ScreenLayer
- (id) initWithScreen: (Screen *) screen
{
if (self = [super init])
{
backingScreen = screen;
// get the image to display
NSString *imagePath = [[NSBundle mainBundle] pathForImageResource: @"screen"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile: imagePath];
CGImageRef screenImage = CGImageRetain([image cgImage]);
[image release];
// initialize thyself
self.layoutManager = [CAConstraintLayoutManager layoutManager];
self.name = screen.name;
self.selected = NO;
CALayer *imagelayer = [[CALayer layer] retain];
imagelayer.name = @"image";
imagelayer.bounds = CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
imagelayer.contents = (id) screenImage;
CATextLayer *text = [[CATextLayer layer] retain];
text.name = @"name";
text.string = screen.name;
text.font = @"LucidaGrande-Bold";
text.fontSize = 11.0;
text.foregroundColor = CGColorCreateGenericGray(0.8, 0.9);
text.alignmentMode = kCAAlignmentCenter;
// height of the layer is height of the img + 16 + height of text (auto)
float width = 14.0 + ((text.bounds.size.width > IMAGE_WIDTH) ? text.bounds.size.width : IMAGE_WIDTH);
float height = 14.0 + IMAGE_HEIGHT + TEXT_OFFSET + text.bounds.size.height;
float size = width > height ? width : height;
self.bounds = CGRectMake(0, 0, size, size);
[imagelayer addConstraint: [CAConstraint constraintWithAttribute: kCAConstraintMidX relativeTo: @"superlayer" attribute: kCAConstraintMidX]];
[imagelayer addConstraint: [CAConstraint constraintWithAttribute: kCAConstraintMaxY relativeTo: @"superlayer" attribute: kCAConstraintMaxY offset: -7.0]];
[text addConstraint: [CAConstraint constraintWithAttribute: kCAConstraintMidX relativeTo: @"superlayer" attribute: kCAConstraintMidX]];
[text addConstraint: [CAConstraint constraintWithAttribute: kCAConstraintMinY relativeTo: @"superlayer" attribute: kCAConstraintMinY offset: 7.0]];
[self addSublayer: imagelayer];
[self addSublayer: text];
}
return self;
}
@end