| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/aura/app_list_window.h" | 5 #include "chrome/browser/ui/views/aura/app_list_window.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/profiles/profile_manager.h" | 7 #include "chrome/browser/profiles/profile_manager.h" |
| 9 #include "chrome/browser/ui/views/dom_view.h" | 8 #include "chrome/browser/ui/views/dom_view.h" |
| 10 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 11 #include "content/browser/renderer_host/render_view_host.h" | 10 #include "content/browser/renderer_host/render_view_host.h" |
| 12 #include "content/browser/renderer_host/render_widget_host_view.h" | 11 #include "content/browser/renderer_host/render_widget_host_view.h" |
| 13 #include "views/widget/widget.h" | 12 #include "views/widget/widget.h" |
| 14 #include "ui/aura/desktop.h" | |
| 15 #include "ui/gfx/compositor/layer_animator.h" | |
| 16 #include "ui/gfx/screen.h" | |
| 17 | 13 |
| 18 namespace { | 14 AppListWindow::AppListWindow(const aura_shell::SetWidgetCallback& callback) |
| 19 | |
| 20 // Gets preferred bounds of app list window in show/hide state. | |
| 21 gfx::Rect GetPreferredBounds(bool show) { | |
| 22 // The y-axis offset used at the beginning of showing animation. | |
| 23 static const int kMoveUpAnimationOffset = 50; | |
| 24 | |
| 25 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint(); | |
| 26 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestPoint(cursor); | |
| 27 gfx::Rect widget_bounds(work_area); | |
| 28 widget_bounds.Inset(150, 100); | |
| 29 if (!show) | |
| 30 widget_bounds.Offset(0, kMoveUpAnimationOffset); | |
| 31 | |
| 32 return widget_bounds; | |
| 33 } | |
| 34 | |
| 35 ui::Layer* GetWidgetLayer(views::Widget* widget) { | |
| 36 return widget->GetNativeView()->layer(); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 // static | |
| 42 AppListWindow* AppListWindow::instance_ = NULL; | |
| 43 | |
| 44 // static | |
| 45 void AppListWindow::SetVisible(bool visible) { | |
| 46 if (!instance_) { | |
| 47 instance_ = new AppListWindow; | |
| 48 instance_->Init(); | |
| 49 } | |
| 50 | |
| 51 instance_->DoSetVisible(visible); | |
| 52 } | |
| 53 | |
| 54 // static | |
| 55 bool AppListWindow::IsVisible() { | |
| 56 return instance_ && instance_->is_visible_; | |
| 57 } | |
| 58 | |
| 59 AppListWindow::AppListWindow() | |
| 60 : widget_(NULL), | 15 : widget_(NULL), |
| 61 contents_(NULL), | 16 contents_(NULL), |
| 62 is_visible_(false), | 17 callback_(callback) { |
| 63 content_rendered_(false) { | 18 Init(); |
| 64 } | 19 } |
| 65 | 20 |
| 66 AppListWindow::~AppListWindow() { | 21 AppListWindow::~AppListWindow() { |
| 67 } | 22 } |
| 68 | 23 |
| 69 void AppListWindow::DeleteDelegate() { | 24 void AppListWindow::DeleteDelegate() { |
| 70 delete this; | 25 delete this; |
| 71 } | 26 } |
| 72 | 27 |
| 73 views::View* AppListWindow::GetContentsView() { | 28 views::View* AppListWindow::GetContentsView() { |
| 74 return contents_; | 29 return contents_; |
| 75 } | 30 } |
| 76 | 31 |
| 77 void AppListWindow::WindowClosing() { | |
| 78 aura::Desktop::GetInstance()->RemoveObserver(this); | |
| 79 widget_ = NULL; | |
| 80 } | |
| 81 | |
| 82 views::Widget* AppListWindow::GetWidget() { | 32 views::Widget* AppListWindow::GetWidget() { |
| 83 return widget_; | 33 return widget_; |
| 84 } | 34 } |
| 85 | 35 |
| 86 const views::Widget* AppListWindow::GetWidget() const { | 36 const views::Widget* AppListWindow::GetWidget() const { |
| 87 return widget_; | 37 return widget_; |
| 88 } | 38 } |
| 89 | 39 |
| 90 void AppListWindow::OnActiveWindowChanged(aura::Window* active) { | |
| 91 if (widget_ && !widget_->IsActive() && is_visible_) | |
| 92 DoSetVisible(false); | |
| 93 } | |
| 94 | |
| 95 void AppListWindow::OnLayerAnimationEnded( | |
| 96 const ui::LayerAnimationSequence* sequence) { | |
| 97 if (!is_visible_ ) | |
| 98 widget_->Close(); | |
| 99 } | |
| 100 | |
| 101 void AppListWindow::OnLayerAnimationAborted( | |
| 102 const ui::LayerAnimationSequence* sequence) { | |
| 103 } | |
| 104 | |
| 105 void AppListWindow::OnLayerAnimationScheduled( | |
| 106 const ui::LayerAnimationSequence* sequence) { | |
| 107 } | |
| 108 | |
| 109 void AppListWindow::OnRenderHostCreated(RenderViewHost* host) { | 40 void AppListWindow::OnRenderHostCreated(RenderViewHost* host) { |
| 110 } | 41 } |
| 111 | 42 |
| 112 void AppListWindow::OnTabMainFrameLoaded() { | 43 void AppListWindow::OnTabMainFrameLoaded() { |
| 113 } | 44 } |
| 114 | 45 |
| 115 void AppListWindow::OnTabMainFrameFirstRender() { | 46 void AppListWindow::OnTabMainFrameFirstRender() { |
| 116 content_rendered_ = true; | 47 callback_.Run(widget_); |
| 117 | |
| 118 // Do deferred show animation if necessary. | |
| 119 if (is_visible_ && GetWidgetLayer(widget_)->opacity() == 0) { | |
| 120 is_visible_ = false; | |
| 121 DoSetVisible(true); | |
| 122 } | |
| 123 } | 48 } |
| 124 | 49 |
| 125 void AppListWindow::Init() { | 50 void AppListWindow::Init() { |
| 126 DCHECK(!widget_ && !contents_); | 51 DCHECK(!widget_ && !contents_); |
| 127 | 52 |
| 128 contents_ = new DOMView(); | 53 contents_ = new DOMView(); |
| 129 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); | 54 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); |
| 130 | 55 |
| 131 TabContents* tab = contents_->dom_contents()->tab_contents(); | 56 TabContents* tab = contents_->dom_contents()->tab_contents(); |
| 132 tab_watcher_.reset(new TabFirstRenderWatcher(tab, this)); | 57 tab_watcher_.reset(new TabFirstRenderWatcher(tab, this)); |
| 133 content_rendered_ = false; | |
| 134 | 58 |
| 135 contents_->LoadURL(GURL(chrome::kChromeUIAppListURL)); | 59 contents_->LoadURL(GURL(chrome::kChromeUIAppListURL)); |
| 136 | 60 |
| 137 // Use a background with transparency to trigger transparent webkit. | 61 // Use a background with transparency to trigger transparent webkit. |
| 138 SkBitmap background; | 62 SkBitmap background; |
| 139 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | 63 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 140 background.allocPixels(); | 64 background.allocPixels(); |
| 141 background.eraseARGB(0x00, 0x00, 0x00, 0x00); | 65 background.eraseARGB(0x00, 0x00, 0x00, 0x00); |
| 142 | 66 |
| 143 RenderViewHost* host = tab->render_view_host(); | 67 RenderViewHost* host = tab->render_view_host(); |
| 144 host->view()->SetBackground(background); | 68 host->view()->SetBackground(background); |
| 145 | 69 |
| 146 views::Widget::InitParams widget_params( | 70 views::Widget::InitParams widget_params( |
| 147 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 71 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 148 widget_params.bounds = GetPreferredBounds(false); | 72 widget_params.bounds = gfx::Rect(0, 0, 500, 400); // a non-empty bounds. |
| 149 widget_params.delegate = this; | 73 widget_params.delegate = this; |
| 150 widget_params.keep_on_top = true; | 74 widget_params.keep_on_top = true; |
| 151 widget_params.transparent = true; | 75 widget_params.transparent = true; |
| 152 | 76 |
| 153 widget_ = new views::Widget; | 77 widget_ = new views::Widget; |
| 154 widget_->Init(widget_params); | 78 widget_->Init(widget_params); |
| 155 widget_->SetContentsView(contents_); | 79 widget_->SetContentsView(contents_); |
| 156 widget_->SetOpacity(0); | |
| 157 | |
| 158 GetWidgetLayer(widget_)->GetAnimator()->AddObserver(this); | |
| 159 aura::Desktop::GetInstance()->AddObserver(this); | |
| 160 } | 80 } |
| 161 | |
| 162 void AppListWindow::DoSetVisible(bool visible) { | |
| 163 if (visible == is_visible_) | |
| 164 return; | |
| 165 | |
| 166 is_visible_ = visible; | |
| 167 | |
| 168 // Skip show animation if contents is not rendered. | |
| 169 // TODO(xiyuan): Should we show a loading UI if it takes too long? | |
| 170 if (visible && !content_rendered_) | |
| 171 return; | |
| 172 | |
| 173 ui::Layer* layer = GetWidgetLayer(widget_); | |
| 174 ui::LayerAnimator::ScopedSettings settings(layer->GetAnimator()); | |
| 175 layer->SetBounds(GetPreferredBounds(visible)); | |
| 176 layer->SetOpacity(visible ? 1.0 : 0.0); | |
| 177 | |
| 178 if (visible) { | |
| 179 widget_->Show(); | |
| 180 widget_->Activate(); | |
| 181 } else { | |
| 182 instance_ = NULL; // Closing and don't reuse this instance_. | |
| 183 } | |
| 184 } | |
| OLD | NEW |