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 "ui/views/window/custom_frame_view.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "grit/ui_resources.h"
11 #include "grit/ui_strings.h"
12 #include "ui/base/hit_test.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/font.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/gfx/path.h"
19 #include "ui/views/color_constants.h"
20 #include "ui/views/controls/button/image_button.h"
21 #include "ui/views/views_delegate.h"
22 #include "ui/views/widget/native_widget_private.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
25 #include "ui/views/window/client_view.h"
26 #include "ui/views/window/frame_background.h"
27 #include "ui/views/window/window_resources.h"
28 #include "ui/views/window/window_shape.h"
34 // The frame border is only visible in restored mode and is hardcoded to 4 px on
35 // each side regardless of the system window border size.
36 const int kFrameBorderThickness = 4;
37 // In the window corners, the resize areas don't actually expand bigger, but the
38 // 16 px at the end of each edge triggers diagonal resizing.
39 const int kResizeAreaCornerSize = 16;
40 // The titlebar never shrinks too short to show the caption button plus some
42 const int kCaptionButtonHeightWithPadding = 19;
43 // The titlebar has a 2 px 3D edge along the top and bottom.
44 const int kTitlebarTopAndBottomEdgeThickness = 2;
45 // The icon is inset 2 px from the left frame border.
46 const int kIconLeftSpacing = 2;
47 // The icon never shrinks below 16 px on a side.
48 const int kIconMinimumSize = 16;
49 // The space between the window icon and the title text.
50 const int kTitleIconOffsetX = 4;
51 // The space between the title text and the caption buttons.
52 const int kTitleCaptionSpacing = 5;
54 #if defined(OS_CHROMEOS)
55 // Chrome OS uses a dark gray.
56 const SkColor kDefaultColorFrame = SkColorSetRGB(109, 109, 109);
57 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(176, 176, 176);
59 // Windows and Linux use a blue.
60 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
61 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
64 const gfx::FontList& GetTitleFontList() {
65 static const gfx::FontList title_font_list =
66 internal::NativeWidgetPrivate::GetWindowTitleFontList();
67 return title_font_list;
72 ///////////////////////////////////////////////////////////////////////////////
73 // CustomFrameView, public:
75 CustomFrameView::CustomFrameView()
78 minimize_button_(NULL),
79 maximize_button_(NULL),
80 restore_button_(NULL),
82 should_show_maximize_button_(false),
83 frame_background_(new FrameBackground()) {
86 CustomFrameView::~CustomFrameView() {
89 void CustomFrameView::Init(Widget* frame) {
92 close_button_ = new ImageButton(this);
93 close_button_->SetAccessibleName(
94 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE));
96 // Close button images will be set in LayoutWindowControls().
97 AddChildView(close_button_);
99 minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE,
100 IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
102 maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE,
103 IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
105 restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE,
106 IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
108 should_show_maximize_button_ = frame_->widget_delegate()->CanMaximize();
110 if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
111 window_icon_ = new ImageButton(this);
112 AddChildView(window_icon_);
116 ///////////////////////////////////////////////////////////////////////////////
117 // CustomFrameView, NonClientFrameView implementation:
119 gfx::Rect CustomFrameView::GetBoundsForClientView() const {
120 return client_view_bounds_;
123 gfx::Rect CustomFrameView::GetWindowBoundsForClientBounds(
124 const gfx::Rect& client_bounds) const {
125 int top_height = NonClientTopBorderHeight();
126 int border_thickness = NonClientBorderThickness();
127 return gfx::Rect(client_bounds.x() - border_thickness,
128 client_bounds.y() - top_height,
129 client_bounds.width() + (2 * border_thickness),
130 client_bounds.height() + top_height + border_thickness);
133 int CustomFrameView::NonClientHitTest(const gfx::Point& point) {
135 if (!bounds().Contains(point))
138 int frame_component = frame_->client_view()->NonClientHitTest(point);
140 // See if we're in the sysmenu region. (We check the ClientView first to be
141 // consistent with OpaqueBrowserFrameView; it's not really necessary here.)
142 gfx::Rect sysmenu_rect(IconBounds());
143 // In maximized mode we extend the rect to the screen corner to take advantage
145 if (frame_->IsMaximized())
146 sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
147 sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
148 if (sysmenu_rect.Contains(point))
149 return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
151 if (frame_component != HTNOWHERE)
152 return frame_component;
154 // Then see if the point is within any of the window controls.
155 if (close_button_->GetMirroredBounds().Contains(point))
157 if (restore_button_->GetMirroredBounds().Contains(point))
159 if (maximize_button_->GetMirroredBounds().Contains(point))
161 if (minimize_button_->GetMirroredBounds().Contains(point))
163 if (window_icon_ && window_icon_->GetMirroredBounds().Contains(point))
166 int window_component = GetHTComponentForFrame(point, FrameBorderThickness(),
167 NonClientBorderThickness(), kResizeAreaCornerSize, kResizeAreaCornerSize,
168 frame_->widget_delegate()->CanResize());
169 // Fall back to the caption if no other component matches.
170 return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
173 void CustomFrameView::GetWindowMask(const gfx::Size& size,
174 gfx::Path* window_mask) {
176 if (frame_->IsMaximized() || !ShouldShowTitleBarAndBorder())
179 GetDefaultWindowMask(size, window_mask);
182 void CustomFrameView::ResetWindowControls() {
183 restore_button_->SetState(CustomButton::STATE_NORMAL);
184 minimize_button_->SetState(CustomButton::STATE_NORMAL);
185 maximize_button_->SetState(CustomButton::STATE_NORMAL);
186 // The close button isn't affected by this constraint.
189 void CustomFrameView::UpdateWindowIcon() {
191 window_icon_->SchedulePaint();
194 void CustomFrameView::UpdateWindowTitle() {
195 if (frame_->widget_delegate()->ShouldShowWindowTitle())
196 SchedulePaintInRect(title_bounds_);
199 ///////////////////////////////////////////////////////////////////////////////
200 // CustomFrameView, View overrides:
202 void CustomFrameView::OnPaint(gfx::Canvas* canvas) {
203 if (!ShouldShowTitleBarAndBorder())
206 if (frame_->IsMaximized())
207 PaintMaximizedFrameBorder(canvas);
209 PaintRestoredFrameBorder(canvas);
210 PaintTitleBar(canvas);
211 if (ShouldShowClientEdge())
212 PaintRestoredClientEdge(canvas);
215 void CustomFrameView::Layout() {
216 if (ShouldShowTitleBarAndBorder()) {
217 LayoutWindowControls();
224 gfx::Size CustomFrameView::GetPreferredSize() {
225 return frame_->non_client_view()->GetWindowBoundsForClientBounds(
226 gfx::Rect(frame_->client_view()->GetPreferredSize())).size();
229 gfx::Size CustomFrameView::GetMinimumSize() {
230 return frame_->non_client_view()->GetWindowBoundsForClientBounds(
231 gfx::Rect(frame_->client_view()->GetMinimumSize())).size();
234 gfx::Size CustomFrameView::GetMaximumSize() {
235 gfx::Size max_size = frame_->client_view()->GetMaximumSize();
236 gfx::Size converted_size =
237 frame_->non_client_view()->GetWindowBoundsForClientBounds(
238 gfx::Rect(max_size)).size();
239 return gfx::Size(max_size.width() == 0 ? 0 : converted_size.width(),
240 max_size.height() == 0 ? 0 : converted_size.height());
243 ///////////////////////////////////////////////////////////////////////////////
244 // CustomFrameView, ButtonListener implementation:
246 void CustomFrameView::ButtonPressed(Button* sender, const ui::Event& event) {
247 if (sender == close_button_)
249 else if (sender == minimize_button_)
251 else if (sender == maximize_button_)
253 else if (sender == restore_button_)
257 ///////////////////////////////////////////////////////////////////////////////
258 // CustomFrameView, private:
260 int CustomFrameView::FrameBorderThickness() const {
261 return frame_->IsMaximized() ? 0 : kFrameBorderThickness;
264 int CustomFrameView::NonClientBorderThickness() const {
265 // In maximized mode, we don't show a client edge.
266 return FrameBorderThickness() +
267 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
270 int CustomFrameView::NonClientTopBorderHeight() const {
271 return std::max(FrameBorderThickness() + IconSize(),
272 CaptionButtonY() + kCaptionButtonHeightWithPadding) +
273 TitlebarBottomThickness();
276 int CustomFrameView::CaptionButtonY() const {
277 // Maximized buttons start at window top so that even if their images aren't
278 // drawn flush with the screen edge, they still obey Fitts' Law.
279 return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness;
282 int CustomFrameView::TitlebarBottomThickness() const {
283 return kTitlebarTopAndBottomEdgeThickness +
284 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
287 int CustomFrameView::IconSize() const {
289 // This metric scales up if either the titlebar height or the titlebar font
290 // size are increased.
291 return GetSystemMetrics(SM_CYSMICON);
293 return std::max(GetTitleFontList().GetHeight(), kIconMinimumSize);
297 gfx::Rect CustomFrameView::IconBounds() const {
298 int size = IconSize();
299 int frame_thickness = FrameBorderThickness();
300 // Our frame border has a different "3D look" than Windows'. Theirs has a
301 // more complex gradient on the top that they push their icon/title below;
302 // then the maximized window cuts this off and the icon/title are centered
303 // in the remaining space. Because the apparent shape of our border is
304 // simpler, using the same positioning makes things look slightly uncentered
305 // with restored windows, so when the window is restored, instead of
306 // calculating the remaining space from below the frame border, we calculate
307 // from below the 3D edge.
308 int unavailable_px_at_top = frame_->IsMaximized() ?
309 frame_thickness : kTitlebarTopAndBottomEdgeThickness;
310 // When the icon is shorter than the minimum space we reserve for the caption
311 // button, we vertically center it. We want to bias rounding to put extra
312 // space above the icon, since the 3D edge (+ client edge, for restored
313 // windows) below looks (to the eye) more like additional space than does the
314 // 3D edge (or nothing at all, for maximized windows) above; hence the +1.
315 int y = unavailable_px_at_top + (NonClientTopBorderHeight() -
316 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2;
317 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size);
320 bool CustomFrameView::ShouldShowTitleBarAndBorder() const {
321 if (frame_->IsFullscreen())
324 if (ViewsDelegate::views_delegate) {
325 return ViewsDelegate::views_delegate->ShouldShowTitleBar() &&
326 !ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar(
327 frame_->IsMaximized());
333 bool CustomFrameView::ShouldShowClientEdge() const {
334 return !frame_->IsMaximized() && ShouldShowTitleBarAndBorder();
337 void CustomFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
338 frame_background_->set_frame_color(GetFrameColor());
339 const gfx::ImageSkia* frame_image = GetFrameImage();
340 frame_background_->set_theme_image(frame_image);
341 frame_background_->set_top_area_height(frame_image->height());
343 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
345 frame_background_->SetCornerImages(
346 rb.GetImageNamed(IDR_WINDOW_TOP_LEFT_CORNER).ToImageSkia(),
347 rb.GetImageNamed(IDR_WINDOW_TOP_RIGHT_CORNER).ToImageSkia(),
348 rb.GetImageNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER).ToImageSkia(),
349 rb.GetImageNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER).ToImageSkia());
350 frame_background_->SetSideImages(
351 rb.GetImageNamed(IDR_WINDOW_LEFT_SIDE).ToImageSkia(),
352 rb.GetImageNamed(IDR_WINDOW_TOP_CENTER).ToImageSkia(),
353 rb.GetImageNamed(IDR_WINDOW_RIGHT_SIDE).ToImageSkia(),
354 rb.GetImageNamed(IDR_WINDOW_BOTTOM_CENTER).ToImageSkia());
356 frame_background_->PaintRestored(canvas, this);
359 void CustomFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
360 const gfx::ImageSkia* frame_image = GetFrameImage();
361 frame_background_->set_theme_image(frame_image);
362 frame_background_->set_top_area_height(frame_image->height());
363 frame_background_->PaintMaximized(canvas, this);
365 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
367 // TODO(jamescook): Migrate this into FrameBackground.
368 // The bottom of the titlebar actually comes from the top of the Client Edge
369 // graphic, with the actual client edge clipped off the bottom.
370 const gfx::ImageSkia* titlebar_bottom = rb.GetImageNamed(
371 IDR_APP_TOP_CENTER).ToImageSkia();
372 int edge_height = titlebar_bottom->height() -
373 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
374 canvas->TileImageInt(*titlebar_bottom, 0,
375 frame_->client_view()->y() - edge_height, width(), edge_height);
378 void CustomFrameView::PaintTitleBar(gfx::Canvas* canvas) {
379 WidgetDelegate* delegate = frame_->widget_delegate();
381 // It seems like in some conditions we can be asked to paint after the window
382 // that contains us is WM_DESTROYed. At this point, our delegate is NULL. The
383 // correct long term fix may be to shut down the RootView in WM_DESTROY.
384 if (!delegate || !delegate->ShouldShowWindowTitle())
387 gfx::Rect rect = title_bounds_;
388 rect.set_x(GetMirroredXForRect(title_bounds_));
389 canvas->DrawStringRect(delegate->GetWindowTitle(), GetTitleFontList(),
390 SK_ColorWHITE, rect);
393 void CustomFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
394 gfx::Rect client_area_bounds = frame_->client_view()->bounds();
395 int client_area_top = client_area_bounds.y();
397 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
399 // Top: left, center, right sides.
400 const gfx::ImageSkia* top_left = rb.GetImageSkiaNamed(IDR_APP_TOP_LEFT);
401 const gfx::ImageSkia* top_center = rb.GetImageSkiaNamed(IDR_APP_TOP_CENTER);
402 const gfx::ImageSkia* top_right = rb.GetImageSkiaNamed(IDR_APP_TOP_RIGHT);
403 int top_edge_y = client_area_top - top_center->height();
404 canvas->DrawImageInt(*top_left,
405 client_area_bounds.x() - top_left->width(),
407 canvas->TileImageInt(*top_center,
408 client_area_bounds.x(),
410 client_area_bounds.width(),
411 top_center->height());
412 canvas->DrawImageInt(*top_right, client_area_bounds.right(), top_edge_y);
415 const gfx::ImageSkia* right = rb.GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
416 int client_area_bottom =
417 std::max(client_area_top, client_area_bounds.bottom());
418 int client_area_height = client_area_bottom - client_area_top;
419 canvas->TileImageInt(*right,
420 client_area_bounds.right(),
425 // Bottom: left, center, right sides.
426 const gfx::ImageSkia* bottom_left =
427 rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
428 const gfx::ImageSkia* bottom_center =
429 rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
430 const gfx::ImageSkia* bottom_right =
431 rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER);
433 canvas->DrawImageInt(*bottom_left,
434 client_area_bounds.x() - bottom_left->width(),
437 canvas->TileImageInt(*bottom_center,
438 client_area_bounds.x(),
440 client_area_bounds.width(),
441 bottom_right->height());
443 canvas->DrawImageInt(*bottom_right,
444 client_area_bounds.right(),
447 const gfx::ImageSkia* left = rb.GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE);
448 canvas->TileImageInt(*left,
449 client_area_bounds.x() - left->width(),
454 // Draw the color to fill in the edges.
455 canvas->FillRect(gfx::Rect(client_area_bounds.x() - 1,
457 client_area_bounds.width() + 1,
458 client_area_bottom - client_area_top + 1),
462 SkColor CustomFrameView::GetFrameColor() const {
463 return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive;
466 const gfx::ImageSkia* CustomFrameView::GetFrameImage() const {
467 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
468 frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia();
471 void CustomFrameView::LayoutWindowControls() {
472 close_button_->SetImageAlignment(ImageButton::ALIGN_LEFT,
473 ImageButton::ALIGN_BOTTOM);
474 int caption_y = CaptionButtonY();
475 bool is_maximized = frame_->IsMaximized();
476 // There should always be the same number of non-shadow pixels visible to the
477 // side of the caption buttons. In maximized mode we extend the rightmost
478 // button to the screen corner to obey Fitts' Law.
479 int right_extra_width = is_maximized ?
480 (kFrameBorderThickness - kFrameShadowThickness) : 0;
481 gfx::Size close_button_size = close_button_->GetPreferredSize();
482 close_button_->SetBounds(width() - FrameBorderThickness() -
483 right_extra_width - close_button_size.width(), caption_y,
484 close_button_size.width() + right_extra_width,
485 close_button_size.height());
487 // When the window is restored, we show a maximized button; otherwise, we show
489 bool is_restored = !is_maximized && !frame_->IsMinimized();
490 ImageButton* invisible_button = is_restored ? restore_button_
492 invisible_button->SetVisible(false);
494 ImageButton* visible_button = is_restored ? maximize_button_
496 FramePartImage normal_part, hot_part, pushed_part;
498 if (should_show_maximize_button_) {
499 visible_button->SetVisible(true);
500 visible_button->SetImageAlignment(ImageButton::ALIGN_LEFT,
501 ImageButton::ALIGN_BOTTOM);
502 gfx::Size visible_button_size = visible_button->GetPreferredSize();
503 visible_button->SetBounds(close_button_->x() - visible_button_size.width(),
504 caption_y, visible_button_size.width(),
505 visible_button_size.height());
506 next_button_x = visible_button->x();
508 visible_button->SetVisible(false);
509 next_button_x = close_button_->x();
512 minimize_button_->SetVisible(true);
513 minimize_button_->SetImageAlignment(ImageButton::ALIGN_LEFT,
514 ImageButton::ALIGN_BOTTOM);
515 gfx::Size minimize_button_size = minimize_button_->GetPreferredSize();
516 minimize_button_->SetBounds(
517 next_button_x - minimize_button_size.width(), caption_y,
518 minimize_button_size.width(),
519 minimize_button_size.height());
521 normal_part = IDR_CLOSE;
522 hot_part = IDR_CLOSE_H;
523 pushed_part = IDR_CLOSE_P;
525 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
527 close_button_->SetImage(CustomButton::STATE_NORMAL,
528 rb.GetImageNamed(normal_part).ToImageSkia());
529 close_button_->SetImage(CustomButton::STATE_HOVERED,
530 rb.GetImageNamed(hot_part).ToImageSkia());
531 close_button_->SetImage(CustomButton::STATE_PRESSED,
532 rb.GetImageNamed(pushed_part).ToImageSkia());
535 void CustomFrameView::LayoutTitleBar() {
536 // The window title position is calculated based on the icon position, even
537 // when there is no icon.
538 gfx::Rect icon_bounds(IconBounds());
539 bool show_window_icon = window_icon_ != NULL;
540 if (show_window_icon)
541 window_icon_->SetBoundsRect(icon_bounds);
543 if (!frame_->widget_delegate()->ShouldShowWindowTitle())
546 // The offset between the window left edge and the title text.
547 int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
549 int title_height = GetTitleFontList().GetHeight();
550 // We bias the title position so that when the difference between the icon and
551 // title heights is odd, the extra pixel of the title is above the vertical
552 // midline rather than below. This compensates for how the icon is already
553 // biased downwards (see IconBounds()) and helps prevent descenders on the
554 // title from overlapping the 3D edge at the bottom of the titlebar.
555 title_bounds_.SetRect(title_x,
556 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
557 std::max(0, minimize_button_->x() - kTitleCaptionSpacing -
558 title_x), title_height);
561 void CustomFrameView::LayoutClientView() {
562 if (!ShouldShowTitleBarAndBorder()) {
563 client_view_bounds_ = bounds();
567 int top_height = NonClientTopBorderHeight();
568 int border_thickness = NonClientBorderThickness();
569 client_view_bounds_.SetRect(border_thickness, top_height,
570 std::max(0, width() - (2 * border_thickness)),
571 std::max(0, height() - top_height - border_thickness));
574 ImageButton* CustomFrameView::InitWindowCaptionButton(
575 int accessibility_string_id,
578 int pushed_image_id) {
579 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
580 ImageButton* button = new ImageButton(this);
581 button->SetAccessibleName(l10n_util::GetStringUTF16(accessibility_string_id));
582 button->SetImage(CustomButton::STATE_NORMAL,
583 rb.GetImageNamed(normal_image_id).ToImageSkia());
584 button->SetImage(CustomButton::STATE_HOVERED,
585 rb.GetImageNamed(hot_image_id).ToImageSkia());
586 button->SetImage(CustomButton::STATE_PRESSED,
587 rb.GetImageNamed(pushed_image_id).ToImageSkia());
588 AddChildView(button);