OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/ui/panels/panel_titlebar_view_cocoa.h" | 5 #import "chrome/browser/ui/panels/panel_titlebar_view_cocoa.h" |
6 | 6 |
7 #include <Carbon/Carbon.h> // kVK_Escape | 7 #include <Carbon/Carbon.h> // kVK_Escape |
8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/scoped_nsautorelease_pool.h" | 11 #include "base/mac/scoped_nsautorelease_pool.h" |
12 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
13 #import "chrome/browser/ui/cocoa/hover_image_button.h" | 13 #import "chrome/browser/ui/cocoa/hover_image_button.h" |
14 #import "chrome/browser/ui/cocoa/nsview_additions.h" | 14 #import "chrome/browser/ui/cocoa/nsview_additions.h" |
15 #import "chrome/browser/ui/cocoa/themed_window.h" | 15 #import "chrome/browser/ui/cocoa/themed_window.h" |
16 #import "chrome/browser/ui/cocoa/tracking_area.h" | 16 #import "chrome/browser/ui/cocoa/tracking_area.h" |
17 #import "chrome/browser/ui/panels/panel_window_controller_cocoa.h" | 17 #import "chrome/browser/ui/panels/panel_window_controller_cocoa.h" |
18 #include "grit/theme_resources_standard.h" | 18 #include "grit/theme_resources_standard.h" |
19 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h" | 19 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h" |
20 #include "ui/base/resource/resource_bundle.h" | |
20 #include "ui/gfx/mac/nsimage_cache.h" | 21 #include "ui/gfx/mac/nsimage_cache.h" |
21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
22 | 23 |
23 const int kRoundedCornerSize = 3; | 24 const int kRoundedCornerSize = 3; |
24 const int kCloseButtonLeftPadding = 8; | 25 const int kButtonPadding = 8; |
26 const int kIconAndTextPadding = 5; | |
25 | 27 |
26 // Used to implement TestingAPI | 28 // Used to implement TestingAPI |
27 static NSEvent* MakeMouseEvent(NSEventType type, NSPoint point) { | 29 static NSEvent* MakeMouseEvent(NSEventType type, NSPoint point) { |
28 return [NSEvent mouseEventWithType:type | 30 return [NSEvent mouseEventWithType:type |
29 location:point | 31 location:point |
30 modifierFlags:0 | 32 modifierFlags:0 |
31 timestamp:0 | 33 timestamp:0 |
32 windowNumber:0 | 34 windowNumber:0 |
33 context:nil | 35 context:nil |
34 eventNumber:0 | 36 eventNumber:0 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 [settingsButton_ setDefaultOpacity:0.6]; | 177 [settingsButton_ setDefaultOpacity:0.6]; |
176 [settingsButton_ setHoverImage:image]; | 178 [settingsButton_ setHoverImage:image]; |
177 [settingsButton_ setHoverOpacity:0.9]; | 179 [settingsButton_ setHoverOpacity:0.9]; |
178 [settingsButton_ setPressedImage:image]; | 180 [settingsButton_ setPressedImage:image]; |
179 [settingsButton_ setPressedOpacity:1.0]; | 181 [settingsButton_ setPressedOpacity:1.0]; |
180 [[settingsButton_ cell] setHighlightsBy:NSNoCellMask]; | 182 [[settingsButton_ cell] setHighlightsBy:NSNoCellMask]; |
181 [self checkMouseAndUpdateSettingsButtonVisibility]; | 183 [self checkMouseAndUpdateSettingsButtonVisibility]; |
182 | 184 |
183 // Update layout of controls in the titlebar. | 185 // Update layout of controls in the titlebar. |
184 [self updateCloseButtonLayout]; | 186 [self updateCloseButtonLayout]; |
187 [self updateIconAndTitleLayout]; | |
185 | 188 |
186 // Set autoresizing behavior: glued to edges on left, top and right. | 189 // Set autoresizing behavior: glued to edges on left, top and right. |
187 [self setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)]; | 190 [self setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)]; |
188 | 191 |
189 [[NSNotificationCenter defaultCenter] | 192 [[NSNotificationCenter defaultCenter] |
190 addObserver:self | 193 addObserver:self |
191 selector:@selector(didChangeTheme:) | 194 selector:@selector(didChangeTheme:) |
192 name:kBrowserThemeDidChangeNotification | 195 name:kBrowserThemeDidChangeNotification |
193 object:nil]; | 196 object:nil]; |
194 // Register for various window focus changes, so we can update our custom | 197 // Register for various window focus changes, so we can update our custom |
195 // titlebar appropriately. | 198 // titlebar appropriately. |
196 [[NSNotificationCenter defaultCenter] | 199 [[NSNotificationCenter defaultCenter] |
197 addObserver:self | 200 addObserver:self |
198 selector:@selector(didChangeMainWindow:) | 201 selector:@selector(didChangeMainWindow:) |
199 name:NSWindowDidBecomeMainNotification | 202 name:NSWindowDidBecomeMainNotification |
200 object:[self window]]; | 203 object:[self window]]; |
201 [[NSNotificationCenter defaultCenter] | 204 [[NSNotificationCenter defaultCenter] |
202 addObserver:self | 205 addObserver:self |
203 selector:@selector(didChangeMainWindow:) | 206 selector:@selector(didChangeMainWindow:) |
204 name:NSWindowDidResignMainNotification | 207 name:NSWindowDidResignMainNotification |
205 object:[self window]]; | 208 object:[self window]]; |
206 } | 209 } |
207 | 210 |
208 - (void)setTitle:(NSString*)newTitle { | 211 - (void)setTitle:(NSString*)newTitle { |
209 [title_ setStringValue:newTitle]; | 212 [title_ setStringValue:newTitle]; |
213 | |
Avi (use Gerrit)
2011/09/21 21:04:25
remove extra line
jianli
2011/09/21 23:15:25
Done.
| |
214 } | |
215 | |
216 - (void)setIcon:(NSImage*)newIcon { | |
217 [icon_ setImage:newIcon]; | |
210 } | 218 } |
211 | 219 |
212 - (void)updateCloseButtonLayout { | 220 - (void)updateCloseButtonLayout { |
213 NSRect buttonBounds = [closeButton_ bounds]; | 221 NSRect buttonBounds = [closeButton_ bounds]; |
214 NSRect bounds = [self bounds]; | 222 NSRect bounds = [self bounds]; |
215 | 223 |
216 int x = kCloseButtonLeftPadding; | 224 int x = kButtonPadding; |
217 int y = (NSHeight(bounds) - NSHeight(buttonBounds)) / 2; | 225 int y = (NSHeight(bounds) - NSHeight(buttonBounds)) / 2; |
218 NSRect buttonFrame = NSMakeRect(x, | 226 NSRect buttonFrame = NSMakeRect(x, |
219 y, | 227 y, |
220 NSWidth(buttonBounds), | 228 NSWidth(buttonBounds), |
221 NSHeight(buttonBounds)); | 229 NSHeight(buttonBounds)); |
222 [closeButton_ setFrame:buttonFrame]; | 230 [closeButton_ setFrame:buttonFrame]; |
223 | 231 |
224 DCHECK(!closeButtonTrackingArea_.get()); | 232 DCHECK(!closeButtonTrackingArea_.get()); |
225 closeButtonTrackingArea_.reset( | 233 closeButtonTrackingArea_.reset( |
226 [[CrTrackingArea alloc] initWithRect:buttonFrame | 234 [[CrTrackingArea alloc] initWithRect:buttonFrame |
227 options:(NSTrackingMouseEnteredAndExited | | 235 options:(NSTrackingMouseEnteredAndExited | |
228 NSTrackingActiveAlways) | 236 NSTrackingActiveAlways) |
229 proxiedOwner:self | 237 proxiedOwner:self |
230 userInfo:nil]); | 238 userInfo:nil]); |
231 NSWindow* panelWindow = [self window]; | 239 NSWindow* panelWindow = [self window]; |
232 [closeButtonTrackingArea_.get() clearOwnerWhenWindowWillClose:panelWindow]; | 240 [closeButtonTrackingArea_.get() clearOwnerWhenWindowWillClose:panelWindow]; |
233 [self addTrackingArea:closeButtonTrackingArea_.get()]; | 241 [self addTrackingArea:closeButtonTrackingArea_.get()]; |
234 } | 242 } |
235 | 243 |
244 - (void)updateIconAndTitleLayout { | |
245 NSRect closeButtonBounds = [closeButton_ bounds]; | |
Dmitry Titov
2011/09/21 21:32:11
We should call those 'frame' ratehr then 'bounds'
jennb
2011/09/21 22:10:38
Is updateCloseButtonLayout also using bounds incor
jianli
2011/09/21 23:15:25
Changed to call sizeToFit before querying its boun
jianli
2011/09/21 23:15:25
Done.
jianli
2011/09/21 23:15:25
Done.
| |
246 NSRect iconBounds = [icon_ bounds]; | |
247 NSRect titleBounds = [title_ bounds]; | |
248 NSRect settingsButtonBounds = [settingsButton_ bounds]; | |
249 NSRect bounds = [self bounds]; | |
250 | |
251 // Compute the size of the title text. | |
252 NSDictionary* attributes = | |
253 [NSDictionary dictionaryWithObjectsAndKeys:[NSFont userFontOfSize:12], | |
Avi (use Gerrit)
2011/09/21 21:04:25
Don't make assumptions. You should be able to use
jianli
2011/09/21 23:15:25
Removed since it is not needed.
| |
254 NSFontAttributeName, | |
255 nil]; | |
256 NSAttributedString* text = | |
257 [[NSAttributedString alloc] initWithString:[title_ stringValue] | |
258 attributes:attributes]; | |
Avi (use Gerrit)
2011/09/21 21:04:25
Alternatively, does [title_ attributedStringValue]
Dmitry Titov
2011/09/21 21:32:11
'test' should be autoreleased in the same statemen
jianli
2011/09/21 23:15:25
Removed since it is not needed.
| |
259 NSSize titleSize = [text size]; | |
260 int titleWidth = titleSize.width; | |
261 | |
262 // Place the icon and title at the center of the titlebar. | |
263 int iconWidthWithPadding = NSWidth(iconBounds) + kIconAndTextPadding; | |
264 int availableWidth = NSWidth(bounds) - kButtonPadding * 4 - | |
265 NSWidth(closeButtonBounds) - NSWidth(settingsButtonBounds); | |
266 if (iconWidthWithPadding + titleWidth > availableWidth) | |
267 titleWidth = availableWidth - iconWidthWithPadding; | |
268 int startX = kButtonPadding * 2 + NSWidth(closeButtonBounds) + | |
269 (availableWidth - iconWidthWithPadding - titleWidth) / 2; | |
270 | |
271 iconBounds.origin.x = startX; | |
272 iconBounds.origin.y = (NSHeight(bounds) - NSHeight(iconBounds)) / 2; | |
273 [icon_ setFrame:iconBounds]; | |
274 | |
275 titleBounds.origin.x = startX + iconWidthWithPadding; | |
276 titleBounds.origin.y = (NSHeight(bounds) - NSHeight(titleBounds)) / 2; | |
277 titleBounds.size.width = titleWidth; | |
278 [title_ setFrame:titleBounds]; | |
279 } | |
280 | |
236 // PanelManager controls size/position of the window. | 281 // PanelManager controls size/position of the window. |
237 - (BOOL)mouseDownCanMoveWindow { | 282 - (BOOL)mouseDownCanMoveWindow { |
238 return NO; | 283 return NO; |
239 } | 284 } |
240 | 285 |
241 - (BOOL)acceptsFirstMouse:(NSEvent*)event { | 286 - (BOOL)acceptsFirstMouse:(NSEvent*)event { |
242 return YES; | 287 return YES; |
243 } | 288 } |
244 | 289 |
245 - (void)mouseEntered:(NSEvent*)event { | 290 - (void)mouseEntered:(NSEvent*)event { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 } | 424 } |
380 | 425 |
381 - (void)checkMouseAndUpdateSettingsButtonVisibility { | 426 - (void)checkMouseAndUpdateSettingsButtonVisibility { |
382 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], | 427 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], |
383 [[controller_ window] frame]); | 428 [[controller_ window] frame]); |
384 [self updateSettingsButtonVisibility:mouseOverWindow]; | 429 [self updateSettingsButtonVisibility:mouseOverWindow]; |
385 } | 430 } |
386 | 431 |
387 @end | 432 @end |
388 | 433 |
OLD | NEW |