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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/views/frame/glass_browser_frame_view.h" 6 7 #include "base/command_line.h" 8 #include "base/prefs/pref_service.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/app/chrome_command_ids.h" 11 #include "chrome/app/chrome_dll_resource.h" 12 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/themes/theme_properties.h" 15 #include "chrome/browser/ui/views/avatar_menu_button.h" 16 #include "chrome/browser/ui/views/frame/browser_view.h" 17 #include "chrome/browser/ui/views/tabs/tab.h" 18 #include "chrome/browser/ui/views/tabs/tab_strip.h" 19 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/pref_names.h" 21 #include "content/public/browser/notification_service.h" 22 #include "grit/generated_resources.h" 23 #include "grit/theme_resources.h" 24 #include "grit/ui_resources.h" 25 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/resource/resource_bundle_win.h" 27 #include "ui/base/theme_provider.h" 28 #include "ui/base/win/dpi.h" 29 #include "ui/gfx/canvas.h" 30 #include "ui/gfx/icon_util.h" 31 #include "ui/gfx/image/image.h" 32 #include "ui/views/controls/label.h" 33 #include "ui/views/layout/layout_constants.h" 34 #include "ui/views/win/hwnd_util.h" 35 #include "ui/views/window/client_view.h" 36 37 HICON GlassBrowserFrameView::throbber_icons_[ 38 GlassBrowserFrameView::kThrobberIconCount]; 39 40 namespace { 41 // There are 3 px of client edge drawn inside the outer frame borders. 42 const int kNonClientBorderThickness = 3; 43 // Besides the frame border, there's another 9 px of empty space atop the 44 // window in restored mode, to use to drag the window around. 45 const int kNonClientRestoredExtraThickness = 9; 46 // In the window corners, the resize areas don't actually expand bigger, but the 47 // 16 px at the end of the top and bottom edges triggers diagonal resizing. 48 const int kResizeAreaCornerSize = 16; 49 // The avatar ends 2 px above the bottom of the tabstrip (which, given the 50 // way the tabstrip draws its bottom edge, will appear like a 1 px gap to the 51 // user). 52 const int kAvatarBottomSpacing = 2; 53 // Space between the frame border and the left edge of the avatar. 54 const int kAvatarLeftSpacing = 2; 55 // Space between the right edge of the avatar and the tabstrip. 56 const int kAvatarRightSpacing = -2; 57 // The content left/right images have a shadow built into them. 58 const int kContentEdgeShadowThickness = 2; 59 // The top 3 px of the tabstrip is shadow; in maximized mode we push this off 60 // the top of the screen so the tabs appear flush against the screen edge. 61 const int kTabstripTopShadowThickness = 3; 62 // In restored mode, the New Tab button isn't at the same height as the caption 63 // buttons, but the space will look cluttered if it actually slides under them, 64 // so we stop it when the gap between the two is down to 5 px. 65 const int kNewTabCaptionRestoredSpacing = 5; 66 // In maximized mode, where the New Tab button and the caption buttons are at 67 // similar vertical coordinates, we need to reserve a larger, 16 px gap to avoid 68 // looking too cluttered. 69 const int kNewTabCaptionMaximizedSpacing = 16; 70 // How far to indent the tabstrip from the left side of the screen when there 71 // is no avatar icon. 72 const int kTabStripIndent = -6; 73 74 } // namespace 75 76 /////////////////////////////////////////////////////////////////////////////// 77 // GlassBrowserFrameView, public: 78 79 GlassBrowserFrameView::GlassBrowserFrameView(BrowserFrame* frame, 80 BrowserView* browser_view) 81 : BrowserNonClientFrameView(frame, browser_view), 82 throbber_running_(false), 83 throbber_frame_(0) { 84 if (browser_view->ShouldShowWindowIcon()) 85 InitThrobberIcons(); 86 87 UpdateAvatarInfo(); 88 if (!browser_view->IsOffTheRecord()) { 89 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, 90 content::NotificationService::AllSources()); 91 } 92 } 93 94 GlassBrowserFrameView::~GlassBrowserFrameView() { 95 } 96 97 /////////////////////////////////////////////////////////////////////////////// 98 // GlassBrowserFrameView, BrowserNonClientFrameView implementation: 99 100 gfx::Rect GlassBrowserFrameView::GetBoundsForTabStrip( 101 views::View* tabstrip) const { 102 int minimize_button_offset = 103 std::min(frame()->GetMinimizeButtonOffset(), width()); 104 int tabstrip_x = browser_view()->ShouldShowAvatar() ? 105 (avatar_bounds_.right() + kAvatarRightSpacing) : 106 NonClientBorderThickness() + kTabStripIndent; 107 // In RTL languages, we have moved an avatar icon left by the size of window 108 // controls to prevent it from being rendered over them. So, we use its x 109 // position to move this tab strip left when maximized. Also, we can render 110 // a tab strip until the left end of this window without considering the size 111 // of window controls in RTL languages. 112 if (base::i18n::IsRTL()) { 113 if (!browser_view()->ShouldShowAvatar() && frame()->IsMaximized()) 114 tabstrip_x += avatar_bounds_.x(); 115 minimize_button_offset = width(); 116 } 117 int tabstrip_width = minimize_button_offset - tabstrip_x - 118 (frame()->IsMaximized() ? 119 kNewTabCaptionMaximizedSpacing : kNewTabCaptionRestoredSpacing); 120 return gfx::Rect(tabstrip_x, GetTabStripInsets(false).top, 121 std::max(0, tabstrip_width), 122 tabstrip->GetPreferredSize().height()); 123 } 124 125 BrowserNonClientFrameView::TabStripInsets 126 GlassBrowserFrameView::GetTabStripInsets(bool restored) const { 127 // TODO: include OTR and caption. 128 return TabStripInsets(NonClientTopBorderHeight(restored), 0, 0); 129 } 130 131 int GlassBrowserFrameView::GetThemeBackgroundXInset() const { 132 return 0; 133 } 134 135 void GlassBrowserFrameView::UpdateThrobber(bool running) { 136 if (throbber_running_) { 137 if (running) { 138 DisplayNextThrobberFrame(); 139 } else { 140 StopThrobber(); 141 } 142 } else if (running) { 143 StartThrobber(); 144 } 145 } 146 147 gfx::Size GlassBrowserFrameView::GetMinimumSize() { 148 gfx::Size min_size(browser_view()->GetMinimumSize()); 149 150 // Account for the client area insets. 151 gfx::Insets insets = GetClientAreaInsets(); 152 min_size.Enlarge(insets.width(), insets.height()); 153 // Client area insets do not include the shadow thickness. 154 min_size.Enlarge(2 * kContentEdgeShadowThickness, 0); 155 156 // Ensure that the minimum width is enough to hold a tab strip with minimum 157 // width at its usual insets. 158 if (browser_view()->IsTabStripVisible()) { 159 TabStrip* tabstrip = browser_view()->tabstrip(); 160 int min_tabstrip_width = tabstrip->GetMinimumSize().width(); 161 int min_tabstrip_area_width = 162 width() - GetBoundsForTabStrip(tabstrip).width() + min_tabstrip_width; 163 min_size.set_width(std::max(min_tabstrip_area_width, min_size.width())); 164 } 165 166 return min_size; 167 } 168 169 /////////////////////////////////////////////////////////////////////////////// 170 // GlassBrowserFrameView, views::NonClientFrameView implementation: 171 172 gfx::Rect GlassBrowserFrameView::GetBoundsForClientView() const { 173 return client_view_bounds_; 174 } 175 176 gfx::Rect GlassBrowserFrameView::GetWindowBoundsForClientBounds( 177 const gfx::Rect& client_bounds) const { 178 HWND hwnd = views::HWNDForWidget(frame()); 179 if (!browser_view()->IsTabStripVisible() && hwnd) { 180 // If we don't have a tabstrip, we're either a popup or an app window, in 181 // which case we have a standard size non-client area and can just use 182 // AdjustWindowRectEx to obtain it. We check for a non-NULL window handle in 183 // case this gets called before the window is actually created. 184 RECT rect = client_bounds.ToRECT(); 185 AdjustWindowRectEx(&rect, GetWindowLong(hwnd, GWL_STYLE), FALSE, 186 GetWindowLong(hwnd, GWL_EXSTYLE)); 187 return gfx::Rect(rect); 188 } 189 190 gfx::Insets insets = GetClientAreaInsets(); 191 return gfx::Rect(std::max(0, client_bounds.x() - insets.left()), 192 std::max(0, client_bounds.y() - insets.top()), 193 client_bounds.width() + insets.width(), 194 client_bounds.height() + insets.height()); 195 } 196 197 int GlassBrowserFrameView::NonClientHitTest(const gfx::Point& point) { 198 // If the browser isn't in normal mode, we haven't customized the frame, so 199 // Windows can figure this out. If the point isn't within our bounds, then 200 // it's in the native portion of the frame, so again Windows can figure it 201 // out. 202 if (!browser_view()->IsBrowserTypeNormal() || !bounds().Contains(point)) 203 return HTNOWHERE; 204 205 // See if the point is within the avatar menu button or within the avatar 206 // label. 207 if (avatar_button() && avatar_button()->GetMirroredBounds().Contains(point)) 208 return HTCLIENT; 209 210 int frame_component = frame()->client_view()->NonClientHitTest(point); 211 212 // See if we're in the sysmenu region. We still have to check the tabstrip 213 // first so that clicks in a tab don't get treated as sysmenu clicks. 214 int nonclient_border_thickness = NonClientBorderThickness(); 215 if (gfx::Rect(nonclient_border_thickness, GetSystemMetrics(SM_CXSIZEFRAME), 216 GetSystemMetrics(SM_CXSMICON), 217 GetSystemMetrics(SM_CYSMICON)).Contains(point)) 218 return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU; 219 220 if (frame_component != HTNOWHERE) 221 return frame_component; 222 223 int frame_border_thickness = FrameBorderThickness(); 224 int window_component = GetHTComponentForFrame(point, frame_border_thickness, 225 nonclient_border_thickness, frame_border_thickness, 226 kResizeAreaCornerSize - frame_border_thickness, 227 frame()->widget_delegate()->CanResize()); 228 // Fall back to the caption if no other component matches. 229 return (window_component == HTNOWHERE) ? HTCAPTION : window_component; 230 } 231 232 /////////////////////////////////////////////////////////////////////////////// 233 // GlassBrowserFrameView, views::View overrides: 234 235 void GlassBrowserFrameView::OnPaint(gfx::Canvas* canvas) { 236 if (!browser_view()->IsTabStripVisible()) 237 return; // Nothing is visible, so don't bother to paint. 238 239 PaintToolbarBackground(canvas); 240 if (!frame()->IsMaximized()) 241 PaintRestoredClientEdge(canvas); 242 } 243 244 void GlassBrowserFrameView::Layout() { 245 LayoutAvatar(); 246 LayoutClientView(); 247 } 248 249 bool GlassBrowserFrameView::HitTestRect(const gfx::Rect& rect) const { 250 return (avatar_button() && 251 avatar_button()->GetMirroredBounds().Intersects(rect)) || 252 !frame()->client_view()->bounds().Intersects(rect); 253 } 254 255 /////////////////////////////////////////////////////////////////////////////// 256 // GlassBrowserFrameView, private: 257 258 int GlassBrowserFrameView::FrameBorderThickness() const { 259 return (frame()->IsMaximized() || frame()->IsFullscreen()) ? 260 0 : GetSystemMetrics(SM_CXSIZEFRAME); 261 } 262 263 int GlassBrowserFrameView::NonClientBorderThickness() const { 264 if (frame()->IsMaximized() || frame()->IsFullscreen()) 265 return 0; 266 267 return kNonClientBorderThickness; 268 } 269 270 int GlassBrowserFrameView::NonClientTopBorderHeight( 271 bool restored) const { 272 if (!restored && frame()->IsFullscreen()) 273 return 0; 274 275 // We'd like to use FrameBorderThickness() here, but the maximized Aero glass 276 // frame has a 0 frame border around most edges and a CYSIZEFRAME-thick border 277 // at the top (see AeroGlassFrame::OnGetMinMaxInfo()). 278 return ui::win::GetSystemMetricsInDIP(SM_CYSIZEFRAME) + 279 ((!restored && !frame()->ShouldLeaveOffsetNearTopBorder()) ? 280 -kTabstripTopShadowThickness : kNonClientRestoredExtraThickness); 281 } 282 283 void GlassBrowserFrameView::PaintToolbarBackground(gfx::Canvas* canvas) { 284 ui::ThemeProvider* tp = GetThemeProvider(); 285 286 gfx::Rect toolbar_bounds(browser_view()->GetToolbarBounds()); 287 gfx::Point toolbar_origin(toolbar_bounds.origin()); 288 View::ConvertPointToTarget(browser_view(), this, &toolbar_origin); 289 toolbar_bounds.set_origin(toolbar_origin); 290 int x = toolbar_bounds.x(); 291 int w = toolbar_bounds.width(); 292 int left_x = x - kContentEdgeShadowThickness; 293 294 gfx::ImageSkia* theme_toolbar = tp->GetImageSkiaNamed(IDR_THEME_TOOLBAR); 295 gfx::ImageSkia* toolbar_left = tp->GetImageSkiaNamed( 296 IDR_CONTENT_TOP_LEFT_CORNER); 297 gfx::ImageSkia* toolbar_center = tp->GetImageSkiaNamed( 298 IDR_CONTENT_TOP_CENTER); 299 300 // Tile the toolbar image starting at the frame edge on the left and where 301 // the tabstrip is on the top. 302 int y = toolbar_bounds.y(); 303 int dest_y = y + (kFrameShadowThickness * 2); 304 canvas->TileImageInt(*theme_toolbar, 305 x + GetThemeBackgroundXInset(), 306 dest_y - GetTabStripInsets(false).top, x, 307 dest_y, w, theme_toolbar->height()); 308 309 // Draw rounded corners for the tab. 310 gfx::ImageSkia* toolbar_left_mask = 311 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER_MASK); 312 gfx::ImageSkia* toolbar_right_mask = 313 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER_MASK); 314 315 // We mask out the corners by using the DestinationIn transfer mode, 316 // which keeps the RGB pixels from the destination and the alpha from 317 // the source. 318 SkPaint paint; 319 paint.setXfermodeMode(SkXfermode::kDstIn_Mode); 320 321 // Mask out the top left corner. 322 canvas->DrawImageInt(*toolbar_left_mask, left_x, y, paint); 323 324 // Mask out the top right corner. 325 int right_x = 326 x + w + kContentEdgeShadowThickness - toolbar_right_mask->width(); 327 canvas->DrawImageInt(*toolbar_right_mask, right_x, y, paint); 328 329 // Draw left edge. 330 canvas->DrawImageInt(*toolbar_left, left_x, y); 331 332 // Draw center edge. 333 canvas->TileImageInt(*toolbar_center, left_x + toolbar_left->width(), y, 334 right_x - (left_x + toolbar_left->width()), toolbar_center->height()); 335 336 // Right edge. 337 canvas->DrawImageInt(*tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER), 338 right_x, y); 339 340 // Draw the content/toolbar separator. 341 canvas->FillRect( 342 gfx::Rect(x + kClientEdgeThickness, 343 toolbar_bounds.bottom() - kClientEdgeThickness, 344 w - (2 * kClientEdgeThickness), 345 kClientEdgeThickness), 346 ThemeProperties::GetDefaultColor( 347 ThemeProperties::COLOR_TOOLBAR_SEPARATOR)); 348 } 349 350 void GlassBrowserFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) { 351 ui::ThemeProvider* tp = GetThemeProvider(); 352 gfx::Rect client_area_bounds = CalculateClientAreaBounds(width(), height()); 353 354 // The client edges start below the toolbar upper corner images regardless 355 // of how tall the toolbar itself is. 356 int client_area_top = frame()->client_view()->y() + 357 browser_view()->GetToolbarBounds().y() + 358 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER)->height(); 359 int client_area_bottom = 360 std::max(client_area_top, height() - NonClientBorderThickness()); 361 int client_area_height = client_area_bottom - client_area_top; 362 363 // Draw the client edge images. 364 gfx::ImageSkia* right = tp->GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE); 365 canvas->TileImageInt(*right, client_area_bounds.right(), client_area_top, 366 right->width(), client_area_height); 367 canvas->DrawImageInt( 368 *tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER), 369 client_area_bounds.right(), client_area_bottom); 370 gfx::ImageSkia* bottom = tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER); 371 canvas->TileImageInt(*bottom, client_area_bounds.x(), 372 client_area_bottom, client_area_bounds.width(), 373 bottom->height()); 374 gfx::ImageSkia* bottom_left = 375 tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER); 376 canvas->DrawImageInt(*bottom_left, 377 client_area_bounds.x() - bottom_left->width(), client_area_bottom); 378 gfx::ImageSkia* left = tp->GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE); 379 canvas->TileImageInt(*left, client_area_bounds.x() - left->width(), 380 client_area_top, left->width(), client_area_height); 381 382 // Draw the toolbar color so that the client edges show the right color even 383 // where not covered by the toolbar image. NOTE: We do this after drawing the 384 // images because the images are meant to alpha-blend atop the frame whereas 385 // these rects are meant to be fully opaque, without anything overlaid. 386 SkColor toolbar_color = tp->GetColor(ThemeProperties::COLOR_TOOLBAR); 387 canvas->FillRect(gfx::Rect(client_area_bounds.x() - kClientEdgeThickness, 388 client_area_top, kClientEdgeThickness, 389 client_area_bottom + kClientEdgeThickness - client_area_top), 390 toolbar_color); 391 canvas->FillRect(gfx::Rect(client_area_bounds.x(), client_area_bottom, 392 client_area_bounds.width(), kClientEdgeThickness), 393 toolbar_color); 394 canvas->FillRect(gfx::Rect(client_area_bounds.right(), client_area_top, 395 kClientEdgeThickness, 396 client_area_bottom + kClientEdgeThickness - client_area_top), 397 toolbar_color); 398 } 399 400 void GlassBrowserFrameView::LayoutAvatar() { 401 // Even though the avatar is used for both incognito and profiles we always 402 // use the incognito icon to layout the avatar button. The profile icon 403 // can be customized so we can't depend on its size to perform layout. 404 gfx::ImageSkia incognito_icon = browser_view()->GetOTRAvatarIcon(); 405 406 int avatar_x = NonClientBorderThickness() + kAvatarLeftSpacing; 407 // Move this avatar icon by the size of window controls to prevent it from 408 // being rendered over them in RTL languages. This code also needs to adjust 409 // the width of a tab strip to avoid decreasing this size twice. (See the 410 // comment in GetBoundsForTabStrip().) 411 if (base::i18n::IsRTL()) 412 avatar_x += width() - frame()->GetMinimizeButtonOffset(); 413 414 int avatar_bottom = GetTabStripInsets(false).top + 415 browser_view()->GetTabStripHeight() - kAvatarBottomSpacing; 416 int avatar_restored_y = avatar_bottom - incognito_icon.height(); 417 int avatar_y = frame()->IsMaximized() ? 418 (NonClientTopBorderHeight(false) + kTabstripTopShadowThickness) : 419 avatar_restored_y; 420 avatar_bounds_.SetRect(avatar_x, avatar_y, incognito_icon.width(), 421 browser_view()->ShouldShowAvatar() ? (avatar_bottom - avatar_y) : 0); 422 if (avatar_button()) 423 avatar_button()->SetBoundsRect(avatar_bounds_); 424 } 425 426 void GlassBrowserFrameView::LayoutClientView() { 427 client_view_bounds_ = CalculateClientAreaBounds(width(), height()); 428 } 429 430 gfx::Insets GlassBrowserFrameView::GetClientAreaInsets() const { 431 if (!browser_view()->IsTabStripVisible()) 432 return gfx::Insets(); 433 434 const int top_height = NonClientTopBorderHeight(false); 435 const int border_thickness = NonClientBorderThickness(); 436 return gfx::Insets(top_height, 437 border_thickness, 438 border_thickness, 439 border_thickness); 440 } 441 442 gfx::Rect GlassBrowserFrameView::CalculateClientAreaBounds(int width, 443 int height) const { 444 gfx::Rect bounds(0, 0, width, height); 445 bounds.Inset(GetClientAreaInsets()); 446 return bounds; 447 } 448 449 void GlassBrowserFrameView::StartThrobber() { 450 if (!throbber_running_) { 451 throbber_running_ = true; 452 throbber_frame_ = 0; 453 InitThrobberIcons(); 454 SendMessage(views::HWNDForWidget(frame()), WM_SETICON, 455 static_cast<WPARAM>(ICON_SMALL), 456 reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_])); 457 } 458 } 459 460 void GlassBrowserFrameView::StopThrobber() { 461 if (throbber_running_) { 462 throbber_running_ = false; 463 464 HICON frame_icon = NULL; 465 466 // Check if hosted BrowserView has a window icon to use. 467 if (browser_view()->ShouldShowWindowIcon()) { 468 gfx::ImageSkia icon = browser_view()->GetWindowIcon(); 469 if (!icon.isNull()) 470 frame_icon = IconUtil::CreateHICONFromSkBitmap(*icon.bitmap()); 471 } 472 473 // Fallback to class icon. 474 if (!frame_icon) { 475 frame_icon = reinterpret_cast<HICON>(GetClassLongPtr( 476 views::HWNDForWidget(frame()), GCLP_HICONSM)); 477 } 478 479 // This will reset the small icon which we set in the throbber code. 480 // WM_SETICON with NULL icon restores the icon for title bar but not 481 // for taskbar. See http://crbug.com/29996 482 SendMessage(views::HWNDForWidget(frame()), WM_SETICON, 483 static_cast<WPARAM>(ICON_SMALL), 484 reinterpret_cast<LPARAM>(frame_icon)); 485 } 486 } 487 488 void GlassBrowserFrameView::DisplayNextThrobberFrame() { 489 throbber_frame_ = (throbber_frame_ + 1) % kThrobberIconCount; 490 SendMessage(views::HWNDForWidget(frame()), WM_SETICON, 491 static_cast<WPARAM>(ICON_SMALL), 492 reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_])); 493 } 494 495 void GlassBrowserFrameView::Observe( 496 int type, 497 const content::NotificationSource& source, 498 const content::NotificationDetails& details) { 499 switch (type) { 500 case chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED: 501 UpdateAvatarInfo(); 502 break; 503 default: 504 NOTREACHED() << "Got a notification we didn't register for!"; 505 break; 506 } 507 } 508 509 // static 510 void GlassBrowserFrameView::InitThrobberIcons() { 511 static bool initialized = false; 512 if (!initialized) { 513 for (int i = 0; i < kThrobberIconCount; ++i) { 514 throbber_icons_[i] = 515 ui::LoadThemeIconFromResourcesDataDLL(IDI_THROBBER_01 + i); 516 DCHECK(throbber_icons_[i]); 517 } 518 initialized = true; 519 } 520 } 521