diff options
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/app_controller_mac.h | 7 | ||||
-rw-r--r-- | chrome/browser/app_controller_mac.mm | 112 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.mm | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.h | 4 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.mm | 12 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller_unittest.mm | 7 |
6 files changed, 147 insertions, 0 deletions
diff --git a/chrome/browser/app_controller_mac.h b/chrome/browser/app_controller_mac.h index bf2aebc..e47bc00 100644 --- a/chrome/browser/app_controller_mac.h +++ b/chrome/browser/app_controller_mac.h @@ -34,6 +34,13 @@ class Profile; // only needed during early startup, it points to a valid vector during early // startup and is NULL during the rest of app execution. scoped_ptr<std::vector<GURL> > pendingURLs_; + + // Outlets for the close tab/window menu items so that we can adjust the + // commmand-key equivalent depending on the kind of window and how many + // tabs it has. + IBOutlet NSMenuItem* closeTabMenuItem_; + IBOutlet NSMenuItem* closeWindowMenuItem_; + BOOL fileMenuUpdatePending_; // ensure we only do this once per notificaion. } - (IBAction)quit:(id)sender; diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm index 436fa6c..fbf2ef0 100644 --- a/chrome/browser/app_controller_mac.mm +++ b/chrome/browser/app_controller_mac.mm @@ -17,6 +17,8 @@ #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" #import "chrome/browser/cocoa/menu_localizer.h" #import "chrome/browser/cocoa/preferences_window_controller.h" +#import "chrome/browser/cocoa/tab_strip_controller.h" +#import "chrome/browser/cocoa/tab_window_controller.h" #include "chrome/browser/command_updater.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" @@ -31,6 +33,7 @@ withReply:(NSAppleEventDescriptor*)reply; - (void)openFiles:(NSAppleEventDescriptor*)event withReply:(NSAppleEventDescriptor*)reply; +- (void)windowLayeringDidChange:(NSNotification*)inNotification; @end @implementation AppController @@ -56,10 +59,119 @@ forEventClass:kCoreEventClass andEventID:kAEOpenDocuments]; + // Register for various window layering changes. We use these to update + // various UI elements (command-key equivalents, etc) when the frontmost + // window changes. + NSNotificationCenter* notificationCenter = + [NSNotificationCenter defaultCenter]; + [notificationCenter + addObserver:self + selector:@selector(windowLayeringDidChange:) + name:NSWindowDidBecomeKeyNotification + object:nil]; + [notificationCenter + addObserver:self + selector:@selector(windowLayeringDidChange:) + name:NSWindowDidResignKeyNotification + object:nil]; + [notificationCenter + addObserver:self + selector:@selector(windowLayeringDidChange:) + name:NSWindowDidBecomeMainNotification + object:nil]; + [notificationCenter + addObserver:self + selector:@selector(windowLayeringDidChange:) + name:NSWindowDidResignMainNotification + object:nil]; + + // Register for a notification that the number of tabs changes in windows + // so we can adjust the close tab/window command keys. + [notificationCenter + addObserver:self + selector:@selector(tabsChanged:) + name:kTabStripNumberOfTabsChanged + object:nil]; + // Set up the command updater for when there are no windows open [self initMenuState]; } +// Called when the app is shutting down. Clean-up as appropriate. +- (void)applicationWillTerminate:(NSNotification *)aNotification { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +// Helper routine to get the window controller if the key window is a tabbed +// window, or nil if not. Examples of non-tabbed windows are "about" or +// "preferences". +- (TabWindowController*)keyWindowTabController { + NSWindowController* keyWindowController = + [[[NSApplication sharedApplication] keyWindow] windowController]; + if ([keyWindowController isKindOfClass:[TabWindowController class]]) + return (TabWindowController*)keyWindowController; + + return nil; +} + +// If the window has tabs, make "close window" be cmd-shift-w, otherwise leave +// it as the normal cmd-w. Capitalization of the key equivalent affects whether +// the shift modifer is used. +- (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { + [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; +} + +// If the window has tabs, make "close tab" take over cmd-w, otherwise it +// shouldn't have any key-equivalent because it should be disabled. +- (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { + if (hasTabs) { + [closeTabMenuItem_ setKeyEquivalent:@"w"]; + [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; + } else { + [closeTabMenuItem_ setKeyEquivalent:@""]; + [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; + } +} + +// See if we have a window with tabs open, and adjust the key equivalents for +// Close Tab/Close Window accordingly +- (void)fixCloseMenuItemKeyEquivalents { + TabWindowController* tabController = [self keyWindowTabController]; + BOOL windowWithMultipleTabs = + (tabController && [tabController numberOfTabs] > 1); + [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; + [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; + fileMenuUpdatePending_ = NO; +} + +// Fix up the "close tab/close window" command-key equivalents. We do this +// after a delay to ensure that window layer state has been set by the time +// we do the enabling. +- (void)delayedFixCloseMenuItemKeyEquivalents { + if (!fileMenuUpdatePending_) { + [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) + withObject:nil + afterDelay:0]; + fileMenuUpdatePending_ = YES; + } +} + +// Called when we get a notification about the window layering changing to +// update the UI based on the new main window. +- (void)windowLayeringDidChange:(NSNotification*)notify { + [self delayedFixCloseMenuItemKeyEquivalents]; + + // TODO(pinkerton): If we have other things here, such as inspector panels + // that follow the contents of the selected webpage, we would update those + // here. +} + +// Called when the number of tabs changes in one of the browser windows. The +// object is the tab strip controller, but we don't currently care. +- (void)tabsChanged:(NSNotification*)notify { + [self delayedFixCloseMenuItemKeyEquivalents]; +} + // If the auto-update interval is not set, make it 5 hours. // This code is specific to Mac Chrome Dev Channel. // Placed here for 2 reasons: diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm index 2070be8..1129a6c 100644 --- a/chrome/browser/cocoa/browser_window_controller.mm +++ b/chrome/browser/cocoa/browser_window_controller.mm @@ -309,6 +309,11 @@ willPositionSheet:(NSWindow *)sheet // Generate return value (enabled state) enable = browser_->command_updater()->IsCommandEnabled(tag) ? YES : NO; + // Disable "close tab" if we're not the key window or if there's only + // one tab. + if (tag == IDC_CLOSE_TAB) + enable &= [self numberOfTabs] > 1 && [[self window] isKeyWindow]; + // If the item is toggleable, find it's toggle state and // try to update it. This is a little awkward, but the alternative is // to check after a commandDispatch, which seems worse. diff --git a/chrome/browser/cocoa/tab_strip_controller.h b/chrome/browser/cocoa/tab_strip_controller.h index eeab6b0..6004768 100644 --- a/chrome/browser/cocoa/tab_strip_controller.h +++ b/chrome/browser/cocoa/tab_strip_controller.h @@ -106,4 +106,8 @@ class ToolbarModel; + (CGFloat)defaultTabHeight; @end +// Notification sent when the number of tabs changes. The object will be this +// controller. +extern NSString* const kTabStripNumberOfTabsChanged; + #endif // CHROME_BROWSER_COCOA_TAB_STRIP_CONTROLLER_H_ diff --git a/chrome/browser/cocoa/tab_strip_controller.mm b/chrome/browser/cocoa/tab_strip_controller.mm index 6fed79f..b81b455 100644 --- a/chrome/browser/cocoa/tab_strip_controller.mm +++ b/chrome/browser/cocoa/tab_strip_controller.mm @@ -22,6 +22,8 @@ #include "chrome/browser/tabs/tab_strip_model.h" #include "grit/generated_resources.h" +NSString* const kTabStripNumberOfTabsChanged = @"kTabStripNumberOfTabsChanged"; + // A simple view class that prevents the windowserver from dragging the // area behind tabs. Sometimes core animation confuses it. @interface TabStripControllerDragBlockingView : NSView @@ -337,6 +339,11 @@ if (!inForeground) { [self layoutTabs]; } + + // Send a broadcast that the number of tabs have changed. + [[NSNotificationCenter defaultCenter] + postNotificationName:kTabStripNumberOfTabsChanged + object:self]; } // Called when a notification is received from the model to select a particular @@ -397,6 +404,11 @@ // Once we're totally done with the tab, delete its controller [tabArray_ removeObjectAtIndex:index]; + // Send a broadcast that the number of tabs have changed. + [[NSNotificationCenter defaultCenter] + postNotificationName:kTabStripNumberOfTabsChanged + object:self]; + [self layoutTabs]; } diff --git a/chrome/browser/cocoa/tab_strip_controller_unittest.mm b/chrome/browser/cocoa/tab_strip_controller_unittest.mm index 386b8e1..7470bb9 100644 --- a/chrome/browser/cocoa/tab_strip_controller_unittest.mm +++ b/chrome/browser/cocoa/tab_strip_controller_unittest.mm @@ -127,4 +127,11 @@ TEST_F(TabStripControllerTest, RearrangeTabs) { // if you don't do anything with it. http://crbug.com/10899 } +// Test that changing the number of tabs broadcasts a +// kTabStripNumberOfTabsChanged notifiction. +TEST_F(TabStripControllerTest, Notifications) { + // TODO(pinkerton): Creating a TabContents crashes an unrelated test, even + // if you don't do anything with it. http://crbug.com/10899 +} + } // namespace |