Introduce updateTitle: for NSMenuItem

Since we create an attributed title when the menu item has a tab trigger or “inactive” key equivalent, we can’t later update the title via the title property.
This commit is contained in:
Allan Odgaard
2013-03-16 17:49:48 +01:00
parent 8313dcb407
commit 04e8eb44a2
2 changed files with 19 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
@interface NSMenuItem (FileIcon)
- (void)updateTitle:(NSString*)newTitle;
- (void)setIconForFile:(NSString*)path;
- (void)setKeyEquivalentCxxString:(std::string const&)aKeyEquivalent;
- (void)setInactiveKeyEquivalentCxxString:(std::string const&)aKeyEquivalent;

View File

@@ -4,7 +4,7 @@
#import <OakFoundation/NSString Additions.h>
#import <text/case.h>
#import <text/utf8.h>
#import <ns/event.h>
#import <ns/ns.h>
@interface MenuMutableAttributedString : NSMutableAttributedString
{
@@ -103,6 +103,9 @@
@end
static char const* kOakMenuItemKeyEquivalent = "OakMenuItemKeyEquivalent";
static char const* kOakMenuItemTabTrigger = "OakMenuItemTabTrigger";
@implementation NSMenuItem (FileIcon)
- (void)setIconForFile:(NSString*)path;
{
@@ -121,6 +124,18 @@
}
}
- (void)updateTitle:(NSString*)newTitle
{
if([self.title isEqualToString:newTitle])
return;
self.title = newTitle;
if(NSString* keyEquivalent = objc_getAssociatedObject(self, kOakMenuItemKeyEquivalent))
[self setInactiveKeyEquivalentCxxString:to_s(keyEquivalent)];
if(NSString* tabTrigger = objc_getAssociatedObject(self, kOakMenuItemTabTrigger))
[self setTabTriggerCxxString:to_s(tabTrigger)];
}
- (void)setKeyEquivalentCxxString:(std::string const&)aKeyEquivalent
{
if(aKeyEquivalent == NULL_STR || aKeyEquivalent.empty())
@@ -154,6 +169,7 @@
- (void)setInactiveKeyEquivalentCxxString:(std::string const&)aKeyEquivalent
{
objc_setAssociatedObject(self, kOakMenuItemKeyEquivalent, [NSString stringWithCxxString:aKeyEquivalent], OBJC_ASSOCIATION_RETAIN);
if(aKeyEquivalent == NULL_STR || aKeyEquivalent.empty())
return;
@@ -174,6 +190,7 @@
- (void)setTabTriggerCxxString:(std::string const&)aTabTrigger
{
objc_setAssociatedObject(self, kOakMenuItemTabTrigger, [NSString stringWithCxxString:aTabTrigger], OBJC_ASSOCIATION_RETAIN);
if(aTabTrigger == NULL_STR)
return;