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 "ash/frame/custom_frame_view_ash.h"
10 #include "ash/ash_switches.h"
11 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h"
12 #include "ash/frame/default_header_painter.h"
13 #include "ash/frame/frame_border_hit_test_controller.h"
14 #include "ash/frame/frame_util.h"
15 #include "ash/frame/header_painter.h"
16 #include "ash/session/session_state_delegate.h"
17 #include "ash/shell.h"
18 #include "ash/shell_observer.h"
19 #include "ash/wm/immersive_fullscreen_controller.h"
20 #include "ash/wm/window_state.h"
21 #include "ash/wm/window_state_delegate.h"
22 #include "ash/wm/window_state_observer.h"
23 #include "base/command_line.h"
24 #include "ui/aura/client/aura_constants.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_observer.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/image/image.h"
29 #include "ui/gfx/rect.h"
30 #include "ui/gfx/rect_conversions.h"
31 #include "ui/gfx/size.h"
32 #include "ui/views/controls/image_view.h"
33 #include "ui/views/view.h"
34 #include "ui/views/view_targeter.h"
35 #include "ui/views/widget/widget.h"
36 #include "ui/views/widget/widget_delegate.h"
40 ///////////////////////////////////////////////////////////////////////////////
41 // CustomFrameViewAshWindowStateDelegate
43 // Handles a user's fullscreen request (Shift+F4/F4). Puts the window into
44 // immersive fullscreen if immersive fullscreen is enabled for non-browser
46 class CustomFrameViewAshWindowStateDelegate
47 : public ash::wm::WindowStateDelegate
,
48 public ash::wm::WindowStateObserver
,
49 public aura::WindowObserver
{
51 CustomFrameViewAshWindowStateDelegate(
52 ash::wm::WindowState
* window_state
,
53 ash::CustomFrameViewAsh
* custom_frame_view
)
54 : window_state_(NULL
) {
55 immersive_fullscreen_controller_
.reset(
56 new ash::ImmersiveFullscreenController
);
57 custom_frame_view
->InitImmersiveFullscreenControllerForView(
58 immersive_fullscreen_controller_
.get());
60 // Add a window state observer to exit fullscreen properly in case
61 // fullscreen is exited without going through
62 // WindowState::ToggleFullscreen(). This is the case when exiting
63 // immersive fullscreen via the "Restore" window control.
64 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
65 window_state_
= window_state
;
66 window_state_
->AddObserver(this);
67 window_state_
->window()->AddObserver(this);
69 ~CustomFrameViewAshWindowStateDelegate() override
{
71 window_state_
->RemoveObserver(this);
72 window_state_
->window()->RemoveObserver(this);
76 // Overridden from ash::wm::WindowStateDelegate:
77 bool ToggleFullscreen(ash::wm::WindowState
* window_state
) override
{
78 bool enter_fullscreen
= !window_state
->IsFullscreen();
79 if (enter_fullscreen
) {
80 window_state
->window()->SetProperty(aura::client::kShowStateKey
,
81 ui::SHOW_STATE_FULLSCREEN
);
83 window_state
->Restore();
85 if (immersive_fullscreen_controller_
) {
86 immersive_fullscreen_controller_
->SetEnabled(
87 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER
,
92 // Overridden from aura::WindowObserver:
93 void OnWindowDestroying(aura::Window
* window
) override
{
94 window_state_
->RemoveObserver(this);
95 window_state_
->window()->RemoveObserver(this);
98 // Overridden from ash::wm::WindowStateObserver:
99 void OnPostWindowStateTypeChange(ash::wm::WindowState
* window_state
,
100 ash::wm::WindowStateType old_type
) override
{
101 if (!window_state
->IsFullscreen() &&
102 !window_state
->IsMinimized() &&
103 immersive_fullscreen_controller_
.get() &&
104 immersive_fullscreen_controller_
->IsEnabled()) {
105 immersive_fullscreen_controller_
->SetEnabled(
106 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER
,
111 ash::wm::WindowState
* window_state_
;
112 scoped_ptr
<ash::ImmersiveFullscreenController
>
113 immersive_fullscreen_controller_
;
115 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshWindowStateDelegate
);
122 ///////////////////////////////////////////////////////////////////////////////
123 // CustomFrameViewAsh::HeaderView
125 // View which paints the header. It slides off and on screen in immersive
127 class CustomFrameViewAsh::HeaderView
128 : public views::View
,
129 public ImmersiveFullscreenController::Delegate
,
130 public ShellObserver
{
132 // |frame| is the widget that the caption buttons act on.
133 explicit HeaderView(views::Widget
* frame
);
134 ~HeaderView() override
;
136 // Schedules a repaint for the entire title.
137 void SchedulePaintForTitle();
139 // Tells the window controls to reset themselves to the normal state.
140 void ResetWindowControls();
142 // Returns the amount of the view's pixels which should be on screen.
143 int GetPreferredOnScreenHeight() const;
145 // Returns the view's preferred height.
146 int GetPreferredHeight() const;
148 // Returns the view's minimum width.
149 int GetMinimumWidth() const;
151 void UpdateAvatarIcon();
153 void SizeConstraintsChanged();
155 void SetFrameColors(SkColor active_frame_color
, SkColor inactive_frame_color
);
158 void Layout() override
;
159 void OnPaint(gfx::Canvas
* canvas
) override
;
160 void ChildPreferredSizeChanged(views::View
* child
) override
;
163 void OnMaximizeModeStarted() override
;
164 void OnMaximizeModeEnded() override
;
166 FrameCaptionButtonContainerView
* caption_button_container() {
167 return caption_button_container_
;
170 views::View
* avatar_icon() const {
175 // ImmersiveFullscreenController::Delegate:
176 void OnImmersiveRevealStarted() override
;
177 void OnImmersiveRevealEnded() override
;
178 void OnImmersiveFullscreenExited() override
;
179 void SetVisibleFraction(double visible_fraction
) override
;
180 std::vector
<gfx::Rect
> GetVisibleBoundsInScreen() const override
;
182 // The widget that the caption buttons act on.
183 views::Widget
* frame_
;
185 // Helper for painting the header.
186 scoped_ptr
<DefaultHeaderPainter
> header_painter_
;
188 views::ImageView
* avatar_icon_
;
190 // View which contains the window caption buttons.
191 FrameCaptionButtonContainerView
* caption_button_container_
;
193 // The fraction of the header's height which is visible while in fullscreen.
194 // This value is meaningless when not in fullscreen.
195 double fullscreen_visible_fraction_
;
197 DISALLOW_COPY_AND_ASSIGN(HeaderView
);
200 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget
* frame
)
202 header_painter_(new ash::DefaultHeaderPainter
),
204 caption_button_container_(NULL
),
205 fullscreen_visible_fraction_(0) {
206 caption_button_container_
= new FrameCaptionButtonContainerView(frame_
);
207 caption_button_container_
->UpdateSizeButtonVisibility();
208 AddChildView(caption_button_container_
);
210 header_painter_
->Init(frame_
, this, caption_button_container_
);
213 Shell::GetInstance()->AddShellObserver(this);
216 CustomFrameViewAsh::HeaderView::~HeaderView() {
217 Shell::GetInstance()->RemoveShellObserver(this);
220 void CustomFrameViewAsh::HeaderView::SchedulePaintForTitle() {
221 header_painter_
->SchedulePaintForTitle();
224 void CustomFrameViewAsh::HeaderView::ResetWindowControls() {
225 caption_button_container_
->ResetWindowControls();
228 int CustomFrameViewAsh::HeaderView::GetPreferredOnScreenHeight() const {
229 if (frame_
->IsFullscreen()) {
230 return static_cast<int>(
231 GetPreferredHeight() * fullscreen_visible_fraction_
);
233 return GetPreferredHeight();
236 int CustomFrameViewAsh::HeaderView::GetPreferredHeight() const {
237 return header_painter_
->GetHeaderHeightForPainting();
240 int CustomFrameViewAsh::HeaderView::GetMinimumWidth() const {
241 return header_painter_
->GetMinimumHeaderWidth();
244 void CustomFrameViewAsh::HeaderView::UpdateAvatarIcon() {
245 SessionStateDelegate
* delegate
=
246 Shell::GetInstance()->session_state_delegate();
247 aura::Window
* window
= frame_
->GetNativeView();
248 bool show
= delegate
->ShouldShowAvatar(window
);
255 gfx::ImageSkia image
= GetAvatarImageForContext(
256 delegate
->GetBrowserContextForWindow(window
)).AsImageSkia();
257 DCHECK(!image
.isNull());
258 DCHECK_EQ(image
.width(), image
.height());
260 avatar_icon_
= new views::ImageView();
261 AddChildView(avatar_icon_
);
263 avatar_icon_
->SetImage(image
);
265 header_painter_
->UpdateLeftHeaderView(avatar_icon_
);
269 void CustomFrameViewAsh::HeaderView::SizeConstraintsChanged() {
270 caption_button_container_
->ResetWindowControls();
271 caption_button_container_
->UpdateSizeButtonVisibility();
275 void CustomFrameViewAsh::HeaderView::SetFrameColors(
276 SkColor active_frame_color
, SkColor inactive_frame_color
) {
277 header_painter_
->SetFrameColors(active_frame_color
, inactive_frame_color
);
280 ///////////////////////////////////////////////////////////////////////////////
281 // CustomFrameViewAsh::HeaderView, views::View overrides:
283 void CustomFrameViewAsh::HeaderView::Layout() {
284 header_painter_
->LayoutHeader();
287 void CustomFrameViewAsh::HeaderView::OnPaint(gfx::Canvas
* canvas
) {
288 bool paint_as_active
=
289 frame_
->non_client_view()->frame_view()->ShouldPaintAsActive();
290 caption_button_container_
->SetPaintAsActive(paint_as_active
);
292 HeaderPainter::Mode header_mode
= paint_as_active
?
293 HeaderPainter::MODE_ACTIVE
: HeaderPainter::MODE_INACTIVE
;
294 header_painter_
->PaintHeader(canvas
, header_mode
);
297 void CustomFrameViewAsh::HeaderView::
298 ChildPreferredSizeChanged(views::View
* child
) {
299 // FrameCaptionButtonContainerView animates the visibility changes in
300 // UpdateSizeButtonVisibility(false). Due to this a new size is not available
301 // until the completion of the animation. Layout in response to the preferred
303 if (child
!= caption_button_container_
)
308 ///////////////////////////////////////////////////////////////////////////////
309 // CustomFrameViewAsh::HeaderView, ShellObserver overrides:
311 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() {
312 caption_button_container_
->UpdateSizeButtonVisibility();
316 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() {
317 caption_button_container_
->UpdateSizeButtonVisibility();
321 ///////////////////////////////////////////////////////////////////////////////
322 // CustomFrameViewAsh::HeaderView,
323 // ImmersiveFullscreenController::Delegate overrides:
325 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() {
326 fullscreen_visible_fraction_
= 0;
327 SetPaintToLayer(true);
328 SetFillsBoundsOpaquely(false);
332 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() {
333 fullscreen_visible_fraction_
= 0;
334 SetPaintToLayer(false);
338 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() {
339 fullscreen_visible_fraction_
= 0;
340 SetPaintToLayer(false);
344 void CustomFrameViewAsh::HeaderView::SetVisibleFraction(
345 double visible_fraction
) {
346 if (fullscreen_visible_fraction_
!= visible_fraction
) {
347 fullscreen_visible_fraction_
= visible_fraction
;
352 std::vector
<gfx::Rect
>
353 CustomFrameViewAsh::HeaderView::GetVisibleBoundsInScreen() const {
354 // TODO(pkotwicz): Implement views::View::ConvertRectToScreen().
355 gfx::Rect
visible_bounds(GetVisibleBounds());
356 gfx::Point
visible_origin_in_screen(visible_bounds
.origin());
357 views::View::ConvertPointToScreen(this, &visible_origin_in_screen
);
358 std::vector
<gfx::Rect
> bounds_in_screen
;
359 bounds_in_screen
.push_back(
360 gfx::Rect(visible_origin_in_screen
, visible_bounds
.size()));
361 return bounds_in_screen
;
364 ///////////////////////////////////////////////////////////////////////////////
365 // CustomFrameViewAsh::OverlayView
367 // View which takes up the entire widget and contains the HeaderView. HeaderView
368 // is a child of OverlayView to avoid creating a larger texture than necessary
369 // when painting the HeaderView to its own layer.
370 class CustomFrameViewAsh::OverlayView
: public views::View
,
371 public views::ViewTargeterDelegate
{
373 explicit OverlayView(HeaderView
* header_view
);
374 ~OverlayView() override
;
377 void Layout() override
;
380 // views::ViewTargeterDelegate:
381 bool DoesIntersectRect(const views::View
* target
,
382 const gfx::Rect
& rect
) const override
;
384 HeaderView
* header_view_
;
386 DISALLOW_COPY_AND_ASSIGN(OverlayView
);
389 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView
* header_view
)
390 : header_view_(header_view
) {
391 AddChildView(header_view
);
393 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
396 CustomFrameViewAsh::OverlayView::~OverlayView() {
399 ///////////////////////////////////////////////////////////////////////////////
400 // CustomFrameViewAsh::OverlayView, views::View overrides:
402 void CustomFrameViewAsh::OverlayView::Layout() {
403 // Layout |header_view_| because layout affects the result of
404 // GetPreferredOnScreenHeight().
405 header_view_
->Layout();
407 int onscreen_height
= header_view_
->GetPreferredOnScreenHeight();
408 if (onscreen_height
== 0) {
409 header_view_
->SetVisible(false);
411 int height
= header_view_
->GetPreferredHeight();
412 header_view_
->SetBounds(0, onscreen_height
- height
, width(), height
);
413 header_view_
->SetVisible(true);
417 ///////////////////////////////////////////////////////////////////////////////
418 // CustomFrameViewAsh::OverlayView, views::ViewTargeterDelegate overrides:
420 bool CustomFrameViewAsh::OverlayView::DoesIntersectRect(
421 const views::View
* target
,
422 const gfx::Rect
& rect
) const {
423 CHECK_EQ(target
, this);
424 // Grab events in the header view. Return false for other events so that they
425 // can be handled by the client view.
426 return header_view_
->HitTestRect(rect
);
429 ////////////////////////////////////////////////////////////////////////////////
430 // CustomFrameViewAsh, public:
433 const char CustomFrameViewAsh::kViewClassName
[] = "CustomFrameViewAsh";
435 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget
* frame
)
437 header_view_(new HeaderView(frame
)),
438 frame_border_hit_test_controller_(
439 new FrameBorderHitTestController(frame_
)) {
440 // |header_view_| is set as the non client view's overlay view so that it can
441 // overlay the web contents in immersive fullscreen.
442 frame
->non_client_view()->SetOverlayView(new OverlayView(header_view_
));
444 // A delegate for a more complex way of fullscreening the window may already
445 // be set. This is the case for packaged apps.
446 wm::WindowState
* window_state
= wm::GetWindowState(frame
->GetNativeWindow());
447 if (!window_state
->HasDelegate()) {
448 window_state
->SetDelegate(scoped_ptr
<wm::WindowStateDelegate
>(
449 new CustomFrameViewAshWindowStateDelegate(
450 window_state
, this)));
454 CustomFrameViewAsh::~CustomFrameViewAsh() {
457 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView(
458 ImmersiveFullscreenController
* immersive_fullscreen_controller
) {
459 immersive_fullscreen_controller
->Init(header_view_
, frame_
, header_view_
);
462 void CustomFrameViewAsh::SetFrameColors(SkColor active_frame_color
,
463 SkColor inactive_frame_color
) {
464 header_view_
->SetFrameColors(active_frame_color
, inactive_frame_color
);
467 ////////////////////////////////////////////////////////////////////////////////
468 // CustomFrameViewAsh, views::NonClientFrameView overrides:
470 gfx::Rect
CustomFrameViewAsh::GetBoundsForClientView() const {
471 gfx::Rect client_bounds
= bounds();
472 client_bounds
.Inset(0, NonClientTopBorderHeight(), 0, 0);
473 return client_bounds
;
476 gfx::Rect
CustomFrameViewAsh::GetWindowBoundsForClientBounds(
477 const gfx::Rect
& client_bounds
) const {
478 gfx::Rect window_bounds
= client_bounds
;
479 window_bounds
.Inset(0, -NonClientTopBorderHeight(), 0, 0);
480 return window_bounds
;
483 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point
& point
) {
484 return FrameBorderHitTestController::NonClientHitTest(this,
485 header_view_
->caption_button_container(), point
);
488 void CustomFrameViewAsh::GetWindowMask(const gfx::Size
& size
,
489 gfx::Path
* window_mask
) {
490 // No window masks in Aura.
493 void CustomFrameViewAsh::ResetWindowControls() {
494 header_view_
->ResetWindowControls();
497 void CustomFrameViewAsh::UpdateWindowIcon() {
500 void CustomFrameViewAsh::UpdateWindowTitle() {
501 header_view_
->SchedulePaintForTitle();
504 void CustomFrameViewAsh::SizeConstraintsChanged() {
505 header_view_
->SizeConstraintsChanged();
508 ////////////////////////////////////////////////////////////////////////////////
509 // CustomFrameViewAsh, views::View overrides:
511 gfx::Size
CustomFrameViewAsh::GetPreferredSize() const {
512 gfx::Size pref
= frame_
->client_view()->GetPreferredSize();
513 gfx::Rect
bounds(0, 0, pref
.width(), pref
.height());
514 return frame_
->non_client_view()->GetWindowBoundsForClientBounds(
518 const char* CustomFrameViewAsh::GetClassName() const {
519 return kViewClassName
;
522 gfx::Size
CustomFrameViewAsh::GetMinimumSize() const {
523 gfx::Size
min_client_view_size(frame_
->client_view()->GetMinimumSize());
525 std::max(header_view_
->GetMinimumWidth(), min_client_view_size
.width()),
526 NonClientTopBorderHeight() + min_client_view_size
.height());
529 gfx::Size
CustomFrameViewAsh::GetMaximumSize() const {
530 gfx::Size
max_client_size(frame_
->client_view()->GetMaximumSize());
534 if (max_client_size
.width() > 0)
535 width
= std::max(header_view_
->GetMinimumWidth(), max_client_size
.width());
536 if (max_client_size
.height() > 0)
537 height
= NonClientTopBorderHeight() + max_client_size
.height();
539 return gfx::Size(width
, height
);
542 void CustomFrameViewAsh::SchedulePaintInRect(const gfx::Rect
& r
) {
543 // We may end up here before |header_view_| has been added to the Widget.
544 if (header_view_
->GetWidget()) {
545 // The HeaderView is not a child of CustomFrameViewAsh. Redirect the paint
546 // to HeaderView instead.
547 gfx::RectF
to_paint(r
);
548 views::View::ConvertRectToTarget(this, header_view_
, &to_paint
);
549 header_view_
->SchedulePaintInRect(gfx::ToEnclosingRect(to_paint
));
551 views::NonClientFrameView::SchedulePaintInRect(r
);
555 void CustomFrameViewAsh::VisibilityChanged(views::View
* starting_from
,
558 header_view_
->UpdateAvatarIcon();
561 ////////////////////////////////////////////////////////////////////////////////
562 // CustomFrameViewAsh, views::ViewTargeterDelegate overrides:
564 views::View
* CustomFrameViewAsh::GetHeaderView() {
568 const views::View
* CustomFrameViewAsh::GetAvatarIconViewForTest() const {
569 return header_view_
->avatar_icon();
572 ////////////////////////////////////////////////////////////////////////////////
573 // CustomFrameViewAsh, private:
575 // views::NonClientFrameView:
576 bool CustomFrameViewAsh::DoesIntersectRect(const views::View
* target
,
577 const gfx::Rect
& rect
) const {
578 CHECK_EQ(target
, this);
579 // NonClientView hit tests the NonClientFrameView first instead of going in
580 // z-order. Return false so that events get to the OverlayView.
584 FrameCaptionButtonContainerView
* CustomFrameViewAsh::
585 GetFrameCaptionButtonContainerViewForTest() {
586 return header_view_
->caption_button_container();
589 int CustomFrameViewAsh::NonClientTopBorderHeight() const {
590 return frame_
->IsFullscreen() ? 0 : header_view_
->GetPreferredHeight();