/
BundleItemMenuItem.mm
104 lines (85 loc) · 4.19 KB
/
BundleItemMenuItem.mm
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
#import "BundleItemMenuItem.h"
#import <OakFoundation/NSString Additions.h>
#import <ns/ns.h>
@interface BundleItemMenuItem ()
- (BundleItemMenuItem*)initWithBundleItem:(bundles::item_ptr const&)bundleItem alignmentData:(BundleItemMenuItemAlignment&)alignment;
- (void)setAttributedTitleWithTitle:(NSAttributedString*)itemTitle equivLeft:(NSAttributedString*)equivLeft equivRight:(NSAttributedString*)equivRight alignmentData:(BundleItemMenuItemAlignment&)alignment;
@end
@implementation BundleItemMenuItem
+ (BundleItemMenuItem*)menuItemWithBundleItem:(bundles::item_ptr const&)bundleItem alignmentData:(BundleItemMenuItemAlignment&)alignment
{
return [[[self alloc] initWithBundleItem:bundleItem alignmentData:alignment] autorelease];
}
- (BundleItemMenuItem*)initWithBundleItem:(bundles::item_ptr const&)bundleItem alignmentData:(BundleItemMenuItemAlignment&)alignment
{
if ((self = [super init]))
{
NSDictionary* fontAttrs = @{ NSFontAttributeName : [NSFont menuFontOfSize:14] /* passing 0 should return the default size, but it doesn’t */ };
NSDictionary* smallFontAttrs = @{ NSFontAttributeName : [NSFont menuFontOfSize:11] };
NSAttributedString* title = [[NSAttributedString alloc] initWithString:[NSString stringWithCxxString:bundleItem->name()] attributes:fontAttrs];
NSAttributedString* equivLeft = nil;
NSAttributedString* equivRight = nil;
std::string const tabTrigger(bundleItem->value_for_field(bundles::kFieldTabTrigger));
std::string const keyEquiv(bundleItem->value_for_field(bundles::kFieldKeyEquivalent));
if(tabTrigger != NULL_STR)
{
equivLeft = [[[NSAttributedString alloc] initWithString:[NSString stringWithCxxString:(" "+tabTrigger+"\u21E5 ")] attributes:smallFontAttrs] autorelease];
}
else if(keyEquiv != NULL_STR)
{
size_t keyStart = 0;
std::string const glyphStr(ns::glyphs_for_event_string(keyEquiv, &keyStart));
equivLeft = [[[NSAttributedString alloc] initWithString:[NSString stringWithCxxString:glyphStr.substr(0, keyStart)] attributes:fontAttrs] autorelease];
equivRight = [[[NSAttributedString alloc] initWithString:[NSString stringWithCxxString:glyphStr.substr(keyStart)] attributes:fontAttrs] autorelease];
}
[self setAttributedTitleWithTitle:title equivLeft:equivLeft equivRight:equivRight alignmentData:alignment];
}
return self;
}
- (void)setAttributedTitleWithTitle:(NSAttributedString*)itemTitle equivLeft:(NSAttributedString*)equivLeft equivRight:(NSAttributedString*)equivRight alignmentData:(BundleItemMenuItemAlignment&)alignment
{
NSMutableAttributedString* title = [[[NSMutableAttributedString alloc] initWithAttributedString:itemTitle] autorelease];
[title beginEditing];
CGFloat alignmentWidth = [itemTitle size].width;
CGFloat rightWidth = [equivRight size].width;
CGFloat leftWidth = [equivLeft size].width;
if(equivLeft)
{
[title appendAttributedString:[[[NSAttributedString alloc] initWithString:@"\t"] autorelease]];
[title appendAttributedString:equivLeft];
alignmentWidth += leftWidth + 20;
if(equivRight)
{
[title appendAttributedString:[[[NSAttributedString alloc] initWithString:@"\t"] autorelease]];
[title appendAttributedString:equivRight];
hasRightPart = YES;
}
}
if(alignmentWidth > alignment.maxAlignmentWidth)
alignment.maxAlignmentWidth = alignmentWidth;
if(rightWidth > alignment.maxRightWidth)
alignment.maxRightWidth = rightWidth;
[title endEditing];
[self setAttributedTitle:title];
}
- (void)updateAlignment:(BundleItemMenuItemAlignment&)alignment
{
NSMutableParagraphStyle* pStyle = [[NSMutableParagraphStyle new] autorelease];
if(hasRightPart)
{
[pStyle setTabStops:@[
[[[NSTextTab alloc] initWithType:NSRightTabStopType location:alignment.maxAlignmentWidth] autorelease],
[[[NSTextTab alloc] initWithType:NSLeftTabStopType location:alignment.maxAlignmentWidth + 0.01] autorelease]
]];
}
else
{
[pStyle setTabStops:@[
[[[NSTextTab alloc] initWithType:NSRightTabStopType location:alignment.maxAlignmentWidth + alignment.maxRightWidth] autorelease]
]];
}
NSMutableAttributedString* title = [[self attributedTitle] mutableCopy];
[title addAttribute:NSParagraphStyleAttributeName value:pStyle range:NSMakeRange(0, [title length])];
[self setAttributedTitle:title];
}
@end