Add chrome.usb.getConfiguration and expose extra descriptors.
[chromium-blink-merge.git] / ash / frame / custom_frame_view_ash.cc
blob450015a32a3256d21b24c3845886d9bdcd4755b3
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 <algorithm>
8 #include <vector>
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"
38 namespace {
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
45 // windows.
46 class CustomFrameViewAshWindowStateDelegate
47 : public ash::wm::WindowStateDelegate,
48 public ash::wm::WindowStateObserver,
49 public aura::WindowObserver {
50 public:
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() {
70 if (window_state_) {
71 window_state_->RemoveObserver(this);
72 window_state_->window()->RemoveObserver(this);
75 private:
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);
82 } else {
83 window_state->Restore();
85 if (immersive_fullscreen_controller_) {
86 immersive_fullscreen_controller_->SetEnabled(
87 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER,
88 enter_fullscreen);
90 return true;
92 // Overridden from aura::WindowObserver:
93 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
94 window_state_->RemoveObserver(this);
95 window_state_->window()->RemoveObserver(this);
96 window_state_ = NULL;
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,
108 false);
112 ash::wm::WindowState* window_state_;
113 scoped_ptr<ash::ImmersiveFullscreenController>
114 immersive_fullscreen_controller_;
116 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshWindowStateDelegate);
119 } // namespace
121 namespace ash {
123 ///////////////////////////////////////////////////////////////////////////////
124 // CustomFrameViewAsh::HeaderView
126 // View which paints the header. It slides off and on screen in immersive
127 // fullscreen.
128 class CustomFrameViewAsh::HeaderView
129 : public views::View,
130 public ImmersiveFullscreenController::Delegate,
131 public ShellObserver {
132 public:
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();
154 void SizeConstraintsChanged();
156 // views::View:
157 virtual void Layout() OVERRIDE;
158 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
159 virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE;
161 // ShellObserver:
162 virtual void OnMaximizeModeStarted() OVERRIDE;
163 virtual void OnMaximizeModeEnded() OVERRIDE;
165 FrameCaptionButtonContainerView* caption_button_container() {
166 return caption_button_container_;
169 views::View* avatar_icon() const {
170 return avatar_icon_;
173 private:
174 // ImmersiveFullscreenController::Delegate:
175 virtual void OnImmersiveRevealStarted() OVERRIDE;
176 virtual void OnImmersiveRevealEnded() OVERRIDE;
177 virtual void OnImmersiveFullscreenExited() OVERRIDE;
178 virtual void SetVisibleFraction(double visible_fraction) OVERRIDE;
179 virtual std::vector<gfx::Rect> GetVisibleBoundsInScreen() const OVERRIDE;
181 // The widget that the caption buttons act on.
182 views::Widget* frame_;
184 // Helper for painting the header.
185 scoped_ptr<DefaultHeaderPainter> header_painter_;
187 views::ImageView* avatar_icon_;
189 // View which contains the window caption buttons.
190 FrameCaptionButtonContainerView* caption_button_container_;
192 // The fraction of the header's height which is visible while in fullscreen.
193 // This value is meaningless when not in fullscreen.
194 double fullscreen_visible_fraction_;
196 DISALLOW_COPY_AND_ASSIGN(HeaderView);
199 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget* frame)
200 : frame_(frame),
201 header_painter_(new ash::DefaultHeaderPainter),
202 avatar_icon_(NULL),
203 caption_button_container_(NULL),
204 fullscreen_visible_fraction_(0) {
205 // Unfortunately, there is no views::WidgetDelegate::CanMinimize(). Assume
206 // that the window frame can be minimized if it can be maximized.
207 FrameCaptionButtonContainerView::MinimizeAllowed minimize_allowed =
208 frame_->widget_delegate()->CanMaximize() ?
209 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED :
210 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED;
211 caption_button_container_ = new FrameCaptionButtonContainerView(frame_,
212 minimize_allowed);
213 caption_button_container_->UpdateSizeButtonVisibility();
214 AddChildView(caption_button_container_);
216 header_painter_->Init(frame_, this, caption_button_container_);
217 UpdateAvatarIcon();
219 Shell::GetInstance()->AddShellObserver(this);
222 CustomFrameViewAsh::HeaderView::~HeaderView() {
223 Shell::GetInstance()->RemoveShellObserver(this);
226 void CustomFrameViewAsh::HeaderView::SchedulePaintForTitle() {
227 header_painter_->SchedulePaintForTitle();
230 void CustomFrameViewAsh::HeaderView::ResetWindowControls() {
231 caption_button_container_->ResetWindowControls();
234 int CustomFrameViewAsh::HeaderView::GetPreferredOnScreenHeight() const {
235 if (frame_->IsFullscreen()) {
236 return static_cast<int>(
237 GetPreferredHeight() * fullscreen_visible_fraction_);
239 return GetPreferredHeight();
242 int CustomFrameViewAsh::HeaderView::GetPreferredHeight() const {
243 return header_painter_->GetHeaderHeightForPainting();
246 int CustomFrameViewAsh::HeaderView::GetMinimumWidth() const {
247 return header_painter_->GetMinimumHeaderWidth();
250 void CustomFrameViewAsh::HeaderView::UpdateAvatarIcon() {
251 SessionStateDelegate* delegate =
252 Shell::GetInstance()->session_state_delegate();
253 aura::Window* window = frame_->GetNativeView();
254 bool show = delegate->ShouldShowAvatar(window);
255 if (!show) {
256 if (!avatar_icon_)
257 return;
258 delete avatar_icon_;
259 avatar_icon_ = NULL;
260 } else {
261 gfx::ImageSkia image = GetAvatarImageForContext(
262 delegate->GetBrowserContextForWindow(window)).AsImageSkia();
263 DCHECK(!image.isNull());
264 DCHECK_EQ(image.width(), image.height());
265 if (!avatar_icon_) {
266 avatar_icon_ = new views::ImageView();
267 AddChildView(avatar_icon_);
269 avatar_icon_->SetImage(image);
271 header_painter_->UpdateLeftHeaderView(avatar_icon_);
272 Layout();
275 void CustomFrameViewAsh::HeaderView::SizeConstraintsChanged() {
276 caption_button_container_->ResetWindowControls();
277 caption_button_container_->UpdateSizeButtonVisibility();
278 Layout();
281 ///////////////////////////////////////////////////////////////////////////////
282 // CustomFrameViewAsh::HeaderView, views::View overrides:
284 void CustomFrameViewAsh::HeaderView::Layout() {
285 header_painter_->LayoutHeader();
288 void CustomFrameViewAsh::HeaderView::OnPaint(gfx::Canvas* canvas) {
289 bool paint_as_active =
290 frame_->non_client_view()->frame_view()->ShouldPaintAsActive();
291 caption_button_container_->SetPaintAsActive(paint_as_active);
293 HeaderPainter::Mode header_mode = paint_as_active ?
294 HeaderPainter::MODE_ACTIVE : HeaderPainter::MODE_INACTIVE;
295 header_painter_->PaintHeader(canvas, header_mode);
298 void CustomFrameViewAsh::HeaderView::
299 ChildPreferredSizeChanged(views::View* child) {
300 // FrameCaptionButtonContainerView animates the visibility changes in
301 // UpdateSizeButtonVisibility(false). Due to this a new size is not available
302 // until the completion of the animation. Layout in response to the preferred
303 // size changes.
304 if (child != caption_button_container_)
305 return;
306 parent()->Layout();
309 ///////////////////////////////////////////////////////////////////////////////
310 // CustomFrameViewAsh::HeaderView, ShellObserver overrides:
312 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() {
313 caption_button_container_->UpdateSizeButtonVisibility();
314 parent()->Layout();
317 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() {
318 caption_button_container_->UpdateSizeButtonVisibility();
319 parent()->Layout();
322 ///////////////////////////////////////////////////////////////////////////////
323 // CustomFrameViewAsh::HeaderView,
324 // ImmersiveFullscreenController::Delegate overrides:
326 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() {
327 fullscreen_visible_fraction_ = 0;
328 SetPaintToLayer(true);
329 SetFillsBoundsOpaquely(false);
330 parent()->Layout();
333 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() {
334 fullscreen_visible_fraction_ = 0;
335 SetPaintToLayer(false);
336 parent()->Layout();
339 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() {
340 fullscreen_visible_fraction_ = 0;
341 SetPaintToLayer(false);
342 parent()->Layout();
345 void CustomFrameViewAsh::HeaderView::SetVisibleFraction(
346 double visible_fraction) {
347 if (fullscreen_visible_fraction_ != visible_fraction) {
348 fullscreen_visible_fraction_ = visible_fraction;
349 parent()->Layout();
353 std::vector<gfx::Rect>
354 CustomFrameViewAsh::HeaderView::GetVisibleBoundsInScreen() const {
355 // TODO(pkotwicz): Implement views::View::ConvertRectToScreen().
356 gfx::Rect visible_bounds(GetVisibleBounds());
357 gfx::Point visible_origin_in_screen(visible_bounds.origin());
358 views::View::ConvertPointToScreen(this, &visible_origin_in_screen);
359 std::vector<gfx::Rect> bounds_in_screen;
360 bounds_in_screen.push_back(
361 gfx::Rect(visible_origin_in_screen, visible_bounds.size()));
362 return bounds_in_screen;
365 ///////////////////////////////////////////////////////////////////////////////
366 // CustomFrameViewAsh::OverlayView
368 // View which takes up the entire widget and contains the HeaderView. HeaderView
369 // is a child of OverlayView to avoid creating a larger texture than necessary
370 // when painting the HeaderView to its own layer.
371 class CustomFrameViewAsh::OverlayView : public views::View,
372 public views::ViewTargeterDelegate {
373 public:
374 explicit OverlayView(HeaderView* header_view);
375 virtual ~OverlayView();
377 // views::View:
378 virtual void Layout() OVERRIDE;
380 private:
381 // views::ViewTargeterDelegate:
382 virtual bool DoesIntersectRect(const views::View* target,
383 const gfx::Rect& rect) const OVERRIDE;
385 HeaderView* header_view_;
387 DISALLOW_COPY_AND_ASSIGN(OverlayView);
390 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView* header_view)
391 : header_view_(header_view) {
392 AddChildView(header_view);
393 SetEventTargeter(
394 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
397 CustomFrameViewAsh::OverlayView::~OverlayView() {
400 ///////////////////////////////////////////////////////////////////////////////
401 // CustomFrameViewAsh::OverlayView, views::View overrides:
403 void CustomFrameViewAsh::OverlayView::Layout() {
404 // Layout |header_view_| because layout affects the result of
405 // GetPreferredOnScreenHeight().
406 header_view_->Layout();
408 int onscreen_height = header_view_->GetPreferredOnScreenHeight();
409 if (onscreen_height == 0) {
410 header_view_->SetVisible(false);
411 } else {
412 int height = header_view_->GetPreferredHeight();
413 header_view_->SetBounds(0, onscreen_height - height, width(), height);
414 header_view_->SetVisible(true);
418 ///////////////////////////////////////////////////////////////////////////////
419 // CustomFrameViewAsh::OverlayView, views::ViewTargeterDelegate overrides:
421 bool CustomFrameViewAsh::OverlayView::DoesIntersectRect(
422 const views::View* target,
423 const gfx::Rect& rect) const {
424 CHECK_EQ(target, this);
425 // Grab events in the header view. Return false for other events so that they
426 // can be handled by the client view.
427 return header_view_->HitTestRect(rect);
430 ////////////////////////////////////////////////////////////////////////////////
431 // CustomFrameViewAsh, public:
433 // static
434 const char CustomFrameViewAsh::kViewClassName[] = "CustomFrameViewAsh";
436 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget* frame)
437 : frame_(frame),
438 header_view_(new HeaderView(frame)),
439 frame_border_hit_test_controller_(
440 new FrameBorderHitTestController(frame_)) {
441 // |header_view_| is set as the non client view's overlay view so that it can
442 // overlay the web contents in immersive fullscreen.
443 frame->non_client_view()->SetOverlayView(new OverlayView(header_view_));
445 // A delegate for a more complex way of fullscreening the window may already
446 // be set. This is the case for packaged apps.
447 wm::WindowState* window_state = wm::GetWindowState(frame->GetNativeWindow());
448 if (!window_state->HasDelegate()) {
449 window_state->SetDelegate(scoped_ptr<wm::WindowStateDelegate>(
450 new CustomFrameViewAshWindowStateDelegate(
451 window_state, this)));
455 CustomFrameViewAsh::~CustomFrameViewAsh() {
458 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView(
459 ImmersiveFullscreenController* immersive_fullscreen_controller) {
460 immersive_fullscreen_controller->Init(header_view_, frame_, header_view_);
463 ////////////////////////////////////////////////////////////////////////////////
464 // CustomFrameViewAsh, views::NonClientFrameView overrides:
466 gfx::Rect CustomFrameViewAsh::GetBoundsForClientView() const {
467 gfx::Rect client_bounds = bounds();
468 client_bounds.Inset(0, NonClientTopBorderHeight(), 0, 0);
469 return client_bounds;
472 gfx::Rect CustomFrameViewAsh::GetWindowBoundsForClientBounds(
473 const gfx::Rect& client_bounds) const {
474 gfx::Rect window_bounds = client_bounds;
475 window_bounds.Inset(0, -NonClientTopBorderHeight(), 0, 0);
476 return window_bounds;
479 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point& point) {
480 return FrameBorderHitTestController::NonClientHitTest(this,
481 header_view_->caption_button_container(), point);
484 void CustomFrameViewAsh::GetWindowMask(const gfx::Size& size,
485 gfx::Path* window_mask) {
486 // No window masks in Aura.
489 void CustomFrameViewAsh::ResetWindowControls() {
490 header_view_->ResetWindowControls();
493 void CustomFrameViewAsh::UpdateWindowIcon() {
496 void CustomFrameViewAsh::UpdateWindowTitle() {
497 header_view_->SchedulePaintForTitle();
500 void CustomFrameViewAsh::SizeConstraintsChanged() {
501 header_view_->SizeConstraintsChanged();
504 ////////////////////////////////////////////////////////////////////////////////
505 // CustomFrameViewAsh, views::View overrides:
507 gfx::Size CustomFrameViewAsh::GetPreferredSize() const {
508 gfx::Size pref = frame_->client_view()->GetPreferredSize();
509 gfx::Rect bounds(0, 0, pref.width(), pref.height());
510 return frame_->non_client_view()->GetWindowBoundsForClientBounds(
511 bounds).size();
514 const char* CustomFrameViewAsh::GetClassName() const {
515 return kViewClassName;
518 gfx::Size CustomFrameViewAsh::GetMinimumSize() const {
519 gfx::Size min_client_view_size(frame_->client_view()->GetMinimumSize());
520 return gfx::Size(
521 std::max(header_view_->GetMinimumWidth(), min_client_view_size.width()),
522 NonClientTopBorderHeight() + min_client_view_size.height());
525 gfx::Size CustomFrameViewAsh::GetMaximumSize() const {
526 gfx::Size max_client_size(frame_->client_view()->GetMaximumSize());
527 int width = 0;
528 int height = 0;
530 if (max_client_size.width() > 0)
531 width = std::max(header_view_->GetMinimumWidth(), max_client_size.width());
532 if (max_client_size.height() > 0)
533 height = NonClientTopBorderHeight() + max_client_size.height();
535 return gfx::Size(width, height);
538 void CustomFrameViewAsh::SchedulePaintInRect(const gfx::Rect& r) {
539 // We may end up here before |header_view_| has been added to the Widget.
540 if (header_view_->GetWidget()) {
541 // The HeaderView is not a child of CustomFrameViewAsh. Redirect the paint
542 // to HeaderView instead.
543 gfx::RectF to_paint(r);
544 views::View::ConvertRectToTarget(this, header_view_, &to_paint);
545 header_view_->SchedulePaintInRect(gfx::ToEnclosingRect(to_paint));
546 } else {
547 views::NonClientFrameView::SchedulePaintInRect(r);
551 void CustomFrameViewAsh::VisibilityChanged(views::View* starting_from,
552 bool is_visible) {
553 if (is_visible)
554 header_view_->UpdateAvatarIcon();
557 ////////////////////////////////////////////////////////////////////////////////
558 // CustomFrameViewAsh, views::ViewTargeterDelegate overrides:
560 views::View* CustomFrameViewAsh::GetHeaderView() {
561 return header_view_;
564 const views::View* CustomFrameViewAsh::GetAvatarIconViewForTest() const {
565 return header_view_->avatar_icon();
568 ////////////////////////////////////////////////////////////////////////////////
569 // CustomFrameViewAsh, private:
571 // views::NonClientFrameView:
572 bool CustomFrameViewAsh::DoesIntersectRect(const views::View* target,
573 const gfx::Rect& rect) const {
574 CHECK_EQ(target, this);
575 // NonClientView hit tests the NonClientFrameView first instead of going in
576 // z-order. Return false so that events get to the OverlayView.
577 return false;
580 FrameCaptionButtonContainerView* CustomFrameViewAsh::
581 GetFrameCaptionButtonContainerViewForTest() {
582 return header_view_->caption_button_container();
585 int CustomFrameViewAsh::NonClientTopBorderHeight() const {
586 return frame_->IsFullscreen() ? 0 : header_view_->GetPreferredHeight();
589 } // namespace ash