-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPRAlbumListViewCell.m
120 lines (101 loc) · 5.25 KB
/
PRAlbumListViewCell.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
#import "PRAlbumListViewCell.h"
#import "PRDb.h"
#import "PRLibrary.h"
#import "NSImage+Extensions.h"
#import "PRDefaults.h"
#import "NSParagraphStyle+Extensions.h"
@implementation PRAlbumListViewCell
- (void)drawInteriorWithFrame:(NSRect)theCellFrame inView:(NSView *)theControlView {
NSDictionary *dict = [self objectValue];
PRDb *db = [dict objectForKey:@"db"];
PRItemID *item = [dict objectForKey:@"file"];
NSImage *icon = [dict objectForKey:@"icon"];
if (!icon || ![icon isValid]) {
icon = [NSImage imageNamed:@"PRLightAlbumArt"];
}
NSString *artist = [[db library] artistValueForItem:item];
NSString *album = [[db library] valueForItem:item attr:PRItemAttrAlbum];
NSNumber *year = [[db library] valueForItem:item attr:PRItemAttrYear];
if ([[[db library] valueForItem:item attr:PRItemAttrCompilation] boolValue] && [[PRDefaults sharedDefaults] boolForKey:PRDefaultsUseCompilation]) {
artist = @"Compilation";
}
// Inset the cell frame to give everything a little horizontal padding
NSRect insetRect = NSInsetRect(theCellFrame, 10, 9);
NSSize iconSize = NSMakeSize(150, 150);
// Make attributes for our strings
NSDictionary *titleAttributes = @{NSFontAttributeName:[NSFont boldSystemFontOfSize:11],
NSParagraphStyleAttributeName:[NSParagraphStyle centerAlignStyle],
NSForegroundColorAttributeName:[NSColor colorWithCalibratedWhite:0.2 alpha:1.0]};
NSDictionary *subtitleAttributes = @{NSFontAttributeName:[NSFont systemFontOfSize:11],
NSParagraphStyleAttributeName:[NSParagraphStyle centerAlignStyle],
NSForegroundColorAttributeName:[NSColor colorWithCalibratedWhite:0.2 alpha:1.0]};
// Make a Title string
NSString *title = album;
NSString *subtitle = artist;
NSString *subSubtitle = [year stringValue];
if ([title isEqualToString:@""]) {
title = @"Unknown Album";
}
if ([subtitle isEqualToString:@""]) {
subtitle = @"Unknown Artist";
}
if ([subSubtitle isEqualToString:@"0"]) {
subSubtitle = @"";
}
// get the size of the string for layout
NSSize titleSize = [title sizeWithAttributes:titleAttributes];
NSSize subtitleSize = [subtitle sizeWithAttributes:subtitleAttributes];
NSSize subSubtitleSize = [subSubtitle sizeWithAttributes:subtitleAttributes];
// Vertical padding between the lines of text
// Horizontal padding between icon and text
float verticalPadding = 0.0;
float subVerticalPadding = 1.0;
// Icon box: center the icon vertically inside of the inset rect
NSRect iconBox = NSMakeRect(insetRect.origin.x, insetRect.origin.y, iconSize.width, iconSize.height);
// Make a box for our text
// Place it next to the icon with horizontal padding
// Size it horizontally to fill out the rest of the inset rect
// Center it vertically inside of the inset rect
float combinedHeight = titleSize.height + subtitleSize.height + subSubtitleSize.height + 2 * subVerticalPadding;
NSRect textBox = NSMakeRect(insetRect.origin.x, insetRect.origin.y + iconBox.size.height + verticalPadding,
iconSize.width, combinedHeight);
// Now split the text box in half and put the title box in the top half and subtitle box in bottom half
NSRect titleBox = NSMakeRect(textBox.origin.x, textBox.origin.y, textBox.size.width, titleSize.height);
NSRect subtitleBox = NSMakeRect(textBox.origin.x, textBox.origin.y + titleSize.height + subVerticalPadding,
textBox.size.width, subtitleSize.height);
NSImage *image = icon;
NSRect inRect = NSInsetRect(iconBox, 7, 7);
// create a destination rect scaled to fit inside the frame
NSRect drawnRect;
drawnRect.origin = inRect.origin;
if ([image size].width > [image size].height) {
drawnRect.size.height = [image size].height * inRect.size.width/[image size].width;
drawnRect.size.width = inRect.size.width;
} else {
drawnRect.size.width = [image size].width * inRect.size.height/[image size].height;
drawnRect.size.height = inRect.size.height;
}
// center it in the frame
drawnRect.origin.x += (inRect.size.width - drawnRect.size.width)/2;
drawnRect.origin.y += (inRect.size.height - drawnRect.size.height)/2;
drawnRect.origin.x = floor(drawnRect.origin.x);
drawnRect.origin.y = floor(drawnRect.origin.y);
drawnRect.size.width = floor(drawnRect.size.width);
drawnRect.size.height = floor(drawnRect.size.height);
// drawBorder
[NSGraphicsContext saveGraphicsState];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0.0, -2.0)];
[shadow setShadowBlurRadius:5];
[shadow setShadowColor:[NSColor colorWithDeviceWhite:0.0 alpha:0.4]];
[shadow set];
[image drawInRect:drawnRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:NO];
[NSGraphicsContext restoreGraphicsState];
// draw the text
[title drawInRect:titleBox withAttributes:titleAttributes];
[subtitle drawInRect:subtitleBox withAttributes:subtitleAttributes];
}
- (NSRect)expansionFrameWithFrame:(NSRect)cellFrame inView:(NSView *)view {
return NSZeroRect;
}
@end