app_panel_browser_frame_view.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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/views/frame/app_panel_browser_frame_view.h" 6 7#include "app/l10n_util.h" 8#include "app/resource_bundle.h" 9#include "base/compiler_specific.h" 10#include "base/utf_string_conversions.h" 11#include "chrome/browser/tab_contents/tab_contents.h" 12#include "chrome/browser/views/frame/browser_frame.h" 13#include "chrome/browser/views/frame/browser_view.h" 14#include "gfx/canvas.h" 15#include "gfx/font.h" 16#include "gfx/path.h" 17#include "grit/app_resources.h" 18#include "grit/chromium_strings.h" 19#include "grit/generated_resources.h" 20#include "grit/theme_resources.h" 21#include "views/controls/button/image_button.h" 22#include "views/window/window.h" 23#include "views/window/window_resources.h" 24 25#if defined(OS_LINUX) 26#include "views/window/hit_test.h" 27#endif 28 29namespace { 30// The frame border is only visible in restored mode and is hardcoded to 1 px on 31// each side regardless of the system window border size. 32const int kFrameBorderThickness = 1; 33// In the window corners, the resize areas don't actually expand bigger, but the 34// 16 px at the end of each edge triggers diagonal resizing. 35const int kResizeAreaCornerSize = 16; 36// The titlebar never shrinks too short to show the caption button plus some 37// padding below it. 38const int kCaptionButtonHeightWithPadding = 27; 39// The titlebar has a 2 px 3D edge along the bottom, and we reserve 2 px (1 for 40// border, 1 for padding) along the top. 41const int kTitlebarTopAndBottomEdgeThickness = 2; 42// The icon is inset 6 px from the left frame border. 43const int kIconLeftSpacing = 6; 44// The icon never shrinks below 16 px on a side. 45const int kIconMinimumSize = 16; 46// There is a 4 px gap between the icon and the title text. 47const int kIconTitleSpacing = 4; 48// There is a 5 px gap between the title text and the close button. 49const int kTitleCloseButtonSpacing = 5; 50// There is a 4 px gap between the close button and the frame border. 51const int kCloseButtonFrameBorderSpacing = 4; 52} 53 54/////////////////////////////////////////////////////////////////////////////// 55// AppPanelBrowserFrameView, public: 56 57AppPanelBrowserFrameView::AppPanelBrowserFrameView(BrowserFrame* frame, 58 BrowserView* browser_view) 59 : BrowserNonClientFrameView(), 60 ALLOW_THIS_IN_INITIALIZER_LIST( 61 close_button_(new views::ImageButton(this))), 62 window_icon_(NULL), 63 frame_(frame), 64 browser_view_(browser_view) { 65 DCHECK(browser_view->ShouldShowWindowIcon()); 66 DCHECK(browser_view->ShouldShowWindowTitle()); 67 68 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 69 close_button_->SetImage(views::CustomButton::BS_NORMAL, 70 rb.GetBitmapNamed(IDR_CLOSE_BAR)); 71 close_button_->SetImage(views::CustomButton::BS_HOT, 72 rb.GetBitmapNamed(IDR_CLOSE_BAR_H)); 73 close_button_->SetImage(views::CustomButton::BS_PUSHED, 74 rb.GetBitmapNamed(IDR_CLOSE_BAR_P)); 75 close_button_->SetAccessibleName( 76 UTF16ToWide(l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE))); 77 AddChildView(close_button_); 78 79 window_icon_ = new TabIconView(this); 80 window_icon_->set_is_light(true); 81 AddChildView(window_icon_); 82 window_icon_->Update(); 83} 84 85AppPanelBrowserFrameView::~AppPanelBrowserFrameView() { 86} 87 88/////////////////////////////////////////////////////////////////////////////// 89// AppPanelBrowserFrameView, BrowserNonClientFrameView implementation: 90 91gfx::Rect AppPanelBrowserFrameView::GetBoundsForTabStrip( 92 BaseTabStrip* tabstrip) const { 93 // App panels never show a tab strip. 94 NOTREACHED(); 95 return gfx::Rect(); 96} 97 98int AppPanelBrowserFrameView::GetHorizontalTabStripVerticalOffset( 99 bool restored) const { 100 // App panels are not themed and don't need this. 101 return 0; 102} 103 104void AppPanelBrowserFrameView::UpdateThrobber(bool running) { 105 window_icon_->Update(); 106} 107 108gfx::Size AppPanelBrowserFrameView::GetMinimumSize() { 109 gfx::Size min_size(browser_view_->GetMinimumSize()); 110 int border_thickness = NonClientBorderThickness(); 111 min_size.Enlarge(2 * border_thickness, 112 NonClientTopBorderHeight() + border_thickness); 113 114 min_size.set_width(std::max(min_size.width(), 115 (2 * FrameBorderThickness()) + kIconLeftSpacing + IconSize() + 116 kTitleCloseButtonSpacing + kCloseButtonFrameBorderSpacing)); 117 return min_size; 118} 119 120/////////////////////////////////////////////////////////////////////////////// 121// AppPanelBrowserFrameView, views::NonClientFrameView implementation: 122 123gfx::Rect AppPanelBrowserFrameView::GetBoundsForClientView() const { 124 return client_view_bounds_; 125} 126 127bool AppPanelBrowserFrameView::AlwaysUseCustomFrame() const { 128 return true; 129} 130 131bool AppPanelBrowserFrameView::AlwaysUseNativeFrame() const { 132 return frame_->AlwaysUseNativeFrame(); 133} 134 135gfx::Rect AppPanelBrowserFrameView::GetWindowBoundsForClientBounds( 136 const gfx::Rect& client_bounds) const { 137 int top_height = NonClientTopBorderHeight(); 138 int border_thickness = NonClientBorderThickness(); 139 return gfx::Rect(std::max(0, client_bounds.x() - border_thickness), 140 std::max(0, client_bounds.y() - top_height), 141 client_bounds.width() + (2 * border_thickness), 142 client_bounds.height() + top_height + border_thickness); 143} 144 145int AppPanelBrowserFrameView::NonClientHitTest(const gfx::Point& point) { 146 if (!bounds().Contains(point)) 147 return HTNOWHERE; 148 149 int frame_component = 150 frame_->GetWindow()->GetClientView()->NonClientHitTest(point); 151 152 // See if we're in the sysmenu region. (We check the ClientView first to be 153 // consistent with OpaqueBrowserFrameView; it's not really necessary here.) 154 gfx::Rect sysmenu_rect(IconBounds()); 155 // In maximized mode we extend the rect to the screen corner to take advantage 156 // of Fitts' Law. 157 if (frame_->GetWindow()->IsMaximized()) 158 sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom()); 159 sysmenu_rect.set_x(MirroredLeftPointForRect(sysmenu_rect)); 160 if (sysmenu_rect.Contains(point)) 161 return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU; 162 163 if (frame_component != HTNOWHERE) 164 return frame_component; 165 166 // Then see if the point is within any of the window controls. 167 if (close_button_->IsVisible() && 168 close_button_->GetBounds(APPLY_MIRRORING_TRANSFORMATION).Contains(point)) 169 return HTCLOSE; 170 171 int window_component = GetHTComponentForFrame(point, 172 NonClientBorderThickness(), NonClientBorderThickness(), 173 kResizeAreaCornerSize, kResizeAreaCornerSize, 174 frame_->GetWindow()->GetDelegate()->CanResize()); 175 // Fall back to the caption if no other component matches. 176 return (window_component == HTNOWHERE) ? HTCAPTION : window_component; 177} 178 179void AppPanelBrowserFrameView::GetWindowMask(const gfx::Size& size, 180 gfx::Path* window_mask) { 181 DCHECK(window_mask); 182 183 if (frame_->GetWindow()->IsMaximized()) 184 return; 185 186 // Redefine the window visible region for the new size. 187 window_mask->moveTo(0, 3); 188 window_mask->lineTo(1, 2); 189 window_mask->lineTo(1, 1); 190 window_mask->lineTo(2, 1); 191 window_mask->lineTo(3, 0); 192 193 window_mask->lineTo(SkIntToScalar(size.width() - 3), 0); 194 window_mask->lineTo(SkIntToScalar(size.width() - 2), 1); 195 window_mask->lineTo(SkIntToScalar(size.width() - 1), 1); 196 window_mask->lineTo(SkIntToScalar(size.width() - 1), 2); 197 window_mask->lineTo(SkIntToScalar(size.width()), 3); 198 199 window_mask->lineTo(SkIntToScalar(size.width()), 200 SkIntToScalar(size.height())); 201 window_mask->lineTo(0, SkIntToScalar(size.height())); 202 window_mask->close(); 203} 204 205void AppPanelBrowserFrameView::EnableClose(bool enable) { 206 close_button_->SetEnabled(enable); 207} 208 209void AppPanelBrowserFrameView::ResetWindowControls() { 210 // The close button isn't affected by this constraint. 211} 212 213/////////////////////////////////////////////////////////////////////////////// 214// AppPanelBrowserFrameView, views::View overrides: 215 216void AppPanelBrowserFrameView::Paint(gfx::Canvas* canvas) { 217 views::Window* window = frame_->GetWindow(); 218 if (window->IsMaximized()) 219 PaintMaximizedFrameBorder(canvas); 220 else 221 PaintRestoredFrameBorder(canvas); 222 PaintTitleBar(canvas); 223 if (!window->IsMaximized()) 224 PaintRestoredClientEdge(canvas); 225} 226 227void AppPanelBrowserFrameView::Layout() { 228 LayoutWindowControls(); 229 LayoutTitleBar(); 230 client_view_bounds_ = CalculateClientAreaBounds(width(), height()); 231} 232 233/////////////////////////////////////////////////////////////////////////////// 234// AppPanelBrowserFrameView, views::ButtonListener implementation: 235 236void AppPanelBrowserFrameView::ButtonPressed(views::Button* sender, 237 const views::Event& event) { 238 if (sender == close_button_) 239 frame_->GetWindow()->Close(); 240} 241 242/////////////////////////////////////////////////////////////////////////////// 243// AppPanelBrowserFrameView, TabIconView::TabContentsProvider implementation: 244 245bool AppPanelBrowserFrameView::ShouldTabIconViewAnimate() const { 246 // This function is queried during the creation of the window as the 247 // TabIconView we host is initialized, so we need to NULL check the selected 248 // TabContents because in this condition there is not yet a selected tab. 249 TabContents* current_tab = browser_view_->GetSelectedTabContents(); 250 return current_tab ? current_tab->is_loading() : false; 251} 252 253SkBitmap AppPanelBrowserFrameView::GetFavIconForTabIconView() { 254 return frame_->GetWindow()->GetDelegate()->GetWindowIcon(); 255} 256 257/////////////////////////////////////////////////////////////////////////////// 258// AppPanelBrowserFrameView, private: 259 260int AppPanelBrowserFrameView::FrameBorderThickness() const { 261 return frame_->GetWindow()->IsMaximized() ? 0 : kFrameBorderThickness; 262} 263 264int AppPanelBrowserFrameView::NonClientBorderThickness() const { 265 return FrameBorderThickness() + 266 (frame_->GetWindow()->IsMaximized() ? 0 : kClientEdgeThickness); 267} 268 269int AppPanelBrowserFrameView::NonClientTopBorderHeight() const { 270 return std::max(FrameBorderThickness() + IconSize(), 271 FrameBorderThickness() + kCaptionButtonHeightWithPadding) + 272 TitlebarBottomThickness(); 273} 274 275int AppPanelBrowserFrameView::TitlebarBottomThickness() const { 276 return kTitlebarTopAndBottomEdgeThickness + 277 (frame_->GetWindow()->IsMaximized() ? 0 : kClientEdgeThickness); 278} 279 280int AppPanelBrowserFrameView::IconSize() const { 281#if defined(OS_WIN) 282 // This metric scales up if either the titlebar height or the titlebar font 283 // size are increased. 284 return GetSystemMetrics(SM_CYSMICON); 285#else 286 return std::max(BrowserFrame::GetTitleFont().height(), kIconMinimumSize); 287#endif 288} 289 290gfx::Rect AppPanelBrowserFrameView::IconBounds() const { 291 int size = IconSize(); 292 int frame_thickness = FrameBorderThickness(); 293 // Our frame border has a different "3D look" than Windows'. Theirs has a 294 // more complex gradient on the top that they push their icon/title below; 295 // then the maximized window cuts this off and the icon/title are centered 296 // in the remaining space. Because the apparent shape of our border is 297 // simpler, using the same positioning makes things look slightly uncentered 298 // with restored windows, so when the window is restored, instead of 299 // calculating the remaining space from below the frame border, we calculate 300 // from below the top border-plus-padding. 301 int unavailable_px_at_top = frame_->GetWindow()->IsMaximized() ? 302 frame_thickness : kTitlebarTopAndBottomEdgeThickness; 303 // When the icon is shorter than the minimum space we reserve for the caption 304 // button, we vertically center it. We want to bias rounding to put extra 305 // space above the icon, since the 3D edge (+ client edge, for restored 306 // windows) below looks (to the eye) more like additional space than does the 307 // border + padding (or nothing at all, for maximized windows) above; hence 308 // the +1. 309 int y = unavailable_px_at_top + (NonClientTopBorderHeight() - 310 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2; 311 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size); 312} 313 314void AppPanelBrowserFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) { 315 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 316 317 SkBitmap* top_left_corner = rb.GetBitmapNamed(IDR_WINDOW_TOP_LEFT_CORNER); 318 SkBitmap* top_right_corner = 319 rb.GetBitmapNamed(IDR_WINDOW_TOP_RIGHT_CORNER); 320 SkBitmap* top_edge = rb.GetBitmapNamed(IDR_WINDOW_TOP_CENTER); 321 SkBitmap* right_edge = rb.GetBitmapNamed(IDR_WINDOW_RIGHT_SIDE); 322 SkBitmap* left_edge = rb.GetBitmapNamed(IDR_WINDOW_LEFT_SIDE); 323 SkBitmap* bottom_left_corner = 324 rb.GetBitmapNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER); 325 SkBitmap* bottom_right_corner = 326 rb.GetBitmapNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER); 327 SkBitmap* bottom_edge = rb.GetBitmapNamed(IDR_WINDOW_BOTTOM_CENTER); 328 329 // Window frame mode and color. 330 SkBitmap* theme_frame; 331 SkColor frame_color; 332 if (ShouldPaintAsActive()) { 333 theme_frame = rb.GetBitmapNamed(IDR_FRAME_APP_PANEL); 334 frame_color = ResourceBundle::frame_color_app_panel; 335 } else { 336 theme_frame = rb.GetBitmapNamed(IDR_FRAME_APP_PANEL); // TODO 337 frame_color = ResourceBundle::frame_color_app_panel_inactive; 338 } 339 340 // Fill with the frame color first so we have a constant background for 341 // areas not covered by the theme image. 342 canvas->FillRectInt(frame_color, 0, 0, width(), theme_frame->height()); 343 // Now fill down the sides. 344 canvas->FillRectInt(frame_color, 0, theme_frame->height(), left_edge->width(), 345 height() - theme_frame->height()); 346 canvas->FillRectInt(frame_color, width() - right_edge->width(), 347 theme_frame->height(), right_edge->width(), 348 height() - theme_frame->height()); 349 // Now fill the bottom area. 350 canvas->FillRectInt(frame_color, left_edge->width(), 351 height() - bottom_edge->height(), 352 width() - left_edge->width() - right_edge->width(), 353 bottom_edge->height()); 354 355 // Draw the theme frame. 356 canvas->TileImageInt(*theme_frame, 0, 0, width(), theme_frame->height()); 357 358 // Top. 359 canvas->DrawBitmapInt(*top_left_corner, 0, 0); 360 canvas->TileImageInt(*top_edge, top_left_corner->width(), 0, 361 width() - top_right_corner->width(), top_edge->height()); 362 canvas->DrawBitmapInt(*top_right_corner, 363 width() - top_right_corner->width(), 0); 364 365 // Right. 366 canvas->TileImageInt(*right_edge, width() - right_edge->width(), 367 top_right_corner->height(), right_edge->width(), 368 height() - top_right_corner->height() - bottom_right_corner->height()); 369 370 // Bottom. 371 canvas->DrawBitmapInt(*bottom_right_corner, 372 width() - bottom_right_corner->width(), 373 height() - bottom_right_corner->height()); 374 canvas->TileImageInt(*bottom_edge, bottom_left_corner->width(), 375 height() - bottom_edge->height(), 376 width() - bottom_left_corner->width() - bottom_right_corner->width(), 377 bottom_edge->height()); 378 canvas->DrawBitmapInt(*bottom_left_corner, 0, 379 height() - bottom_left_corner->height()); 380 381 // Left. 382 canvas->TileImageInt(*left_edge, 0, top_left_corner->height(), 383 left_edge->width(), 384 height() - top_left_corner->height() - bottom_left_corner->height()); 385} 386 387void AppPanelBrowserFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) { 388 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 389 390 SkBitmap* frame_image = rb.GetBitmapNamed(IDR_FRAME_APP_PANEL); 391 canvas->TileImageInt(*frame_image, 0, FrameBorderThickness(), width(), 392 frame_image->height()); 393 394 // The bottom of the titlebar actually comes from the top of the Client Edge 395 // graphic, with the actual client edge clipped off the bottom. 396 SkBitmap* titlebar_bottom = rb.GetBitmapNamed(IDR_APP_TOP_CENTER); 397 int edge_height = titlebar_bottom->height() - kClientEdgeThickness; 398 canvas->TileImageInt(*titlebar_bottom, 0, 399 frame_->GetWindow()->GetClientView()->y() - edge_height, 400 width(), edge_height); 401} 402 403void AppPanelBrowserFrameView::PaintTitleBar(gfx::Canvas* canvas) { 404 // The window icon is painted by the TabIconView. 405 views::WindowDelegate* d = frame_->GetWindow()->GetDelegate(); 406 canvas->DrawStringInt(d->GetWindowTitle(), BrowserFrame::GetTitleFont(), 407 SK_ColorBLACK, MirroredLeftPointForRect(title_bounds_), title_bounds_.y(), 408 title_bounds_.width(), title_bounds_.height()); 409} 410 411void AppPanelBrowserFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) { 412 gfx::Rect client_area_bounds = CalculateClientAreaBounds(width(), height()); 413 int client_area_top = client_area_bounds.y(); 414 415 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 416 SkBitmap* top_left = rb.GetBitmapNamed(IDR_APP_TOP_LEFT); 417 SkBitmap* top = rb.GetBitmapNamed(IDR_APP_TOP_CENTER); 418 SkBitmap* top_right = rb.GetBitmapNamed(IDR_APP_TOP_RIGHT); 419 SkBitmap* right = rb.GetBitmapNamed(IDR_CONTENT_RIGHT_SIDE); 420 SkBitmap* bottom_right = 421 rb.GetBitmapNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER); 422 SkBitmap* bottom = rb.GetBitmapNamed(IDR_CONTENT_BOTTOM_CENTER); 423 SkBitmap* bottom_left = 424 rb.GetBitmapNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER); 425 SkBitmap* left = rb.GetBitmapNamed(IDR_CONTENT_LEFT_SIDE); 426 427 // Top. 428 int top_edge_y = client_area_top - top->height(); 429 canvas->DrawBitmapInt(*top_left, client_area_bounds.x() - top_left->width(), 430 top_edge_y); 431 canvas->TileImageInt(*top, client_area_bounds.x(), top_edge_y, 432 client_area_bounds.width(), top->height()); 433 canvas->DrawBitmapInt(*top_right, client_area_bounds.right(), top_edge_y); 434 435 // Right. 436 int client_area_bottom = 437 std::max(client_area_top, client_area_bounds.bottom()); 438 int client_area_height = client_area_bottom - client_area_top; 439 canvas->TileImageInt(*right, client_area_bounds.right(), client_area_top, 440 right->width(), client_area_height); 441 442 // Bottom. 443 canvas->DrawBitmapInt(*bottom_right, client_area_bounds.right(), 444 client_area_bottom); 445 canvas->TileImageInt(*bottom, client_area_bounds.x(), client_area_bottom, 446 client_area_bounds.width(), bottom_right->height()); 447 canvas->DrawBitmapInt(*bottom_left, 448 client_area_bounds.x() - bottom_left->width(), client_area_bottom); 449 450 // Left. 451 canvas->TileImageInt(*left, client_area_bounds.x() - left->width(), 452 client_area_top, left->width(), client_area_height); 453 454 // Draw the toolbar color to fill in the edges. 455 canvas->DrawRectInt(ResourceBundle::toolbar_color, 456 client_area_bounds.x() - kClientEdgeThickness, 457 client_area_top - kClientEdgeThickness, 458 client_area_bounds.width() + kClientEdgeThickness, 459 client_area_bottom - client_area_top + kClientEdgeThickness); 460} 461 462void AppPanelBrowserFrameView::LayoutWindowControls() { 463 close_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT, 464 views::ImageButton::ALIGN_BOTTOM); 465 bool is_maximized = frame_->GetWindow()->IsMaximized(); 466 // There should always be the same number of non-border pixels visible to the 467 // side of the close button. In maximized mode we extend the button to the 468 // screen corner to obey Fitts' Law. 469 int right_extra_width = is_maximized ? kCloseButtonFrameBorderSpacing : 0; 470 gfx::Size close_button_size = close_button_->GetPreferredSize(); 471 int close_button_y = 472 (NonClientTopBorderHeight() - close_button_size.height()) / 2; 473 int top_extra_height = is_maximized ? close_button_y : 0; 474 close_button_->SetBounds(width() - FrameBorderThickness() - 475 kCloseButtonFrameBorderSpacing - close_button_size.width(), 476 close_button_y - top_extra_height, 477 close_button_size.width() + right_extra_width, 478 close_button_size.height() + top_extra_height); 479} 480 481void AppPanelBrowserFrameView::LayoutTitleBar() { 482 // Size the icon first; the window title is based on the icon position. 483 gfx::Rect icon_bounds(IconBounds()); 484 window_icon_->SetBounds(icon_bounds); 485 486 // Size the title. 487 int title_x = icon_bounds.right() + kIconTitleSpacing; 488 int title_height = BrowserFrame::GetTitleFont().GetHeight(); 489 // We bias the title position so that when the difference between the icon 490 // and title heights is odd, the extra pixel of the title is above the 491 // vertical midline rather than below. This compensates for how the icon is 492 // already biased downwards (see IconBounds()) and helps prevent descenders 493 // on the title from overlapping the 3D edge at the bottom of the titlebar. 494 title_bounds_.SetRect(title_x, 495 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2), 496 std::max(0, close_button_->x() - kTitleCloseButtonSpacing - title_x), 497 title_height); 498} 499 500gfx::Rect AppPanelBrowserFrameView::CalculateClientAreaBounds(int width, 501 int height) const { 502 int top_height = NonClientTopBorderHeight(); 503 int border_thickness = NonClientBorderThickness(); 504 return gfx::Rect(border_thickness, top_height, 505 std::max(0, width - (2 * border_thickness)), 506 std::max(0, height - top_height - border_thickness)); 507} 508