Commit 4a56f1ee authored by Trent Apted's avatar Trent Apted Committed by Commit Bot

Clean up menu_controller.mm a bit

Nothing inherits from the Mac MenuController, except its own tests.
It also sends out some notifications that were once used by a test,
which was recently deleted.

Bug: 832676
Change-Id: Ie95206a3a17461c41683422d61f5a18e4ae5c95a
Reviewed-on: https://chromium-review.googlesource.com/1250466Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595718}
parent 59d29f03
...@@ -15,9 +15,6 @@ namespace ui { ...@@ -15,9 +15,6 @@ namespace ui {
class MenuModel; class MenuModel;
} }
UI_BASE_EXPORT extern NSString* const kMenuControllerMenuWillOpenNotification;
UI_BASE_EXPORT extern NSString* const kMenuControllerMenuDidCloseNotification;
// A controller for the cross-platform menu model. The menu that's created // A controller for the cross-platform menu model. The menu that's created
// has the tag and represented object set for each menu item. The object is a // has the tag and represented object set for each menu item. The object is a
// NSValue holding a pointer to the model for that level of the menu (to // NSValue holding a pointer to the model for that level of the menu (to
...@@ -25,12 +22,10 @@ UI_BASE_EXPORT extern NSString* const kMenuControllerMenuDidCloseNotification; ...@@ -25,12 +22,10 @@ UI_BASE_EXPORT extern NSString* const kMenuControllerMenuDidCloseNotification;
// that particular item. It is important that the model outlives this object // that particular item. It is important that the model outlives this object
// as it only maintains weak references. // as it only maintains weak references.
UI_BASE_EXPORT UI_BASE_EXPORT
@interface MenuControllerCocoa : NSObject<NSMenuDelegate> { @interface MenuControllerCocoa
@protected : NSObject<NSMenuDelegate, NSUserInterfaceValidations>
ui::MenuModel* model_; // Weak.
base::scoped_nsobject<NSMenu> menu_;
}
// The Model passed in to -initWithModel:.
@property(nonatomic, assign) ui::MenuModel* model; @property(nonatomic, assign) ui::MenuModel* model;
// Whether to activate selected menu items via a posted task. This may allow the // Whether to activate selected menu items via a posted task. This may allow the
...@@ -41,7 +36,7 @@ UI_BASE_EXPORT ...@@ -41,7 +36,7 @@ UI_BASE_EXPORT
// Note that changing this will have no effect if you use // Note that changing this will have no effect if you use
// |-initWithModel:useWithPopUpButtonCell:| or after the first call to |-menu|. // |-initWithModel:useWithPopUpButtonCell:| or after the first call to |-menu|.
@property(nonatomic) BOOL useWithPopUpButtonCell; @property(nonatomic, assign) BOOL useWithPopUpButtonCell;
+ (base::string16)elideMenuTitle:(const base::string16&)title + (base::string16)elideMenuTitle:(const base::string16&)title
toWidth:(int)width; toWidth:(int)width;
...@@ -69,35 +64,6 @@ UI_BASE_EXPORT ...@@ -69,35 +64,6 @@ UI_BASE_EXPORT
// Whether the menu is currently open. // Whether the menu is currently open.
- (BOOL)isMenuOpen; - (BOOL)isMenuOpen;
// NSMenuDelegate methods this class implements. Subclasses should call super
// if extending the behavior.
- (void)menuWillOpen:(NSMenu*)menu;
- (void)menuDidClose:(NSMenu*)menu;
@end
// Protected methods that subclassers can override and/or invoke.
@interface MenuControllerCocoa (Protected)
// Called before the menu is to be displayed to update the state (enabled,
// radio, etc) of each item in the menu. Also will update the title if the item
// is marked as "dynamic".
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item;
// Adds the item at |index| in |model| as an NSMenuItem at |index| of |menu|.
// Associates a submenu if the MenuModel::ItemType is TYPE_SUBMENU.
- (void)addItemToMenu:(NSMenu*)menu
atIndex:(NSInteger)index
fromModel:(ui::MenuModel*)model;
// Creates a NSMenu from the given model. If the model has submenus, this can
// be invoked recursively.
- (NSMenu*)menuFromModel:(ui::MenuModel*)model;
// Returns the maximum width for the menu item. Returns -1 to indicate that
// there's no maximum width.
- (int)maxWidthForMenuModel:(ui::MenuModel*)model
modelIndex:(int)modelIndex;
@end @end
#endif // UI_BASE_COCOA_MENU_CONTROLLER_H_ #endif // UI_BASE_COCOA_MENU_CONTROLLER_H_
...@@ -45,13 +45,23 @@ bool MenuHasVisibleItems(const ui::MenuModel* model) { ...@@ -45,13 +45,23 @@ bool MenuHasVisibleItems(const ui::MenuModel* model) {
} // namespace } // namespace
NSString* const kMenuControllerMenuWillOpenNotification =
@"MenuControllerMenuWillOpen";
NSString* const kMenuControllerMenuDidCloseNotification =
@"MenuControllerMenuDidClose";
// Internal methods. // Internal methods.
@interface MenuControllerCocoa () @interface MenuControllerCocoa ()
// Called before the menu is to be displayed to update the state (enabled,
// radio, etc) of each item in the menu. Also will update the title if the item
// is marked as "dynamic".
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item;
// Adds the item at |index| in |model| as an NSMenuItem at |index| of |menu|.
// Associates a submenu if the MenuModel::ItemType is TYPE_SUBMENU.
- (void)addItemToMenu:(NSMenu*)menu
atIndex:(NSInteger)index
fromModel:(ui::MenuModel*)model;
// Creates a NSMenu from the given model. If the model has submenus, this can
// be invoked recursively.
- (NSMenu*)menuFromModel:(ui::MenuModel*)model;
// Adds a separator item at the given index. As the separator doesn't need // Adds a separator item at the given index. As the separator doesn't need
// anything from the model, this method doesn't need the model index as the // anything from the model, this method doesn't need the model index as the
// other method below does. // other method below does.
...@@ -74,6 +84,8 @@ NSString* const kMenuControllerMenuDidCloseNotification = ...@@ -74,6 +84,8 @@ NSString* const kMenuControllerMenuDidCloseNotification =
@end @end
@implementation MenuControllerCocoa { @implementation MenuControllerCocoa {
ui::MenuModel* model_; // Weak.
base::scoped_nsobject<NSMenu> menu_;
BOOL useWithPopUpButtonCell_; // If YES, 0th item is blank BOOL useWithPopUpButtonCell_; // If YES, 0th item is blank
BOOL isMenuOpen_; BOOL isMenuOpen_;
BOOL postItemSelectedAsTask_; BOOL postItemSelectedAsTask_;
...@@ -139,11 +151,6 @@ NSString* const kMenuControllerMenuDidCloseNotification = ...@@ -139,11 +151,6 @@ NSString* const kMenuControllerMenuDidCloseNotification =
return menu; return menu;
} }
- (int)maxWidthForMenuModel:(ui::MenuModel*)model
modelIndex:(int)modelIndex {
return -1;
}
- (void)addSeparatorToMenu:(NSMenu*)menu - (void)addSeparatorToMenu:(NSMenu*)menu
atIndex:(int)index { atIndex:(int)index {
NSMenuItem* separator = [NSMenuItem separatorItem]; NSMenuItem* separator = [NSMenuItem separatorItem];
...@@ -153,12 +160,7 @@ NSString* const kMenuControllerMenuDidCloseNotification = ...@@ -153,12 +160,7 @@ NSString* const kMenuControllerMenuDidCloseNotification =
- (void)addItemToMenu:(NSMenu*)menu - (void)addItemToMenu:(NSMenu*)menu
atIndex:(NSInteger)index atIndex:(NSInteger)index
fromModel:(ui::MenuModel*)model { fromModel:(ui::MenuModel*)model {
base::string16 label16 = model->GetLabelAt(index); NSString* label = l10n_util::FixUpWindowsStyleLabel(model->GetLabelAt(index));
int maxWidth = [self maxWidthForMenuModel:model modelIndex:index];
if (maxWidth != -1)
label16 = [MenuControllerCocoa elideMenuTitle:label16 toWidth:maxWidth];
NSString* label = l10n_util::FixUpWindowsStyleLabel(label16);
base::scoped_nsobject<NSMenuItem> item([[ResponsiveNSMenuItem alloc] base::scoped_nsobject<NSMenuItem> item([[ResponsiveNSMenuItem alloc]
initWithTitle:label initWithTitle:label
action:@selector(itemSelected:) action:@selector(itemSelected:)
...@@ -335,16 +337,10 @@ NSString* const kMenuControllerMenuDidCloseNotification = ...@@ -335,16 +337,10 @@ NSString* const kMenuControllerMenuDidCloseNotification =
- (void)menuWillOpen:(NSMenu*)menu { - (void)menuWillOpen:(NSMenu*)menu {
isMenuOpen_ = YES; isMenuOpen_ = YES;
[[NSNotificationCenter defaultCenter]
postNotificationName:kMenuControllerMenuWillOpenNotification
object:self];
model_->MenuWillShow(); // Note: |model_| may trigger -[self dealloc]. model_->MenuWillShow(); // Note: |model_| may trigger -[self dealloc].
} }
- (void)menuDidClose:(NSMenu*)menu { - (void)menuDidClose:(NSMenu*)menu {
[[NSNotificationCenter defaultCenter]
postNotificationName:kMenuControllerMenuDidCloseNotification
object:self];
if (isMenuOpen_) { if (isMenuOpen_) {
isMenuOpen_ = NO; isMenuOpen_ = NO;
model_->MenuWillClose(); // Note: |model_| may trigger -[self dealloc]. model_->MenuWillClose(); // Note: |model_| may trigger -[self dealloc].
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment