Add Native.resetMainMenu and call it in global afterEach

This method removes any AtomMenuItems that aren't marked as 'global'. It ignores menu items that aren't instances of our custom subclass. This is needed by specs to clear any menu items added during tests. It will also be needed when a window loses focus and we want to remove any non-global menus associated with the window.
This commit is contained in:
Nathan Sobo 2011-12-23 12:43:32 -06:00
parent 09510f60a6
commit ae4fdf8812
8 changed files with 126 additions and 6 deletions

View File

@ -27,6 +27,7 @@
047F26021457978C006DC904 /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 047F26011457978C006DC904 /* JavaScriptCore.framework */; };
047F260414579792006DC904 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 047F260314579792006DC904 /* WebKit.framework */; };
047F260E145883B9006DC904 /* Icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 047F260D145883B9006DC904 /* Icon.icns */; };
ECBB172814A4F92400ACAAC1 /* AtomMenuItem.m in Sources */ = {isa = PBXBuildFile; fileRef = ECBB172714A4F92400ACAAC1 /* AtomMenuItem.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -69,6 +70,8 @@
047F26011457978C006DC904 /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
047F260314579792006DC904 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
047F260D145883B9006DC904 /* Icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Icon.icns; sourceTree = "<group>"; };
ECBB172614A4F92400ACAAC1 /* AtomMenuItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AtomMenuItem.h; path = Classes/AtomMenuItem.h; sourceTree = "<group>"; };
ECBB172714A4F92400ACAAC1 /* AtomMenuItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AtomMenuItem.m; path = Classes/AtomMenuItem.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -130,6 +133,8 @@
043D7E91145795B70078D710 /* AtomApp.m */,
043D7E92145795B70078D710 /* AtomController.h */,
043D7E93145795B70078D710 /* AtomController.m */,
ECBB172614A4F92400ACAAC1 /* AtomMenuItem.h */,
ECBB172714A4F92400ACAAC1 /* AtomMenuItem.m */,
043D7E6B145795B20078D710 /* JSCocoa */,
043D7E79145795B20078D710 /* UKKQueue */,
043D7E52145794990078D710 /* Supporting Files */,
@ -281,6 +286,7 @@
043D7E8D145795B20078D710 /* UKMainThreadProxy.m in Sources */,
043D7E94145795B70078D710 /* AtomApp.m in Sources */,
043D7E95145795B70078D710 /* AtomController.m in Sources */,
ECBB172814A4F92400ACAAC1 /* AtomMenuItem.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -8,5 +8,6 @@
- (void)removeController:(AtomController *)controller;
- (IBAction)runSpecs:(id)sender;
- (void)resetMainMenu;
@end

View File

@ -1,9 +1,11 @@
#import "AtomApp.h"
#import "AtomController.h"
#import "JSCocoa.h"
#import "JSCocoa.h"
#import <WebKit/WebKit.h>
#import "AtomController.h"
#import "AtomMenuItem.h"
#define ATOM_USER_PATH ([[NSString stringWithString:@"~/.atom/"] stringByStandardizingPath])
#define ATOM_STORAGE_PATH ([ATOM_USER_PATH stringByAppendingPathComponent:@".app-storage"])
@ -78,4 +80,24 @@
}
}
- (void)resetMenu:(NSMenu *)menu {
for (AtomMenuItem *item in menu.itemArray) {
if (![item isKindOfClass:[AtomMenuItem class]]) continue;
if (item.submenu) {
[self resetMenu:item.submenu];
if (item.submenu.numberOfItems == 0) {
[menu removeItem:item];
}
}
else if (!item.global) {
[menu removeItem:item];
}
}
}
- (void)resetMainMenu {
[self resetMenu:self.mainMenu];
}
@end

View File

@ -0,0 +1,7 @@
#import <AppKit/AppKit.h>
@interface AtomMenuItem : NSMenuItem
@property BOOL global;
@end

View File

@ -0,0 +1,7 @@
#import "AtomMenuItem.h"
@implementation AtomMenuItem
@synthesize global;
@end

View File

@ -2,10 +2,10 @@
<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1070</int>
<string key="IBDocument.SystemVersion">11C74</string>
<string key="IBDocument.SystemVersion">11B26</string>
<string key="IBDocument.InterfaceBuilderVersion">1938</string>
<string key="IBDocument.AppKitVersion">1138.23</string>
<string key="IBDocument.HIToolboxVersion">567.00</string>
<string key="IBDocument.AppKitVersion">1138</string>
<string key="IBDocument.HIToolboxVersion">566.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="NS.object.0">1938</string>
@ -258,15 +258,77 @@
<string key="-1.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="-2.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="-3.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="136.CustomClassName">AtomMenuItem</string>
<object class="NSMutableDictionary" key="136.IBAttributePlaceholdersKey">
<string key="NS.key.0">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<object class="IBUserDefinedRuntimeAttributesPlaceholder" key="NS.object.0">
<string key="name">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<reference key="object" ref="632727374"/>
<array key="userDefinedRuntimeAttributes">
<object class="IBUserDefinedRuntimeAttribute">
<string key="typeIdentifier">com.apple.InterfaceBuilder.userDefinedRuntimeAttributeType.boolean</string>
<string key="keyPath">global</string>
<boolean value="YES" key="value"/>
</object>
</array>
</object>
</object>
<string key="136.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="29.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="420.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="537.CustomClassName">AtomMenuItem</string>
<object class="NSMutableDictionary" key="537.IBAttributePlaceholdersKey">
<string key="NS.key.0">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<object class="IBUserDefinedRuntimeAttributesPlaceholder" key="NS.object.0">
<string key="name">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<reference key="object" ref="208934156"/>
<array key="userDefinedRuntimeAttributes">
<object class="IBUserDefinedRuntimeAttribute">
<string key="typeIdentifier">com.apple.InterfaceBuilder.userDefinedRuntimeAttributeType.boolean</string>
<string key="keyPath">global</string>
<boolean value="YES" key="value"/>
</object>
</array>
</object>
</object>
<string key="537.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="56.CustomClassName">AtomMenuItem</string>
<string key="56.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="57.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="72.CustomClassName">AtomMenuItem</string>
<object class="NSMutableDictionary" key="72.IBAttributePlaceholdersKey">
<string key="NS.key.0">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<object class="IBUserDefinedRuntimeAttributesPlaceholder" key="NS.object.0">
<string key="name">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<reference key="object" ref="722745758"/>
<array key="userDefinedRuntimeAttributes">
<object class="IBUserDefinedRuntimeAttribute">
<string key="typeIdentifier">com.apple.InterfaceBuilder.userDefinedRuntimeAttributeType.boolean</string>
<string key="keyPath">global</string>
<boolean value="YES" key="value"/>
</object>
</array>
</object>
</object>
<string key="72.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="81.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="82.CustomClassName">AtomMenuItem</string>
<object class="NSMutableDictionary" key="82.IBAttributePlaceholdersKey">
<string key="NS.key.0">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<object class="IBUserDefinedRuntimeAttributesPlaceholder" key="NS.object.0">
<string key="name">IBUserDefinedRuntimeAttributesPlaceholderName</string>
<reference key="object" ref="705341025"/>
<array key="userDefinedRuntimeAttributes">
<object class="IBUserDefinedRuntimeAttribute">
<string key="typeIdentifier">com.apple.InterfaceBuilder.userDefinedRuntimeAttributeType.boolean</string>
<string key="keyPath">global</string>
<boolean value="YES" key="value"/>
</object>
</array>
</object>
</object>
<string key="82.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="83.CustomClassName">AtomMenuItem</string>
<string key="83.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="unlocalizedProperties"/>
@ -296,6 +358,14 @@
<string key="minorKey">./Classes/AtomApp.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">AtomMenuItem</string>
<string key="superclassName">NSMenuItem</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/AtomMenuItem.h</string>
</object>
</object>
</array>
</object>
<int key="IBDocument.localizationMode">0</int>

View File

@ -1,10 +1,13 @@
$ = require 'jquery'
_ = require 'underscore'
Native = require 'native'
afterEach ->
(new Native).resetMainMenu()
window.atom = new (require 'app')
window.keydown = (pattern) ->
$(document).trigger @createKeyEvent pattern
window.createKeyEvent = (pattern) ->

View File

@ -39,3 +39,7 @@ class Native
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType
resetMainMenu: (menu) ->
OSX.NSApp.resetMainMenu