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 virtual ~CustomFrameViewAshWindowStateDelegate() {
71 window_state_
->RemoveObserver(this);
72 window_state_
->window()->RemoveObserver(this);
76 // Overridden from ash::wm::WindowStateDelegate:
77 virtual 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 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{
94 window_state_
->RemoveObserver(this);
95 window_state_
->window()->RemoveObserver(this);
98 // Overridden from ash::wm::WindowStateObserver:
99 virtual void OnPostWindowStateTypeChange(
100 ash::wm::WindowState
* window_state
,
101 ash::wm::WindowStateType old_type
) OVERRIDE
{
102 if (!window_state
->IsFullscreen() &&
103 !window_state
->IsMinimized() &&
104 immersive_fullscreen_controller_
.get() &&
105 immersive_fullscreen_controller_
->IsEnabled()) {
106 immersive_fullscreen_controller_
->SetEnabled(
107 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER
,
112 ash::wm::WindowState
* window_state_
;
113 scoped_ptr
<ash::ImmersiveFullscreenController
>
114 immersive_fullscreen_controller_
;
116 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshWindowStateDelegate
);
123 ///////////////////////////////////////////////////////////////////////////////
124 // CustomFrameViewAsh::HeaderView
126 // View which paints the header. It slides off and on screen in immersive
128 class CustomFrameViewAsh::HeaderView
129 : public views::View
,
130 public ImmersiveFullscreenController::Delegate
,
131 public ShellObserver
{
133 // |frame| is the widget that the caption buttons act on.
134 explicit HeaderView(views::Widget
* frame
);
135 virtual ~HeaderView();
137 // Schedules a repaint for the entire title.
138 void SchedulePaintForTitle();
140 // Tells the window controls to reset themselves to the normal state.
141 void ResetWindowControls();
143 // Returns the amount of the view's pixels which should be on screen.
144 int GetPreferredOnScreenHeight() const;
146 // Returns the view's preferred height.
147 int GetPreferredHeight() const;
149 // Returns the view's minimum width.
150 int GetMinimumWidth() const;
152 void UpdateAvatarIcon();
155 virtual void Layout() OVERRIDE
;
156 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
157 virtual void ChildPreferredSizeChanged(views::View
* child
) OVERRIDE
;
160 virtual void OnMaximizeModeStarted() OVERRIDE
;
161 virtual void OnMaximizeModeEnded() OVERRIDE
;
163 FrameCaptionButtonContainerView
* caption_button_container() {
164 return caption_button_container_
;
167 views::View
* avatar_icon() const {
172 // ImmersiveFullscreenController::Delegate:
173 virtual void OnImmersiveRevealStarted() OVERRIDE
;
174 virtual void OnImmersiveRevealEnded() OVERRIDE
;
175 virtual void OnImmersiveFullscreenExited() OVERRIDE
;
176 virtual void SetVisibleFraction(double visible_fraction
) OVERRIDE
;
177 virtual std::vector
<gfx::Rect
> GetVisibleBoundsInScreen() const OVERRIDE
;
179 // The widget that the caption buttons act on.
180 views::Widget
* frame_
;
182 // Helper for painting the header.
183 scoped_ptr
<DefaultHeaderPainter
> header_painter_
;
185 views::ImageView
* avatar_icon_
;
187 // View which contains the window caption buttons.
188 FrameCaptionButtonContainerView
* caption_button_container_
;
190 // The fraction of the header's height which is visible while in fullscreen.
191 // This value is meaningless when not in fullscreen.
192 double fullscreen_visible_fraction_
;
194 DISALLOW_COPY_AND_ASSIGN(HeaderView
);
197 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget
* frame
)
199 header_painter_(new ash::DefaultHeaderPainter
),
201 caption_button_container_(NULL
),
202 fullscreen_visible_fraction_(0) {
203 // Unfortunately, there is no views::WidgetDelegate::CanMinimize(). Assume
204 // that the window frame can be minimized if it can be maximized.
205 FrameCaptionButtonContainerView::MinimizeAllowed minimize_allowed
=
206 frame_
->widget_delegate()->CanMaximize() ?
207 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED
:
208 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED
;
209 caption_button_container_
= new FrameCaptionButtonContainerView(frame_
,
211 caption_button_container_
->UpdateSizeButtonVisibility();
212 AddChildView(caption_button_container_
);
214 header_painter_
->Init(frame_
, this, caption_button_container_
);
217 Shell::GetInstance()->AddShellObserver(this);
220 CustomFrameViewAsh::HeaderView::~HeaderView() {
221 Shell::GetInstance()->RemoveShellObserver(this);
224 void CustomFrameViewAsh::HeaderView::SchedulePaintForTitle() {
225 header_painter_
->SchedulePaintForTitle();
228 void CustomFrameViewAsh::HeaderView::ResetWindowControls() {
229 caption_button_container_
->ResetWindowControls();
232 int CustomFrameViewAsh::HeaderView::GetPreferredOnScreenHeight() const {
233 if (frame_
->IsFullscreen()) {
234 return static_cast<int>(
235 GetPreferredHeight() * fullscreen_visible_fraction_
);
237 return GetPreferredHeight();
240 int CustomFrameViewAsh::HeaderView::GetPreferredHeight() const {
241 return header_painter_
->GetHeaderHeightForPainting();
244 int CustomFrameViewAsh::HeaderView::GetMinimumWidth() const {
245 return header_painter_
->GetMinimumHeaderWidth();
248 void CustomFrameViewAsh::HeaderView::UpdateAvatarIcon() {
249 SessionStateDelegate
* delegate
=
250 Shell::GetInstance()->session_state_delegate();
251 aura::Window
* window
= frame_
->GetNativeView();
252 bool show
= delegate
->ShouldShowAvatar(window
);
259 gfx::ImageSkia image
= GetAvatarImageForContext(
260 delegate
->GetBrowserContextForWindow(window
)).AsImageSkia();
261 DCHECK(!image
.isNull());
262 DCHECK_EQ(image
.width(), image
.height());
264 avatar_icon_
= new views::ImageView();
265 AddChildView(avatar_icon_
);
267 avatar_icon_
->SetImage(image
);
269 header_painter_
->UpdateLeftHeaderView(avatar_icon_
);
273 ///////////////////////////////////////////////////////////////////////////////
274 // CustomFrameViewAsh::HeaderView, views::View overrides:
276 void CustomFrameViewAsh::HeaderView::Layout() {
277 header_painter_
->LayoutHeader();
280 void CustomFrameViewAsh::HeaderView::OnPaint(gfx::Canvas
* canvas
) {
281 bool paint_as_active
=
282 frame_
->non_client_view()->frame_view()->ShouldPaintAsActive();
283 caption_button_container_
->SetPaintAsActive(paint_as_active
);
285 HeaderPainter::Mode header_mode
= paint_as_active
?
286 HeaderPainter::MODE_ACTIVE
: HeaderPainter::MODE_INACTIVE
;
287 header_painter_
->PaintHeader(canvas
, header_mode
);
290 void CustomFrameViewAsh::HeaderView::
291 ChildPreferredSizeChanged(views::View
* child
) {
292 // FrameCaptionButtonContainerView animates the visibility changes in
293 // UpdateSizeButtonVisibility(false). Due to this a new size is not available
294 // until the completion of the animation. Layout in response to the preferred
296 if (child
!= caption_button_container_
)
301 ///////////////////////////////////////////////////////////////////////////////
302 // CustomFrameViewAsh::HeaderView, ShellObserver overrides:
304 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() {
305 caption_button_container_
->UpdateSizeButtonVisibility();
309 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() {
310 caption_button_container_
->UpdateSizeButtonVisibility();
314 ///////////////////////////////////////////////////////////////////////////////
315 // CustomFrameViewAsh::HeaderView,
316 // ImmersiveFullscreenController::Delegate overrides:
318 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() {
319 fullscreen_visible_fraction_
= 0;
320 SetPaintToLayer(true);
321 SetFillsBoundsOpaquely(false);
325 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() {
326 fullscreen_visible_fraction_
= 0;
327 SetPaintToLayer(false);
331 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() {
332 fullscreen_visible_fraction_
= 0;
333 SetPaintToLayer(false);
337 void CustomFrameViewAsh::HeaderView::SetVisibleFraction(
338 double visible_fraction
) {
339 if (fullscreen_visible_fraction_
!= visible_fraction
) {
340 fullscreen_visible_fraction_
= visible_fraction
;
345 std::vector
<gfx::Rect
>
346 CustomFrameViewAsh::HeaderView::GetVisibleBoundsInScreen() const {
347 // TODO(pkotwicz): Implement views::View::ConvertRectToScreen().
348 gfx::Rect
visible_bounds(GetVisibleBounds());
349 gfx::Point
visible_origin_in_screen(visible_bounds
.origin());
350 views::View::ConvertPointToScreen(this, &visible_origin_in_screen
);
351 std::vector
<gfx::Rect
> bounds_in_screen
;
352 bounds_in_screen
.push_back(
353 gfx::Rect(visible_origin_in_screen
, visible_bounds
.size()));
354 return bounds_in_screen
;
357 ///////////////////////////////////////////////////////////////////////////////
358 // CustomFrameViewAsh::OverlayView
360 // View which takes up the entire widget and contains the HeaderView. HeaderView
361 // is a child of OverlayView to avoid creating a larger texture than necessary
362 // when painting the HeaderView to its own layer.
363 class CustomFrameViewAsh::OverlayView
: public views::View
,
364 public views::ViewTargeterDelegate
{
366 explicit OverlayView(HeaderView
* header_view
);
367 virtual ~OverlayView();
370 virtual void Layout() OVERRIDE
;
373 // views::ViewTargeterDelegate:
374 virtual bool DoesIntersectRect(const views::View
* target
,
375 const gfx::Rect
& rect
) const OVERRIDE
;
377 HeaderView
* header_view_
;
379 DISALLOW_COPY_AND_ASSIGN(OverlayView
);
382 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView
* header_view
)
383 : header_view_(header_view
) {
384 AddChildView(header_view
);
386 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
389 CustomFrameViewAsh::OverlayView::~OverlayView() {
392 ///////////////////////////////////////////////////////////////////////////////
393 // CustomFrameViewAsh::OverlayView, views::View overrides:
395 void CustomFrameViewAsh::OverlayView::Layout() {
396 // Layout |header_view_| because layout affects the result of
397 // GetPreferredOnScreenHeight().
398 header_view_
->Layout();
400 int onscreen_height
= header_view_
->GetPreferredOnScreenHeight();
401 if (onscreen_height
== 0) {
402 header_view_
->SetVisible(false);
404 int height
= header_view_
->GetPreferredHeight();
405 header_view_
->SetBounds(0, onscreen_height
- height
, width(), height
);
406 header_view_
->SetVisible(true);
410 ///////////////////////////////////////////////////////////////////////////////
411 // CustomFrameViewAsh::OverlayView, views::ViewTargeterDelegate overrides:
413 bool CustomFrameViewAsh::OverlayView::DoesIntersectRect(
414 const views::View
* target
,
415 const gfx::Rect
& rect
) const {
416 CHECK_EQ(target
, this);
417 // Grab events in the header view. Return false for other events so that they
418 // can be handled by the client view.
419 return header_view_
->HitTestRect(rect
);
422 ////////////////////////////////////////////////////////////////////////////////
423 // CustomFrameViewAsh, public:
426 const char CustomFrameViewAsh::kViewClassName
[] = "CustomFrameViewAsh";
428 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget
* frame
)
430 header_view_(new HeaderView(frame
)),
431 frame_border_hit_test_controller_(
432 new FrameBorderHitTestController(frame_
)) {
433 // |header_view_| is set as the non client view's overlay view so that it can
434 // overlay the web contents in immersive fullscreen.
435 frame
->non_client_view()->SetOverlayView(new OverlayView(header_view_
));
437 // A delegate for a more complex way of fullscreening the window may already
438 // be set. This is the case for packaged apps.
439 wm::WindowState
* window_state
= wm::GetWindowState(frame
->GetNativeWindow());
440 if (!window_state
->HasDelegate()) {
441 window_state
->SetDelegate(scoped_ptr
<wm::WindowStateDelegate
>(
442 new CustomFrameViewAshWindowStateDelegate(
443 window_state
, this)));
447 CustomFrameViewAsh::~CustomFrameViewAsh() {
450 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView(
451 ImmersiveFullscreenController
* immersive_fullscreen_controller
) {
452 immersive_fullscreen_controller
->Init(header_view_
, frame_
, header_view_
);
455 ////////////////////////////////////////////////////////////////////////////////
456 // CustomFrameViewAsh, views::NonClientFrameView overrides:
458 gfx::Rect
CustomFrameViewAsh::GetBoundsForClientView() const {
459 gfx::Rect client_bounds
= bounds();
460 client_bounds
.Inset(0, NonClientTopBorderHeight(), 0, 0);
461 return client_bounds
;
464 gfx::Rect
CustomFrameViewAsh::GetWindowBoundsForClientBounds(
465 const gfx::Rect
& client_bounds
) const {
466 gfx::Rect window_bounds
= client_bounds
;
467 window_bounds
.Inset(0, -NonClientTopBorderHeight(), 0, 0);
468 return window_bounds
;
471 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point
& point
) {
472 return FrameBorderHitTestController::NonClientHitTest(this,
473 header_view_
->caption_button_container(), point
);
476 void CustomFrameViewAsh::GetWindowMask(const gfx::Size
& size
,
477 gfx::Path
* window_mask
) {
478 // No window masks in Aura.
481 void CustomFrameViewAsh::ResetWindowControls() {
482 header_view_
->ResetWindowControls();
485 void CustomFrameViewAsh::UpdateWindowIcon() {
488 void CustomFrameViewAsh::UpdateWindowTitle() {
489 header_view_
->SchedulePaintForTitle();
492 ////////////////////////////////////////////////////////////////////////////////
493 // CustomFrameViewAsh, views::View overrides:
495 gfx::Size
CustomFrameViewAsh::GetPreferredSize() const {
496 gfx::Size pref
= frame_
->client_view()->GetPreferredSize();
497 gfx::Rect
bounds(0, 0, pref
.width(), pref
.height());
498 return frame_
->non_client_view()->GetWindowBoundsForClientBounds(
502 const char* CustomFrameViewAsh::GetClassName() const {
503 return kViewClassName
;
506 gfx::Size
CustomFrameViewAsh::GetMinimumSize() const {
507 gfx::Size
min_client_view_size(frame_
->client_view()->GetMinimumSize());
509 std::max(header_view_
->GetMinimumWidth(), min_client_view_size
.width()),
510 NonClientTopBorderHeight() + min_client_view_size
.height());
513 gfx::Size
CustomFrameViewAsh::GetMaximumSize() const {
514 gfx::Size
max_client_size(frame_
->client_view()->GetMaximumSize());
518 if (max_client_size
.width() > 0)
519 width
= std::max(header_view_
->GetMinimumWidth(), max_client_size
.width());
520 if (max_client_size
.height() > 0)
521 height
= NonClientTopBorderHeight() + max_client_size
.height();
523 return gfx::Size(width
, height
);
526 void CustomFrameViewAsh::SchedulePaintInRect(const gfx::Rect
& r
) {
527 // We may end up here before |header_view_| has been added to the Widget.
528 if (header_view_
->GetWidget()) {
529 // The HeaderView is not a child of CustomFrameViewAsh. Redirect the paint
530 // to HeaderView instead.
531 gfx::RectF
to_paint(r
);
532 views::View::ConvertRectToTarget(this, header_view_
, &to_paint
);
533 header_view_
->SchedulePaintInRect(gfx::ToEnclosingRect(to_paint
));
535 views::NonClientFrameView::SchedulePaintInRect(r
);
539 void CustomFrameViewAsh::VisibilityChanged(views::View
* starting_from
,
542 header_view_
->UpdateAvatarIcon();
545 ////////////////////////////////////////////////////////////////////////////////
546 // CustomFrameViewAsh, views::ViewTargeterDelegate overrides:
548 views::View
* CustomFrameViewAsh::GetHeaderView() {
552 const views::View
* CustomFrameViewAsh::GetAvatarIconViewForTest() const {
553 return header_view_
->avatar_icon();
556 ////////////////////////////////////////////////////////////////////////////////
557 // CustomFrameViewAsh, private:
559 // views::NonClientFrameView:
560 bool CustomFrameViewAsh::DoesIntersectRect(const views::View
* target
,
561 const gfx::Rect
& rect
) const {
562 CHECK_EQ(target
, this);
563 // NonClientView hit tests the NonClientFrameView first instead of going in
564 // z-order. Return false so that events get to the OverlayView.
568 FrameCaptionButtonContainerView
* CustomFrameViewAsh::
569 GetFrameCaptionButtonContainerViewForTest() {
570 return header_view_
->caption_button_container();
573 int CustomFrameViewAsh::NonClientTopBorderHeight() const {
574 return frame_
->IsFullscreen() ? 0 : header_view_
->GetPreferredHeight();