From 04e8eb44a22d9cd2e9f2e72a1e5ac80e75c198ae Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sat, 16 Mar 2013 17:49:48 +0100 Subject: [PATCH] Introduce updateTitle: for NSMenuItem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../OakAppKit/src/NSMenuItem Additions.h | 1 + .../OakAppKit/src/NSMenuItem Additions.mm | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Frameworks/OakAppKit/src/NSMenuItem Additions.h b/Frameworks/OakAppKit/src/NSMenuItem Additions.h index eb5ae5b8..1f9736ff 100644 --- a/Frameworks/OakAppKit/src/NSMenuItem Additions.h +++ b/Frameworks/OakAppKit/src/NSMenuItem Additions.h @@ -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; diff --git a/Frameworks/OakAppKit/src/NSMenuItem Additions.mm b/Frameworks/OakAppKit/src/NSMenuItem Additions.mm index a6f6dda7..9cb81254 100644 --- a/Frameworks/OakAppKit/src/NSMenuItem Additions.mm +++ b/Frameworks/OakAppKit/src/NSMenuItem Additions.mm @@ -4,7 +4,7 @@ #import #import #import -#import +#import @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;