ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / opaque_browser_frame_view.cc
blob22ea8e4a11405fffd5b2b7796fa12deb3bad5853
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.
5 #include "chrome/browser/ui/views/frame/opaque_browser_frame_view.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/profiles/profiles_state.h"
15 #include "chrome/browser/signin/signin_header_helper.h"
16 #include "chrome/browser/themes/theme_properties.h"
17 #include "chrome/browser/ui/views/frame/browser_frame.h"
18 #include "chrome/browser/ui/views/frame/browser_view.h"
19 #include "chrome/browser/ui/views/frame/opaque_browser_frame_view_layout.h"
20 #include "chrome/browser/ui/views/frame/opaque_browser_frame_view_platform_specific.h"
21 #include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
22 #include "chrome/browser/ui/views/profiles/new_avatar_button.h"
23 #include "chrome/browser/ui/views/tab_icon_view.h"
24 #include "chrome/browser/ui/views/tabs/tab_strip.h"
25 #include "chrome/browser/ui/views/theme_image_mapper.h"
26 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "components/signin/core/common/profile_management_switches.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/web_contents.h"
31 #include "grit/theme_resources.h"
32 #include "ui/accessibility/ax_view_state.h"
33 #include "ui/base/hit_test.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/base/theme_provider.h"
37 #include "ui/gfx/canvas.h"
38 #include "ui/gfx/font_list.h"
39 #include "ui/gfx/geometry/rect_conversions.h"
40 #include "ui/gfx/image/image.h"
41 #include "ui/gfx/image/image_skia.h"
42 #include "ui/gfx/path.h"
43 #include "ui/resources/grit/ui_resources.h"
44 #include "ui/views/controls/button/image_button.h"
45 #include "ui/views/controls/image_view.h"
46 #include "ui/views/controls/label.h"
47 #include "ui/views/layout/layout_constants.h"
48 #include "ui/views/views_delegate.h"
49 #include "ui/views/widget/root_view.h"
50 #include "ui/views/window/frame_background.h"
51 #include "ui/views/window/window_shape.h"
53 #if defined(ENABLE_SUPERVISED_USERS)
54 #include "chrome/browser/ui/views/profiles/supervised_user_avatar_label.h"
55 #endif
57 #if defined(OS_LINUX)
58 #include "ui/views/controls/menu/menu_runner.h"
59 #endif
61 using content::WebContents;
63 namespace {
65 // While resize areas on Windows are normally the same size as the window
66 // borders, our top area is shrunk by 1 px to make it easier to move the window
67 // around with our thinner top grabbable strip. (Incidentally, our side and
68 // bottom resize areas don't match the frame border thickness either -- they
69 // span the whole nonclient area, so there's no "dead zone" for the mouse.)
70 const int kTopResizeAdjust = 1;
72 // In the window corners, the resize areas don't actually expand bigger, but the
73 // 16 px at the end of each edge triggers diagonal resizing.
74 const int kResizeAreaCornerSize = 16;
76 // The content left/right images have a shadow built into them.
77 const int kContentEdgeShadowThickness = 2;
79 // The icon never shrinks below 16 px on a side.
80 const int kIconMinimumSize = 16;
82 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
83 // The number of pixels to move the frame background image upwards when using
84 // the GTK+ theme and the titlebar is condensed.
85 const int kGTKThemeCondensedFrameTopInset = 15;
86 #endif
88 } // namespace
90 ///////////////////////////////////////////////////////////////////////////////
91 // OpaqueBrowserFrameView, public:
93 OpaqueBrowserFrameView::OpaqueBrowserFrameView(BrowserFrame* frame,
94 BrowserView* browser_view)
95 : BrowserNonClientFrameView(frame, browser_view),
96 layout_(new OpaqueBrowserFrameViewLayout(this)),
97 minimize_button_(nullptr),
98 maximize_button_(nullptr),
99 restore_button_(nullptr),
100 close_button_(nullptr),
101 window_icon_(nullptr),
102 window_title_(nullptr),
103 frame_background_(new views::FrameBackground()) {
104 SetLayoutManager(layout_);
106 minimize_button_ = InitWindowCaptionButton(IDR_MINIMIZE,
107 IDR_MINIMIZE_H,
108 IDR_MINIMIZE_P,
109 IDR_MINIMIZE_BUTTON_MASK,
110 IDS_ACCNAME_MINIMIZE,
111 VIEW_ID_MINIMIZE_BUTTON);
112 maximize_button_ = InitWindowCaptionButton(IDR_MAXIMIZE,
113 IDR_MAXIMIZE_H,
114 IDR_MAXIMIZE_P,
115 IDR_MAXIMIZE_BUTTON_MASK,
116 IDS_ACCNAME_MAXIMIZE,
117 VIEW_ID_MAXIMIZE_BUTTON);
118 restore_button_ = InitWindowCaptionButton(IDR_RESTORE,
119 IDR_RESTORE_H,
120 IDR_RESTORE_P,
121 IDR_RESTORE_BUTTON_MASK,
122 IDS_ACCNAME_RESTORE,
123 VIEW_ID_RESTORE_BUTTON);
124 close_button_ = InitWindowCaptionButton(IDR_CLOSE,
125 IDR_CLOSE_H,
126 IDR_CLOSE_P,
127 IDR_CLOSE_BUTTON_MASK,
128 IDS_ACCNAME_CLOSE,
129 VIEW_ID_CLOSE_BUTTON);
131 // Initializing the TabIconView is expensive, so only do it if we need to.
132 if (browser_view->ShouldShowWindowIcon()) {
133 window_icon_ = new TabIconView(this, this);
134 window_icon_->set_is_light(true);
135 window_icon_->set_id(VIEW_ID_WINDOW_ICON);
136 AddChildView(window_icon_);
137 window_icon_->Update();
140 window_title_ = new views::Label(
141 browser_view->GetWindowTitle(),
142 gfx::FontList(BrowserFrame::GetTitleFontList()));
143 window_title_->SetVisible(browser_view->ShouldShowWindowTitle());
144 window_title_->SetEnabledColor(SK_ColorWHITE);
145 window_title_->SetSubpixelRenderingEnabled(false);
146 window_title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
147 window_title_->set_id(VIEW_ID_WINDOW_TITLE);
148 AddChildView(window_title_);
150 UpdateAvatar();
152 platform_observer_.reset(OpaqueBrowserFrameViewPlatformSpecific::Create(
153 this, layout_, browser_view->browser()->profile()));
156 OpaqueBrowserFrameView::~OpaqueBrowserFrameView() {
159 ///////////////////////////////////////////////////////////////////////////////
160 // OpaqueBrowserFrameView, BrowserNonClientFrameView implementation:
162 gfx::Rect OpaqueBrowserFrameView::GetBoundsForTabStrip(
163 views::View* tabstrip) const {
164 if (!tabstrip)
165 return gfx::Rect();
167 return layout_->GetBoundsForTabStrip(tabstrip->GetPreferredSize(), width());
170 int OpaqueBrowserFrameView::GetTopInset() const {
171 return browser_view()->IsTabStripVisible() ?
172 layout_->GetTabStripInsetsTop(false) :
173 layout_->NonClientTopBorderHeight(false);
176 int OpaqueBrowserFrameView::GetThemeBackgroundXInset() const {
177 return 0;
180 void OpaqueBrowserFrameView::UpdateThrobber(bool running) {
181 // TODO(robliao): Remove ScopedTracker below once crbug.com/461137 is fixed.
182 tracked_objects::ScopedTracker tracking_profile(
183 FROM_HERE_WITH_EXPLICIT_FUNCTION(
184 "461137 OpaqueBrowserFrameView::UpdateThrobber"));
186 if (window_icon_)
187 window_icon_->Update();
190 gfx::Size OpaqueBrowserFrameView::GetMinimumSize() const {
191 return layout_->GetMinimumSize(width());
194 ///////////////////////////////////////////////////////////////////////////////
195 // OpaqueBrowserFrameView, views::NonClientFrameView implementation:
197 gfx::Rect OpaqueBrowserFrameView::GetBoundsForClientView() const {
198 return layout_->client_view_bounds();
201 gfx::Rect OpaqueBrowserFrameView::GetWindowBoundsForClientBounds(
202 const gfx::Rect& client_bounds) const {
203 return layout_->GetWindowBoundsForClientBounds(client_bounds);
206 bool OpaqueBrowserFrameView::IsWithinAvatarMenuButtons(
207 const gfx::Point& point) const {
208 if (avatar_button() &&
209 avatar_button()->GetMirroredBounds().Contains(point)) {
210 return true;
212 if (new_avatar_button() &&
213 new_avatar_button()->GetMirroredBounds().Contains(point)) {
214 return true;
217 return false;
220 int OpaqueBrowserFrameView::NonClientHitTest(const gfx::Point& point) {
221 if (!bounds().Contains(point))
222 return HTNOWHERE;
224 // See if the point is within the avatar menu button.
225 if (IsWithinAvatarMenuButtons(point))
226 return HTCLIENT;
227 #if defined(ENABLE_SUPERVISED_USERS)
228 // ...or within the avatar label, if it's a supervised user.
229 if ((supervised_user_avatar_label() &&
230 supervised_user_avatar_label()->GetMirroredBounds().Contains(point)))
231 return HTCLIENT;
232 #endif
234 int frame_component = frame()->client_view()->NonClientHitTest(point);
236 // See if we're in the sysmenu region. We still have to check the tabstrip
237 // first so that clicks in a tab don't get treated as sysmenu clicks.
238 gfx::Rect sysmenu_rect(IconBounds());
239 // In maximized mode we extend the rect to the screen corner to take advantage
240 // of Fitts' Law.
241 if (layout_->IsTitleBarCondensed())
242 sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
243 sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
244 if (sysmenu_rect.Contains(point))
245 return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
247 if (frame_component != HTNOWHERE)
248 return frame_component;
250 // Then see if the point is within any of the window controls.
251 if (close_button_ && close_button_->visible() &&
252 close_button_->GetMirroredBounds().Contains(point))
253 return HTCLOSE;
254 if (restore_button_ && restore_button_->visible() &&
255 restore_button_->GetMirroredBounds().Contains(point))
256 return HTMAXBUTTON;
257 if (maximize_button_ && maximize_button_->visible() &&
258 maximize_button_->GetMirroredBounds().Contains(point))
259 return HTMAXBUTTON;
260 if (minimize_button_ && minimize_button_->visible() &&
261 minimize_button_->GetMirroredBounds().Contains(point))
262 return HTMINBUTTON;
264 views::WidgetDelegate* delegate = frame()->widget_delegate();
265 if (!delegate) {
266 LOG(WARNING) << "delegate is null, returning safe default.";
267 return HTCAPTION;
269 int window_component = GetHTComponentForFrame(point, TopResizeHeight(),
270 NonClientBorderThickness(), kResizeAreaCornerSize, kResizeAreaCornerSize,
271 delegate->CanResize());
272 // Fall back to the caption if no other component matches.
273 return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
276 void OpaqueBrowserFrameView::GetWindowMask(const gfx::Size& size,
277 gfx::Path* window_mask) {
278 DCHECK(window_mask);
280 if (layout_->IsTitleBarCondensed() || frame()->IsFullscreen())
281 return;
283 views::GetDefaultWindowMask(size, window_mask);
286 void OpaqueBrowserFrameView::ResetWindowControls() {
287 restore_button_->SetState(views::CustomButton::STATE_NORMAL);
288 minimize_button_->SetState(views::CustomButton::STATE_NORMAL);
289 maximize_button_->SetState(views::CustomButton::STATE_NORMAL);
290 // The close button isn't affected by this constraint.
293 void OpaqueBrowserFrameView::UpdateWindowIcon() {
294 if (window_icon_)
295 window_icon_->SchedulePaint();
298 void OpaqueBrowserFrameView::UpdateWindowTitle() {
299 if (!frame()->IsFullscreen())
300 window_title_->SchedulePaint();
303 void OpaqueBrowserFrameView::SizeConstraintsChanged() {
306 ///////////////////////////////////////////////////////////////////////////////
307 // OpaqueBrowserFrameView, views::View overrides:
309 void OpaqueBrowserFrameView::GetAccessibleState(
310 ui::AXViewState* state) {
311 state->role = ui::AX_ROLE_TITLE_BAR;
314 ///////////////////////////////////////////////////////////////////////////////
315 // OpaqueBrowserFrameView, views::ButtonListener implementation:
317 void OpaqueBrowserFrameView::ButtonPressed(views::Button* sender,
318 const ui::Event& event) {
319 if (sender == minimize_button_) {
320 frame()->Minimize();
321 } else if (sender == maximize_button_) {
322 frame()->Maximize();
323 } else if (sender == restore_button_) {
324 frame()->Restore();
325 } else if (sender == close_button_) {
326 frame()->Close();
327 } else if (sender == new_avatar_button()) {
328 BrowserWindow::AvatarBubbleMode mode =
329 BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT;
330 if (event.IsMouseEvent() &&
331 static_cast<const ui::MouseEvent&>(event).IsRightMouseButton()) {
332 mode = BrowserWindow::AVATAR_BUBBLE_MODE_FAST_USER_SWITCH;
334 browser_view()->ShowAvatarBubbleFromAvatarButton(
335 mode,
336 signin::ManageAccountsParams());
340 void OpaqueBrowserFrameView::OnMenuButtonClicked(views::View* source,
341 const gfx::Point& point) {
342 #if defined(OS_LINUX)
343 views::MenuRunner menu_runner(frame()->GetSystemMenuModel(),
344 views::MenuRunner::HAS_MNEMONICS);
345 ignore_result(menu_runner.RunMenuAt(browser_view()->GetWidget(),
346 window_icon_,
347 window_icon_->GetBoundsInScreen(),
348 views::MENU_ANCHOR_TOPLEFT,
349 ui::MENU_SOURCE_MOUSE));
350 #endif
353 ///////////////////////////////////////////////////////////////////////////////
354 // OpaqueBrowserFrameView, TabIconView::TabContentsProvider implementation:
356 bool OpaqueBrowserFrameView::ShouldTabIconViewAnimate() const {
357 // This function is queried during the creation of the window as the
358 // TabIconView we host is initialized, so we need to null check the selected
359 // WebContents because in this condition there is not yet a selected tab.
360 WebContents* current_tab = browser_view()->GetActiveWebContents();
361 return current_tab ? current_tab->IsLoading() : false;
364 gfx::ImageSkia OpaqueBrowserFrameView::GetFaviconForTabIconView() {
365 views::WidgetDelegate* delegate = frame()->widget_delegate();
366 if (!delegate) {
367 LOG(WARNING) << "delegate is null, returning safe default.";
368 return gfx::ImageSkia();
370 return delegate->GetWindowIcon();
373 ///////////////////////////////////////////////////////////////////////////////
374 // OpaqueBrowserFrameView, OpaqueBrowserFrameViewLayoutDelegate implementation:
376 bool OpaqueBrowserFrameView::ShouldShowWindowIcon() const {
377 views::WidgetDelegate* delegate = frame()->widget_delegate();
378 return ShouldShowWindowTitleBar() && delegate &&
379 delegate->ShouldShowWindowIcon();
382 bool OpaqueBrowserFrameView::ShouldShowWindowTitle() const {
383 // |delegate| may be null if called from callback of InputMethodChanged while
384 // a window is being destroyed.
385 // See more discussion at http://crosbug.com/8958
386 views::WidgetDelegate* delegate = frame()->widget_delegate();
387 return ShouldShowWindowTitleBar() && delegate &&
388 delegate->ShouldShowWindowTitle();
391 base::string16 OpaqueBrowserFrameView::GetWindowTitle() const {
392 return frame()->widget_delegate()->GetWindowTitle();
395 int OpaqueBrowserFrameView::GetIconSize() const {
396 #if defined(OS_WIN)
397 // This metric scales up if either the titlebar height or the titlebar font
398 // size are increased.
399 return GetSystemMetrics(SM_CYSMICON);
400 #else
401 return std::max(BrowserFrame::GetTitleFontList().GetHeight(),
402 kIconMinimumSize);
403 #endif
406 bool OpaqueBrowserFrameView::ShouldLeaveOffsetNearTopBorder() const {
407 return frame()->ShouldLeaveOffsetNearTopBorder();
410 gfx::Size OpaqueBrowserFrameView::GetBrowserViewMinimumSize() const {
411 return browser_view()->GetMinimumSize();
414 bool OpaqueBrowserFrameView::ShouldShowCaptionButtons() const {
415 return ShouldShowWindowTitleBar();
418 bool OpaqueBrowserFrameView::ShouldShowAvatar() const {
419 return browser_view()->ShouldShowAvatar();
422 bool OpaqueBrowserFrameView::IsRegularOrGuestSession() const {
423 return browser_view()->IsRegularOrGuestSession();
426 gfx::ImageSkia OpaqueBrowserFrameView::GetOTRAvatarIcon() const {
427 return browser_view()->GetOTRAvatarIcon();
430 bool OpaqueBrowserFrameView::IsMaximized() const {
431 return frame()->IsMaximized();
434 bool OpaqueBrowserFrameView::IsMinimized() const {
435 return frame()->IsMinimized();
438 bool OpaqueBrowserFrameView::IsFullscreen() const {
439 return frame()->IsFullscreen();
442 bool OpaqueBrowserFrameView::IsTabStripVisible() const {
443 return browser_view()->IsTabStripVisible();
446 int OpaqueBrowserFrameView::GetTabStripHeight() const {
447 return browser_view()->GetTabStripHeight();
450 gfx::Size OpaqueBrowserFrameView::GetTabstripPreferredSize() const {
451 gfx::Size s = browser_view()->tabstrip()->GetPreferredSize();
452 return s;
455 ///////////////////////////////////////////////////////////////////////////////
456 // OpaqueBrowserFrameView, protected:
458 // views::View:
459 void OpaqueBrowserFrameView::OnPaint(gfx::Canvas* canvas) {
460 if (frame()->IsFullscreen())
461 return; // Nothing is visible, so don't bother to paint.
463 if (layout_->IsTitleBarCondensed())
464 PaintMaximizedFrameBorder(canvas);
465 else
466 PaintRestoredFrameBorder(canvas);
468 // The window icon and title are painted by their respective views.
469 /* TODO(pkasting): If this window is active, we should also draw a drop
470 * shadow on the title. This is tricky, because we don't want to hardcode a
471 * shadow color (since we want to work with various themes), but we can't
472 * alpha-blend either (since the Windows text APIs don't really do this).
473 * So we'd need to sample the background color at the right location and
474 * synthesize a good shadow color. */
476 if (browser_view()->IsToolbarVisible())
477 PaintToolbarBackground(canvas);
478 if (!layout_->IsTitleBarCondensed())
479 PaintRestoredClientEdge(canvas);
482 // BrowserNonClientFrameView:
483 void OpaqueBrowserFrameView::UpdateNewStyleAvatar() {
484 UpdateNewStyleAvatarInfo(this, NewAvatarButton::THEMED_BUTTON);
487 ///////////////////////////////////////////////////////////////////////////////
488 // OpaqueBrowserFrameView, private:
490 // views::NonClientFrameView:
491 bool OpaqueBrowserFrameView::DoesIntersectRect(const views::View* target,
492 const gfx::Rect& rect) const {
493 CHECK_EQ(target, this);
494 if (!views::ViewTargeterDelegate::DoesIntersectRect(this, rect)) {
495 // |rect| is outside OpaqueBrowserFrameView's bounds.
496 return false;
499 // If the rect is outside the bounds of the client area, claim it.
500 gfx::RectF rect_in_client_view_coords_f(rect);
501 View::ConvertRectToTarget(this, frame()->client_view(),
502 &rect_in_client_view_coords_f);
503 gfx::Rect rect_in_client_view_coords = gfx::ToEnclosingRect(
504 rect_in_client_view_coords_f);
505 if (!frame()->client_view()->HitTestRect(rect_in_client_view_coords))
506 return true;
508 // Otherwise, claim |rect| only if it is above the bottom of the tabstrip in
509 // a non-tab portion.
510 TabStrip* tabstrip = browser_view()->tabstrip();
511 if (!tabstrip || !browser_view()->IsTabStripVisible())
512 return false;
514 gfx::RectF rect_in_tabstrip_coords_f(rect);
515 View::ConvertRectToTarget(this, tabstrip, &rect_in_tabstrip_coords_f);
516 gfx::Rect rect_in_tabstrip_coords = gfx::ToEnclosingRect(
517 rect_in_tabstrip_coords_f);
518 if (rect_in_tabstrip_coords.bottom() > tabstrip->GetLocalBounds().bottom()) {
519 // |rect| is below the tabstrip.
520 return false;
523 if (tabstrip->HitTestRect(rect_in_tabstrip_coords)) {
524 // Claim |rect| if it is in a non-tab portion of the tabstrip.
525 return tabstrip->IsRectInWindowCaption(rect_in_tabstrip_coords);
528 // We claim |rect| because it is above the bottom of the tabstrip, but
529 // not in the tabstrip itself. In particular, the avatar label/button is left
530 // of the tabstrip and the window controls are right of the tabstrip.
531 return true;
534 views::ImageButton* OpaqueBrowserFrameView::InitWindowCaptionButton(
535 int normal_image_id,
536 int hot_image_id,
537 int pushed_image_id,
538 int mask_image_id,
539 int accessibility_string_id,
540 ViewID view_id) {
541 views::ImageButton* button = new views::ImageButton(this);
542 ui::ThemeProvider* tp = frame()->GetThemeProvider();
543 button->SetImage(views::CustomButton::STATE_NORMAL,
544 tp->GetImageSkiaNamed(normal_image_id));
545 button->SetImage(views::CustomButton::STATE_HOVERED,
546 tp->GetImageSkiaNamed(hot_image_id));
547 button->SetImage(views::CustomButton::STATE_PRESSED,
548 tp->GetImageSkiaNamed(pushed_image_id));
549 if (browser_view()->IsBrowserTypeNormal()) {
550 button->SetBackground(
551 tp->GetColor(ThemeProperties::COLOR_BUTTON_BACKGROUND),
552 tp->GetImageSkiaNamed(IDR_THEME_WINDOW_CONTROL_BACKGROUND),
553 tp->GetImageSkiaNamed(mask_image_id));
555 button->SetAccessibleName(
556 l10n_util::GetStringUTF16(accessibility_string_id));
557 button->set_id(view_id);
558 AddChildView(button);
559 return button;
562 int OpaqueBrowserFrameView::FrameBorderThickness(bool restored) const {
563 return layout_->FrameBorderThickness(restored);
566 int OpaqueBrowserFrameView::TopResizeHeight() const {
567 return FrameBorderThickness(false) - kTopResizeAdjust;
570 int OpaqueBrowserFrameView::NonClientBorderThickness() const {
571 return layout_->NonClientBorderThickness();
574 gfx::Rect OpaqueBrowserFrameView::IconBounds() const {
575 return layout_->IconBounds();
578 bool OpaqueBrowserFrameView::ShouldShowWindowTitleBar() const {
579 // Do not show the custom title bar if the system title bar option is enabled.
580 if (!frame()->UseCustomFrame())
581 return false;
583 // Do not show caption buttons if the window manager is forcefully providing a
584 // title bar (e.g., in Ubuntu Unity, if the window is maximized).
585 if (!views::ViewsDelegate::views_delegate)
586 return true;
587 return !views::ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar(
588 IsMaximized());
591 void OpaqueBrowserFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
592 frame_background_->set_frame_color(GetFrameColor());
593 frame_background_->set_theme_image(GetFrameImage());
594 frame_background_->set_theme_overlay_image(GetFrameOverlayImage());
595 frame_background_->set_top_area_height(GetTopAreaHeight());
597 ui::ThemeProvider* tp = GetThemeProvider();
598 frame_background_->SetSideImages(
599 tp->GetImageSkiaNamed(IDR_WINDOW_LEFT_SIDE),
600 tp->GetImageSkiaNamed(IDR_WINDOW_TOP_CENTER),
601 tp->GetImageSkiaNamed(IDR_WINDOW_RIGHT_SIDE),
602 tp->GetImageSkiaNamed(IDR_WINDOW_BOTTOM_CENTER));
603 frame_background_->SetCornerImages(
604 tp->GetImageSkiaNamed(IDR_WINDOW_TOP_LEFT_CORNER),
605 tp->GetImageSkiaNamed(IDR_WINDOW_TOP_RIGHT_CORNER),
606 tp->GetImageSkiaNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER),
607 tp->GetImageSkiaNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER));
608 frame_background_->PaintRestored(canvas, this);
610 // Note: When we don't have a toolbar, we need to draw some kind of bottom
611 // edge here. Because the App Window graphics we use for this have an
612 // attached client edge and their sizing algorithm is a little involved, we do
613 // all this in PaintRestoredClientEdge().
616 void OpaqueBrowserFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
617 ui::ThemeProvider* tp = GetThemeProvider();
618 frame_background_->set_frame_color(GetFrameColor());
619 frame_background_->set_theme_image(GetFrameImage());
620 frame_background_->set_theme_overlay_image(GetFrameOverlayImage());
621 frame_background_->set_top_area_height(GetTopAreaHeight());
622 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
623 // The window manager typically shows a gradient in the native title bar (when
624 // the system title bar pref is set, or when maximized on Ubuntu). Hide the
625 // gradient in the tab strip (by shifting it up vertically) to avoid a
626 // double-gradient effect.
627 if (tp->UsingSystemTheme())
628 frame_background_->set_maximized_top_inset(kGTKThemeCondensedFrameTopInset);
629 #endif
631 frame_background_->PaintMaximized(canvas, this);
633 // TODO(jamescook): Migrate this into FrameBackground.
634 if (!browser_view()->IsToolbarVisible()) {
635 // There's no toolbar to edge the frame border, so we need to draw a bottom
636 // edge. The graphic we use for this has a built in client edge, so we clip
637 // it off the bottom.
638 gfx::ImageSkia* top_center = tp->GetImageSkiaNamed(IDR_APP_TOP_CENTER);
639 int edge_height = top_center->height() - kClientEdgeThickness;
640 canvas->TileImageInt(*top_center, 0,
641 frame()->client_view()->y() - edge_height, width(), edge_height);
645 void OpaqueBrowserFrameView::PaintToolbarBackground(gfx::Canvas* canvas) {
646 gfx::Rect toolbar_bounds(browser_view()->GetToolbarBounds());
647 if (toolbar_bounds.IsEmpty())
648 return;
649 gfx::Point toolbar_origin(toolbar_bounds.origin());
650 ConvertPointToTarget(browser_view(), this, &toolbar_origin);
651 toolbar_bounds.set_origin(toolbar_origin);
653 int x = toolbar_bounds.x();
654 int w = toolbar_bounds.width();
655 int y = toolbar_bounds.y();
656 int h = toolbar_bounds.height();
658 // Gross hack: We split the toolbar images into two pieces, since sometimes
659 // (popup mode) the toolbar isn't tall enough to show the whole image. The
660 // split happens between the top shadow section and the bottom gradient
661 // section so that we never break the gradient.
662 int split_point = kFrameShadowThickness * 2;
663 int bottom_y = y + split_point;
664 ui::ThemeProvider* tp = GetThemeProvider();
665 gfx::ImageSkia* toolbar_left = tp->GetImageSkiaNamed(
666 IDR_CONTENT_TOP_LEFT_CORNER);
667 int bottom_edge_height = std::min(toolbar_left->height(), h) - split_point;
669 // Split our canvas out so we can mask out the corners of the toolbar
670 // without masking out the frame.
671 canvas->SaveLayerAlpha(
672 255, gfx::Rect(x - kClientEdgeThickness, y, w + kClientEdgeThickness * 3,
673 h));
675 // Paint the bottom rect.
676 canvas->FillRect(gfx::Rect(x, bottom_y, w, bottom_edge_height),
677 tp->GetColor(ThemeProperties::COLOR_TOOLBAR));
679 // Tile the toolbar image starting at the frame edge on the left and where the
680 // horizontal tabstrip is (or would be) on the top.
681 gfx::ImageSkia* theme_toolbar = tp->GetImageSkiaNamed(IDR_THEME_TOOLBAR);
682 canvas->TileImageInt(*theme_toolbar,
683 x + GetThemeBackgroundXInset(),
684 bottom_y - GetTopInset(),
685 x, bottom_y, w, theme_toolbar->height());
687 // Draw rounded corners for the tab.
688 gfx::ImageSkia* toolbar_left_mask =
689 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER_MASK);
690 gfx::ImageSkia* toolbar_right_mask =
691 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER_MASK);
693 // We mask out the corners by using the DestinationIn transfer mode,
694 // which keeps the RGB pixels from the destination and the alpha from
695 // the source.
696 SkPaint paint;
697 paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
699 // Mask the left edge.
700 int left_x = x - kContentEdgeShadowThickness;
701 canvas->DrawImageInt(*toolbar_left_mask, 0, 0, toolbar_left_mask->width(),
702 split_point, left_x, y, toolbar_left_mask->width(),
703 split_point, false, paint);
704 canvas->DrawImageInt(*toolbar_left_mask, 0,
705 toolbar_left_mask->height() - bottom_edge_height,
706 toolbar_left_mask->width(), bottom_edge_height, left_x, bottom_y,
707 toolbar_left_mask->width(), bottom_edge_height, false, paint);
709 // Mask the right edge.
710 int right_x =
711 x + w - toolbar_right_mask->width() + kContentEdgeShadowThickness;
712 canvas->DrawImageInt(*toolbar_right_mask, 0, 0, toolbar_right_mask->width(),
713 split_point, right_x, y, toolbar_right_mask->width(),
714 split_point, false, paint);
715 canvas->DrawImageInt(*toolbar_right_mask, 0,
716 toolbar_right_mask->height() - bottom_edge_height,
717 toolbar_right_mask->width(), bottom_edge_height, right_x, bottom_y,
718 toolbar_right_mask->width(), bottom_edge_height, false, paint);
719 canvas->Restore();
721 canvas->DrawImageInt(*toolbar_left, 0, 0, toolbar_left->width(), split_point,
722 left_x, y, toolbar_left->width(), split_point, false);
723 canvas->DrawImageInt(*toolbar_left, 0,
724 toolbar_left->height() - bottom_edge_height, toolbar_left->width(),
725 bottom_edge_height, left_x, bottom_y, toolbar_left->width(),
726 bottom_edge_height, false);
728 gfx::ImageSkia* toolbar_center =
729 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_CENTER);
730 canvas->TileImageInt(*toolbar_center, 0, 0, left_x + toolbar_left->width(),
731 y, right_x - (left_x + toolbar_left->width()),
732 split_point);
734 gfx::ImageSkia* toolbar_right = tp->GetImageSkiaNamed(
735 IDR_CONTENT_TOP_RIGHT_CORNER);
736 canvas->DrawImageInt(*toolbar_right, 0, 0, toolbar_right->width(),
737 split_point, right_x, y, toolbar_right->width(), split_point, false);
738 canvas->DrawImageInt(*toolbar_right, 0,
739 toolbar_right->height() - bottom_edge_height, toolbar_right->width(),
740 bottom_edge_height, right_x, bottom_y, toolbar_right->width(),
741 bottom_edge_height, false);
743 // Draw the content/toolbar separator.
744 canvas->FillRect(
745 gfx::Rect(x + kClientEdgeThickness,
746 toolbar_bounds.bottom() - kClientEdgeThickness,
747 w - (2 * kClientEdgeThickness),
748 kClientEdgeThickness),
749 ThemeProperties::GetDefaultColor(
750 ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
753 void OpaqueBrowserFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
754 ui::ThemeProvider* tp = GetThemeProvider();
755 int client_area_top = frame()->client_view()->y();
756 int image_top = client_area_top;
758 gfx::Rect client_area_bounds =
759 layout_->CalculateClientAreaBounds(width(), height());
760 SkColor toolbar_color = tp->GetColor(ThemeProperties::COLOR_TOOLBAR);
762 if (browser_view()->IsToolbarVisible()) {
763 // The client edge images always start below the toolbar corner images. The
764 // client edge filled rects start there or at the bottom of the toolbar,
765 // whichever is shorter.
766 gfx::Rect toolbar_bounds(browser_view()->GetToolbarBounds());
768 gfx::ImageSkia* content_top_left_corner =
769 tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER);
770 // TODO(oshima): Sanity checks for crbug.com/374273. Remove when it's fixed.
771 CHECK(content_top_left_corner);
772 CHECK(!content_top_left_corner->isNull());
774 image_top += toolbar_bounds.y() + content_top_left_corner->height();
775 client_area_top = std::min(image_top,
776 client_area_top + toolbar_bounds.bottom() - kClientEdgeThickness);
777 } else if (!browser_view()->IsTabStripVisible()) {
778 // The toolbar isn't going to draw a client edge for us, so draw one
779 // ourselves.
780 gfx::ImageSkia* top_left = tp->GetImageSkiaNamed(IDR_APP_TOP_LEFT);
781 gfx::ImageSkia* top_center = tp->GetImageSkiaNamed(IDR_APP_TOP_CENTER);
782 gfx::ImageSkia* top_right = tp->GetImageSkiaNamed(IDR_APP_TOP_RIGHT);
783 int top_edge_y = client_area_top - top_center->height();
784 int height = client_area_top - top_edge_y;
786 canvas->DrawImageInt(*top_left, 0, 0, top_left->width(), height,
787 client_area_bounds.x() - top_left->width(), top_edge_y,
788 top_left->width(), height, false);
789 canvas->TileImageInt(*top_center, 0, 0, client_area_bounds.x(), top_edge_y,
790 client_area_bounds.width(), std::min(height, top_center->height()));
791 canvas->DrawImageInt(*top_right, 0, 0, top_right->width(), height,
792 client_area_bounds.right(), top_edge_y,
793 top_right->width(), height, false);
795 // Draw the toolbar color across the top edge.
796 canvas->FillRect(gfx::Rect(client_area_bounds.x() - kClientEdgeThickness,
797 client_area_top - kClientEdgeThickness,
798 client_area_bounds.width() + (2 * kClientEdgeThickness),
799 kClientEdgeThickness), toolbar_color);
802 int client_area_bottom =
803 std::max(client_area_top, height() - NonClientBorderThickness());
804 int image_height = client_area_bottom - image_top;
806 // Draw the client edge images.
807 gfx::ImageSkia* right = tp->GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
808 canvas->TileImageInt(*right, client_area_bounds.right(), image_top,
809 right->width(), image_height);
810 canvas->DrawImageInt(
811 *tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER),
812 client_area_bounds.right(), client_area_bottom);
813 gfx::ImageSkia* bottom = tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
814 canvas->TileImageInt(*bottom, client_area_bounds.x(),
815 client_area_bottom, client_area_bounds.width(),
816 bottom->height());
817 gfx::ImageSkia* bottom_left =
818 tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
819 canvas->DrawImageInt(*bottom_left,
820 client_area_bounds.x() - bottom_left->width(), client_area_bottom);
821 gfx::ImageSkia* left = tp->GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE);
822 canvas->TileImageInt(*left, client_area_bounds.x() - left->width(),
823 image_top, left->width(), image_height);
825 // Draw the toolbar color so that the client edges show the right color even
826 // where not covered by the toolbar image. NOTE: We do this after drawing the
827 // images because the images are meant to alpha-blend atop the frame whereas
828 // these rects are meant to be fully opaque, without anything overlaid.
829 canvas->FillRect(gfx::Rect(client_area_bounds.x() - kClientEdgeThickness,
830 client_area_top, kClientEdgeThickness,
831 client_area_bottom + kClientEdgeThickness - client_area_top),
832 toolbar_color);
833 canvas->FillRect(gfx::Rect(client_area_bounds.x(), client_area_bottom,
834 client_area_bounds.width(), kClientEdgeThickness),
835 toolbar_color);
836 canvas->FillRect(gfx::Rect(client_area_bounds.right(), client_area_top,
837 kClientEdgeThickness,
838 client_area_bottom + kClientEdgeThickness - client_area_top),
839 toolbar_color);
842 SkColor OpaqueBrowserFrameView::GetFrameColor() const {
843 bool is_incognito = browser_view()->IsOffTheRecord();
844 ThemeProperties::OverwritableByUserThemeProperty color_id;
845 if (ShouldPaintAsActive()) {
846 color_id = is_incognito ?
847 ThemeProperties::COLOR_FRAME_INCOGNITO :
848 ThemeProperties::COLOR_FRAME;
849 } else {
850 color_id = is_incognito ?
851 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE :
852 ThemeProperties::COLOR_FRAME_INACTIVE;
855 if (browser_view()->IsBrowserTypeNormal() ||
856 platform_observer_->IsUsingSystemTheme()) {
857 return GetThemeProvider()->GetColor(color_id);
860 // Never theme app and popup windows unless the |platform_observer_|
861 // requested an override.
862 return ThemeProperties::GetDefaultColor(color_id);
865 gfx::ImageSkia* OpaqueBrowserFrameView::GetFrameImage() const {
866 bool is_incognito = browser_view()->IsOffTheRecord();
867 int resource_id;
868 if (browser_view()->IsBrowserTypeNormal()) {
869 if (ShouldPaintAsActive()) {
870 resource_id = is_incognito ?
871 IDR_THEME_FRAME_INCOGNITO : IDR_THEME_FRAME;
872 } else {
873 resource_id = is_incognito ?
874 IDR_THEME_FRAME_INCOGNITO_INACTIVE : IDR_THEME_FRAME_INACTIVE;
876 return GetThemeProvider()->GetImageSkiaNamed(resource_id);
878 if (ShouldPaintAsActive()) {
879 resource_id = is_incognito ?
880 IDR_THEME_FRAME_INCOGNITO : IDR_FRAME;
881 } else {
882 resource_id = is_incognito ?
883 IDR_THEME_FRAME_INCOGNITO_INACTIVE : IDR_THEME_FRAME_INACTIVE;
886 if (platform_observer_->IsUsingSystemTheme()) {
887 // We want to use theme images provided by the system theme when enabled,
888 // even if we are an app or popup window.
889 return GetThemeProvider()->GetImageSkiaNamed(resource_id);
892 // Otherwise, never theme app and popup windows.
893 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
894 return rb.GetImageSkiaNamed(chrome::MapThemeImage(
895 chrome::GetHostDesktopTypeForNativeWindow(
896 browser_view()->GetNativeWindow()),
897 resource_id));
900 gfx::ImageSkia* OpaqueBrowserFrameView::GetFrameOverlayImage() const {
901 ui::ThemeProvider* tp = GetThemeProvider();
902 if (tp->HasCustomImage(IDR_THEME_FRAME_OVERLAY) &&
903 browser_view()->IsBrowserTypeNormal() &&
904 !browser_view()->IsOffTheRecord()) {
905 return tp->GetImageSkiaNamed(ShouldPaintAsActive() ?
906 IDR_THEME_FRAME_OVERLAY : IDR_THEME_FRAME_OVERLAY_INACTIVE);
908 return nullptr;
911 int OpaqueBrowserFrameView::GetTopAreaHeight() const {
912 gfx::ImageSkia* frame_image = GetFrameImage();
913 int top_area_height = frame_image->height();
914 if (browser_view()->IsTabStripVisible()) {
915 top_area_height = std::max(top_area_height,
916 GetBoundsForTabStrip(browser_view()->tabstrip()).bottom());
918 return top_area_height;