Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / frame / custom_frame_view_ash.cc
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.
4
5 #include "ash/frame/custom_frame_view_ash.h"
6
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"
33
34 namespace {
35
36 ///////////////////////////////////////////////////////////////////////////////
37 // CustomFrameViewAshWindowStateDelegate
38
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
41 // windows.
42 class CustomFrameViewAshWindowStateDelegate
43     : public ash::wm::WindowStateDelegate,
44       public ash::wm::WindowStateObserver,
45       public aura::WindowObserver {
46  public:
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());
55
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);
64   }
65   virtual ~CustomFrameViewAshWindowStateDelegate() {
66     if (window_state_) {
67       window_state_->RemoveObserver(this);
68       window_state_->window()->RemoveObserver(this);
69     }
70   }
71  private:
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);
78     } else {
79       window_state->Restore();
80     }
81     if (immersive_fullscreen_controller_) {
82       immersive_fullscreen_controller_->SetEnabled(
83           ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER,
84           enter_fullscreen);
85     }
86     return true;
87   }
88   // Overridden from aura::WindowObserver:
89   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
90     window_state_->RemoveObserver(this);
91     window_state_->window()->RemoveObserver(this);
92     window_state_ = NULL;
93   }
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,
104           false);
105     }
106   }
107
108   ash::wm::WindowState* window_state_;
109   scoped_ptr<ash::ImmersiveFullscreenController>
110       immersive_fullscreen_controller_;
111
112   DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshWindowStateDelegate);
113 };
114
115 }  // namespace
116
117 namespace ash {
118
119 ///////////////////////////////////////////////////////////////////////////////
120 // CustomFrameViewAsh::HeaderView
121
122 // View which paints the header. It slides off and on screen in immersive
123 // fullscreen.
124 class CustomFrameViewAsh::HeaderView
125     : public views::View,
126       public ImmersiveFullscreenController::Delegate,
127       public ShellObserver {
128  public:
129   // |frame| is the widget that the caption buttons act on.
130   explicit HeaderView(views::Widget* frame);
131   virtual ~HeaderView();
132
133   // Schedules a repaint for the entire title.
134   void SchedulePaintForTitle();
135
136   // Tells the window controls to reset themselves to the normal state.
137   void ResetWindowControls();
138
139   // Returns the amount of the view's pixels which should be on screen.
140   int GetPreferredOnScreenHeight() const;
141
142   // Returns the view's preferred height.
143   int GetPreferredHeight() const;
144
145   // Returns the view's minimum width.
146   int GetMinimumWidth() const;
147
148   void UpdateAvatarIcon();
149
150   // views::View:
151   virtual void Layout() OVERRIDE;
152   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
153
154   // ShellObserver:
155   virtual void OnMaximizeModeStarted() OVERRIDE;
156   virtual void OnMaximizeModeEnded() OVERRIDE;
157
158   FrameCaptionButtonContainerView* caption_button_container() {
159     return caption_button_container_;
160   }
161
162   views::View* avatar_icon() const {
163     return avatar_icon_;
164   }
165
166  private:
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;
173
174   // The widget that the caption buttons act on.
175   views::Widget* frame_;
176
177   // Helper for painting the header.
178   scoped_ptr<DefaultHeaderPainter> header_painter_;
179
180   views::ImageView* avatar_icon_;
181
182   // View which contains the window caption buttons.
183   FrameCaptionButtonContainerView* caption_button_container_;
184
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_;
188
189   DISALLOW_COPY_AND_ASSIGN(HeaderView);
190 };
191
192 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget* frame)
193     : frame_(frame),
194       header_painter_(new ash::DefaultHeaderPainter),
195       avatar_icon_(NULL),
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_,
205       minimize_allowed);
206   caption_button_container_->UpdateSizeButtonVisibility(Shell::GetInstance()->
207       IsMaximizeModeWindowManagerEnabled());
208   AddChildView(caption_button_container_);
209
210   header_painter_->Init(frame_, this, NULL, caption_button_container_);
211   UpdateAvatarIcon();
212
213   Shell::GetInstance()->AddShellObserver(this);
214 }
215
216 CustomFrameViewAsh::HeaderView::~HeaderView() {
217   Shell::GetInstance()->RemoveShellObserver(this);
218 }
219
220 void CustomFrameViewAsh::HeaderView::SchedulePaintForTitle() {
221   header_painter_->SchedulePaintForTitle();
222 }
223
224 void CustomFrameViewAsh::HeaderView::ResetWindowControls() {
225   caption_button_container_->ResetWindowControls();
226 }
227
228 int CustomFrameViewAsh::HeaderView::GetPreferredOnScreenHeight() const {
229   if (frame_->IsFullscreen()) {
230     return static_cast<int>(
231         GetPreferredHeight() * fullscreen_visible_fraction_);
232   }
233   return GetPreferredHeight();
234 }
235
236 int CustomFrameViewAsh::HeaderView::GetPreferredHeight() const {
237   return header_painter_->GetHeaderHeightForPainting();
238 }
239
240 int CustomFrameViewAsh::HeaderView::GetMinimumWidth() const {
241   return header_painter_->GetMinimumHeaderWidth();
242 }
243
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);
249   int icon_size = 0;
250   if (!show) {
251     if (!avatar_icon_)
252       return;
253     delete avatar_icon_;
254     avatar_icon_ = NULL;
255   } else {
256     gfx::ImageSkia image = GetAvatarImageForContext(
257         delegate->GetBrowserContextForWindow(window)).AsImageSkia();
258     DCHECK(!image.isNull());
259     DCHECK_EQ(image.width(), image.height());
260     if (!avatar_icon_) {
261       avatar_icon_ = new views::ImageView();
262       AddChildView(avatar_icon_);
263     }
264     avatar_icon_->SetImage(image);
265     icon_size = image.width();
266   }
267   header_painter_->UpdateWindowIcon(avatar_icon_, icon_size);
268   Layout();
269 }
270
271 ///////////////////////////////////////////////////////////////////////////////
272 // CustomFrameViewAsh::HeaderView, views::View overrides:
273
274 void CustomFrameViewAsh::HeaderView::Layout() {
275   header_painter_->LayoutHeader();
276 }
277
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);
282
283   HeaderPainter::Mode header_mode = paint_as_active ?
284       HeaderPainter::MODE_ACTIVE : HeaderPainter::MODE_INACTIVE;
285   header_painter_->PaintHeader(canvas, header_mode);
286 }
287
288 ///////////////////////////////////////////////////////////////////////////////
289 // CustomFrameViewAsh::HeaderView, ShellObserver overrides:
290
291 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() {
292   caption_button_container_->UpdateSizeButtonVisibility(true);
293   parent()->Layout();
294 }
295
296 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() {
297   caption_button_container_->UpdateSizeButtonVisibility(false);
298   parent()->Layout();
299 }
300
301 ///////////////////////////////////////////////////////////////////////////////
302 // CustomFrameViewAsh::HeaderView,
303 //   ImmersiveFullscreenController::Delegate overrides:
304
305 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() {
306   fullscreen_visible_fraction_ = 0;
307   SetPaintToLayer(true);
308   SetFillsBoundsOpaquely(false);
309   parent()->Layout();
310 }
311
312 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() {
313   fullscreen_visible_fraction_ = 0;
314   SetPaintToLayer(false);
315   parent()->Layout();
316 }
317
318 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() {
319   fullscreen_visible_fraction_ = 0;
320   SetPaintToLayer(false);
321   parent()->Layout();
322 }
323
324 void CustomFrameViewAsh::HeaderView::SetVisibleFraction(
325     double visible_fraction) {
326   if (fullscreen_visible_fraction_ != visible_fraction) {
327     fullscreen_visible_fraction_ = visible_fraction;
328     parent()->Layout();
329   }
330 }
331
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;
342 }
343
344 ///////////////////////////////////////////////////////////////////////////////
345 // CustomFrameViewAsh::OverlayView
346
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 {
351  public:
352   explicit OverlayView(HeaderView* header_view);
353   virtual ~OverlayView();
354
355   // views::View override:
356   virtual void Layout() OVERRIDE;
357   virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
358
359  private:
360   HeaderView* header_view_;
361
362   DISALLOW_COPY_AND_ASSIGN(OverlayView);
363 };
364
365 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView* header_view)
366     : header_view_(header_view) {
367   AddChildView(header_view);
368 }
369
370 CustomFrameViewAsh::OverlayView::~OverlayView() {
371 }
372
373 ///////////////////////////////////////////////////////////////////////////////
374 // CustomFrameViewAsh::OverlayView, views::View overrides:
375
376 void CustomFrameViewAsh::OverlayView::Layout() {
377   // Layout |header_view_| because layout affects the result of
378   // GetPreferredOnScreenHeight().
379   header_view_->Layout();
380
381   int onscreen_height = header_view_->GetPreferredOnScreenHeight();
382   if (onscreen_height == 0) {
383     header_view_->SetVisible(false);
384   } else {
385     int height = header_view_->GetPreferredHeight();
386     header_view_->SetBounds(0, onscreen_height - height, width(), height);
387     header_view_->SetVisible(true);
388   }
389 }
390
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);
395 }
396
397 ////////////////////////////////////////////////////////////////////////////////
398 // CustomFrameViewAsh, public:
399
400 // static
401 const char CustomFrameViewAsh::kViewClassName[] = "CustomFrameViewAsh";
402
403 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget* frame)
404     : frame_(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_));
411
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)));
419   }
420 }
421
422 CustomFrameViewAsh::~CustomFrameViewAsh() {
423 }
424
425 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView(
426     ImmersiveFullscreenController* immersive_fullscreen_controller) {
427   immersive_fullscreen_controller->Init(header_view_, frame_, header_view_);
428 }
429
430 ////////////////////////////////////////////////////////////////////////////////
431 // CustomFrameViewAsh, views::NonClientFrameView overrides:
432
433 gfx::Rect CustomFrameViewAsh::GetBoundsForClientView() const {
434   gfx::Rect client_bounds = bounds();
435   client_bounds.Inset(0, NonClientTopBorderHeight(), 0, 0);
436   return client_bounds;
437 }
438
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;
444 }
445
446 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point& point) {
447   return FrameBorderHitTestController::NonClientHitTest(this,
448       header_view_->caption_button_container(), point);
449 }
450
451 void CustomFrameViewAsh::GetWindowMask(const gfx::Size& size,
452                                        gfx::Path* window_mask) {
453   // No window masks in Aura.
454 }
455
456 void CustomFrameViewAsh::ResetWindowControls() {
457   header_view_->ResetWindowControls();
458 }
459
460 void CustomFrameViewAsh::UpdateWindowIcon() {
461 }
462
463 void CustomFrameViewAsh::UpdateWindowTitle() {
464   header_view_->SchedulePaintForTitle();
465 }
466
467 ////////////////////////////////////////////////////////////////////////////////
468 // CustomFrameViewAsh, views::View overrides:
469
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(
474       bounds).size();
475 }
476
477 const char* CustomFrameViewAsh::GetClassName() const {
478   return kViewClassName;
479 }
480
481 gfx::Size CustomFrameViewAsh::GetMinimumSize() {
482   gfx::Size min_client_view_size(frame_->client_view()->GetMinimumSize());
483   return gfx::Size(
484       std::max(header_view_->GetMinimumWidth(), min_client_view_size.width()),
485       NonClientTopBorderHeight() + min_client_view_size.height());
486 }
487
488 gfx::Size CustomFrameViewAsh::GetMaximumSize() {
489   gfx::Size max_client_size(frame_->client_view()->GetMaximumSize());
490   int width = 0;
491   int height = 0;
492
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();
497
498   return gfx::Size(width, height);
499 }
500
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));
509   } else {
510     views::NonClientFrameView::SchedulePaintInRect(r);
511   }
512 }
513
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.
517   return false;
518 }
519
520 void CustomFrameViewAsh::VisibilityChanged(views::View* starting_from,
521                                            bool is_visible) {
522   if (is_visible)
523     header_view_->UpdateAvatarIcon();
524 }
525
526 views::View* CustomFrameViewAsh::GetHeaderView() {
527   return header_view_;
528 }
529
530 const views::View* CustomFrameViewAsh::GetAvatarIconViewForTest() const {
531   return header_view_->avatar_icon();
532 }
533
534 ////////////////////////////////////////////////////////////////////////////////
535 // CustomFrameViewAsh, private:
536
537 FrameCaptionButtonContainerView* CustomFrameViewAsh::
538     GetFrameCaptionButtonContainerViewForTest() {
539   return header_view_->caption_button_container();
540 }
541
542 int CustomFrameViewAsh::NonClientTopBorderHeight() const {
543   return frame_->IsFullscreen() ? 0 : header_view_->GetPreferredHeight();
544 }
545
546 }  // namespace ash