Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chrome/browser/app_controller_mac.mm

Issue 1025793002: Mac: Possible fix for Cmd-W closing window instead of a tab (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@grd
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/app_controller_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 104
105 using apps::AppShimHandler; 105 using apps::AppShimHandler;
106 using apps::ExtensionAppShimHandler; 106 using apps::ExtensionAppShimHandler;
107 using base::UserMetricsAction; 107 using base::UserMetricsAction;
108 using content::BrowserContext; 108 using content::BrowserContext;
109 using content::BrowserThread; 109 using content::BrowserThread;
110 using content::DownloadManager; 110 using content::DownloadManager;
111 111
112 namespace { 112 namespace {
113 113
114 // Declare notification names from the 10.7 SDK.
115 #if !defined(MAC_OS_X_VERSION_10_7) || \
116 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
117 NSString* NSPopoverDidShowNotification = @"NSPopoverDidShowNotification";
118 NSString* NSPopoverDidCloseNotification = @"NSPopoverDidCloseNotification";
119 #endif
120
121 // How long we allow a workspace change notification to wait to be 114 // How long we allow a workspace change notification to wait to be
122 // associated with a dock activation. The animation lasts 250ms. See 115 // associated with a dock activation. The animation lasts 250ms. See
123 // applicationShouldHandleReopen:hasVisibleWindows:. 116 // applicationShouldHandleReopen:hasVisibleWindows:.
124 static const int kWorkspaceChangeTimeoutMs = 500; 117 static const int kWorkspaceChangeTimeoutMs = 500;
125 118
126 // True while AppController is calling chrome::NewEmptyWindow(). We need a 119 // True while AppController is calling chrome::NewEmptyWindow(). We need a
127 // global flag here, analogue to StartupBrowserCreator::InProcessStartup() 120 // global flag here, analogue to StartupBrowserCreator::InProcessStartup()
128 // because otherwise the SessionService will try to restore sessions when we 121 // because otherwise the SessionService will try to restore sessions when we
129 // make a new window while there are no other active windows. 122 // make a new window while there are no other active windows.
130 bool g_is_opening_new_window = false; 123 bool g_is_opening_new_window = false;
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 addObserver:self 339 addObserver:self
347 selector:@selector(windowDidBecomeMain:) 340 selector:@selector(windowDidBecomeMain:)
348 name:NSWindowDidBecomeMainNotification 341 name:NSWindowDidBecomeMainNotification
349 object:nil]; 342 object:nil];
350 [notificationCenter 343 [notificationCenter
351 addObserver:self 344 addObserver:self
352 selector:@selector(windowDidResignMain:) 345 selector:@selector(windowDidResignMain:)
353 name:NSWindowDidResignMainNotification 346 name:NSWindowDidResignMainNotification
354 object:nil]; 347 object:nil];
355 348
356 if (base::mac::IsOSLionOrLater()) {
357 [notificationCenter
358 addObserver:self
359 selector:@selector(popoverDidShow:)
360 name:NSPopoverDidShowNotification
361 object:nil];
362 [notificationCenter
363 addObserver:self
364 selector:@selector(popoverDidClose:)
365 name:NSPopoverDidCloseNotification
366 object:nil];
367 }
368
369 // Register for space change notifications. 349 // Register for space change notifications.
370 [[[NSWorkspace sharedWorkspace] notificationCenter] 350 [[[NSWorkspace sharedWorkspace] notificationCenter]
371 addObserver:self 351 addObserver:self
372 selector:@selector(activeSpaceDidChange:) 352 selector:@selector(activeSpaceDidChange:)
373 name:NSWorkspaceActiveSpaceDidChangeNotification 353 name:NSWorkspaceActiveSpaceDidChangeNotification
374 object:nil]; 354 object:nil];
375 355
376 [[[NSWorkspace sharedWorkspace] notificationCenter] 356 [[[NSWorkspace sharedWorkspace] notificationCenter]
377 addObserver:self 357 addObserver:self
378 selector:@selector(willPowerOff:) 358 selector:@selector(willPowerOff:)
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 } else { 535 } else {
556 [closeTabMenuItem_ setKeyEquivalent:@""]; 536 [closeTabMenuItem_ setKeyEquivalent:@""];
557 [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; 537 [closeTabMenuItem_ setKeyEquivalentModifierMask:0];
558 } 538 }
559 } 539 }
560 540
561 // See if the focused window window has tabs, and adjust the key equivalents for 541 // See if the focused window window has tabs, and adjust the key equivalents for
562 // Close Tab/Close Window accordingly. 542 // Close Tab/Close Window accordingly.
563 - (void)menuNeedsUpdate:(NSMenu*)menu { 543 - (void)menuNeedsUpdate:(NSMenu*)menu {
564 DCHECK(menu == [closeTabMenuItem_ menu]); 544 DCHECK(menu == [closeTabMenuItem_ menu]);
565 NSWindow* window = [NSApp keyWindow]; 545
566 NSWindow* mainWindow = [NSApp mainWindow]; 546 BOOL enableCloseTabShortcut = NO;
567 if (!window || ([window parentWindow] == mainWindow)) { 547 id target = [NSApp targetForAction:@selector(performClose:)];
568 // If the key window is a child of the main window (e.g. a bubble), the main 548
569 // window should be the one that handles the close menu item action. 549 // |target| is an instance of NSPopover or NSWindow.
570 // Also, there might be a small amount of time where there is no key window; 550 // If a popover (likely the dictionary lookup popover), we want Cmd-W to
571 // in that case as well, just use our main browser window if there is one. 551 // close the popover so map it to "Close Window".
572 // You might think that we should just always use the main window, but the 552 // Otherwise, map Cmd-W to "Close Tab" if it's a browser window.
573 // "About Chrome" window serves as a counterexample. 553 if ([target isKindOfClass:[NSWindow class]]) {
574 window = mainWindow; 554 NSWindow* window = target;
555 NSWindow* mainWindow = [NSApp mainWindow];
556 if (!window || ([window parentWindow] == mainWindow)) {
557 // If the target window is a child of the main window (e.g. a bubble), the
558 // main window should be the one that handles the close menu item action.
559 window = mainWindow;
560 }
561 enableCloseTabShortcut =
562 [[window windowController] isKindOfClass:[TabWindowController class]];
575 } 563 }
576 564
577 BOOL hasTabs =
578 [[window windowController] isKindOfClass:[TabWindowController class]];
579 BOOL enableCloseTabShortcut = hasTabs && !hasPopover_;
580 [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut]; 565 [self adjustCloseWindowMenuItemKeyEquivalent:enableCloseTabShortcut];
581 [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut]; 566 [self adjustCloseTabMenuItemKeyEquivalent:enableCloseTabShortcut];
582 } 567 }
583 568
584 - (void)windowDidResignKey:(NSNotification*)notify { 569 - (void)windowDidResignKey:(NSNotification*)notify {
585 // If a window is closed, this notification is fired but |[NSApp keyWindow]| 570 // If a window is closed, this notification is fired but |[NSApp keyWindow]|
586 // returns nil regardless of whether any suitable candidates for the key 571 // returns nil regardless of whether any suitable candidates for the key
587 // window remain. It seems that the new key window for the app is not set 572 // window remain. It seems that the new key window for the app is not set
588 // until after this notification is fired, so a check is performed after the 573 // until after this notification is fired, so a check is performed after the
589 // run loop is allowed to spin. 574 // run loop is allowed to spin.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 } 617 }
633 } 618 }
634 619
635 // Called when shutting down or logging out. 620 // Called when shutting down or logging out.
636 - (void)willPowerOff:(NSNotification*)notify { 621 - (void)willPowerOff:(NSNotification*)notify {
637 // Don't attempt any shutdown here. Cocoa will shortly call 622 // Don't attempt any shutdown here. Cocoa will shortly call
638 // -[BrowserCrApplication terminate:]. 623 // -[BrowserCrApplication terminate:].
639 isPoweringOff_ = YES; 624 isPoweringOff_ = YES;
640 } 625 }
641 626
642 // Called on Lion and later when a popover (e.g. dictionary) is shown.
643 - (void)popoverDidShow:(NSNotification*)notify {
644 hasPopover_ = YES;
645 }
646
647 // Called on Lion and later when a popover (e.g. dictionary) is closed.
648 - (void)popoverDidClose:(NSNotification*)notify {
649 hasPopover_ = NO;
650 }
651
652 - (void)checkForAnyKeyWindows { 627 - (void)checkForAnyKeyWindows {
653 if ([NSApp keyWindow]) 628 if ([NSApp keyWindow])
654 return; 629 return;
655 630
656 content::NotificationService::current()->Notify( 631 content::NotificationService::current()->Notify(
657 chrome::NOTIFICATION_NO_KEY_WINDOW, 632 chrome::NOTIFICATION_NO_KEY_WINDOW,
658 content::NotificationService::AllSources(), 633 content::NotificationService::AllSources(),
659 content::NotificationService::NoDetails()); 634 content::NotificationService::NoDetails());
660 } 635 }
661 636
(...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after
1709 1684
1710 //--------------------------------------------------------------------------- 1685 //---------------------------------------------------------------------------
1711 1686
1712 namespace app_controller_mac { 1687 namespace app_controller_mac {
1713 1688
1714 bool IsOpeningNewWindow() { 1689 bool IsOpeningNewWindow() {
1715 return g_is_opening_new_window; 1690 return g_is_opening_new_window;
1716 } 1691 }
1717 1692
1718 } // namespace app_controller_mac 1693 } // namespace app_controller_mac
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698