| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "ash/common/system/tray/system_tray_bubble.h" | |
| 6 | |
| 7 #include "ash/common/system/tray/system_tray_delegate.h" | |
| 8 #include "ash/common/system/tray/system_tray_item.h" | |
| 9 #include "ash/common/system/tray/tray_bubble_wrapper.h" | |
| 10 #include "ash/common/system/tray/tray_constants.h" | |
| 11 #include "ash/common/system/tray/tray_popup_item_container.h" | |
| 12 #include "ash/common/wm_shell.h" | |
| 13 #include "ash/system/tray/system_tray.h" | |
| 14 #include "base/threading/thread_task_runner_handle.h" | |
| 15 #include "ui/compositor/layer.h" | |
| 16 #include "ui/compositor/layer_animation_observer.h" | |
| 17 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 18 #include "ui/gfx/canvas.h" | |
| 19 #include "ui/views/layout/box_layout.h" | |
| 20 #include "ui/views/view.h" | |
| 21 #include "ui/views/widget/widget.h" | |
| 22 | |
| 23 using views::TrayBubbleView; | |
| 24 | |
| 25 namespace ash { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // Normally a detailed view is the same size as the default view. However, | |
| 30 // when showing a detailed view directly (e.g. clicking on a notification), | |
| 31 // we may not know the height of the default view, or the default view may | |
| 32 // be too short, so we use this as a default and minimum height for any | |
| 33 // detailed view. | |
| 34 const int kDetailedBubbleMaxHeight = kTrayPopupItemHeight * 5; | |
| 35 | |
| 36 // Duration of swipe animation used when transitioning from a default to | |
| 37 // detailed view or vice versa. | |
| 38 const int kSwipeDelayMS = 150; | |
| 39 | |
| 40 // Implicit animation observer that deletes itself and the layer at the end of | |
| 41 // the animation. | |
| 42 class AnimationObserverDeleteLayer : public ui::ImplicitAnimationObserver { | |
| 43 public: | |
| 44 explicit AnimationObserverDeleteLayer(ui::Layer* layer) | |
| 45 : layer_(layer) { | |
| 46 } | |
| 47 | |
| 48 ~AnimationObserverDeleteLayer() override {} | |
| 49 | |
| 50 void OnImplicitAnimationsCompleted() override { | |
| 51 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 std::unique_ptr<ui::Layer> layer_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(AnimationObserverDeleteLayer); | |
| 58 }; | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 // SystemTrayBubble | |
| 63 | |
| 64 SystemTrayBubble::SystemTrayBubble( | |
| 65 ash::SystemTray* tray, | |
| 66 const std::vector<ash::SystemTrayItem*>& items, | |
| 67 BubbleType bubble_type) | |
| 68 : tray_(tray), | |
| 69 bubble_view_(NULL), | |
| 70 items_(items), | |
| 71 bubble_type_(bubble_type), | |
| 72 autoclose_delay_(0) { | |
| 73 } | |
| 74 | |
| 75 SystemTrayBubble::~SystemTrayBubble() { | |
| 76 DestroyItemViews(); | |
| 77 // Reset the host pointer in bubble_view_ in case its destruction is deferred. | |
| 78 if (bubble_view_) | |
| 79 bubble_view_->reset_delegate(); | |
| 80 } | |
| 81 | |
| 82 void SystemTrayBubble::UpdateView( | |
| 83 const std::vector<ash::SystemTrayItem*>& items, | |
| 84 BubbleType bubble_type) { | |
| 85 DCHECK(bubble_type != BUBBLE_TYPE_NOTIFICATION); | |
| 86 | |
| 87 std::unique_ptr<ui::Layer> scoped_layer; | |
| 88 if (bubble_type != bubble_type_) { | |
| 89 base::TimeDelta swipe_duration = | |
| 90 base::TimeDelta::FromMilliseconds(kSwipeDelayMS); | |
| 91 scoped_layer = bubble_view_->RecreateLayer(); | |
| 92 // Keep the reference to layer as we need it after releasing it. | |
| 93 ui::Layer* layer = scoped_layer.get(); | |
| 94 DCHECK(layer); | |
| 95 layer->SuppressPaint(); | |
| 96 | |
| 97 // When transitioning from detailed view to default view, animate the | |
| 98 // existing view (slide out towards the right). | |
| 99 if (bubble_type == BUBBLE_TYPE_DEFAULT) { | |
| 100 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
| 101 settings.AddObserver( | |
| 102 new AnimationObserverDeleteLayer(scoped_layer.release())); | |
| 103 settings.SetTransitionDuration(swipe_duration); | |
| 104 settings.SetTweenType(gfx::Tween::EASE_OUT); | |
| 105 gfx::Transform transform; | |
| 106 transform.Translate(layer->bounds().width(), 0.0); | |
| 107 layer->SetTransform(transform); | |
| 108 } | |
| 109 | |
| 110 { | |
| 111 // Add a shadow layer to make the old layer darker as the animation | |
| 112 // progresses. | |
| 113 ui::Layer* shadow = new ui::Layer(ui::LAYER_SOLID_COLOR); | |
| 114 shadow->SetColor(SK_ColorBLACK); | |
| 115 shadow->SetOpacity(0.01f); | |
| 116 shadow->SetBounds(layer->bounds()); | |
| 117 layer->Add(shadow); | |
| 118 layer->StackAtTop(shadow); | |
| 119 { | |
| 120 // Animate the darkening effect a little longer than the swipe-in. This | |
| 121 // is to make sure the darkening animation does not end up finishing | |
| 122 // early, because the dark layer goes away at the end of the animation, | |
| 123 // and there is a brief moment when the old view is still visible, but | |
| 124 // it does not have the shadow layer on top. | |
| 125 ui::ScopedLayerAnimationSettings settings(shadow->GetAnimator()); | |
| 126 settings.AddObserver(new AnimationObserverDeleteLayer(shadow)); | |
| 127 settings.SetTransitionDuration(swipe_duration + | |
| 128 base::TimeDelta::FromMilliseconds(150)); | |
| 129 settings.SetTweenType(gfx::Tween::LINEAR); | |
| 130 shadow->SetOpacity(0.15f); | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 DestroyItemViews(); | |
| 136 bubble_view_->RemoveAllChildViews(true); | |
| 137 | |
| 138 items_ = items; | |
| 139 bubble_type_ = bubble_type; | |
| 140 CreateItemViews(WmShell::Get()->system_tray_delegate()->GetUserLoginStatus()); | |
| 141 | |
| 142 // Close bubble view if we failed to create the item view. | |
| 143 if (!bubble_view_->has_children()) { | |
| 144 Close(); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 bubble_view_->GetWidget()->GetContentsView()->Layout(); | |
| 149 // Make sure that the bubble is large enough for the default view. | |
| 150 if (bubble_type_ == BUBBLE_TYPE_DEFAULT) { | |
| 151 bubble_view_->SetMaxHeight(0); // Clear max height limit. | |
| 152 } | |
| 153 | |
| 154 if (scoped_layer) { | |
| 155 // When transitioning from default view to detailed view, animate the new | |
| 156 // view (slide in from the right). | |
| 157 if (bubble_type == BUBBLE_TYPE_DETAILED) { | |
| 158 ui::Layer* new_layer = bubble_view_->layer(); | |
| 159 | |
| 160 // Make sure the new layer is stacked above the old layer during the | |
| 161 // animation. | |
| 162 new_layer->parent()->StackAbove(new_layer, scoped_layer.get()); | |
| 163 | |
| 164 gfx::Rect bounds = new_layer->bounds(); | |
| 165 gfx::Transform transform; | |
| 166 transform.Translate(bounds.width(), 0.0); | |
| 167 new_layer->SetTransform(transform); | |
| 168 { | |
| 169 ui::ScopedLayerAnimationSettings settings(new_layer->GetAnimator()); | |
| 170 settings.AddObserver( | |
| 171 new AnimationObserverDeleteLayer(scoped_layer.release())); | |
| 172 settings.SetTransitionDuration( | |
| 173 base::TimeDelta::FromMilliseconds(kSwipeDelayMS)); | |
| 174 settings.SetTweenType(gfx::Tween::EASE_OUT); | |
| 175 new_layer->SetTransform(gfx::Transform()); | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 void SystemTrayBubble::InitView(views::View* anchor, | |
| 182 LoginStatus login_status, | |
| 183 TrayBubbleView::InitParams* init_params) { | |
| 184 DCHECK(anchor); | |
| 185 DCHECK(!bubble_view_); | |
| 186 | |
| 187 if (bubble_type_ == BUBBLE_TYPE_DETAILED && | |
| 188 init_params->max_height < kDetailedBubbleMaxHeight) { | |
| 189 init_params->max_height = kDetailedBubbleMaxHeight; | |
| 190 } else if (bubble_type_ == BUBBLE_TYPE_NOTIFICATION) { | |
| 191 init_params->close_on_deactivate = false; | |
| 192 } | |
| 193 // The TrayBubbleView will use |anchor| and |tray_| to determine the parent | |
| 194 // container for the bubble. | |
| 195 bubble_view_ = TrayBubbleView::Create(anchor, tray_, init_params); | |
| 196 bubble_view_->set_adjust_if_offscreen(false); | |
| 197 CreateItemViews(login_status); | |
| 198 | |
| 199 if (bubble_view_->CanActivate()) { | |
| 200 bubble_view_->NotifyAccessibilityEvent( | |
| 201 ui::AX_EVENT_ALERT, true); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 void SystemTrayBubble::FocusDefaultIfNeeded() { | |
| 206 views::FocusManager* manager = bubble_view_->GetFocusManager(); | |
| 207 if (!manager || manager->GetFocusedView()) | |
| 208 return; | |
| 209 | |
| 210 views::View* view = manager->GetNextFocusableView(NULL, NULL, false, false); | |
| 211 if (view) | |
| 212 view->RequestFocus(); | |
| 213 } | |
| 214 | |
| 215 void SystemTrayBubble::DestroyItemViews() { | |
| 216 for (std::vector<ash::SystemTrayItem*>::iterator it = items_.begin(); | |
| 217 it != items_.end(); | |
| 218 ++it) { | |
| 219 switch (bubble_type_) { | |
| 220 case BUBBLE_TYPE_DEFAULT: | |
| 221 (*it)->DestroyDefaultView(); | |
| 222 break; | |
| 223 case BUBBLE_TYPE_DETAILED: | |
| 224 (*it)->DestroyDetailedView(); | |
| 225 break; | |
| 226 case BUBBLE_TYPE_NOTIFICATION: | |
| 227 (*it)->DestroyNotificationView(); | |
| 228 break; | |
| 229 } | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 void SystemTrayBubble::BubbleViewDestroyed() { | |
| 234 bubble_view_ = NULL; | |
| 235 } | |
| 236 | |
| 237 void SystemTrayBubble::StartAutoCloseTimer(int seconds) { | |
| 238 autoclose_.Stop(); | |
| 239 autoclose_delay_ = seconds; | |
| 240 if (autoclose_delay_) { | |
| 241 autoclose_.Start(FROM_HERE, | |
| 242 base::TimeDelta::FromSeconds(autoclose_delay_), | |
| 243 this, &SystemTrayBubble::Close); | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 void SystemTrayBubble::StopAutoCloseTimer() { | |
| 248 autoclose_.Stop(); | |
| 249 } | |
| 250 | |
| 251 void SystemTrayBubble::RestartAutoCloseTimer() { | |
| 252 if (autoclose_delay_) | |
| 253 StartAutoCloseTimer(autoclose_delay_); | |
| 254 } | |
| 255 | |
| 256 void SystemTrayBubble::Close() { | |
| 257 tray_->HideBubbleWithView(bubble_view()); | |
| 258 } | |
| 259 | |
| 260 void SystemTrayBubble::SetVisible(bool is_visible) { | |
| 261 if (!bubble_view_) | |
| 262 return; | |
| 263 views::Widget* bubble_widget = bubble_view_->GetWidget(); | |
| 264 if (is_visible) | |
| 265 bubble_widget->Show(); | |
| 266 else | |
| 267 bubble_widget->Hide(); | |
| 268 } | |
| 269 | |
| 270 bool SystemTrayBubble::IsVisible() { | |
| 271 return bubble_view() && bubble_view()->GetWidget()->IsVisible(); | |
| 272 } | |
| 273 | |
| 274 bool SystemTrayBubble::ShouldShowShelf() const { | |
| 275 for (std::vector<ash::SystemTrayItem*>::const_iterator it = items_.begin(); | |
| 276 it != items_.end(); | |
| 277 ++it) { | |
| 278 if ((*it)->ShouldShowShelf()) | |
| 279 return true; | |
| 280 } | |
| 281 return false; | |
| 282 } | |
| 283 | |
| 284 void SystemTrayBubble::CreateItemViews(LoginStatus login_status) { | |
| 285 std::vector<views::View*> item_views; | |
| 286 // If a system modal dialog is present, create the same tray as | |
| 287 // in locked state. | |
| 288 if (WmShell::Get()->IsSystemModalWindowOpen() && | |
| 289 login_status != LoginStatus::NOT_LOGGED_IN) { | |
| 290 login_status = LoginStatus::LOCKED; | |
| 291 } | |
| 292 | |
| 293 views::View* focus_view = NULL; | |
| 294 for (size_t i = 0; i < items_.size(); ++i) { | |
| 295 views::View* view = NULL; | |
| 296 switch (bubble_type_) { | |
| 297 case BUBBLE_TYPE_DEFAULT: | |
| 298 view = items_[i]->CreateDefaultView(login_status); | |
| 299 if (items_[i]->restore_focus()) | |
| 300 focus_view = view; | |
| 301 break; | |
| 302 case BUBBLE_TYPE_DETAILED: | |
| 303 view = items_[i]->CreateDetailedView(login_status); | |
| 304 break; | |
| 305 case BUBBLE_TYPE_NOTIFICATION: | |
| 306 view = items_[i]->CreateNotificationView(login_status); | |
| 307 break; | |
| 308 } | |
| 309 if (view) | |
| 310 item_views.push_back(view); | |
| 311 } | |
| 312 | |
| 313 bool is_default_bubble = bubble_type_ == BUBBLE_TYPE_DEFAULT; | |
| 314 for (size_t i = 0; i < item_views.size(); ++i) { | |
| 315 // For default view, draw bottom border for each item, except the last | |
| 316 // 2 items, which are the bottom header row and the one just above it. | |
| 317 bubble_view_->AddChildView(new TrayPopupItemContainer( | |
| 318 item_views[i], is_default_bubble, | |
| 319 is_default_bubble && (i < item_views.size() - 2))); | |
| 320 } | |
| 321 if (focus_view) | |
| 322 focus_view->RequestFocus(); | |
| 323 } | |
| 324 | |
| 325 } // namespace ash | |
| OLD | NEW |