OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/ui/views/aura/app_list_window.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/profiles/profile_manager.h" |
| 9 #include "chrome/browser/ui/views/dom_view.h" |
| 10 #include "content/browser/renderer_host/render_view_host.h" |
| 11 #include "content/browser/renderer_host/render_widget_host_view.h" |
| 12 #include "views/widget/widget.h" |
| 13 #include "ui/aura/desktop.h" |
| 14 #include "ui/gfx/compositor/layer_animator.h" |
| 15 #include "ui/gfx/screen.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Gets preferred bounds of app list window in show/hide state. |
| 20 gfx::Rect GetPreferredBounds(bool show) { |
| 21 // The y-axis offset used at the beginning of showing animation. |
| 22 static const int kMoveUpAnimationOffset = 50; |
| 23 |
| 24 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint(); |
| 25 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestPoint(cursor); |
| 26 gfx::Rect widget_bounds(work_area); |
| 27 widget_bounds.Inset(150, 100); |
| 28 if (!show) |
| 29 widget_bounds.Offset(0, kMoveUpAnimationOffset); |
| 30 |
| 31 return widget_bounds; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 // static |
| 37 AppListWindow* AppListWindow::instance_ = NULL; |
| 38 |
| 39 // static |
| 40 void AppListWindow::SetVisible(bool visible) { |
| 41 if (!instance_) { |
| 42 // TODO(xiyuan): Fix first time animation jankiness. |
| 43 instance_ = new AppListWindow; |
| 44 instance_->Init(); |
| 45 } |
| 46 |
| 47 instance_->SetVisible(visible, true); |
| 48 } |
| 49 |
| 50 bool AppListWindow::IsVisible() { |
| 51 return instance_ && instance_->is_visible(); |
| 52 } |
| 53 |
| 54 AppListWindow::AppListWindow() |
| 55 : widget_(NULL), |
| 56 contents_(NULL), |
| 57 is_visible_(false) { |
| 58 } |
| 59 |
| 60 AppListWindow::~AppListWindow() { |
| 61 } |
| 62 |
| 63 void AppListWindow::DeleteDelegate() { |
| 64 delete this; |
| 65 } |
| 66 |
| 67 views::View* AppListWindow::GetContentsView() { |
| 68 return contents_; |
| 69 } |
| 70 |
| 71 void AppListWindow::WindowClosing() { |
| 72 aura::Desktop::GetInstance()->RemoveObserver(this); |
| 73 widget_ = NULL; |
| 74 } |
| 75 |
| 76 views::Widget* AppListWindow::GetWidget() { |
| 77 return widget_; |
| 78 } |
| 79 |
| 80 const views::Widget* AppListWindow::GetWidget() const { |
| 81 return widget_; |
| 82 } |
| 83 |
| 84 void AppListWindow::OnActiveWindowChanged(aura::Window* active) { |
| 85 if (widget_ && !widget_->IsActive() && is_visible_) |
| 86 SetVisible(false, true); |
| 87 } |
| 88 |
| 89 void AppListWindow::Init() { |
| 90 DCHECK(!widget_ && !contents_); |
| 91 |
| 92 contents_ = new DOMView(); |
| 93 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); |
| 94 contents_->LoadURL(GURL("chrome://newtab#applist")); |
| 95 |
| 96 // Use a background with transparency to trigger transparent webkit. |
| 97 SkBitmap background; |
| 98 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 99 background.allocPixels(); |
| 100 background.eraseARGB(0x00, 0x00, 0x00, 0x00); |
| 101 |
| 102 TabContents* tab = contents_->dom_contents()->tab_contents(); |
| 103 RenderViewHost* host = tab->render_view_host(); |
| 104 host->view()->SetBackground(background); |
| 105 |
| 106 views::Widget::InitParams widget_params( |
| 107 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 108 widget_params.bounds = GetPreferredBounds(false); |
| 109 widget_params.delegate = this; |
| 110 |
| 111 widget_ = new views::Widget; |
| 112 widget_->Init(widget_params); |
| 113 widget_->SetContentsView(contents_); |
| 114 widget_->SetOpacity(0); |
| 115 |
| 116 aura::Desktop::GetInstance()->AddObserver(this); |
| 117 } |
| 118 |
| 119 void AppListWindow::SetVisible(bool visible, bool animate) { |
| 120 if (visible == is_visible_) |
| 121 return; |
| 122 |
| 123 is_visible_ = visible; |
| 124 |
| 125 if (animate) { |
| 126 gfx::Point dummy; |
| 127 ui::Layer* layer; |
| 128 widget_->CalculateOffsetToAncestorWithLayer(&dummy, &layer); |
| 129 |
| 130 ui::LayerAnimator::ScopedSettings settings(layer->GetAnimator()); |
| 131 layer->SetBounds(GetPreferredBounds(visible)); |
| 132 layer->SetOpacity(visible ? 1.0 : 0.0); |
| 133 } |
| 134 |
| 135 if (visible) { |
| 136 widget_->Activate(); |
| 137 widget_->Show(); |
| 138 } else { |
| 139 instance_ = NULL; // Closing and don't reuse this instance_. |
| 140 |
| 141 if (animate) { |
| 142 // TODO(xiyuan): Properly close widget after animation finishes. |
| 143 MessageLoop::current()->PostDelayedTask( |
| 144 FROM_HERE, |
| 145 base::Bind(&views::Widget::Close, base::Unretained(widget_)), |
| 146 1000); |
| 147 } else { |
| 148 widget_->Close(); |
| 149 } |
| 150 } |
| 151 } |
OLD | NEW |