summaryrefslogtreecommitdiffstats
path: root/chrome/browser/app_controller_mac.mm
diff options
context:
space:
mode:
authorpinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-27 13:04:00 +0000
committerpinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-27 13:04:00 +0000
commit449dd2fa0f79ea68566f04399b3860b59762bc62 (patch)
treef701362fb866e7dbd065db7a4b9d8881d24fb9c1 /chrome/browser/app_controller_mac.mm
parentb004209b94e258e9ca5a2356e4bd108f8443e142 (diff)
downloadchromium_src-449dd2fa0f79ea68566f04399b3860b59762bc62.zip
chromium_src-449dd2fa0f79ea68566f04399b3860b59762bc62.tar.gz
chromium_src-449dd2fa0f79ea68566f04399b3860b59762bc62.tar.bz2
Fix issue where cmd-w was hard-coded to closing a browser tab regardless of the frontmost window type. Have cmd-key equiv correctly set depending on the window type and the number of tabs in the window. Broadcast notification when the number of tabs changes in the model. Disable "close tab" item when there's only 1 tab in the browser window.
BUG=10047 TEST=cmd-w correctly closes the expected thing (frontmost window, or tab in the frontmost window). close tab should be disabled when the frontmost tab is not a browser or if there is only 1 tab in the window. Review URL: http://codereview.chromium.org/115789 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16981 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/app_controller_mac.mm')
-rw-r--r--chrome/browser/app_controller_mac.mm112
1 files changed, 112 insertions, 0 deletions
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: