| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ash/shelf/shelf_tooltip_manager.h" | 5 #include "ash/common/shelf/shelf_tooltip_manager.h" |
| 6 | 6 |
| 7 #include "ash/common/shelf/wm_shelf.h" |
| 7 #include "ash/common/shell_window_ids.h" | 8 #include "ash/common/shell_window_ids.h" |
| 8 #include "ash/shelf/shelf.h" | 9 #include "ash/common/wm_lookup.h" |
| 9 #include "ash/shelf/shelf_layout_manager.h" | 10 #include "ash/common/wm_root_window_controller.h" |
| 11 #include "ash/common/wm_shell.h" |
| 12 #include "ash/common/wm_window.h" |
| 10 #include "ash/shelf/shelf_view.h" | 13 #include "ash/shelf/shelf_view.h" |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/wm/window_animations.h" | |
| 13 #include "base/bind.h" | 14 #include "base/bind.h" |
| 14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "ui/aura/window.h" | |
| 18 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
| 19 #include "ui/events/event_constants.h" | 19 #include "ui/events/event_constants.h" |
| 20 #include "ui/gfx/geometry/insets.h" | 20 #include "ui/gfx/geometry/insets.h" |
| 21 #include "ui/views/bubble/bubble_dialog_delegate.h" | 21 #include "ui/views/bubble/bubble_dialog_delegate.h" |
| 22 #include "ui/views/controls/label.h" | 22 #include "ui/views/controls/label.h" |
| 23 #include "ui/views/layout/fill_layout.h" | 23 #include "ui/views/layout/fill_layout.h" |
| 24 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 25 #include "ui/wm/core/window_animations.h" | |
| 26 | 25 |
| 27 namespace ash { | 26 namespace ash { |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 const int kTooltipAppearanceDelay = 1000; // msec | 29 const int kTooltipAppearanceDelay = 1000; // msec |
| 31 | 30 |
| 32 // Tooltip layout constants. | 31 // Tooltip layout constants. |
| 33 | 32 |
| 34 // Shelf item tooltip height. | 33 // Shelf item tooltip height. |
| 35 const int kTooltipHeight = 24; | 34 const int kTooltipHeight = 24; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 66 if (anchor->border()) | 65 if (anchor->border()) |
| 67 insets += anchor->border()->GetInsets(); | 66 insets += anchor->border()->GetInsets(); |
| 68 | 67 |
| 69 set_anchor_view_insets(insets); | 68 set_anchor_view_insets(insets); |
| 70 set_close_on_deactivate(false); | 69 set_close_on_deactivate(false); |
| 71 set_can_activate(false); | 70 set_can_activate(false); |
| 72 set_accept_events(false); | 71 set_accept_events(false); |
| 73 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin)); | 72 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin)); |
| 74 set_shadow(views::BubbleBorder::NO_ASSETS); | 73 set_shadow(views::BubbleBorder::NO_ASSETS); |
| 75 SetLayoutManager(new views::FillLayout()); | 74 SetLayoutManager(new views::FillLayout()); |
| 76 // The anchor may not have the widget in tests. | |
| 77 if (anchor->GetWidget() && anchor->GetWidget()->GetNativeWindow()) { | |
| 78 set_parent_window(ash::Shell::GetContainer( | |
| 79 anchor->GetWidget()->GetNativeWindow()->GetRootWindow(), | |
| 80 ash::kShellWindowId_SettingBubbleContainer)); | |
| 81 } | |
| 82 views::Label* label = new views::Label(text); | 75 views::Label* label = new views::Label(text); |
| 83 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 76 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 84 ui::NativeTheme* theme = anchor->GetWidget()->GetNativeTheme(); | 77 ui::NativeTheme* theme = anchor->GetWidget()->GetNativeTheme(); |
| 85 label->SetEnabledColor( | 78 label->SetEnabledColor( |
| 86 theme->GetSystemColor(ui::NativeTheme::kColorId_TooltipText)); | 79 theme->GetSystemColor(ui::NativeTheme::kColorId_TooltipText)); |
| 87 SkColor background_color = | 80 SkColor background_color = |
| 88 theme->GetSystemColor(ui::NativeTheme::kColorId_TooltipBackground); | 81 theme->GetSystemColor(ui::NativeTheme::kColorId_TooltipBackground); |
| 89 set_color(background_color); | 82 set_color(background_color); |
| 90 label->SetBackgroundColor(background_color); | 83 label->SetBackgroundColor(background_color); |
| 91 AddChildView(label); | 84 AddChildView(label); |
| 92 views::BubbleDialogDelegateView::CreateBubble(this); | 85 views::BubbleDialogDelegateView::CreateBubble(this); |
| 93 // The bubble border has its own background so the background created by the | 86 // The bubble border has its own background so the background created by the |
| 94 // BubbleDialogDelegateView is redundant and would cause extra opacity. | 87 // BubbleDialogDelegateView is redundant and would cause extra opacity. |
| 95 set_background(nullptr); | 88 set_background(nullptr); |
| 96 SetArrowPaintType(views::BubbleBorder::PAINT_TRANSPARENT); | 89 SetArrowPaintType(views::BubbleBorder::PAINT_TRANSPARENT); |
| 97 SetBorderInteriorThickness(kBorderInteriorThickness); | 90 SetBorderInteriorThickness(kBorderInteriorThickness); |
| 98 } | 91 } |
| 99 | 92 |
| 100 private: | 93 private: |
| 101 // BubbleDialogDelegateView overrides: | 94 // BubbleDialogDelegateView overrides: |
| 102 gfx::Size GetPreferredSize() const override { | 95 gfx::Size GetPreferredSize() const override { |
| 103 const gfx::Size size = BubbleDialogDelegateView::GetPreferredSize(); | 96 const gfx::Size size = BubbleDialogDelegateView::GetPreferredSize(); |
| 104 const int kTooltipMinHeight = kTooltipHeight - 2 * kTooltipTopBottomMargin; | 97 const int kTooltipMinHeight = kTooltipHeight - 2 * kTooltipTopBottomMargin; |
| 105 return gfx::Size(std::min(size.width(), kTooltipMaxWidth), | 98 return gfx::Size(std::min(size.width(), kTooltipMaxWidth), |
| 106 std::max(size.height(), kTooltipMinHeight)); | 99 std::max(size.height(), kTooltipMinHeight)); |
| 107 } | 100 } |
| 108 | 101 |
| 102 void OnBeforeBubbleWidgetInit(views::Widget::InitParams* params, |
| 103 views::Widget* bubble_widget) const override { |
| 104 // Place the bubble in the same display as the anchor. |
| 105 WmLookup::Get() |
| 106 ->GetWindowForWidget(anchor_widget()) |
| 107 ->GetRootWindowController() |
| 108 ->ConfigureWidgetInitParamsForContainer( |
| 109 bubble_widget, kShellWindowId_SettingBubbleContainer, params); |
| 110 } |
| 111 |
| 109 int GetDialogButtons() const override { return ui::DIALOG_BUTTON_NONE; } | 112 int GetDialogButtons() const override { return ui::DIALOG_BUTTON_NONE; } |
| 110 | 113 |
| 111 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble); | 114 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble); |
| 112 }; | 115 }; |
| 113 | 116 |
| 114 ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view) | 117 ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view) |
| 115 : timer_delay_(kTooltipAppearanceDelay), | 118 : timer_delay_(kTooltipAppearanceDelay), |
| 116 shelf_view_(shelf_view), | 119 shelf_view_(shelf_view), |
| 117 root_window_(nullptr), | |
| 118 shelf_layout_manager_(nullptr), | |
| 119 bubble_(nullptr), | 120 bubble_(nullptr), |
| 120 weak_factory_(this) {} | 121 weak_factory_(this) { |
| 122 shelf_view_->wm_shelf()->AddObserver(this); |
| 123 WmShell::Get()->AddPointerWatcher(this); |
| 124 } |
| 121 | 125 |
| 122 ShelfTooltipManager::~ShelfTooltipManager() { | 126 ShelfTooltipManager::~ShelfTooltipManager() { |
| 123 WillDeleteShelfLayoutManager(); | 127 WmShell::Get()->RemovePointerWatcher(this); |
| 124 | 128 shelf_view_->wm_shelf()->RemoveObserver(this); |
| 125 Shell::GetInstance()->RemovePointerWatcher(this); | 129 WmWindow* window = nullptr; |
| 126 | 130 if (shelf_view_->GetWidget()) |
| 127 if (root_window_) { | 131 window = WmLookup::Get()->GetWindowForWidget(shelf_view_->GetWidget()); |
| 128 root_window_->RemoveObserver(this); | 132 if (window) |
| 129 root_window_->RemovePreTargetHandler(this); | 133 window->RemoveLimitedPreTargetHandler(this); |
| 130 root_window_ = nullptr; | |
| 131 } | |
| 132 } | 134 } |
| 133 | 135 |
| 134 void ShelfTooltipManager::Init() { | 136 void ShelfTooltipManager::Init() { |
| 135 shelf_layout_manager_ = shelf_view_->shelf()->shelf_layout_manager(); | 137 WmWindow* window = |
| 136 shelf_layout_manager_->AddObserver(this); | 138 WmLookup::Get()->GetWindowForWidget(shelf_view_->GetWidget()); |
| 137 | 139 window->AddLimitedPreTargetHandler(this); |
| 138 root_window_ = shelf_view_->GetWidget()->GetNativeWindow()->GetRootWindow(); | |
| 139 root_window_->AddPreTargetHandler(this); | |
| 140 root_window_->AddObserver(this); | |
| 141 | |
| 142 Shell::GetInstance()->AddPointerWatcher(this); | |
| 143 } | 140 } |
| 144 | 141 |
| 145 void ShelfTooltipManager::Close() { | 142 void ShelfTooltipManager::Close() { |
| 146 timer_.Stop(); | 143 timer_.Stop(); |
| 147 if (bubble_) | 144 if (bubble_) |
| 148 bubble_->GetWidget()->Close(); | 145 bubble_->GetWidget()->Close(); |
| 149 bubble_ = nullptr; | 146 bubble_ = nullptr; |
| 150 } | 147 } |
| 151 | 148 |
| 152 bool ShelfTooltipManager::IsVisible() const { | 149 bool ShelfTooltipManager::IsVisible() const { |
| 153 return bubble_ && bubble_->GetWidget()->IsVisible(); | 150 return bubble_ && bubble_->GetWidget()->IsVisible(); |
| 154 } | 151 } |
| 155 | 152 |
| 156 views::View* ShelfTooltipManager::GetCurrentAnchorView() const { | 153 views::View* ShelfTooltipManager::GetCurrentAnchorView() const { |
| 157 return bubble_ ? bubble_->GetAnchorView() : nullptr; | 154 return bubble_ ? bubble_->GetAnchorView() : nullptr; |
| 158 } | 155 } |
| 159 | 156 |
| 160 void ShelfTooltipManager::ShowTooltip(views::View* view) { | 157 void ShelfTooltipManager::ShowTooltip(views::View* view) { |
| 161 timer_.Stop(); | 158 timer_.Stop(); |
| 162 if (bubble_) { | 159 if (bubble_) { |
| 163 // Cancel the hiding animation to hide the old bubble immediately. | 160 // Cancel the hiding animation to hide the old bubble immediately. |
| 164 gfx::NativeView native_view = bubble_->GetWidget()->GetNativeView(); | 161 WmLookup::Get() |
| 165 ::wm::SetWindowVisibilityAnimationTransition(native_view, | 162 ->GetWindowForWidget(bubble_->GetWidget()) |
| 166 ::wm::ANIMATE_NONE); | 163 ->SetVisibilityAnimationTransition(::wm::ANIMATE_NONE); |
| 167 Close(); | 164 Close(); |
| 168 } | 165 } |
| 169 | 166 |
| 170 if (!ShouldShowTooltipForView(view)) | 167 if (!ShouldShowTooltipForView(view)) |
| 171 return; | 168 return; |
| 172 | 169 |
| 173 Shelf* shelf = shelf_view_->shelf(); | 170 views::BubbleBorder::Arrow arrow = views::BubbleBorder::Arrow::NONE; |
| 174 views::BubbleBorder::Arrow arrow = shelf->SelectValueForShelfAlignment( | 171 switch (shelf_view_->wm_shelf()->GetAlignment()) { |
| 175 views::BubbleBorder::BOTTOM_CENTER, views::BubbleBorder::LEFT_CENTER, | 172 case SHELF_ALIGNMENT_BOTTOM: |
| 176 views::BubbleBorder::RIGHT_CENTER); | 173 case SHELF_ALIGNMENT_BOTTOM_LOCKED: |
| 174 arrow = views::BubbleBorder::BOTTOM_CENTER; |
| 175 break; |
| 176 case SHELF_ALIGNMENT_LEFT: |
| 177 arrow = views::BubbleBorder::LEFT_CENTER; |
| 178 break; |
| 179 case SHELF_ALIGNMENT_RIGHT: |
| 180 arrow = views::BubbleBorder::RIGHT_CENTER; |
| 181 break; |
| 182 } |
| 177 | 183 |
| 178 base::string16 text = shelf_view_->GetTitleForView(view); | 184 base::string16 text = shelf_view_->GetTitleForView(view); |
| 179 bubble_ = new ShelfTooltipBubble(view, arrow, text); | 185 bubble_ = new ShelfTooltipBubble(view, arrow, text); |
| 180 gfx::NativeView native_view = bubble_->GetWidget()->GetNativeView(); | 186 WmWindow* window = WmLookup::Get()->GetWindowForWidget(bubble_->GetWidget()); |
| 181 ::wm::SetWindowVisibilityAnimationType( | 187 window->SetVisibilityAnimationType( |
| 182 native_view, ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); | 188 ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); |
| 183 ::wm::SetWindowVisibilityAnimationTransition(native_view, ::wm::ANIMATE_HIDE); | 189 window->SetVisibilityAnimationTransition(::wm::ANIMATE_HIDE); |
| 184 bubble_->GetWidget()->Show(); | 190 bubble_->GetWidget()->Show(); |
| 185 } | 191 } |
| 186 | 192 |
| 187 void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) { | 193 void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) { |
| 188 if (ShouldShowTooltipForView(view)) { | 194 if (ShouldShowTooltipForView(view)) { |
| 189 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_delay_), | 195 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_delay_), |
| 190 base::Bind(&ShelfTooltipManager::ShowTooltip, | 196 base::Bind(&ShelfTooltipManager::ShowTooltip, |
| 191 weak_factory_.GetWeakPtr(), view)); | 197 weak_factory_.GetWeakPtr(), view)); |
| 192 } | 198 } |
| 193 } | 199 } |
| 194 | 200 |
| 195 void ShelfTooltipManager::OnMousePressed(const ui::MouseEvent& event, | 201 void ShelfTooltipManager::OnMousePressed(const ui::MouseEvent& event, |
| 196 const gfx::Point& location_in_screen, | 202 const gfx::Point& location_in_screen, |
| 197 views::Widget* target) { | 203 views::Widget* target) { |
| 198 // Close on any mouse press events inside or outside the tooltip. | 204 // Close on any mouse press events inside or outside the tooltip. |
| 199 Close(); | 205 Close(); |
| 200 } | 206 } |
| 201 | 207 |
| 202 void ShelfTooltipManager::OnTouchPressed(const ui::TouchEvent& event, | 208 void ShelfTooltipManager::OnTouchPressed(const ui::TouchEvent& event, |
| 203 const gfx::Point& location_in_screen, | 209 const gfx::Point& location_in_screen, |
| 204 views::Widget* target) { | 210 views::Widget* target) { |
| 205 // Close on any touch press events inside or outside the tooltip. | 211 // Close on any touch press events inside or outside the tooltip. |
| 206 Close(); | 212 Close(); |
| 207 } | 213 } |
| 208 | 214 |
| 209 void ShelfTooltipManager::OnEvent(ui::Event* event) { | 215 void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) { |
| 210 // Mouse and touch press events are handled via views::PointerWatcher. | 216 if (event->type() == ui::ET_MOUSE_EXITED) { |
| 211 if (event->type() == ui::ET_MOUSE_PRESSED || | 217 Close(); |
| 212 event->type() == ui::ET_TOUCH_PRESSED) { | |
| 213 return; | 218 return; |
| 214 } | 219 } |
| 215 | 220 |
| 216 if (event->type() == ui::ET_MOUSE_EXITED || !event->IsMouseEvent() || | 221 if (event->type() != ui::ET_MOUSE_MOVED) |
| 217 event->target() != shelf_view_->GetWidget()->GetNativeWindow()) { | |
| 218 if (!event->IsKeyEvent()) | |
| 219 Close(); | |
| 220 return; | 222 return; |
| 221 } | |
| 222 | 223 |
| 223 gfx::Point point = static_cast<ui::LocatedEvent*>(event)->location(); | 224 gfx::Point point = event->location(); |
| 224 aura::Window::ConvertPointToTarget( | |
| 225 static_cast<aura::Window*>(event->target()), | |
| 226 shelf_view_->GetWidget()->GetNativeWindow(), &point); | |
| 227 views::View::ConvertPointFromWidget(shelf_view_, &point); | 225 views::View::ConvertPointFromWidget(shelf_view_, &point); |
| 228 views::View* view = shelf_view_->GetTooltipHandlerForPoint(point); | 226 views::View* view = shelf_view_->GetTooltipHandlerForPoint(point); |
| 229 const bool should_show = ShouldShowTooltipForView(view); | 227 const bool should_show = ShouldShowTooltipForView(view); |
| 230 | 228 |
| 231 timer_.Stop(); | 229 timer_.Stop(); |
| 232 if (IsVisible() && should_show && bubble_->GetAnchorView() != view) | 230 if (IsVisible() && should_show && bubble_->GetAnchorView() != view) |
| 233 ShowTooltip(view); | 231 ShowTooltip(view); |
| 234 else if (!IsVisible() && should_show && event->type() == ui::ET_MOUSE_MOVED) | 232 else if (!IsVisible() && should_show) |
| 235 ShowTooltipWithDelay(view); | 233 ShowTooltipWithDelay(view); |
| 236 else if (IsVisible() && shelf_view_->ShouldHideTooltip(point)) | 234 else if (IsVisible() && shelf_view_->ShouldHideTooltip(point)) |
| 237 Close(); | 235 Close(); |
| 238 } | 236 } |
| 239 | 237 |
| 240 void ShelfTooltipManager::OnWindowDestroying(aura::Window* window) { | |
| 241 if (window == root_window_) { | |
| 242 root_window_->RemoveObserver(this); | |
| 243 root_window_->RemovePreTargetHandler(this); | |
| 244 root_window_ = nullptr; | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 void ShelfTooltipManager::WillDeleteShelfLayoutManager() { | |
| 249 if (shelf_layout_manager_) | |
| 250 shelf_layout_manager_->RemoveObserver(this); | |
| 251 shelf_layout_manager_ = nullptr; | |
| 252 shelf_view_ = nullptr; | |
| 253 } | |
| 254 | |
| 255 void ShelfTooltipManager::WillChangeVisibilityState( | 238 void ShelfTooltipManager::WillChangeVisibilityState( |
| 256 ShelfVisibilityState new_state) { | 239 ShelfVisibilityState new_state) { |
| 257 if (new_state == SHELF_HIDDEN) | 240 if (new_state == SHELF_HIDDEN) |
| 258 Close(); | 241 Close(); |
| 259 } | 242 } |
| 260 | 243 |
| 261 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) { | 244 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) { |
| 262 if (new_state == SHELF_AUTO_HIDE_HIDDEN) { | 245 if (new_state == SHELF_AUTO_HIDE_HIDDEN) { |
| 263 timer_.Stop(); | 246 timer_.Stop(); |
| 264 // AutoHide state change happens during an event filter, so immediate close | 247 // AutoHide state change happens during an event filter, so immediate close |
| 265 // may cause a crash in the HandleMouseEvent() after the filter. So we just | 248 // may cause a crash in the HandleMouseEvent() after the filter. So we just |
| 266 // schedule the Close here. | 249 // schedule the Close here. |
| 267 base::ThreadTaskRunnerHandle::Get()->PostTask( | 250 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 268 FROM_HERE, | 251 FROM_HERE, |
| 269 base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr())); | 252 base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr())); |
| 270 } | 253 } |
| 271 } | 254 } |
| 272 | 255 |
| 273 bool ShelfTooltipManager::ShouldShowTooltipForView(views::View* view) { | 256 bool ShelfTooltipManager::ShouldShowTooltipForView(views::View* view) { |
| 274 return shelf_view_ && shelf_view_->ShouldShowTooltipForView(view) && | 257 return shelf_view_ && shelf_view_->ShouldShowTooltipForView(view) && |
| 275 shelf_layout_manager_ && shelf_layout_manager_->IsVisible(); | 258 shelf_view_->wm_shelf()->GetVisibilityState() == SHELF_VISIBLE; |
| 276 } | 259 } |
| 277 | 260 |
| 278 } // namespace ash | 261 } // namespace ash |
| OLD | NEW |