OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/app_controller_mac.h" | 5 #import "chrome/browser/app_controller_mac.h" |
6 | 6 |
7 #include "app/l10n_util_mac.h" | 7 #include "app/l10n_util_mac.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/mac_util.h" | 9 #include "base/mac_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 // "preferences". | 201 // "preferences". |
202 - (TabWindowController*)keyWindowTabController { | 202 - (TabWindowController*)keyWindowTabController { |
203 NSWindowController* keyWindowController = | 203 NSWindowController* keyWindowController = |
204 [[NSApp keyWindow] windowController]; | 204 [[NSApp keyWindow] windowController]; |
205 if ([keyWindowController isKindOfClass:[TabWindowController class]]) | 205 if ([keyWindowController isKindOfClass:[TabWindowController class]]) |
206 return (TabWindowController*)keyWindowController; | 206 return (TabWindowController*)keyWindowController; |
207 | 207 |
208 return nil; | 208 return nil; |
209 } | 209 } |
210 | 210 |
| 211 // Helper routine to get the window controller if the main window is a tabbed |
| 212 // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 213 // "preferences". |
| 214 - (TabWindowController*)mainWindowTabController { |
| 215 NSWindowController* mainWindowController = |
| 216 [[NSApp mainWindow] windowController]; |
| 217 if ([mainWindowController isKindOfClass:[TabWindowController class]]) |
| 218 return (TabWindowController*)mainWindowController; |
| 219 |
| 220 return nil; |
| 221 } |
| 222 |
211 // If the window has tabs, make "close window" be cmd-shift-w, otherwise leave | 223 // If the window has tabs, make "close window" be cmd-shift-w, otherwise leave |
212 // it as the normal cmd-w. Capitalization of the key equivalent affects whether | 224 // it as the normal cmd-w. Capitalization of the key equivalent affects whether |
213 // the shift modifer is used. | 225 // the shift modifer is used. |
214 - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { | 226 - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { |
215 [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; | 227 [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; |
| 228 [closeWindowMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
216 } | 229 } |
217 | 230 |
218 // If the window has tabs, make "close tab" take over cmd-w, otherwise it | 231 // If the window has tabs, make "close tab" take over cmd-w, otherwise it |
219 // shouldn't have any key-equivalent because it should be disabled. | 232 // shouldn't have any key-equivalent because it should be disabled. |
220 - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { | 233 - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { |
221 if (hasTabs) { | 234 if (hasTabs) { |
222 [closeTabMenuItem_ setKeyEquivalent:@"w"]; | 235 [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
223 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; | 236 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
224 } else { | 237 } else { |
225 [closeTabMenuItem_ setKeyEquivalent:@""]; | 238 [closeTabMenuItem_ setKeyEquivalent:@""]; |
226 [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; | 239 [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
227 } | 240 } |
228 } | 241 } |
229 | 242 |
| 243 // Explicitly remove any command-key equivalents from the close tab/window |
| 244 // menus so that nothing can go haywire if we get a user action during pending |
| 245 // updates. |
| 246 - (void)clearCloseMenuItemKeyEquivalents { |
| 247 [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 248 [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 249 [closeWindowMenuItem_ setKeyEquivalent:@""]; |
| 250 [closeWindowMenuItem_ setKeyEquivalentModifierMask:0]; |
| 251 } |
| 252 |
230 // See if we have a window with tabs open, and adjust the key equivalents for | 253 // See if we have a window with tabs open, and adjust the key equivalents for |
231 // Close Tab/Close Window accordingly | 254 // Close Tab/Close Window accordingly |
232 - (void)fixCloseMenuItemKeyEquivalents { | 255 - (void)fixCloseMenuItemKeyEquivalents { |
233 TabWindowController* tabController = [self keyWindowTabController]; | 256 TabWindowController* tabController = [self keyWindowTabController]; |
| 257 if (!tabController && ![NSApp keyWindow]) { |
| 258 // There might be a small amount of time where there is no key window, |
| 259 // so just use our main browser window if there is one. |
| 260 tabController = [self mainWindowTabController]; |
| 261 } |
234 BOOL windowWithMultipleTabs = | 262 BOOL windowWithMultipleTabs = |
235 (tabController && [tabController numberOfTabs] > 1); | 263 (tabController && [tabController numberOfTabs] > 1); |
236 [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; | 264 [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; |
237 [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; | 265 [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; |
238 fileMenuUpdatePending_ = NO; | 266 fileMenuUpdatePending_ = NO; |
239 } | 267 } |
240 | 268 |
241 // Fix up the "close tab/close window" command-key equivalents. We do this | 269 // Fix up the "close tab/close window" command-key equivalents. We do this |
242 // after a delay to ensure that window layer state has been set by the time | 270 // after a delay to ensure that window layer state has been set by the time |
243 // we do the enabling. | 271 // we do the enabling. |
244 - (void)delayedFixCloseMenuItemKeyEquivalents { | 272 - (void)delayedFixCloseMenuItemKeyEquivalents { |
245 if (!fileMenuUpdatePending_) { | 273 if (!fileMenuUpdatePending_) { |
| 274 // The OS prefers keypresses to timers, so it's possible that a cmd-w |
| 275 // can sneak in before this timer fires. In order to prevent that from |
| 276 // having any bad consequences, just clear the keys combos altogether. They |
| 277 // will be reset when the timer eventually fires. |
| 278 [self clearCloseMenuItemKeyEquivalents]; |
246 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) | 279 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) |
247 withObject:nil | 280 withObject:nil |
248 afterDelay:0]; | 281 afterDelay:0]; |
249 fileMenuUpdatePending_ = YES; | 282 fileMenuUpdatePending_ = YES; |
250 } | 283 } |
251 } | 284 } |
252 | 285 |
253 // Called when we get a notification about the window layering changing to | 286 // Called when we get a notification about the window layering changing to |
254 // update the UI based on the new main window. | 287 // update the UI based on the new main window. |
255 - (void)windowLayeringDidChange:(NSNotification*)notify { | 288 - (void)windowLayeringDidChange:(NSNotification*)notify { |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 @end | 710 @end |
678 | 711 |
679 //--------------------------------------------------------------------------- | 712 //--------------------------------------------------------------------------- |
680 | 713 |
681 // Stub for cross-platform method that isn't called on Mac OS X. | 714 // Stub for cross-platform method that isn't called on Mac OS X. |
682 void ShowOptionsWindow(OptionsPage page, | 715 void ShowOptionsWindow(OptionsPage page, |
683 OptionsGroup highlight_group, | 716 OptionsGroup highlight_group, |
684 Profile* profile) { | 717 Profile* profile) { |
685 NOTIMPLEMENTED(); | 718 NOTIMPLEMENTED(); |
686 } | 719 } |
OLD | NEW |