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/widget/widget.h"
22 #include "ui/views/widget/widget_delegate.h"
23 #include "ui/views/window/client_view.h"
24 #include "ui/views/window/frame_background.h"
25 #include "ui/views/window/window_resources.h"
26 #include "ui/views/window/window_shape.h"
29 #include "ui/views/widget/native_widget_aura.h"
31 #include "ui/views/widget/native_widget_win.h"
38 // The frame border is only visible in restored mode and is hardcoded to 4 px on
39 // each side regardless of the system window border size.
40 const int kFrameBorderThickness = 4;
41 // In the window corners, the resize areas don't actually expand bigger, but the
42 // 16 px at the end of each edge triggers diagonal resizing.
43 const int kResizeAreaCornerSize = 16;
44 // The titlebar never shrinks too short to show the caption button plus some
46 const int kCaptionButtonHeightWithPadding = 19;
47 // The titlebar has a 2 px 3D edge along the top and bottom.
48 const int kTitlebarTopAndBottomEdgeThickness = 2;
49 // The icon is inset 2 px from the left frame border.
50 const int kIconLeftSpacing = 2;
51 // The icon never shrinks below 16 px on a side.
52 const int kIconMinimumSize = 16;
53 // The space between the window icon and the title text.
54 const int kTitleIconOffsetX = 4;
55 // The space between the title text and the caption buttons.
56 const int kTitleCaptionSpacing = 5;
59 const SkColor kDefaultColorFrame = SkColorSetRGB(109, 109, 109);
60 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(176, 176, 176);
62 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
63 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
66 const gfx::Font& GetTitleFont() {
67 static gfx::Font* title_font = NULL;
70 title_font = new gfx::Font(NativeWidgetAura::GetWindowTitleFont());
72 title_font = new gfx::Font(NativeWidgetWin::GetWindowTitleFont());
73 #elif defined(OS_LINUX)
74 // TODO(ben): need to resolve what font this is.
75 title_font = new gfx::Font();
83 ///////////////////////////////////////////////////////////////////////////////
84 // CustomFrameView, public:
86 CustomFrameView::CustomFrameView()
89 minimize_button_(NULL),
90 maximize_button_(NULL),
91 restore_button_(NULL),
93 should_show_minmax_buttons_(false),
94 frame_background_(new FrameBackground()) {
97 CustomFrameView::~CustomFrameView() {
100 void CustomFrameView::Init(Widget* frame) {
103 close_button_ = new ImageButton(this);
104 close_button_->SetAccessibleName(
105 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE));
107 // Close button images will be set in LayoutWindowControls().
108 AddChildView(close_button_);
110 minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE,
111 IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
113 maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE,
114 IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
116 restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE,
117 IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
119 should_show_minmax_buttons_ = frame_->widget_delegate()->CanMaximize();
121 if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
122 window_icon_ = new ImageButton(this);
123 AddChildView(window_icon_);
127 ///////////////////////////////////////////////////////////////////////////////
128 // CustomFrameView, NonClientFrameView implementation:
130 gfx::Rect CustomFrameView::GetBoundsForClientView() const {
131 return client_view_bounds_;
134 gfx::Rect CustomFrameView::GetWindowBoundsForClientBounds(
135 const gfx::Rect& client_bounds) const {
136 int top_height = NonClientTopBorderHeight();
137 int border_thickness = NonClientBorderThickness();
138 return gfx::Rect(std::max(0, client_bounds.x() - border_thickness),
139 std::max(0, client_bounds.y() - top_height),
140 client_bounds.width() + (2 * border_thickness),
141 client_bounds.height() + top_height + border_thickness);
144 int CustomFrameView::NonClientHitTest(const gfx::Point& point) {
146 if (!bounds().Contains(point))
149 int frame_component = frame_->client_view()->NonClientHitTest(point);
151 // See if we're in the sysmenu region. (We check the ClientView first to be
152 // consistent with OpaqueBrowserFrameView; it's not really necessary here.)
153 gfx::Rect sysmenu_rect(IconBounds());
154 // In maximized mode we extend the rect to the screen corner to take advantage
156 if (frame_->IsMaximized())
157 sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
158 sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
159 if (sysmenu_rect.Contains(point))
160 return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
162 if (frame_component != HTNOWHERE)
163 return frame_component;
165 // Then see if the point is within any of the window controls.
166 if (close_button_->GetMirroredBounds().Contains(point))
168 if (restore_button_->GetMirroredBounds().Contains(point))
170 if (maximize_button_->GetMirroredBounds().Contains(point))
172 if (minimize_button_->GetMirroredBounds().Contains(point))
174 if (window_icon_ && window_icon_->GetMirroredBounds().Contains(point))
177 int window_component = GetHTComponentForFrame(point, FrameBorderThickness(),
178 NonClientBorderThickness(), kResizeAreaCornerSize, kResizeAreaCornerSize,
179 frame_->widget_delegate()->CanResize());
180 // Fall back to the caption if no other component matches.
181 return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
184 void CustomFrameView::GetWindowMask(const gfx::Size& size,
185 gfx::Path* window_mask) {
187 if (frame_->IsMaximized())
190 GetDefaultWindowMask(size, window_mask);
193 void CustomFrameView::ResetWindowControls() {
194 restore_button_->SetState(CustomButton::STATE_NORMAL);
195 minimize_button_->SetState(CustomButton::STATE_NORMAL);
196 maximize_button_->SetState(CustomButton::STATE_NORMAL);
197 // The close button isn't affected by this constraint.
200 void CustomFrameView::UpdateWindowIcon() {
202 window_icon_->SchedulePaint();
205 void CustomFrameView::UpdateWindowTitle() {
206 SchedulePaintInRect(title_bounds_);
209 ///////////////////////////////////////////////////////////////////////////////
210 // CustomFrameView, View overrides:
212 void CustomFrameView::OnPaint(gfx::Canvas* canvas) {
213 if (frame_->IsMaximized())
214 PaintMaximizedFrameBorder(canvas);
216 PaintRestoredFrameBorder(canvas);
217 PaintTitleBar(canvas);
218 if (ShouldShowClientEdge())
219 PaintRestoredClientEdge(canvas);
222 void CustomFrameView::Layout() {
223 LayoutWindowControls();
228 gfx::Size CustomFrameView::GetPreferredSize() {
229 return frame_->non_client_view()->GetWindowBoundsForClientBounds(
230 gfx::Rect(frame_->client_view()->GetPreferredSize())).size();
233 gfx::Size CustomFrameView::GetMinimumSize() {
234 return frame_->non_client_view()->GetWindowBoundsForClientBounds(
235 gfx::Rect(frame_->client_view()->GetMinimumSize())).size();
238 gfx::Size CustomFrameView::GetMaximumSize() {
239 gfx::Size max_size = frame_->client_view()->GetMaximumSize();
240 gfx::Size converted_size =
241 frame_->non_client_view()->GetWindowBoundsForClientBounds(
242 gfx::Rect(max_size)).size();
243 return gfx::Size(max_size.width() == 0 ? 0 : converted_size.width(),
244 max_size.height() == 0 ? 0 : converted_size.height());
247 ///////////////////////////////////////////////////////////////////////////////
248 // CustomFrameView, ButtonListener implementation:
250 void CustomFrameView::ButtonPressed(Button* sender, const ui::Event& event) {
251 if (sender == close_button_)
253 else if (sender == minimize_button_)
255 else if (sender == maximize_button_)
257 else if (sender == restore_button_)
261 ///////////////////////////////////////////////////////////////////////////////
262 // CustomFrameView, private:
264 int CustomFrameView::FrameBorderThickness() const {
265 return frame_->IsMaximized() ? 0 : kFrameBorderThickness;
268 int CustomFrameView::NonClientBorderThickness() const {
269 // In maximized mode, we don't show a client edge.
270 return FrameBorderThickness() +
271 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
274 int CustomFrameView::NonClientTopBorderHeight() const {
275 return std::max(FrameBorderThickness() + IconSize(),
276 CaptionButtonY() + kCaptionButtonHeightWithPadding) +
277 TitlebarBottomThickness();
280 int CustomFrameView::CaptionButtonY() const {
281 // Maximized buttons start at window top so that even if their images aren't
282 // drawn flush with the screen edge, they still obey Fitts' Law.
283 return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness;
286 int CustomFrameView::TitlebarBottomThickness() const {
287 return kTitlebarTopAndBottomEdgeThickness +
288 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
291 int CustomFrameView::IconSize() const {
293 // This metric scales up if either the titlebar height or the titlebar font
294 // size are increased.
295 return GetSystemMetrics(SM_CYSMICON);
297 return std::max(GetTitleFont().GetHeight(), kIconMinimumSize);
301 gfx::Rect CustomFrameView::IconBounds() const {
302 int size = IconSize();
303 int frame_thickness = FrameBorderThickness();
304 // Our frame border has a different "3D look" than Windows'. Theirs has a
305 // more complex gradient on the top that they push their icon/title below;
306 // then the maximized window cuts this off and the icon/title are centered
307 // in the remaining space. Because the apparent shape of our border is
308 // simpler, using the same positioning makes things look slightly uncentered
309 // with restored windows, so when the window is restored, instead of
310 // calculating the remaining space from below the frame border, we calculate
311 // from below the 3D edge.
312 int unavailable_px_at_top = frame_->IsMaximized() ?
313 frame_thickness : kTitlebarTopAndBottomEdgeThickness;
314 // When the icon is shorter than the minimum space we reserve for the caption
315 // button, we vertically center it. We want to bias rounding to put extra
316 // space above the icon, since the 3D edge (+ client edge, for restored
317 // windows) below looks (to the eye) more like additional space than does the
318 // 3D edge (or nothing at all, for maximized windows) above; hence the +1.
319 int y = unavailable_px_at_top + (NonClientTopBorderHeight() -
320 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2;
321 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size);
324 bool CustomFrameView::ShouldShowClientEdge() const {
325 return !frame_->IsMaximized();
328 void CustomFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
329 frame_background_->set_frame_color(GetFrameColor());
330 const gfx::ImageSkia* frame_image = GetFrameImage();
331 frame_background_->set_theme_image(frame_image);
332 frame_background_->set_top_area_height(frame_image->height());
334 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
336 frame_background_->SetCornerImages(
337 rb.GetImageNamed(IDR_WINDOW_TOP_LEFT_CORNER).ToImageSkia(),
338 rb.GetImageNamed(IDR_WINDOW_TOP_RIGHT_CORNER).ToImageSkia(),
339 rb.GetImageNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER).ToImageSkia(),
340 rb.GetImageNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER).ToImageSkia());
341 frame_background_->SetSideImages(
342 rb.GetImageNamed(IDR_WINDOW_LEFT_SIDE).ToImageSkia(),
343 rb.GetImageNamed(IDR_WINDOW_TOP_CENTER).ToImageSkia(),
344 rb.GetImageNamed(IDR_WINDOW_RIGHT_SIDE).ToImageSkia(),
345 rb.GetImageNamed(IDR_WINDOW_BOTTOM_CENTER).ToImageSkia());
347 frame_background_->PaintRestored(canvas, this);
350 void CustomFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
351 const gfx::ImageSkia* frame_image = GetFrameImage();
352 frame_background_->set_theme_image(frame_image);
353 frame_background_->set_top_area_height(frame_image->height());
354 frame_background_->PaintMaximized(canvas, this);
356 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
358 // TODO(jamescook): Migrate this into FrameBackground.
359 // The bottom of the titlebar actually comes from the top of the Client Edge
360 // graphic, with the actual client edge clipped off the bottom.
361 const gfx::ImageSkia* titlebar_bottom = rb.GetImageNamed(
362 IDR_APP_TOP_CENTER).ToImageSkia();
363 int edge_height = titlebar_bottom->height() -
364 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
365 canvas->TileImageInt(*titlebar_bottom, 0,
366 frame_->client_view()->y() - edge_height, width(), edge_height);
369 void CustomFrameView::PaintTitleBar(gfx::Canvas* canvas) {
370 WidgetDelegate* delegate = frame_->widget_delegate();
372 // It seems like in some conditions we can be asked to paint after the window
373 // that contains us is WM_DESTROYed. At this point, our delegate is NULL. The
374 // correct long term fix may be to shut down the RootView in WM_DESTROY.
378 canvas->DrawStringInt(delegate->GetWindowTitle(), GetTitleFont(),
379 SK_ColorWHITE, GetMirroredXForRect(title_bounds_),
380 title_bounds_.y(), title_bounds_.width(),
381 title_bounds_.height());
384 void CustomFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
385 gfx::Rect client_area_bounds = frame_->client_view()->bounds();
386 int client_area_top = client_area_bounds.y();
388 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
389 const gfx::ImageSkia* top_left = rb.GetImageNamed(
390 IDR_APP_TOP_LEFT).ToImageSkia();
391 const gfx::ImageSkia* top = rb.GetImageNamed(
392 IDR_APP_TOP_CENTER).ToImageSkia();
393 const gfx::ImageSkia* top_right = rb.GetImageNamed(
394 IDR_APP_TOP_RIGHT).ToImageSkia();
395 const gfx::ImageSkia* right = rb.GetImageNamed(
396 IDR_CONTENT_RIGHT_SIDE).ToImageSkia();
397 const gfx::ImageSkia* bottom_right = rb.GetImageNamed(
398 IDR_CONTENT_BOTTOM_RIGHT_CORNER).ToImageSkia();
399 const gfx::ImageSkia* bottom = rb.GetImageNamed(
400 IDR_CONTENT_BOTTOM_CENTER).ToImageSkia();
401 const gfx::ImageSkia* bottom_left = rb.GetImageNamed(
402 IDR_CONTENT_BOTTOM_LEFT_CORNER).ToImageSkia();
403 const gfx::ImageSkia* left = rb.GetImageNamed(
404 IDR_CONTENT_LEFT_SIDE).ToImageSkia();
407 int top_edge_y = client_area_top - top->height();
408 canvas->DrawImageInt(*top_left, client_area_bounds.x() - top_left->width(),
410 canvas->TileImageInt(*top, client_area_bounds.x(), top_edge_y,
411 client_area_bounds.width(), top->height());
412 canvas->DrawImageInt(*top_right, client_area_bounds.right(), top_edge_y);
415 int client_area_bottom =
416 std::max(client_area_top, client_area_bounds.bottom());
417 int client_area_height = client_area_bottom - client_area_top;
418 canvas->TileImageInt(*right, client_area_bounds.right(), client_area_top,
419 right->width(), client_area_height);
422 canvas->DrawImageInt(*bottom_right, client_area_bounds.right(),
424 canvas->TileImageInt(*bottom, client_area_bounds.x(), client_area_bottom,
425 client_area_bounds.width(), bottom_right->height());
426 canvas->DrawImageInt(*bottom_left,
427 client_area_bounds.x() - bottom_left->width(), client_area_bottom);
430 canvas->TileImageInt(*left, client_area_bounds.x() - left->width(),
431 client_area_top, left->width(), client_area_height);
433 // Draw the color to fill in the edges.
434 canvas->FillRect(gfx::Rect(client_area_bounds.x() - 1, client_area_top - 1,
435 client_area_bounds.width() + 1, client_area_bottom - client_area_top + 1),
439 SkColor CustomFrameView::GetFrameColor() const {
440 return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive;
443 const gfx::ImageSkia* CustomFrameView::GetFrameImage() const {
444 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
445 frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia();
448 void CustomFrameView::LayoutWindowControls() {
449 close_button_->SetImageAlignment(ImageButton::ALIGN_LEFT,
450 ImageButton::ALIGN_BOTTOM);
451 int caption_y = CaptionButtonY();
452 bool is_maximized = frame_->IsMaximized();
453 // There should always be the same number of non-shadow pixels visible to the
454 // side of the caption buttons. In maximized mode we extend the rightmost
455 // button to the screen corner to obey Fitts' Law.
456 int right_extra_width = is_maximized ?
457 (kFrameBorderThickness - kFrameShadowThickness) : 0;
458 gfx::Size close_button_size = close_button_->GetPreferredSize();
459 close_button_->SetBounds(width() - FrameBorderThickness() -
460 right_extra_width - close_button_size.width(), caption_y,
461 close_button_size.width() + right_extra_width,
462 close_button_size.height());
464 // When the window is restored, we show a maximized button; otherwise, we show
466 bool is_restored = !is_maximized && !frame_->IsMinimized();
467 ImageButton* invisible_button = is_restored ? restore_button_
469 invisible_button->SetVisible(false);
471 ImageButton* visible_button = is_restored ? maximize_button_
473 FramePartImage normal_part, hot_part, pushed_part;
474 if (should_show_minmax_buttons_) {
475 visible_button->SetVisible(true);
476 visible_button->SetImageAlignment(ImageButton::ALIGN_LEFT,
477 ImageButton::ALIGN_BOTTOM);
478 gfx::Size visible_button_size = visible_button->GetPreferredSize();
479 visible_button->SetBounds(close_button_->x() - visible_button_size.width(),
480 caption_y, visible_button_size.width(),
481 visible_button_size.height());
483 minimize_button_->SetVisible(true);
484 minimize_button_->SetImageAlignment(ImageButton::ALIGN_LEFT,
485 ImageButton::ALIGN_BOTTOM);
486 gfx::Size minimize_button_size = minimize_button_->GetPreferredSize();
487 minimize_button_->SetBounds(
488 visible_button->x() - minimize_button_size.width(), caption_y,
489 minimize_button_size.width(),
490 minimize_button_size.height());
492 normal_part = IDR_CLOSE;
493 hot_part = IDR_CLOSE_H;
494 pushed_part = IDR_CLOSE_P;
496 visible_button->SetVisible(false);
497 minimize_button_->SetVisible(false);
499 normal_part = IDR_CLOSE_SA;
500 hot_part = IDR_CLOSE_SA_H;
501 pushed_part = IDR_CLOSE_SA_P;
504 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
506 close_button_->SetImage(CustomButton::STATE_NORMAL,
507 rb.GetImageNamed(normal_part).ToImageSkia());
508 close_button_->SetImage(CustomButton::STATE_HOVERED,
509 rb.GetImageNamed(hot_part).ToImageSkia());
510 close_button_->SetImage(CustomButton::STATE_PRESSED,
511 rb.GetImageNamed(pushed_part).ToImageSkia());
514 void CustomFrameView::LayoutTitleBar() {
515 // The window title position is calculated based on the icon position, even
516 // when there is no icon.
517 gfx::Rect icon_bounds(IconBounds());
518 bool show_window_icon = window_icon_ != NULL;
519 if (show_window_icon)
520 window_icon_->SetBoundsRect(icon_bounds);
522 // The offset between the window left edge and the title text.
523 int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
525 int title_height = GetTitleFont().GetHeight();
526 // We bias the title position so that when the difference between the icon and
527 // title heights is odd, the extra pixel of the title is above the vertical
528 // midline rather than below. This compensates for how the icon is already
529 // biased downwards (see IconBounds()) and helps prevent descenders on the
530 // title from overlapping the 3D edge at the bottom of the titlebar.
531 title_bounds_.SetRect(title_x,
532 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
533 std::max(0, (should_show_minmax_buttons_ ?
534 minimize_button_->x() : close_button_->x()) - kTitleCaptionSpacing -
535 title_x), title_height);
538 void CustomFrameView::LayoutClientView() {
539 int top_height = NonClientTopBorderHeight();
540 int border_thickness = NonClientBorderThickness();
541 client_view_bounds_.SetRect(border_thickness, top_height,
542 std::max(0, width() - (2 * border_thickness)),
543 std::max(0, height() - top_height - border_thickness));
546 ImageButton* CustomFrameView::InitWindowCaptionButton(
547 int accessibility_string_id,
550 int pushed_image_id) {
551 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
552 ImageButton* button = new ImageButton(this);
553 button->SetAccessibleName(l10n_util::GetStringUTF16(accessibility_string_id));
554 button->SetImage(CustomButton::STATE_NORMAL,
555 rb.GetImageNamed(normal_image_id).ToImageSkia());
556 button->SetImage(CustomButton::STATE_HOVERED,
557 rb.GetImageNamed(hot_image_id).ToImageSkia());
558 button->SetImage(CustomButton::STATE_PRESSED,
559 rb.GetImageNamed(pushed_image_id).ToImageSkia());
560 AddChildView(button);