File tree Expand file tree Collapse file tree 10 files changed +82
-17
lines changed Expand file tree Collapse file tree 10 files changed +82
-17
lines changed Original file line number Diff line number Diff line change 17
17
NSTextInput
18
18
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_14
19
19
, NSFontChanging
20
+ , NSMenuItemValidation
20
21
#endif
21
22
>
22
23
{
62
63
//
63
64
- (void )changeFont : (id )sender ;
64
65
66
+ //
67
+ // NSMenuItemValidation
68
+ //
69
+ - (BOOL )validateMenuItem : (NSMenuItem *)item ;
70
+
71
+ //
72
+ // Public macaction's
73
+ // Note: New items here need to be handled in validateMenuItem: as well.
74
+ //
75
+ - (IBAction )cut : (id )sender ;
76
+ - (IBAction )copy : (id )sender ;
77
+ - (IBAction )paste : (id )sender ;
78
+ - (IBAction )undo : (id )sender ;
79
+ - (IBAction )redo : (id )sender ;
80
+ - (IBAction )selectAll : (id )sender ;
81
+
65
82
//
66
83
// MMTextStorage methods
67
84
//
Original file line number Diff line number Diff line change @@ -1140,6 +1140,23 @@ - (void)changeFont:(id)sender
1140
1140
}
1141
1141
}
1142
1142
1143
+ // / Specifies whether the menu item should be enabled/disabled.
1144
+ - (BOOL )validateMenuItem : (NSMenuItem *)item
1145
+ {
1146
+ if ([item action ] == @selector (cut: )
1147
+ || [item action ] == @selector (copy: )
1148
+ || [item action ] == @selector (paste: )
1149
+ || [item action ] == @selector (undo: )
1150
+ || [item action ] == @selector (redo: )
1151
+ || [item action ] == @selector (selectAll: ))
1152
+ return [item tag ];
1153
+
1154
+ // This class should not have any special macOS menu itmes, so theoretically
1155
+ // all of them should just return item.tag, but just in case macOS decides
1156
+ // to inject some menu items to the parent NSView class without us knowing,
1157
+ // we let the superclass handle this.
1158
+ return YES ;
1159
+ }
1143
1160
1144
1161
//
1145
1162
// NOTE: The menu items cut/copy/paste/undo/redo/select all/... must be bound
Original file line number Diff line number Diff line change 47
47
- (BOOL )canBecomeMainWindow ;
48
48
49
49
- (void )applicationDidChangeScreenParameters : (NSNotification *)notification ;
50
+
51
+ // Public macaction's.
52
+ // Note: New items here need to be handled in validateMenuItem: as well.
53
+ - (void )performClose : (id )sender ;
54
+
50
55
@end
Original file line number Diff line number Diff line change @@ -444,13 +444,16 @@ - (void)performClose:(id)sender
444
444
[super performClose: sender];
445
445
}
446
446
447
+ // / Validates whether the menu item should be enabled or not.
447
448
- (BOOL )validateMenuItem : (NSMenuItem *)item
448
449
{
449
- if ([item action ] == @selector ( vimMenuItemAction: )
450
- || [item action ] == @selector (performClose: ))
450
+ // This class only really have one action that's bound from Vim
451
+ if ( [item action ] == @selector (performClose: ))
451
452
return [item tag ];
452
453
453
- return YES ;
454
+ // Since this is a subclass of NSWindow, it has a bunch of auto-populated
455
+ // menu from the OS. Just pass it off to the superclass to let it handle it.
456
+ return [super validateMenuItem: item];
454
457
}
455
458
456
459
@end // MMFullScreenWindow
Original file line number Diff line number Diff line change @@ -906,7 +906,7 @@ - (BOOL)validateMenuItem:(NSMenuItem *)item
906
906
|| [item action ] == @selector (selectAll: ))
907
907
return [item tag ];
908
908
909
- return YES ;
909
+ return [ super validateMenuItem: item] ;
910
910
}
911
911
912
912
@end // MMTextView
Original file line number Diff line number Diff line change @@ -1447,10 +1447,15 @@ - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on
1447
1447
return ;
1448
1448
}
1449
1449
1450
- // Use tag to set whether item is enabled or disabled instead of
1451
- // calling setEnabled:. This way the menus can autoenable themselves
1452
- // but at the same time Vim can set if a menu is enabled whenever it
1453
- // wants to.
1450
+ // We are using auto-enabling of menu items, where instead of directly
1451
+ // calling setEnabled:, we rely on validateMenuItem: callbacks in each
1452
+ // target to handle whether they want each menu item to be enabled or not.
1453
+ // This allows us to more easily control the enabled states of OS-injected
1454
+ // menu items if we want to. To remember whether we want to enable/disable
1455
+ // a Vim menu, we use item.tag to remember it. See each validateMenuItem:
1456
+ // implementation for details.
1457
+ //
1458
+ // See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html
1454
1459
[[self menuItemForDescriptor: desc] setTag: on];
1455
1460
1456
1461
const BOOL isPopup = [MMVimController hasPopupPrefix: rootName];
Original file line number Diff line number Diff line change 33
33
- (IBAction )toggleFullScreen : (id )sender ;
34
34
- (IBAction )realToggleFullScreen : (id )sender ;
35
35
36
+ // Public macaction's.
37
+ // Note: New items here need to be handled in validateMenuItem: as well.
38
+ - (void )performClose : (id )sender ;
39
+
36
40
@end
Original file line number Diff line number Diff line change @@ -184,13 +184,17 @@ - (void)performClose:(id)sender
184
184
[super performClose: sender];
185
185
}
186
186
187
+ // / Validates whether the menu item should be enabled or not.
187
188
- (BOOL )validateMenuItem : (NSMenuItem *)item
188
189
{
189
- if ([item action ] == @selector ( vimMenuItemAction: )
190
- || [item action ] == @selector (performClose: ))
190
+ // This class only really have one action that's bound from Vim
191
+ if ( [item action ] == @selector (performClose: )) {
191
192
return [item tag ];
193
+ }
192
194
193
- return YES ;
195
+ // Since this is a subclass of NSWindow, it has a bunch of auto-populated
196
+ // menu from the OS. Just pass it off to the superclass to let it handle it.
197
+ return [super validateMenuItem: item];
194
198
}
195
199
196
200
- (IBAction )zoom : (id )sender
Original file line number Diff line number Diff line change 17
17
@class MMVimController;
18
18
@class MMVimView;
19
19
20
- @interface MMWindowController : NSWindowController <NSWindowDelegate >
20
+ @interface MMWindowController : NSWindowController <
21
+ NSWindowDelegate
22
+ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_14
23
+ , NSMenuItemValidation
24
+ #endif
25
+ >
21
26
{
22
27
MMVimController *vimController;
23
28
MMVimView *vimView;
104
109
- (BOOL )getDefaultTopLeft : (NSPoint *)pt ;
105
110
- (void )runAfterWindowPresentedUsingBlock : (void (^)(void ))block ;
106
111
112
+ //
113
+ // NSMenuItemValidation
114
+ //
115
+ - (BOOL )validateMenuItem : (NSMenuItem *)item ;
116
+
117
+ // Menu items / macactions
107
118
- (IBAction )addNewTab : (id )sender ;
108
119
- (IBAction )toggleToolbar : (id )sender ;
109
120
- (IBAction )performClose : (id )sender ;
Original file line number Diff line number Diff line change @@ -1181,11 +1181,10 @@ - (IBAction)findAndReplace:(id)sender
1181
1181
1182
1182
- (BOOL )validateMenuItem : (NSMenuItem *)item
1183
1183
{
1184
- if ([item action ] == @selector (vimMenuItemAction: )
1185
- || [item action ] == @selector (performClose: ))
1186
- return [item tag ];
1187
-
1188
- return YES ;
1184
+ // This class is a responsder class and this should get called when we have
1185
+ // a specific action that we implement exposed as a menu. As such just return
1186
+ // [item tag] and no need to worry about macOS-injected menus.
1187
+ return [item tag ];
1189
1188
}
1190
1189
1191
1190
// -- NSWindow delegate ------------------------------------------------------
You can’t perform that action at this time.
0 commit comments