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( |
19 | 15 const aura_shell::ShellDelegate::SetWidgetCallback& callback) |
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), | 16 : widget_(NULL), |
61 contents_(NULL), | 17 contents_(NULL), |
62 is_visible_(false), | 18 callback_(callback) { |
63 content_rendered_(false) { | 19 Init(); |
64 } | 20 } |
65 | 21 |
66 AppListWindow::~AppListWindow() { | 22 AppListWindow::~AppListWindow() { |
67 } | 23 } |
68 | 24 |
69 void AppListWindow::DeleteDelegate() { | 25 void AppListWindow::DeleteDelegate() { |
70 delete this; | 26 delete this; |
71 } | 27 } |
72 | 28 |
73 views::View* AppListWindow::GetContentsView() { | 29 views::View* AppListWindow::GetContentsView() { |
74 return contents_; | 30 return contents_; |
75 } | 31 } |
76 | 32 |
77 void AppListWindow::WindowClosing() { | |
78 aura::Desktop::GetInstance()->RemoveObserver(this); | |
79 widget_ = NULL; | |
80 } | |
81 | |
82 views::Widget* AppListWindow::GetWidget() { | 33 views::Widget* AppListWindow::GetWidget() { |
83 return widget_; | 34 return widget_; |
84 } | 35 } |
85 | 36 |
86 const views::Widget* AppListWindow::GetWidget() const { | 37 const views::Widget* AppListWindow::GetWidget() const { |
87 return widget_; | 38 return widget_; |
88 } | 39 } |
89 | 40 |
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) { | 41 void AppListWindow::OnRenderHostCreated(RenderViewHost* host) { |
110 } | 42 } |
111 | 43 |
112 void AppListWindow::OnTabMainFrameLoaded() { | 44 void AppListWindow::OnTabMainFrameLoaded() { |
113 } | 45 } |
114 | 46 |
115 void AppListWindow::OnTabMainFrameFirstRender() { | 47 void AppListWindow::OnTabMainFrameFirstRender() { |
116 content_rendered_ = true; | 48 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 } | 49 } |
124 | 50 |
125 void AppListWindow::Init() { | 51 void AppListWindow::Init() { |
126 DCHECK(!widget_ && !contents_); | 52 DCHECK(!widget_ && !contents_); |
127 | 53 |
128 contents_ = new DOMView(); | 54 contents_ = new DOMView(); |
129 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); | 55 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); |
130 | 56 |
131 TabContents* tab = contents_->dom_contents()->tab_contents(); | 57 TabContents* tab = contents_->dom_contents()->tab_contents(); |
132 tab_watcher_.reset(new TabFirstRenderWatcher(tab, this)); | 58 tab_watcher_.reset(new TabFirstRenderWatcher(tab, this)); |
133 content_rendered_ = false; | |
134 | 59 |
135 contents_->LoadURL(GURL(chrome::kChromeUIAppListURL)); | 60 contents_->LoadURL(GURL(chrome::kChromeUIAppListURL)); |
136 | 61 |
137 // Use a background with transparency to trigger transparent webkit. | 62 // Use a background with transparency to trigger transparent webkit. |
138 SkBitmap background; | 63 SkBitmap background; |
139 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | 64 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
140 background.allocPixels(); | 65 background.allocPixels(); |
141 background.eraseARGB(0x00, 0x00, 0x00, 0x00); | 66 background.eraseARGB(0x00, 0x00, 0x00, 0x00); |
142 | 67 |
143 RenderViewHost* host = tab->render_view_host(); | 68 RenderViewHost* host = tab->render_view_host(); |
144 host->view()->SetBackground(background); | 69 host->view()->SetBackground(background); |
145 | 70 |
146 views::Widget::InitParams widget_params( | 71 views::Widget::InitParams widget_params( |
147 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 72 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
148 widget_params.bounds = GetPreferredBounds(false); | 73 // A non-empty bounds so that we get rendered notification. Make the size |
| 74 // close the final size so that card slider resize handler does no generate |
| 75 // unexpected animation. |
| 76 widget_params.bounds = gfx::Rect(0, 0, 900, 700); |
149 widget_params.delegate = this; | 77 widget_params.delegate = this; |
150 widget_params.keep_on_top = true; | 78 widget_params.keep_on_top = true; |
151 widget_params.transparent = true; | 79 widget_params.transparent = true; |
152 | 80 |
153 widget_ = new views::Widget; | 81 widget_ = new views::Widget; |
154 widget_->Init(widget_params); | 82 widget_->Init(widget_params); |
155 widget_->SetContentsView(contents_); | 83 widget_->SetContentsView(contents_); |
156 widget_->SetOpacity(0); | |
157 | |
158 GetWidgetLayer(widget_)->GetAnimator()->AddObserver(this); | |
159 aura::Desktop::GetInstance()->AddObserver(this); | |
160 } | 84 } |
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 |