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"
7 #include "ash/ash_switches.h"
8 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h"
9 #include "ash/frame/default_header_painter.h"
10 #include "ash/frame/frame_border_hit_test_controller.h"
11 #include "ash/frame/frame_util.h"
12 #include "ash/frame/header_painter.h"
13 #include "ash/session/session_state_delegate.h"
14 #include "ash/shell.h"
15 #include "ash/shell_observer.h"
16 #include "ash/wm/immersive_fullscreen_controller.h"
17 #include "ash/wm/window_state.h"
18 #include "ash/wm/window_state_delegate.h"
19 #include "ash/wm/window_state_observer.h"
20 #include "base/command_line.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_observer.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/image/image.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/gfx/rect_conversions.h"
28 #include "ui/gfx/size.h"
29 #include "ui/views/controls/image_view.h"
30 #include "ui/views/view.h"
31 #include "ui/views/widget/widget.h"
32 #include "ui/views/widget/widget_delegate.h"
36 ///////////////////////////////////////////////////////////////////////////////
37 // CustomFrameViewAshWindowStateDelegate
39 // Handles a user's fullscreen request (Shift+F4/F4). Puts the window into
40 // immersive fullscreen if immersive fullscreen is enabled for non-browser
42 class CustomFrameViewAshWindowStateDelegate
43 : public ash::wm::WindowStateDelegate,
44 public ash::wm::WindowStateObserver,
45 public aura::WindowObserver {
47 CustomFrameViewAshWindowStateDelegate(
48 ash::wm::WindowState* window_state,
49 ash::CustomFrameViewAsh* custom_frame_view)
50 : window_state_(NULL) {
51 immersive_fullscreen_controller_.reset(
52 new ash::ImmersiveFullscreenController);
53 custom_frame_view->InitImmersiveFullscreenControllerForView(
54 immersive_fullscreen_controller_.get());
56 // Add a window state observer to exit fullscreen properly in case
57 // fullscreen is exited without going through
58 // WindowState::ToggleFullscreen(). This is the case when exiting
59 // immersive fullscreen via the "Restore" window control.
60 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
61 window_state_ = window_state;
62 window_state_->AddObserver(this);
63 window_state_->window()->AddObserver(this);
65 virtual ~CustomFrameViewAshWindowStateDelegate() {
67 window_state_->RemoveObserver(this);
68 window_state_->window()->RemoveObserver(this);
72 // Overridden from ash::wm::WindowStateDelegate:
73 virtual bool ToggleFullscreen(ash::wm::WindowState* window_state) OVERRIDE {
74 bool enter_fullscreen = !window_state->IsFullscreen();
75 if (enter_fullscreen) {
76 window_state->window()->SetProperty(aura::client::kShowStateKey,
77 ui::SHOW_STATE_FULLSCREEN);
79 window_state->Restore();
81 if (immersive_fullscreen_controller_) {
82 immersive_fullscreen_controller_->SetEnabled(
83 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER,
88 // Overridden from aura::WindowObserver:
89 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
90 window_state_->RemoveObserver(this);
91 window_state_->window()->RemoveObserver(this);
94 // Overridden from ash::wm::WindowStateObserver:
95 virtual void OnPostWindowStateTypeChange(
96 ash::wm::WindowState* window_state,
97 ash::wm::WindowStateType old_type) OVERRIDE {
98 if (!window_state->IsFullscreen() &&
99 !window_state->IsMinimized() &&
100 immersive_fullscreen_controller_.get() &&
101 immersive_fullscreen_controller_->IsEnabled()) {
102 immersive_fullscreen_controller_->SetEnabled(
103 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER,
108 ash::wm::WindowState* window_state_;
109 scoped_ptr<ash::ImmersiveFullscreenController>
110 immersive_fullscreen_controller_;
112 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshWindowStateDelegate);
119 ///////////////////////////////////////////////////////////////////////////////
120 // CustomFrameViewAsh::HeaderView
122 // View which paints the header. It slides off and on screen in immersive
124 class CustomFrameViewAsh::HeaderView
125 : public views::View,
126 public ImmersiveFullscreenController::Delegate,
127 public ShellObserver {
129 // |frame| is the widget that the caption buttons act on.
130 explicit HeaderView(views::Widget* frame);
131 virtual ~HeaderView();
133 // Schedules a repaint for the entire title.
134 void SchedulePaintForTitle();
136 // Tells the window controls to reset themselves to the normal state.
137 void ResetWindowControls();
139 // Returns the amount of the view's pixels which should be on screen.
140 int GetPreferredOnScreenHeight() const;
142 // Returns the view's preferred height.
143 int GetPreferredHeight() const;
145 // Returns the view's minimum width.
146 int GetMinimumWidth() const;
148 void UpdateAvatarIcon();
151 virtual void Layout() OVERRIDE;
152 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
155 virtual void OnMaximizeModeStarted() OVERRIDE;
156 virtual void OnMaximizeModeEnded() OVERRIDE;
158 FrameCaptionButtonContainerView* caption_button_container() {
159 return caption_button_container_;
162 views::View* avatar_icon() const {
167 // ImmersiveFullscreenController::Delegate:
168 virtual void OnImmersiveRevealStarted() OVERRIDE;
169 virtual void OnImmersiveRevealEnded() OVERRIDE;
170 virtual void OnImmersiveFullscreenExited() OVERRIDE;
171 virtual void SetVisibleFraction(double visible_fraction) OVERRIDE;
172 virtual std::vector<gfx::Rect> GetVisibleBoundsInScreen() const OVERRIDE;
174 // The widget that the caption buttons act on.
175 views::Widget* frame_;
177 // Helper for painting the header.
178 scoped_ptr<DefaultHeaderPainter> header_painter_;
180 views::ImageView* avatar_icon_;
182 // View which contains the window caption buttons.
183 FrameCaptionButtonContainerView* caption_button_container_;
185 // The fraction of the header's height which is visible while in fullscreen.
186 // This value is meaningless when not in fullscreen.
187 double fullscreen_visible_fraction_;
189 DISALLOW_COPY_AND_ASSIGN(HeaderView);
192 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget* frame)
194 header_painter_(new ash::DefaultHeaderPainter),
196 caption_button_container_(NULL),
197 fullscreen_visible_fraction_(0) {
198 // Unfortunately, there is no views::WidgetDelegate::CanMinimize(). Assume
199 // that the window frame can be minimized if it can be maximized.
200 FrameCaptionButtonContainerView::MinimizeAllowed minimize_allowed =
201 frame_->widget_delegate()->CanMaximize() ?
202 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED :
203 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED;
204 caption_button_container_ = new FrameCaptionButtonContainerView(frame_,
206 caption_button_container_->UpdateSizeButtonVisibility(Shell::GetInstance()->
207 IsMaximizeModeWindowManagerEnabled());
208 AddChildView(caption_button_container_);
210 header_painter_->Init(frame_, this, NULL, 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);
256 gfx::ImageSkia image = GetAvatarImageForContext(
257 delegate->GetBrowserContextForWindow(window)).AsImageSkia();
258 DCHECK(!image.isNull());
259 DCHECK_EQ(image.width(), image.height());
261 avatar_icon_ = new views::ImageView();
262 AddChildView(avatar_icon_);
264 avatar_icon_->SetImage(image);
265 icon_size = image.width();
267 header_painter_->UpdateWindowIcon(avatar_icon_, icon_size);
271 ///////////////////////////////////////////////////////////////////////////////
272 // CustomFrameViewAsh::HeaderView, views::View overrides:
274 void CustomFrameViewAsh::HeaderView::Layout() {
275 header_painter_->LayoutHeader();
278 void CustomFrameViewAsh::HeaderView::OnPaint(gfx::Canvas* canvas) {
279 bool paint_as_active =
280 frame_->non_client_view()->frame_view()->ShouldPaintAsActive();
281 caption_button_container_->SetPaintAsActive(paint_as_active);
283 HeaderPainter::Mode header_mode = paint_as_active ?
284 HeaderPainter::MODE_ACTIVE : HeaderPainter::MODE_INACTIVE;
285 header_painter_->PaintHeader(canvas, header_mode);
288 ///////////////////////////////////////////////////////////////////////////////
289 // CustomFrameViewAsh::HeaderView, ShellObserver overrides:
291 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() {
292 caption_button_container_->UpdateSizeButtonVisibility(true);
296 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() {
297 caption_button_container_->UpdateSizeButtonVisibility(false);
301 ///////////////////////////////////////////////////////////////////////////////
302 // CustomFrameViewAsh::HeaderView,
303 // ImmersiveFullscreenController::Delegate overrides:
305 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() {
306 fullscreen_visible_fraction_ = 0;
307 SetPaintToLayer(true);
308 SetFillsBoundsOpaquely(false);
312 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() {
313 fullscreen_visible_fraction_ = 0;
314 SetPaintToLayer(false);
318 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() {
319 fullscreen_visible_fraction_ = 0;
320 SetPaintToLayer(false);
324 void CustomFrameViewAsh::HeaderView::SetVisibleFraction(
325 double visible_fraction) {
326 if (fullscreen_visible_fraction_ != visible_fraction) {
327 fullscreen_visible_fraction_ = visible_fraction;
332 std::vector<gfx::Rect>
333 CustomFrameViewAsh::HeaderView::GetVisibleBoundsInScreen() const {
334 // TODO(pkotwicz): Implement views::View::ConvertRectToScreen().
335 gfx::Rect visible_bounds(GetVisibleBounds());
336 gfx::Point visible_origin_in_screen(visible_bounds.origin());
337 views::View::ConvertPointToScreen(this, &visible_origin_in_screen);
338 std::vector<gfx::Rect> bounds_in_screen;
339 bounds_in_screen.push_back(
340 gfx::Rect(visible_origin_in_screen, visible_bounds.size()));
341 return bounds_in_screen;
344 ///////////////////////////////////////////////////////////////////////////////
345 // CustomFrameViewAsh::OverlayView
347 // View which takes up the entire widget and contains the HeaderView. HeaderView
348 // is a child of OverlayView to avoid creating a larger texture than necessary
349 // when painting the HeaderView to its own layer.
350 class CustomFrameViewAsh::OverlayView : public views::View {
352 explicit OverlayView(HeaderView* header_view);
353 virtual ~OverlayView();
355 // views::View override:
356 virtual void Layout() OVERRIDE;
357 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
360 HeaderView* header_view_;
362 DISALLOW_COPY_AND_ASSIGN(OverlayView);
365 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView* header_view)
366 : header_view_(header_view) {
367 AddChildView(header_view);
370 CustomFrameViewAsh::OverlayView::~OverlayView() {
373 ///////////////////////////////////////////////////////////////////////////////
374 // CustomFrameViewAsh::OverlayView, views::View overrides:
376 void CustomFrameViewAsh::OverlayView::Layout() {
377 // Layout |header_view_| because layout affects the result of
378 // GetPreferredOnScreenHeight().
379 header_view_->Layout();
381 int onscreen_height = header_view_->GetPreferredOnScreenHeight();
382 if (onscreen_height == 0) {
383 header_view_->SetVisible(false);
385 int height = header_view_->GetPreferredHeight();
386 header_view_->SetBounds(0, onscreen_height - height, width(), height);
387 header_view_->SetVisible(true);
391 bool CustomFrameViewAsh::OverlayView::HitTestRect(const gfx::Rect& rect) const {
392 // Grab events in the header view. Return false for other events so that they
393 // can be handled by the client view.
394 return header_view_->HitTestRect(rect);
397 ////////////////////////////////////////////////////////////////////////////////
398 // CustomFrameViewAsh, public:
401 const char CustomFrameViewAsh::kViewClassName[] = "CustomFrameViewAsh";
403 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget* frame)
405 header_view_(new HeaderView(frame)),
406 frame_border_hit_test_controller_(
407 new FrameBorderHitTestController(frame_)) {
408 // |header_view_| is set as the non client view's overlay view so that it can
409 // overlay the web contents in immersive fullscreen.
410 frame->non_client_view()->SetOverlayView(new OverlayView(header_view_));
412 // A delegate for a more complex way of fullscreening the window may already
413 // be set. This is the case for packaged apps.
414 wm::WindowState* window_state = wm::GetWindowState(frame->GetNativeWindow());
415 if (!window_state->HasDelegate()) {
416 window_state->SetDelegate(scoped_ptr<wm::WindowStateDelegate>(
417 new CustomFrameViewAshWindowStateDelegate(
418 window_state, this)));
422 CustomFrameViewAsh::~CustomFrameViewAsh() {
425 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView(
426 ImmersiveFullscreenController* immersive_fullscreen_controller) {
427 immersive_fullscreen_controller->Init(header_view_, frame_, header_view_);
430 ////////////////////////////////////////////////////////////////////////////////
431 // CustomFrameViewAsh, views::NonClientFrameView overrides:
433 gfx::Rect CustomFrameViewAsh::GetBoundsForClientView() const {
434 gfx::Rect client_bounds = bounds();
435 client_bounds.Inset(0, NonClientTopBorderHeight(), 0, 0);
436 return client_bounds;
439 gfx::Rect CustomFrameViewAsh::GetWindowBoundsForClientBounds(
440 const gfx::Rect& client_bounds) const {
441 gfx::Rect window_bounds = client_bounds;
442 window_bounds.Inset(0, -NonClientTopBorderHeight(), 0, 0);
443 return window_bounds;
446 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point& point) {
447 return FrameBorderHitTestController::NonClientHitTest(this,
448 header_view_->caption_button_container(), point);
451 void CustomFrameViewAsh::GetWindowMask(const gfx::Size& size,
452 gfx::Path* window_mask) {
453 // No window masks in Aura.
456 void CustomFrameViewAsh::ResetWindowControls() {
457 header_view_->ResetWindowControls();
460 void CustomFrameViewAsh::UpdateWindowIcon() {
463 void CustomFrameViewAsh::UpdateWindowTitle() {
464 header_view_->SchedulePaintForTitle();
467 ////////////////////////////////////////////////////////////////////////////////
468 // CustomFrameViewAsh, views::View overrides:
470 gfx::Size CustomFrameViewAsh::GetPreferredSize() {
471 gfx::Size pref = frame_->client_view()->GetPreferredSize();
472 gfx::Rect bounds(0, 0, pref.width(), pref.height());
473 return frame_->non_client_view()->GetWindowBoundsForClientBounds(
477 const char* CustomFrameViewAsh::GetClassName() const {
478 return kViewClassName;
481 gfx::Size CustomFrameViewAsh::GetMinimumSize() {
482 gfx::Size min_client_view_size(frame_->client_view()->GetMinimumSize());
484 std::max(header_view_->GetMinimumWidth(), min_client_view_size.width()),
485 NonClientTopBorderHeight() + min_client_view_size.height());
488 gfx::Size CustomFrameViewAsh::GetMaximumSize() {
489 gfx::Size max_client_size(frame_->client_view()->GetMaximumSize());
493 if (max_client_size.width() > 0)
494 width = std::max(header_view_->GetMinimumWidth(), max_client_size.width());
495 if (max_client_size.height() > 0)
496 height = NonClientTopBorderHeight() + max_client_size.height();
498 return gfx::Size(width, height);
501 void CustomFrameViewAsh::SchedulePaintInRect(const gfx::Rect& r) {
502 // We may end up here before |header_view_| has been added to the Widget.
503 if (header_view_->GetWidget()) {
504 // The HeaderView is not a child of CustomFrameViewAsh. Redirect the paint
505 // to HeaderView instead.
506 gfx::RectF to_paint(r);
507 views::View::ConvertRectToTarget(this, header_view_, &to_paint);
508 header_view_->SchedulePaintInRect(gfx::ToEnclosingRect(to_paint));
510 views::NonClientFrameView::SchedulePaintInRect(r);
514 bool CustomFrameViewAsh::HitTestRect(const gfx::Rect& rect) const {
515 // NonClientView hit tests the NonClientFrameView first instead of going in
516 // z-order. Return false so that events get to the OverlayView.
520 void CustomFrameViewAsh::VisibilityChanged(views::View* starting_from,
523 header_view_->UpdateAvatarIcon();
526 views::View* CustomFrameViewAsh::GetHeaderView() {
530 const views::View* CustomFrameViewAsh::GetAvatarIconViewForTest() const {
531 return header_view_->avatar_icon();
534 ////////////////////////////////////////////////////////////////////////////////
535 // CustomFrameViewAsh, private:
537 FrameCaptionButtonContainerView* CustomFrameViewAsh::
538 GetFrameCaptionButtonContainerViewForTest() {
539 return header_view_->caption_button_container();
542 int CustomFrameViewAsh::NonClientTopBorderHeight() const {
543 return frame_->IsFullscreen() ? 0 : header_view_->GetPreferredHeight();