| 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/gfx/mac/nsimage_cache.h" | 20 #include "ui/gfx/mac/nsimage_cache.h" |
| 21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 22 | 22 |
| 23 const int kRoundedCornerSize = 3; | 23 const int kRoundedCornerSize = 3; |
| 24 const int kCloseButtonLeftPadding = 8; | 24 const int kButtonPadding = 8; |
| 25 const int kIconAndTextPadding = 5; |
| 25 | 26 |
| 26 // Used to implement TestingAPI | 27 // Used to implement TestingAPI |
| 27 static NSEvent* MakeMouseEvent(NSEventType type, NSPoint point) { | 28 static NSEvent* MakeMouseEvent(NSEventType type, NSPoint point) { |
| 28 return [NSEvent mouseEventWithType:type | 29 return [NSEvent mouseEventWithType:type |
| 29 location:point | 30 location:point |
| 30 modifierFlags:0 | 31 modifierFlags:0 |
| 31 timestamp:0 | 32 timestamp:0 |
| 32 windowNumber:0 | 33 windowNumber:0 |
| 33 context:nil | 34 context:nil |
| 34 eventNumber:0 | 35 eventNumber:0 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 [settingsButton_ setDefaultOpacity:0.6]; | 176 [settingsButton_ setDefaultOpacity:0.6]; |
| 176 [settingsButton_ setHoverImage:image]; | 177 [settingsButton_ setHoverImage:image]; |
| 177 [settingsButton_ setHoverOpacity:0.9]; | 178 [settingsButton_ setHoverOpacity:0.9]; |
| 178 [settingsButton_ setPressedImage:image]; | 179 [settingsButton_ setPressedImage:image]; |
| 179 [settingsButton_ setPressedOpacity:1.0]; | 180 [settingsButton_ setPressedOpacity:1.0]; |
| 180 [[settingsButton_ cell] setHighlightsBy:NSNoCellMask]; | 181 [[settingsButton_ cell] setHighlightsBy:NSNoCellMask]; |
| 181 [self checkMouseAndUpdateSettingsButtonVisibility]; | 182 [self checkMouseAndUpdateSettingsButtonVisibility]; |
| 182 | 183 |
| 183 // Update layout of controls in the titlebar. | 184 // Update layout of controls in the titlebar. |
| 184 [self updateCloseButtonLayout]; | 185 [self updateCloseButtonLayout]; |
| 186 [self updateIconAndTitleLayout]; |
| 185 | 187 |
| 186 // Set autoresizing behavior: glued to edges on left, top and right. | 188 // Set autoresizing behavior: glued to edges on left, top and right. |
| 187 [self setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)]; | 189 [self setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)]; |
| 188 | 190 |
| 189 [[NSNotificationCenter defaultCenter] | 191 [[NSNotificationCenter defaultCenter] |
| 190 addObserver:self | 192 addObserver:self |
| 191 selector:@selector(didChangeTheme:) | 193 selector:@selector(didChangeTheme:) |
| 192 name:kBrowserThemeDidChangeNotification | 194 name:kBrowserThemeDidChangeNotification |
| 193 object:nil]; | 195 object:nil]; |
| 194 // Register for various window focus changes, so we can update our custom | 196 // Register for various window focus changes, so we can update our custom |
| 195 // titlebar appropriately. | 197 // titlebar appropriately. |
| 196 [[NSNotificationCenter defaultCenter] | 198 [[NSNotificationCenter defaultCenter] |
| 197 addObserver:self | 199 addObserver:self |
| 198 selector:@selector(didChangeMainWindow:) | 200 selector:@selector(didChangeMainWindow:) |
| 199 name:NSWindowDidBecomeMainNotification | 201 name:NSWindowDidBecomeMainNotification |
| 200 object:[self window]]; | 202 object:[self window]]; |
| 201 [[NSNotificationCenter defaultCenter] | 203 [[NSNotificationCenter defaultCenter] |
| 202 addObserver:self | 204 addObserver:self |
| 203 selector:@selector(didChangeMainWindow:) | 205 selector:@selector(didChangeMainWindow:) |
| 204 name:NSWindowDidResignMainNotification | 206 name:NSWindowDidResignMainNotification |
| 205 object:[self window]]; | 207 object:[self window]]; |
| 206 } | 208 } |
| 207 | 209 |
| 208 - (void)setTitle:(NSString*)newTitle { | 210 - (void)setTitle:(NSString*)newTitle { |
| 209 [title_ setStringValue:newTitle]; | 211 [title_ setStringValue:newTitle]; |
| 210 } | 212 } |
| 211 | 213 |
| 214 - (void)setIcon:(NSImage*)newIcon { |
| 215 [icon_ setImage:newIcon]; |
| 216 } |
| 217 |
| 212 - (void)updateCloseButtonLayout { | 218 - (void)updateCloseButtonLayout { |
| 213 NSRect buttonBounds = [closeButton_ bounds]; | 219 NSRect buttonFrame = [closeButton_ frame]; |
| 214 NSRect bounds = [self bounds]; | 220 NSRect bounds = [self bounds]; |
| 215 | 221 |
| 216 int x = kCloseButtonLeftPadding; | 222 buttonFrame.origin.x = kButtonPadding; |
| 217 int y = (NSHeight(bounds) - NSHeight(buttonBounds)) / 2; | 223 buttonFrame.origin.y = (NSHeight(bounds) - NSHeight(buttonFrame)) / 2; |
| 218 NSRect buttonFrame = NSMakeRect(x, | |
| 219 y, | |
| 220 NSWidth(buttonBounds), | |
| 221 NSHeight(buttonBounds)); | |
| 222 [closeButton_ setFrame:buttonFrame]; | 224 [closeButton_ setFrame:buttonFrame]; |
| 223 | 225 |
| 224 DCHECK(!closeButtonTrackingArea_.get()); | 226 DCHECK(!closeButtonTrackingArea_.get()); |
| 225 closeButtonTrackingArea_.reset( | 227 closeButtonTrackingArea_.reset( |
| 226 [[CrTrackingArea alloc] initWithRect:buttonFrame | 228 [[CrTrackingArea alloc] initWithRect:buttonFrame |
| 227 options:(NSTrackingMouseEnteredAndExited | | 229 options:(NSTrackingMouseEnteredAndExited | |
| 228 NSTrackingActiveAlways) | 230 NSTrackingActiveAlways) |
| 229 proxiedOwner:self | 231 proxiedOwner:self |
| 230 userInfo:nil]); | 232 userInfo:nil]); |
| 231 NSWindow* panelWindow = [self window]; | 233 NSWindow* panelWindow = [self window]; |
| 232 [closeButtonTrackingArea_.get() clearOwnerWhenWindowWillClose:panelWindow]; | 234 [closeButtonTrackingArea_.get() clearOwnerWhenWindowWillClose:panelWindow]; |
| 233 [self addTrackingArea:closeButtonTrackingArea_.get()]; | 235 [self addTrackingArea:closeButtonTrackingArea_.get()]; |
| 234 } | 236 } |
| 235 | 237 |
| 238 - (void)updateIconAndTitleLayout { |
| 239 NSRect closeButtonFrame = [closeButton_ frame]; |
| 240 NSRect iconFrame = [icon_ frame]; |
| 241 [title_ sizeToFit]; |
| 242 NSRect titleFrame = [title_ frame]; |
| 243 NSRect settingsButtonFrame = [settingsButton_ frame]; |
| 244 NSRect bounds = [self bounds]; |
| 245 |
| 246 // Place the icon and title at the center of the titlebar. |
| 247 int iconWidthWithPadding = NSWidth(iconFrame) + kIconAndTextPadding; |
| 248 int titleWidth = NSWidth(titleFrame); |
| 249 int availableWidth = NSWidth(bounds) - kButtonPadding * 4 - |
| 250 NSWidth(closeButtonFrame) - NSWidth(settingsButtonFrame); |
| 251 if (iconWidthWithPadding + titleWidth > availableWidth) |
| 252 titleWidth = availableWidth - iconWidthWithPadding; |
| 253 int startX = kButtonPadding * 2 + NSWidth(closeButtonFrame) + |
| 254 (availableWidth - iconWidthWithPadding - titleWidth) / 2; |
| 255 |
| 256 iconFrame.origin.x = startX; |
| 257 iconFrame.origin.y = (NSHeight(bounds) - NSHeight(iconFrame)) / 2; |
| 258 [icon_ setFrame:iconFrame]; |
| 259 |
| 260 titleFrame.origin.x = startX + iconWidthWithPadding; |
| 261 titleFrame.origin.y = (NSHeight(bounds) - NSHeight(titleFrame)) / 2; |
| 262 titleFrame.size.width = titleWidth; |
| 263 [title_ setFrame:titleFrame]; |
| 264 } |
| 265 |
| 236 // PanelManager controls size/position of the window. | 266 // PanelManager controls size/position of the window. |
| 237 - (BOOL)mouseDownCanMoveWindow { | 267 - (BOOL)mouseDownCanMoveWindow { |
| 238 return NO; | 268 return NO; |
| 239 } | 269 } |
| 240 | 270 |
| 241 - (BOOL)acceptsFirstMouse:(NSEvent*)event { | 271 - (BOOL)acceptsFirstMouse:(NSEvent*)event { |
| 242 return YES; | 272 return YES; |
| 243 } | 273 } |
| 244 | 274 |
| 245 - (void)mouseEntered:(NSEvent*)event { | 275 - (void)mouseEntered:(NSEvent*)event { |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 409 } |
| 380 | 410 |
| 381 - (void)checkMouseAndUpdateSettingsButtonVisibility { | 411 - (void)checkMouseAndUpdateSettingsButtonVisibility { |
| 382 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], | 412 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], |
| 383 [[controller_ window] frame]); | 413 [[controller_ window] frame]); |
| 384 [self updateSettingsButtonVisibility:mouseOverWindow]; | 414 [self updateSettingsButtonVisibility:mouseOverWindow]; |
| 385 } | 415 } |
| 386 | 416 |
| 387 @end | 417 @end |
| 388 | 418 |
| OLD | NEW |