Permalink
Browse files

Extract Bundle menu to a framework

  • Loading branch information...
1 parent 0cedb7f commit b0018c209980d57406321f406902035467294585 @elia elia committed with sorbits Aug 12, 2012
@@ -12,83 +12,10 @@
#import <OakFoundation/OakFoundation.h>
#import <OakFoundation/NSString Additions.h>
#import <oak/debug.h>
+#import <BundleMenu/BundleMenuDelegate.h>
OAK_DEBUG_VAR(AppController_Menus);
-@interface BundleMenuDelegate : NSObject <NSMenuDelegate>
-{
- OBJC_WATCH_LEAKS(BundleMenuDelegate);
- bundles::item_ptr umbrellaItem;
-}
-@end
-
-@implementation BundleMenuDelegate
-- (id)initWithBundleItem:(bundles::item_ptr const&)aBundleItem
-{
- if(self = [super init])
- umbrellaItem = aBundleItem;
- return self;
-}
-
-- (BOOL)menuHasKeyEquivalent:(NSMenu*)aMenu forEvent:(NSEvent*)theEvent target:(id*)aTarget action:(SEL*)anAction
-{
- return NO;
-}
-
-- (void)menuNeedsUpdate:(NSMenu*)aMenu
-{
- D(DBF_AppController_Menus, bug("\n"););
- [aMenu removeAllItems];
-
- citerate(item, umbrellaItem->menu())
- {
- switch((*item)->kind())
- {
- case bundles::kItemTypeMenu:
- {
- NSMenuItem* menuItem = [aMenu addItemWithTitle:[NSString stringWithCxxString:(*item)->name()] action:NULL keyEquivalent:@""];
-
- menuItem.submenu = [[NSMenu new] autorelease];
- menuItem.submenu.autoenablesItems = NO;
- BundleMenuDelegate* delegate = [[BundleMenuDelegate alloc] initWithBundleItem:*item];
- menuItem.submenu.delegate = delegate;
- }
- break;
-
- case bundles::kItemTypeMenuItemSeparator:
- [aMenu addItem:[NSMenuItem separatorItem]];
- break;
-
- default:
- {
- NSMenuItem* menuItem = [aMenu addItemWithTitle:[NSString stringWithCxxString:(*item)->name()] action:@selector(doBundleItem:) keyEquivalent:@""];
- [menuItem setKeyEquivalentCxxString:(*item)->value_for_field(bundles::kFieldKeyEquivalent)];
- [menuItem setTabTriggerCxxString:(*item)->value_for_field(bundles::kFieldTabTrigger)];
- [menuItem setRepresentedObject:[NSString stringWithCxxString:(*item)->uuid()]];
- }
- break;
- }
- }
-}
-
-- (void)menuWillOpen:(NSMenu*)aMenu
-{
- [aMenu enableTabTriggers];
-}
-
-- (void)menuDidClose:(NSMenu*)aMenu
-{
- // We are not allowed to modify ‘aMenu’ here so we do it “afterDelay” — I really wish we didn’t have to do this at all…
- [self performSelector:@selector(zapMenu:) withObject:aMenu afterDelay:0.0];
-}
-
-- (void)zapMenu:(NSMenu*)aMenu
-{
- // After a menu has been up, the system will cache all its key equivalents. Even if we set all the key equivalents to the empty string, the system will still remember. The only workaround seems to be to delete all the entries in the menu.
- [aMenu removeAllItems];
-}
-@end
-
@interface NSObject (BundleMenuDelegate)
- (BOOL)canHandleMenuKeyEquivalent:(NSEvent*)anEvent;
- (void)handleMenuKeyEquivalent:(id)sender;
@@ -4,7 +4,7 @@ CP_SharedSupport = support/*
CP_PlugIns = @Dialog @Dialog2
FLAGS += -DREST_API='@"$rest_api"'
LINK += bundles cf command document editor io network ns plist settings text
-LINK += BundleEditor BundlesManager CrashReporter DocumentWindow Find HTMLOutputWindow OakAppKit OakFilterList OakFoundation OakSystem OakTextView Preferences SoftwareUpdate
+LINK += BundleMenu BundleEditor BundlesManager CrashReporter DocumentWindow Find HTMLOutputWindow OakAppKit OakFilterList OakFoundation OakSystem OakTextView Preferences SoftwareUpdate
FRAMEWORKS = Cocoa
HTML_HEADER = templates/header.html
HTML_FOOTER = templates/footer.html
@@ -0,0 +1,10 @@
+#import <bundles/bundles.h>
+#import <oak/debug.h>
+
+@interface BundleMenuDelegate : NSObject <NSMenuDelegate>
+{
+ OBJC_WATCH_LEAKS(BundleMenuDelegate);
+ bundles::item_ptr umbrellaItem;
+}
+- (id)initWithBundleItem:(bundles::item_ptr const&)aBundleItem;
+@end
@@ -0,0 +1,74 @@
+#import "BundleMenuDelegate.h"
+#import <OakAppKit/NSMenu Additions.h>
+#import <OakAppKit/NSMenuItem Additions.h>
+#import <OakFoundation/NSString Additions.h>
+#import <oak/debug.h>
+
+OAK_DEBUG_VAR(BundleMenu);
+
+@implementation BundleMenuDelegate
+- (id)initWithBundleItem:(bundles::item_ptr const&)aBundleItem
+{
+ if(self = [super init])
+ umbrellaItem = aBundleItem;
+ return self;
+}
+
+- (BOOL)menuHasKeyEquivalent:(NSMenu*)aMenu forEvent:(NSEvent*)theEvent target:(id*)aTarget action:(SEL*)anAction
+{
+ return NO;
+}
+
+- (void)menuNeedsUpdate:(NSMenu*)aMenu
+{
+ D(DBF_BundleMenu, bug("\n"););
+ [aMenu removeAllItems];
+
+ citerate(item, umbrellaItem->menu())
+ {
+ switch((*item)->kind())
+ {
+ case bundles::kItemTypeMenu:
+ {
+ NSMenuItem* menuItem = [aMenu addItemWithTitle:[NSString stringWithCxxString:(*item)->name()] action:NULL keyEquivalent:@""];
+
+ menuItem.submenu = [[NSMenu new] autorelease];
+ menuItem.submenu.autoenablesItems = NO;
+ BundleMenuDelegate* delegate = [[BundleMenuDelegate alloc] initWithBundleItem:*item];
+ menuItem.submenu.delegate = delegate;
+ }
+ break;
+
+ case bundles::kItemTypeMenuItemSeparator:
+ [aMenu addItem:[NSMenuItem separatorItem]];
+ break;
+
+ default:
+ {
+ NSMenuItem* menuItem = [aMenu addItemWithTitle:[NSString stringWithCxxString:(*item)->name()] action:@selector(doBundleItem:) keyEquivalent:@""];
+ [menuItem setKeyEquivalentCxxString:(*item)->value_for_field(bundles::kFieldKeyEquivalent)];
+ [menuItem setTabTriggerCxxString:(*item)->value_for_field(bundles::kFieldTabTrigger)];
+ [menuItem setRepresentedObject:[NSString stringWithCxxString:(*item)->uuid()]];
+ }
+ break;
+ }
+ }
+}
+
+- (void)menuWillOpen:(NSMenu*)aMenu
+{
+ [aMenu enableTabTriggers];
+}
+
+- (void)menuDidClose:(NSMenu*)aMenu
+{
+ // We are not allowed to modify ‘aMenu’ here so we do it “afterDelay” — I really wish we didn’t have to do this at all…
+ [self performSelector:@selector(zapMenu:) withObject:aMenu afterDelay:0.0];
+}
+
+- (void)zapMenu:(NSMenu*)aMenu
+{
+ // After a menu has been up, the system will cache all its key equivalents. Even if we set all the key equivalents to the empty string, the system will still remember. The only workaround seems to be to delete all the entries in the menu.
+ [aMenu removeAllItems];
+}
+@end
@@ -0,0 +1,4 @@
+SOURCES = src/*.mm
+LINK += OakAppKit OakFoundation bundles
+EXPORT = src/BundleMenuDelegate.h
+FRAMEWORKS = AppKit

0 comments on commit b0018c2

Please sign in to comment.