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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/app/chrome_dll_resource.h" | 10 #include "chrome/app/chrome_dll_resource.h" |
11 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
12 #include "chrome/browser/browser_init.h" | 12 #include "chrome/browser/browser_init.h" |
13 #include "chrome/browser/browser_list.h" | 13 #include "chrome/browser/browser_list.h" |
14 #include "chrome/browser/browser_shutdown.h" | 14 #include "chrome/browser/browser_shutdown.h" |
15 #import "chrome/browser/cocoa/about_window_controller.h" | 15 #import "chrome/browser/cocoa/about_window_controller.h" |
16 #import "chrome/browser/cocoa/bookmark_menu_bridge.h" | 16 #import "chrome/browser/cocoa/bookmark_menu_bridge.h" |
17 #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" | 17 #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" |
18 #import "chrome/browser/cocoa/menu_localizer.h" | 18 #import "chrome/browser/cocoa/menu_localizer.h" |
19 #import "chrome/browser/cocoa/preferences_window_controller.h" | 19 #import "chrome/browser/cocoa/preferences_window_controller.h" |
| 20 #import "chrome/browser/cocoa/tab_strip_controller.h" |
| 21 #import "chrome/browser/cocoa/tab_window_controller.h" |
20 #include "chrome/browser/command_updater.h" | 22 #include "chrome/browser/command_updater.h" |
21 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
22 #include "chrome/common/pref_service.h" | 24 #include "chrome/common/pref_service.h" |
23 #include "chrome/browser/profile_manager.h" | 25 #include "chrome/browser/profile_manager.h" |
24 #include "chrome/common/temp_scaffolding_stubs.h" | 26 #include "chrome/common/temp_scaffolding_stubs.h" |
25 | 27 |
26 @interface AppController(PRIVATE) | 28 @interface AppController(PRIVATE) |
27 - (void)initMenuState; | 29 - (void)initMenuState; |
28 - (void)openURLs:(const std::vector<GURL>&)urls; | 30 - (void)openURLs:(const std::vector<GURL>&)urls; |
29 - (void)openPendingURLs; | 31 - (void)openPendingURLs; |
30 - (void)getUrl:(NSAppleEventDescriptor*)event | 32 - (void)getUrl:(NSAppleEventDescriptor*)event |
31 withReply:(NSAppleEventDescriptor*)reply; | 33 withReply:(NSAppleEventDescriptor*)reply; |
32 - (void)openFiles:(NSAppleEventDescriptor*)event | 34 - (void)openFiles:(NSAppleEventDescriptor*)event |
33 withReply:(NSAppleEventDescriptor*)reply; | 35 withReply:(NSAppleEventDescriptor*)reply; |
| 36 - (void)windowLayeringDidChange:(NSNotification*)inNotification; |
34 @end | 37 @end |
35 | 38 |
36 @implementation AppController | 39 @implementation AppController |
37 | 40 |
38 // This method is called very early in application startup (ie, before | 41 // This method is called very early in application startup (ie, before |
39 // the profile is loaded or any preferences have been registered). Defer any | 42 // the profile is loaded or any preferences have been registered). Defer any |
40 // user-data initialization until -applicationDidFinishLaunching:. | 43 // user-data initialization until -applicationDidFinishLaunching:. |
41 - (void)awakeFromNib { | 44 - (void)awakeFromNib { |
42 pendingURLs_.reset(new std::vector<GURL>()); | 45 pendingURLs_.reset(new std::vector<GURL>()); |
43 | 46 |
44 // We need to register the handlers early to catch events fired on launch. | 47 // We need to register the handlers early to catch events fired on launch. |
45 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; | 48 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
46 [em setEventHandler:self | 49 [em setEventHandler:self |
47 andSelector:@selector(getUrl:withReply:) | 50 andSelector:@selector(getUrl:withReply:) |
48 forEventClass:kInternetEventClass | 51 forEventClass:kInternetEventClass |
49 andEventID:kAEGetURL]; | 52 andEventID:kAEGetURL]; |
50 [em setEventHandler:self | 53 [em setEventHandler:self |
51 andSelector:@selector(getUrl:withReply:) | 54 andSelector:@selector(getUrl:withReply:) |
52 forEventClass:'WWW!' // A particularly ancient AppleEvent that dates | 55 forEventClass:'WWW!' // A particularly ancient AppleEvent that dates |
53 andEventID:'OURL']; // back to the Spyglass days. | 56 andEventID:'OURL']; // back to the Spyglass days. |
54 [em setEventHandler:self | 57 [em setEventHandler:self |
55 andSelector:@selector(openFiles:withReply:) | 58 andSelector:@selector(openFiles:withReply:) |
56 forEventClass:kCoreEventClass | 59 forEventClass:kCoreEventClass |
57 andEventID:kAEOpenDocuments]; | 60 andEventID:kAEOpenDocuments]; |
58 | 61 |
| 62 // Register for various window layering changes. We use these to update |
| 63 // various UI elements (command-key equivalents, etc) when the frontmost |
| 64 // window changes. |
| 65 NSNotificationCenter* notificationCenter = |
| 66 [NSNotificationCenter defaultCenter]; |
| 67 [notificationCenter |
| 68 addObserver:self |
| 69 selector:@selector(windowLayeringDidChange:) |
| 70 name:NSWindowDidBecomeKeyNotification |
| 71 object:nil]; |
| 72 [notificationCenter |
| 73 addObserver:self |
| 74 selector:@selector(windowLayeringDidChange:) |
| 75 name:NSWindowDidResignKeyNotification |
| 76 object:nil]; |
| 77 [notificationCenter |
| 78 addObserver:self |
| 79 selector:@selector(windowLayeringDidChange:) |
| 80 name:NSWindowDidBecomeMainNotification |
| 81 object:nil]; |
| 82 [notificationCenter |
| 83 addObserver:self |
| 84 selector:@selector(windowLayeringDidChange:) |
| 85 name:NSWindowDidResignMainNotification |
| 86 object:nil]; |
| 87 |
| 88 // Register for a notification that the number of tabs changes in windows |
| 89 // so we can adjust the close tab/window command keys. |
| 90 [notificationCenter |
| 91 addObserver:self |
| 92 selector:@selector(tabsChanged:) |
| 93 name:kTabStripNumberOfTabsChanged |
| 94 object:nil]; |
| 95 |
59 // Set up the command updater for when there are no windows open | 96 // Set up the command updater for when there are no windows open |
60 [self initMenuState]; | 97 [self initMenuState]; |
61 } | 98 } |
62 | 99 |
| 100 // Called when the app is shutting down. Clean-up as appropriate. |
| 101 - (void)applicationWillTerminate:(NSNotification *)aNotification { |
| 102 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 103 } |
| 104 |
| 105 // Helper routine to get the window controller if the key window is a tabbed |
| 106 // window, or nil if not. Examples of non-tabbed windows are "about" or |
| 107 // "preferences". |
| 108 - (TabWindowController*)keyWindowTabController { |
| 109 NSWindowController* keyWindowController = |
| 110 [[[NSApplication sharedApplication] keyWindow] windowController]; |
| 111 if ([keyWindowController isKindOfClass:[TabWindowController class]]) |
| 112 return (TabWindowController*)keyWindowController; |
| 113 |
| 114 return nil; |
| 115 } |
| 116 |
| 117 // If the window has tabs, make "close window" be cmd-shift-w, otherwise leave |
| 118 // it as the normal cmd-w. Capitalization of the key equivalent affects whether |
| 119 // the shift modifer is used. |
| 120 - (void)adjustCloseWindowMenuItemKeyEquivalent:(BOOL)inHaveTabs { |
| 121 [closeWindowMenuItem_ setKeyEquivalent:(inHaveTabs ? @"W" : @"w")]; |
| 122 } |
| 123 |
| 124 // If the window has tabs, make "close tab" take over cmd-w, otherwise it |
| 125 // shouldn't have any key-equivalent because it should be disabled. |
| 126 - (void)adjustCloseTabMenuItemKeyEquivalent:(BOOL)hasTabs { |
| 127 if (hasTabs) { |
| 128 [closeTabMenuItem_ setKeyEquivalent:@"w"]; |
| 129 [closeTabMenuItem_ setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 130 } else { |
| 131 [closeTabMenuItem_ setKeyEquivalent:@""]; |
| 132 [closeTabMenuItem_ setKeyEquivalentModifierMask:0]; |
| 133 } |
| 134 } |
| 135 |
| 136 // See if we have a window with tabs open, and adjust the key equivalents for |
| 137 // Close Tab/Close Window accordingly |
| 138 - (void)fixCloseMenuItemKeyEquivalents { |
| 139 TabWindowController* tabController = [self keyWindowTabController]; |
| 140 BOOL windowWithMultipleTabs = |
| 141 (tabController && [tabController numberOfTabs] > 1); |
| 142 [self adjustCloseWindowMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 143 [self adjustCloseTabMenuItemKeyEquivalent:windowWithMultipleTabs]; |
| 144 fileMenuUpdatePending_ = NO; |
| 145 } |
| 146 |
| 147 // Fix up the "close tab/close window" command-key equivalents. We do this |
| 148 // after a delay to ensure that window layer state has been set by the time |
| 149 // we do the enabling. |
| 150 - (void)delayedFixCloseMenuItemKeyEquivalents { |
| 151 if (!fileMenuUpdatePending_) { |
| 152 [self performSelector:@selector(fixCloseMenuItemKeyEquivalents) |
| 153 withObject:nil |
| 154 afterDelay:0]; |
| 155 fileMenuUpdatePending_ = YES; |
| 156 } |
| 157 } |
| 158 |
| 159 // Called when we get a notification about the window layering changing to |
| 160 // update the UI based on the new main window. |
| 161 - (void)windowLayeringDidChange:(NSNotification*)notify { |
| 162 [self delayedFixCloseMenuItemKeyEquivalents]; |
| 163 |
| 164 // TODO(pinkerton): If we have other things here, such as inspector panels |
| 165 // that follow the contents of the selected webpage, we would update those |
| 166 // here. |
| 167 } |
| 168 |
| 169 // Called when the number of tabs changes in one of the browser windows. The |
| 170 // object is the tab strip controller, but we don't currently care. |
| 171 - (void)tabsChanged:(NSNotification*)notify { |
| 172 [self delayedFixCloseMenuItemKeyEquivalents]; |
| 173 } |
| 174 |
63 // If the auto-update interval is not set, make it 5 hours. | 175 // If the auto-update interval is not set, make it 5 hours. |
64 // This code is specific to Mac Chrome Dev Channel. | 176 // This code is specific to Mac Chrome Dev Channel. |
65 // Placed here for 2 reasons: | 177 // Placed here for 2 reasons: |
66 // 1) Same spot as other Pref stuff | 178 // 1) Same spot as other Pref stuff |
67 // 2) Try and be friendly by keeping this after app launch | 179 // 2) Try and be friendly by keeping this after app launch |
68 // TODO(jrg): remove once we go Beta. | 180 // TODO(jrg): remove once we go Beta. |
69 - (void)setUpdateCheckInterval { | 181 - (void)setUpdateCheckInterval { |
70 #if defined(GOOGLE_CHROME_BUILD) | 182 #if defined(GOOGLE_CHROME_BUILD) |
71 CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent"; | 183 CFStringRef app = (CFStringRef)@"com.google.Keystone.Agent"; |
72 CFStringRef checkInterval = (CFStringRef)@"checkInterval"; | 184 CFStringRef checkInterval = (CFStringRef)@"checkInterval"; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 name:kUserClosedAboutNotification | 477 name:kUserClosedAboutNotification |
366 object:aboutController_.get()]; | 478 object:aboutController_.get()]; |
367 } | 479 } |
368 if (![[aboutController_ window] isVisible]) | 480 if (![[aboutController_ window] isVisible]) |
369 [[aboutController_ window] center]; | 481 [[aboutController_ window] center]; |
370 [aboutController_ showWindow:self]; | 482 [aboutController_ showWindow:self]; |
371 #endif | 483 #endif |
372 } | 484 } |
373 | 485 |
374 @end | 486 @end |
OLD | NEW |