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