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" | 7 #include "base/bind.h" |
8 #include "chrome/browser/profiles/profile_manager.h" | 8 #include "chrome/browser/profiles/profile_manager.h" |
9 #include "chrome/browser/ui/views/dom_view.h" | 9 #include "chrome/browser/ui/views/dom_view.h" |
10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint(); | 25 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint(); |
26 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestPoint(cursor); | 26 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestPoint(cursor); |
27 gfx::Rect widget_bounds(work_area); | 27 gfx::Rect widget_bounds(work_area); |
28 widget_bounds.Inset(150, 100); | 28 widget_bounds.Inset(150, 100); |
29 if (!show) | 29 if (!show) |
30 widget_bounds.Offset(0, kMoveUpAnimationOffset); | 30 widget_bounds.Offset(0, kMoveUpAnimationOffset); |
31 | 31 |
32 return widget_bounds; | 32 return widget_bounds; |
33 } | 33 } |
34 | 34 |
| 35 ui::Layer* GetWidgetLayer(views::Widget* widget) { |
| 36 return widget->GetNativeView()->layer(); |
| 37 } |
| 38 |
35 } // namespace | 39 } // namespace |
36 | 40 |
37 // static | 41 // static |
38 AppListWindow* AppListWindow::instance_ = NULL; | 42 AppListWindow* AppListWindow::instance_ = NULL; |
39 | 43 |
40 // static | 44 // static |
41 void AppListWindow::SetVisible(bool visible) { | 45 void AppListWindow::SetVisible(bool visible) { |
42 if (!instance_) { | 46 if (!instance_) { |
43 // TODO(xiyuan): Fix first time animation jankiness. | |
44 instance_ = new AppListWindow; | 47 instance_ = new AppListWindow; |
45 instance_->Init(); | 48 instance_->Init(); |
46 } | 49 } |
47 | 50 |
48 instance_->SetVisible(visible, true); | 51 instance_->DoSetVisible(visible); |
49 } | 52 } |
50 | 53 |
| 54 // static |
51 bool AppListWindow::IsVisible() { | 55 bool AppListWindow::IsVisible() { |
52 return instance_ && instance_->is_visible(); | 56 return instance_ && instance_->is_visible_; |
53 } | 57 } |
54 | 58 |
55 AppListWindow::AppListWindow() | 59 AppListWindow::AppListWindow() |
56 : widget_(NULL), | 60 : widget_(NULL), |
57 contents_(NULL), | 61 contents_(NULL), |
58 is_visible_(false) { | 62 is_visible_(false), |
| 63 content_rendered_(false) { |
59 } | 64 } |
60 | 65 |
61 AppListWindow::~AppListWindow() { | 66 AppListWindow::~AppListWindow() { |
62 } | 67 } |
63 | 68 |
64 void AppListWindow::DeleteDelegate() { | 69 void AppListWindow::DeleteDelegate() { |
65 delete this; | 70 delete this; |
66 } | 71 } |
67 | 72 |
68 views::View* AppListWindow::GetContentsView() { | 73 views::View* AppListWindow::GetContentsView() { |
69 return contents_; | 74 return contents_; |
70 } | 75 } |
71 | 76 |
72 void AppListWindow::WindowClosing() { | 77 void AppListWindow::WindowClosing() { |
73 aura::Desktop::GetInstance()->RemoveObserver(this); | 78 aura::Desktop::GetInstance()->RemoveObserver(this); |
74 widget_ = NULL; | 79 widget_ = NULL; |
75 } | 80 } |
76 | 81 |
77 views::Widget* AppListWindow::GetWidget() { | 82 views::Widget* AppListWindow::GetWidget() { |
78 return widget_; | 83 return widget_; |
79 } | 84 } |
80 | 85 |
81 const views::Widget* AppListWindow::GetWidget() const { | 86 const views::Widget* AppListWindow::GetWidget() const { |
82 return widget_; | 87 return widget_; |
83 } | 88 } |
84 | 89 |
85 void AppListWindow::OnActiveWindowChanged(aura::Window* active) { | 90 void AppListWindow::OnActiveWindowChanged(aura::Window* active) { |
86 if (widget_ && !widget_->IsActive() && is_visible_) | 91 if (widget_ && !widget_->IsActive() && is_visible_) |
87 SetVisible(false, true); | 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) { |
| 110 } |
| 111 |
| 112 void AppListWindow::OnTabMainFrameLoaded() { |
| 113 } |
| 114 |
| 115 void AppListWindow::OnTabMainFrameFirstRender() { |
| 116 content_rendered_ = true; |
| 117 |
| 118 // Do deferred show animation if necessary. |
| 119 if (is_visible_ && GetWidgetLayer(widget_)->opacity() == 0) { |
| 120 is_visible_ = false; |
| 121 DoSetVisible(true); |
| 122 } |
88 } | 123 } |
89 | 124 |
90 void AppListWindow::Init() { | 125 void AppListWindow::Init() { |
91 DCHECK(!widget_ && !contents_); | 126 DCHECK(!widget_ && !contents_); |
92 | 127 |
93 contents_ = new DOMView(); | 128 contents_ = new DOMView(); |
94 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); | 129 contents_->Init(ProfileManager::GetDefaultProfile(), NULL); |
| 130 |
| 131 TabContents* tab = contents_->dom_contents()->tab_contents(); |
| 132 tab_watcher_.reset(new TabFirstRenderWatcher(tab, this)); |
| 133 content_rendered_ = false; |
| 134 |
95 contents_->LoadURL(GURL(chrome::kChromeUIAppListURL)); | 135 contents_->LoadURL(GURL(chrome::kChromeUIAppListURL)); |
96 | 136 |
97 // Use a background with transparency to trigger transparent webkit. | 137 // Use a background with transparency to trigger transparent webkit. |
98 SkBitmap background; | 138 SkBitmap background; |
99 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | 139 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
100 background.allocPixels(); | 140 background.allocPixels(); |
101 background.eraseARGB(0x00, 0x00, 0x00, 0x00); | 141 background.eraseARGB(0x00, 0x00, 0x00, 0x00); |
102 | 142 |
103 TabContents* tab = contents_->dom_contents()->tab_contents(); | |
104 RenderViewHost* host = tab->render_view_host(); | 143 RenderViewHost* host = tab->render_view_host(); |
105 host->view()->SetBackground(background); | 144 host->view()->SetBackground(background); |
106 | 145 |
107 views::Widget::InitParams widget_params( | 146 views::Widget::InitParams widget_params( |
108 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 147 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
109 widget_params.bounds = GetPreferredBounds(false); | 148 widget_params.bounds = GetPreferredBounds(false); |
110 widget_params.delegate = this; | 149 widget_params.delegate = this; |
| 150 widget_params.keep_on_top = true; |
| 151 widget_params.transparent = true; |
111 | 152 |
112 widget_ = new views::Widget; | 153 widget_ = new views::Widget; |
113 widget_->Init(widget_params); | 154 widget_->Init(widget_params); |
114 widget_->SetContentsView(contents_); | 155 widget_->SetContentsView(contents_); |
115 widget_->SetOpacity(0); | 156 widget_->SetOpacity(0); |
116 | 157 |
| 158 GetWidgetLayer(widget_)->GetAnimator()->AddObserver(this); |
117 aura::Desktop::GetInstance()->AddObserver(this); | 159 aura::Desktop::GetInstance()->AddObserver(this); |
118 } | 160 } |
119 | 161 |
120 void AppListWindow::SetVisible(bool visible, bool animate) { | 162 void AppListWindow::DoSetVisible(bool visible) { |
121 if (visible == is_visible_) | 163 if (visible == is_visible_) |
122 return; | 164 return; |
123 | 165 |
124 is_visible_ = visible; | 166 is_visible_ = visible; |
125 | 167 |
126 if (animate) { | 168 // Skip show animation if contents is not rendered. |
127 gfx::Point dummy; | 169 // TODO(xiyuan): Should we show a loading UI if it takes too long? |
128 ui::Layer* layer; | 170 if (visible && !content_rendered_) |
129 widget_->CalculateOffsetToAncestorWithLayer(&dummy, &layer); | 171 return; |
130 | 172 |
131 ui::LayerAnimator::ScopedSettings settings(layer->GetAnimator()); | 173 ui::Layer* layer = GetWidgetLayer(widget_); |
132 layer->SetBounds(GetPreferredBounds(visible)); | 174 ui::LayerAnimator::ScopedSettings settings(layer->GetAnimator()); |
133 layer->SetOpacity(visible ? 1.0 : 0.0); | 175 layer->SetBounds(GetPreferredBounds(visible)); |
134 } | 176 layer->SetOpacity(visible ? 1.0 : 0.0); |
135 | 177 |
136 if (visible) { | 178 if (visible) { |
| 179 widget_->Show(); |
137 widget_->Activate(); | 180 widget_->Activate(); |
138 widget_->Show(); | |
139 } else { | 181 } else { |
140 instance_ = NULL; // Closing and don't reuse this instance_. | 182 instance_ = NULL; // Closing and don't reuse this instance_. |
141 | |
142 if (animate) { | |
143 // TODO(xiyuan): Properly close widget after animation finishes. | |
144 MessageLoop::current()->PostDelayedTask( | |
145 FROM_HERE, | |
146 base::Bind(&views::Widget::Close, base::Unretained(widget_)), | |
147 1000); | |
148 } else { | |
149 widget_->Close(); | |
150 } | |
151 } | 183 } |
152 } | 184 } |
OLD | NEW |