1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
7 #include <Carbon/Carbon.h>
9 #include "base/logging.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
12 #include "chrome/browser/ui/browser.h"
13 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
14 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
15 #include "content/public/browser/native_web_keyboard_event.h"
17 using content::NativeWebKeyboardEvent;
19 @implementation BrowserWindowUtils
20 + (BOOL)shouldHandleKeyboardEvent:(const NativeWebKeyboardEvent&)event {
21 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char)
23 DCHECK(event.os_event != NULL);
27 + (int)getCommandId:(const NativeWebKeyboardEvent&)event {
28 return CommandForKeyEvent(event.os_event);
31 + (BOOL)handleKeyboardEvent:(NSEvent*)event
32 inWindow:(NSWindow*)window {
33 ChromeEventProcessingWindow* event_window =
34 static_cast<ChromeEventProcessingWindow*>(window);
35 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]);
37 // Do not fire shortcuts on key up.
38 if ([event type] == NSKeyDown) {
39 // Send the event to the menu before sending it to the browser/window
40 // shortcut handling, so that if a user configures cmd-left to mean
41 // "previous tab", it takes precedence over the built-in "history back"
42 // binding. Other than that, the |-redispatchKeyEvent:| call would take care
43 // of invoking the original menu item shortcut as well.
45 if ([[NSApp mainMenu] performKeyEquivalent:event])
48 if ([event_window handleExtraKeyboardShortcut:event])
52 return [event_window redispatchKeyEvent:event];
55 + (NSString*)scheduleReplaceOldTitle:(NSString*)oldTitle
56 withNewTitle:(NSString*)newTitle
57 forWindow:(NSWindow*)window {
59 [[NSRunLoop currentRunLoop]
60 cancelPerformSelector:@selector(setTitle:)
64 [[NSRunLoop currentRunLoop]
65 performSelector:@selector(setTitle:)
69 modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
70 return [newTitle copy];
73 // The titlebar/tabstrip header on the mac is slightly smaller than on Windows.
74 // There is also no window frame to the left and right of the web contents on
77 // - the window background pattern (IDR_THEME_FRAME.*) lined up vertically with
78 // the tab and toolbar patterns
79 // - the toolbar pattern lined up horizontally with the NTP background.
80 // we have to shift the pattern slightly, rather than drawing from the top left
81 // corner of the frame / tabstrip. The offsets below were empirically determined
82 // in order to line these patterns up.
84 // This will make the themes look slightly different than in Windows/Linux
85 // because of the differing heights between window top and tab top, but this has
86 // been approved by UI.
87 const CGFloat kPatternHorizontalOffset = -5;
88 // Without tab strip, offset an extra pixel (determined by experimentation).
89 const CGFloat kPatternVerticalOffset = 2;
90 const CGFloat kPatternVerticalOffsetNoTabStrip = 3;
92 + (NSPoint)themeImagePositionFor:(NSView*)windowView
93 withTabStrip:(NSView*)tabStripView
94 alignment:(ThemeImageAlignment)alignment {
96 return NSMakePoint(kPatternHorizontalOffset,
97 NSHeight([windowView bounds]) +
98 kPatternVerticalOffsetNoTabStrip);
102 [BrowserWindowUtils themeImagePositionInTabStripCoords:tabStripView
103 alignment:alignment];
104 return [tabStripView convertPoint:position toView:windowView];
107 + (NSPoint)themeImagePositionInTabStripCoords:(NSView*)tabStripView
108 alignment:(ThemeImageAlignment)alignment {
109 DCHECK(tabStripView);
111 if (alignment == THEME_IMAGE_ALIGN_WITH_TAB_STRIP) {
112 // The theme image is lined up with the top of the tab which is below the
113 // top of the tab strip.
114 return NSMakePoint(kPatternHorizontalOffset,
115 [TabStripController defaultTabHeight] +
116 kPatternVerticalOffset);
118 // The theme image is lined up with the top of the tab strip (as opposed to
119 // the top of the tab above). This is the same as lining up with the top of
120 // the window's root view when not in presentation mode.
121 return NSMakePoint(kPatternHorizontalOffset,
122 NSHeight([tabStripView bounds]) +
123 kPatternVerticalOffsetNoTabStrip);
126 + (void)activateWindowForController:(NSWindowController*)controller {
127 // Per http://crbug.com/73779 and http://crbug.com/75223, we need this to
128 // properly activate windows if Chrome is not the active application.
129 [[controller window] makeKeyAndOrderFront:controller];
130 ProcessSerialNumber psn;
131 GetCurrentProcess(&psn);
132 SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);