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/widget/widget.h"
35 #include "ui/views/widget/widget_delegate.h"
39 ///////////////////////////////////////////////////////////////////////////////
40 // CustomFrameViewAshWindowStateDelegate
42 // Handles a user's fullscreen request (Shift+F4/F4). Puts the window into
43 // immersive fullscreen if immersive fullscreen is enabled for non-browser
45 class CustomFrameViewAshWindowStateDelegate
46 : public ash::wm::WindowStateDelegate
,
47 public ash::wm::WindowStateObserver
,
48 public aura::WindowObserver
{
50 CustomFrameViewAshWindowStateDelegate(
51 ash::wm::WindowState
* window_state
,
52 ash::CustomFrameViewAsh
* custom_frame_view
)
53 : window_state_(NULL
) {
54 immersive_fullscreen_controller_
.reset(
55 new ash::ImmersiveFullscreenController
);
56 custom_frame_view
->InitImmersiveFullscreenControllerForView(
57 immersive_fullscreen_controller_
.get());
59 // Add a window state observer to exit fullscreen properly in case
60 // fullscreen is exited without going through
61 // WindowState::ToggleFullscreen(). This is the case when exiting
62 // immersive fullscreen via the "Restore" window control.
63 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
64 window_state_
= window_state
;
65 window_state_
->AddObserver(this);
66 window_state_
->window()->AddObserver(this);
68 virtual ~CustomFrameViewAshWindowStateDelegate() {
70 window_state_
->RemoveObserver(this);
71 window_state_
->window()->RemoveObserver(this);
75 // Overridden from ash::wm::WindowStateDelegate:
76 virtual bool ToggleFullscreen(ash::wm::WindowState
* window_state
) OVERRIDE
{
77 bool enter_fullscreen
= !window_state
->IsFullscreen();
78 if (enter_fullscreen
) {
79 window_state
->window()->SetProperty(aura::client::kShowStateKey
,
80 ui::SHOW_STATE_FULLSCREEN
);
82 window_state
->Restore();
84 if (immersive_fullscreen_controller_
) {
85 immersive_fullscreen_controller_
->SetEnabled(
86 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER
,
91 // Overridden from aura::WindowObserver:
92 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{
93 window_state_
->RemoveObserver(this);
94 window_state_
->window()->RemoveObserver(this);
97 // Overridden from ash::wm::WindowStateObserver:
98 virtual void OnPostWindowStateTypeChange(
99 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 virtual ~HeaderView();
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();
154 virtual void Layout() OVERRIDE
;
155 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
158 virtual void OnMaximizeModeStarted() OVERRIDE
;
159 virtual void OnMaximizeModeEnded() OVERRIDE
;
161 FrameCaptionButtonContainerView
* caption_button_container() {
162 return caption_button_container_
;
165 views::View
* avatar_icon() const {
170 // ImmersiveFullscreenController::Delegate:
171 virtual void OnImmersiveRevealStarted() OVERRIDE
;
172 virtual void OnImmersiveRevealEnded() OVERRIDE
;
173 virtual void OnImmersiveFullscreenExited() OVERRIDE
;
174 virtual void SetVisibleFraction(double visible_fraction
) OVERRIDE
;
175 virtual std::vector
<gfx::Rect
> GetVisibleBoundsInScreen() const OVERRIDE
;
177 // The widget that the caption buttons act on.
178 views::Widget
* frame_
;
180 // Helper for painting the header.
181 scoped_ptr
<DefaultHeaderPainter
> header_painter_
;
183 views::ImageView
* avatar_icon_
;
185 // View which contains the window caption buttons.
186 FrameCaptionButtonContainerView
* caption_button_container_
;
188 // The fraction of the header's height which is visible while in fullscreen.
189 // This value is meaningless when not in fullscreen.
190 double fullscreen_visible_fraction_
;
192 DISALLOW_COPY_AND_ASSIGN(HeaderView
);
195 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget
* frame
)
197 header_painter_(new ash::DefaultHeaderPainter
),
199 caption_button_container_(NULL
),
200 fullscreen_visible_fraction_(0) {
201 // Unfortunately, there is no views::WidgetDelegate::CanMinimize(). Assume
202 // that the window frame can be minimized if it can be maximized.
203 FrameCaptionButtonContainerView::MinimizeAllowed minimize_allowed
=
204 frame_
->widget_delegate()->CanMaximize() ?
205 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED
:
206 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED
;
207 caption_button_container_
= new FrameCaptionButtonContainerView(frame_
,
209 caption_button_container_
->UpdateSizeButtonVisibility();
210 AddChildView(caption_button_container_
);
212 header_painter_
->Init(frame_
, this, NULL
, caption_button_container_
);
215 Shell::GetInstance()->AddShellObserver(this);
218 CustomFrameViewAsh::HeaderView::~HeaderView() {
219 Shell::GetInstance()->RemoveShellObserver(this);
222 void CustomFrameViewAsh::HeaderView::SchedulePaintForTitle() {
223 header_painter_
->SchedulePaintForTitle();
226 void CustomFrameViewAsh::HeaderView::ResetWindowControls() {
227 caption_button_container_
->ResetWindowControls();
230 int CustomFrameViewAsh::HeaderView::GetPreferredOnScreenHeight() const {
231 if (frame_
->IsFullscreen()) {
232 return static_cast<int>(
233 GetPreferredHeight() * fullscreen_visible_fraction_
);
235 return GetPreferredHeight();
238 int CustomFrameViewAsh::HeaderView::GetPreferredHeight() const {
239 return header_painter_
->GetHeaderHeightForPainting();
242 int CustomFrameViewAsh::HeaderView::GetMinimumWidth() const {
243 return header_painter_
->GetMinimumHeaderWidth();
246 void CustomFrameViewAsh::HeaderView::UpdateAvatarIcon() {
247 SessionStateDelegate
* delegate
=
248 Shell::GetInstance()->session_state_delegate();
249 aura::Window
* window
= frame_
->GetNativeView();
250 bool show
= delegate
->ShouldShowAvatar(window
);
258 gfx::ImageSkia image
= GetAvatarImageForContext(
259 delegate
->GetBrowserContextForWindow(window
)).AsImageSkia();
260 DCHECK(!image
.isNull());
261 DCHECK_EQ(image
.width(), image
.height());
263 avatar_icon_
= new views::ImageView();
264 AddChildView(avatar_icon_
);
266 avatar_icon_
->SetImage(image
);
267 icon_size
= image
.width();
269 header_painter_
->UpdateWindowIcon(avatar_icon_
, icon_size
);
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 ///////////////////////////////////////////////////////////////////////////////
291 // CustomFrameViewAsh::HeaderView, ShellObserver overrides:
293 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() {
294 caption_button_container_
->UpdateSizeButtonVisibility();
298 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() {
299 caption_button_container_
->UpdateSizeButtonVisibility();
303 ///////////////////////////////////////////////////////////////////////////////
304 // CustomFrameViewAsh::HeaderView,
305 // ImmersiveFullscreenController::Delegate overrides:
307 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() {
308 fullscreen_visible_fraction_
= 0;
309 SetPaintToLayer(true);
310 SetFillsBoundsOpaquely(false);
314 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() {
315 fullscreen_visible_fraction_
= 0;
316 SetPaintToLayer(false);
320 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() {
321 fullscreen_visible_fraction_
= 0;
322 SetPaintToLayer(false);
326 void CustomFrameViewAsh::HeaderView::SetVisibleFraction(
327 double visible_fraction
) {
328 if (fullscreen_visible_fraction_
!= visible_fraction
) {
329 fullscreen_visible_fraction_
= visible_fraction
;
334 std::vector
<gfx::Rect
>
335 CustomFrameViewAsh::HeaderView::GetVisibleBoundsInScreen() const {
336 // TODO(pkotwicz): Implement views::View::ConvertRectToScreen().
337 gfx::Rect
visible_bounds(GetVisibleBounds());
338 gfx::Point
visible_origin_in_screen(visible_bounds
.origin());
339 views::View::ConvertPointToScreen(this, &visible_origin_in_screen
);
340 std::vector
<gfx::Rect
> bounds_in_screen
;
341 bounds_in_screen
.push_back(
342 gfx::Rect(visible_origin_in_screen
, visible_bounds
.size()));
343 return bounds_in_screen
;
346 ///////////////////////////////////////////////////////////////////////////////
347 // CustomFrameViewAsh::OverlayView
349 // View which takes up the entire widget and contains the HeaderView. HeaderView
350 // is a child of OverlayView to avoid creating a larger texture than necessary
351 // when painting the HeaderView to its own layer.
352 class CustomFrameViewAsh::OverlayView
: public views::View
{
354 explicit OverlayView(HeaderView
* header_view
);
355 virtual ~OverlayView();
357 // views::View override:
358 virtual void Layout() OVERRIDE
;
359 virtual bool HitTestRect(const gfx::Rect
& rect
) const OVERRIDE
;
362 HeaderView
* header_view_
;
364 DISALLOW_COPY_AND_ASSIGN(OverlayView
);
367 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView
* header_view
)
368 : header_view_(header_view
) {
369 AddChildView(header_view
);
372 CustomFrameViewAsh::OverlayView::~OverlayView() {
375 ///////////////////////////////////////////////////////////////////////////////
376 // CustomFrameViewAsh::OverlayView, views::View overrides:
378 void CustomFrameViewAsh::OverlayView::Layout() {
379 // Layout |header_view_| because layout affects the result of
380 // GetPreferredOnScreenHeight().
381 header_view_
->Layout();
383 int onscreen_height
= header_view_
->GetPreferredOnScreenHeight();
384 if (onscreen_height
== 0) {
385 header_view_
->SetVisible(false);
387 int height
= header_view_
->GetPreferredHeight();
388 header_view_
->SetBounds(0, onscreen_height
- height
, width(), height
);
389 header_view_
->SetVisible(true);
393 bool CustomFrameViewAsh::OverlayView::HitTestRect(const gfx::Rect
& rect
) const {
394 // Grab events in the header view. Return false for other events so that they
395 // can be handled by the client view.
396 return header_view_
->HitTestRect(rect
);
399 ////////////////////////////////////////////////////////////////////////////////
400 // CustomFrameViewAsh, public:
403 const char CustomFrameViewAsh::kViewClassName
[] = "CustomFrameViewAsh";
405 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget
* frame
)
407 header_view_(new HeaderView(frame
)),
408 frame_border_hit_test_controller_(
409 new FrameBorderHitTestController(frame_
)) {
410 // |header_view_| is set as the non client view's overlay view so that it can
411 // overlay the web contents in immersive fullscreen.
412 frame
->non_client_view()->SetOverlayView(new OverlayView(header_view_
));
414 // A delegate for a more complex way of fullscreening the window may already
415 // be set. This is the case for packaged apps.
416 wm::WindowState
* window_state
= wm::GetWindowState(frame
->GetNativeWindow());
417 if (!window_state
->HasDelegate()) {
418 window_state
->SetDelegate(scoped_ptr
<wm::WindowStateDelegate
>(
419 new CustomFrameViewAshWindowStateDelegate(
420 window_state
, this)));
424 CustomFrameViewAsh::~CustomFrameViewAsh() {
427 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView(
428 ImmersiveFullscreenController
* immersive_fullscreen_controller
) {
429 immersive_fullscreen_controller
->Init(header_view_
, frame_
, header_view_
);
432 ////////////////////////////////////////////////////////////////////////////////
433 // CustomFrameViewAsh, views::NonClientFrameView overrides:
435 gfx::Rect
CustomFrameViewAsh::GetBoundsForClientView() const {
436 gfx::Rect client_bounds
= bounds();
437 client_bounds
.Inset(0, NonClientTopBorderHeight(), 0, 0);
438 return client_bounds
;
441 gfx::Rect
CustomFrameViewAsh::GetWindowBoundsForClientBounds(
442 const gfx::Rect
& client_bounds
) const {
443 gfx::Rect window_bounds
= client_bounds
;
444 window_bounds
.Inset(0, -NonClientTopBorderHeight(), 0, 0);
445 return window_bounds
;
448 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point
& point
) {
449 return FrameBorderHitTestController::NonClientHitTest(this,
450 header_view_
->caption_button_container(), point
);
453 void CustomFrameViewAsh::GetWindowMask(const gfx::Size
& size
,
454 gfx::Path
* window_mask
) {
455 // No window masks in Aura.
458 void CustomFrameViewAsh::ResetWindowControls() {
459 header_view_
->ResetWindowControls();
462 void CustomFrameViewAsh::UpdateWindowIcon() {
465 void CustomFrameViewAsh::UpdateWindowTitle() {
466 header_view_
->SchedulePaintForTitle();
469 ////////////////////////////////////////////////////////////////////////////////
470 // CustomFrameViewAsh, views::View overrides:
472 gfx::Size
CustomFrameViewAsh::GetPreferredSize() const {
473 gfx::Size pref
= frame_
->client_view()->GetPreferredSize();
474 gfx::Rect
bounds(0, 0, pref
.width(), pref
.height());
475 return frame_
->non_client_view()->GetWindowBoundsForClientBounds(
479 const char* CustomFrameViewAsh::GetClassName() const {
480 return kViewClassName
;
483 gfx::Size
CustomFrameViewAsh::GetMinimumSize() const {
484 gfx::Size
min_client_view_size(frame_
->client_view()->GetMinimumSize());
486 std::max(header_view_
->GetMinimumWidth(), min_client_view_size
.width()),
487 NonClientTopBorderHeight() + min_client_view_size
.height());
490 gfx::Size
CustomFrameViewAsh::GetMaximumSize() const {
491 gfx::Size
max_client_size(frame_
->client_view()->GetMaximumSize());
495 if (max_client_size
.width() > 0)
496 width
= std::max(header_view_
->GetMinimumWidth(), max_client_size
.width());
497 if (max_client_size
.height() > 0)
498 height
= NonClientTopBorderHeight() + max_client_size
.height();
500 return gfx::Size(width
, height
);
503 void CustomFrameViewAsh::SchedulePaintInRect(const gfx::Rect
& r
) {
504 // We may end up here before |header_view_| has been added to the Widget.
505 if (header_view_
->GetWidget()) {
506 // The HeaderView is not a child of CustomFrameViewAsh. Redirect the paint
507 // to HeaderView instead.
508 gfx::RectF
to_paint(r
);
509 views::View::ConvertRectToTarget(this, header_view_
, &to_paint
);
510 header_view_
->SchedulePaintInRect(gfx::ToEnclosingRect(to_paint
));
512 views::NonClientFrameView::SchedulePaintInRect(r
);
516 bool CustomFrameViewAsh::HitTestRect(const gfx::Rect
& rect
) const {
517 // NonClientView hit tests the NonClientFrameView first instead of going in
518 // z-order. Return false so that events get to the OverlayView.
522 void CustomFrameViewAsh::VisibilityChanged(views::View
* starting_from
,
525 header_view_
->UpdateAvatarIcon();
528 views::View
* CustomFrameViewAsh::GetHeaderView() {
532 const views::View
* CustomFrameViewAsh::GetAvatarIconViewForTest() const {
533 return header_view_
->avatar_icon();
536 ////////////////////////////////////////////////////////////////////////////////
537 // CustomFrameViewAsh, private:
539 FrameCaptionButtonContainerView
* CustomFrameViewAsh::
540 GetFrameCaptionButtonContainerViewForTest() {
541 return header_view_
->caption_button_container();
544 int CustomFrameViewAsh::NonClientTopBorderHeight() const {
545 return frame_
->IsFullscreen() ? 0 : header_view_
->GetPreferredHeight();