forked from KosmicTask/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMLErrorPopOver.m
68 lines (51 loc) · 2.09 KB
/
SMLErrorPopOver.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
//
// SMLErrorPopOver.m
// Fragaria
//
// Created by Viktor Lidholt on 4/11/13.
//
//
#import "SMLErrorPopOver.h"
#define kSMLErrorPopOverErrorSpacing 16
@implementation SMLErrorPopOver
+ (CGFloat) widthOfString:(NSString *)string withFont:(NSFont *)font {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
}
+ (void) showErrorDescriptions:(NSArray*)errors relativeToView:(NSView*) view
{
// Add labels for each error
int errNo = 0;
int maxWidth = 0;
int numErrors = (int)errors.count;
int viewHeight = kSMLErrorPopOverErrorSpacing * numErrors;
if (!numErrors) return;
// Create view controller
NSViewController* vc = [[NSViewController alloc] initWithNibName:@"ErrorPopoverView" bundle:[NSBundle bundleForClass:[self class]]];
// Create labels and add them to the view
for (NSString* err in errors)
{
NSTextField *textField;
NSFont* font = [NSFont systemFontOfSize:10];
int width = [self widthOfString:err withFont:font];
if (width > maxWidth) maxWidth = width;
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, viewHeight - (kSMLErrorPopOverErrorSpacing * (errNo + 1)), 1024, kSMLErrorPopOverErrorSpacing)];
[textField setStringValue:err.description];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setFont:font];
[vc.view addSubview:textField];
errNo++;
}
[vc.view setFrameSize:NSMakeSize(maxWidth, viewHeight)];
// Open the popover
NSPopover* popover = [[NSPopover alloc] init];
popover.behavior = NSPopoverBehaviorTransient;
popover.contentSize = vc.view.bounds.size;
popover.contentViewController = vc;
popover.animates = YES;
[popover showRelativeToRect:[view bounds] ofView:view preferredEdge:NSMinYEdge];
}
@end