Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: ash/common/shelf/shelf_tooltip_manager.cc

Issue 2176813002: mash: Migrate ShelfTooltipManager to wm common types. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add limited pre-target handler WmWindow interface; Move tooltip manager to common. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 kTooltipTopBottomMargin = 3; 29 const int kTooltipTopBottomMargin = 3;
31 const int kTooltipLeftRightMargin = 10; 30 const int kTooltipLeftRightMargin = 10;
32 const int kTooltipAppearanceDelay = 1000; // msec 31 const int kTooltipAppearanceDelay = 1000; // msec
33 const int kTooltipMinHeight = 29 - 2 * kTooltipTopBottomMargin; 32 const int kTooltipMinHeight = 29 - 2 * kTooltipTopBottomMargin;
34 const SkColor kTooltipTextColor = SkColorSetRGB(0x22, 0x22, 0x22); 33 const SkColor kTooltipTextColor = SkColorSetRGB(0x22, 0x22, 0x22);
35 34
(...skipping 10 matching lines...) Expand all
46 } // namespace 45 } // namespace
47 46
48 // The implementation of tooltip of the launcher. 47 // The implementation of tooltip of the launcher.
49 class ShelfTooltipManager::ShelfTooltipBubble 48 class ShelfTooltipManager::ShelfTooltipBubble
50 : public views::BubbleDialogDelegateView { 49 : public views::BubbleDialogDelegateView {
51 public: 50 public:
52 ShelfTooltipBubble(views::View* anchor, 51 ShelfTooltipBubble(views::View* anchor,
53 views::BubbleBorder::Arrow arrow, 52 views::BubbleBorder::Arrow arrow,
54 const base::string16& text) 53 const base::string16& text)
55 : views::BubbleDialogDelegateView(anchor, arrow) { 54 : views::BubbleDialogDelegateView(anchor, arrow) {
56 gfx::Insets insets = 55 gfx::Insets insets(kArrowOffsetTopBottom, kArrowOffsetLeftRight);
James Cook 2016/07/26 16:31:14 Just FYI, vakha has a lgtm'd patch that touches th
msw 2016/07/26 17:43:25 Acknowledged.
57 gfx::Insets(kArrowOffsetTopBottom, kArrowOffsetLeftRight);
58 // Adjust the anchor location for asymmetrical borders of shelf item. 56 // Adjust the anchor location for asymmetrical borders of shelf item.
59 if (anchor->border()) 57 if (anchor->border())
60 insets += anchor->border()->GetInsets(); 58 insets += anchor->border()->GetInsets();
61 59
62 set_anchor_view_insets(insets); 60 set_anchor_view_insets(insets);
63 set_close_on_deactivate(false); 61 set_close_on_deactivate(false);
64 set_can_activate(false); 62 set_can_activate(false);
65 set_accept_events(false); 63 set_accept_events(false);
66 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin)); 64 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin));
67 set_shadow(views::BubbleBorder::SMALL_SHADOW); 65 set_shadow(views::BubbleBorder::SMALL_SHADOW);
68 SetLayoutManager(new views::FillLayout()); 66 SetLayoutManager(new views::FillLayout());
69 // The anchor may not have the widget in tests.
70 if (anchor->GetWidget() && anchor->GetWidget()->GetNativeWindow()) {
71 set_parent_window(ash::Shell::GetContainer(
72 anchor->GetWidget()->GetNativeWindow()->GetRootWindow(),
73 ash::kShellWindowId_SettingBubbleContainer));
74 }
75 views::Label* label = new views::Label(text); 67 views::Label* label = new views::Label(text);
76 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); 68 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
77 label->SetEnabledColor(kTooltipTextColor); 69 label->SetEnabledColor(kTooltipTextColor);
78 AddChildView(label); 70 AddChildView(label);
79 views::BubbleDialogDelegateView::CreateBubble(this); 71 views::BubbleDialogDelegateView::CreateBubble(this);
80 } 72 }
81 73
82 private: 74 private:
83 // BubbleDialogDelegateView overrides: 75 // BubbleDialogDelegateView overrides:
84 gfx::Size GetPreferredSize() const override { 76 gfx::Size GetPreferredSize() const override {
85 const gfx::Size size = BubbleDialogDelegateView::GetPreferredSize(); 77 const gfx::Size size = BubbleDialogDelegateView::GetPreferredSize();
86 return gfx::Size(std::min(size.width(), kTooltipMaxWidth), 78 return gfx::Size(std::min(size.width(), kTooltipMaxWidth),
87 std::max(size.height(), kTooltipMinHeight)); 79 std::max(size.height(), kTooltipMinHeight));
88 } 80 }
89 81
82 void OnBeforeBubbleWidgetInit(views::Widget::InitParams* params,
83 views::Widget* bubble_widget) const override {
84 // Place the bubble in the same display as the anchor.
85 WmLookup::Get()
86 ->GetWindowForWidget(anchor_widget())
87 ->GetRootWindowController()
88 ->ConfigureWidgetInitParamsForContainer(
89 bubble_widget, kShellWindowId_SettingBubbleContainer, params);
90 }
91
90 int GetDialogButtons() const override { return ui::DIALOG_BUTTON_NONE; } 92 int GetDialogButtons() const override { return ui::DIALOG_BUTTON_NONE; }
91 93
92 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble); 94 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble);
93 }; 95 };
94 96
95 ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view) 97 ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view)
96 : timer_delay_(kTooltipAppearanceDelay), 98 : timer_delay_(kTooltipAppearanceDelay),
97 shelf_view_(shelf_view), 99 shelf_view_(shelf_view),
98 root_window_(nullptr),
99 shelf_layout_manager_(nullptr),
100 bubble_(nullptr), 100 bubble_(nullptr),
101 weak_factory_(this) {} 101 weak_factory_(this) {
102 shelf_view_->wm_shelf()->AddObserver(this);
103 WmShell::Get()->AddPointerWatcher(this);
104 }
102 105
103 ShelfTooltipManager::~ShelfTooltipManager() { 106 ShelfTooltipManager::~ShelfTooltipManager() {
104 WillDeleteShelfLayoutManager(); 107 WmShell::Get()->RemovePointerWatcher(this);
105 108 shelf_view_->wm_shelf()->RemoveObserver(this);
106 Shell::GetInstance()->RemovePointerWatcher(this); 109 WmWindow* window = nullptr;
107 110 if (shelf_view_->GetWidget())
108 if (root_window_) { 111 window = WmLookup::Get()->GetWindowForWidget(shelf_view_->GetWidget());
sky 2016/07/26 13:25:20 You have this in a number of places, is it worth a
msw 2016/07/26 17:43:25 Three is borderline, and only this needs to check
109 root_window_->RemoveObserver(this); 112 if (window)
110 root_window_->RemovePreTargetHandler(this); 113 window->RemoveLimitedPreTargetHandler(this);
111 root_window_ = nullptr;
112 }
113 } 114 }
114 115
115 void ShelfTooltipManager::Init() { 116 void ShelfTooltipManager::Init() {
116 shelf_layout_manager_ = shelf_view_->shelf()->shelf_layout_manager(); 117 WmWindow* window =
117 shelf_layout_manager_->AddObserver(this); 118 WmLookup::Get()->GetWindowForWidget(shelf_view_->GetWidget());
118 119 window->AddLimitedPreTargetHandler(this);
119 root_window_ = shelf_view_->GetWidget()->GetNativeWindow()->GetRootWindow();
120 root_window_->AddPreTargetHandler(this);
121 root_window_->AddObserver(this);
122
123 Shell::GetInstance()->AddPointerWatcher(this);
124 } 120 }
125 121
126 void ShelfTooltipManager::Close() { 122 void ShelfTooltipManager::Close() {
127 timer_.Stop(); 123 timer_.Stop();
128 if (bubble_) 124 if (bubble_)
129 bubble_->GetWidget()->Close(); 125 bubble_->GetWidget()->Close();
130 bubble_ = nullptr; 126 bubble_ = nullptr;
131 } 127 }
132 128
133 bool ShelfTooltipManager::IsVisible() const { 129 bool ShelfTooltipManager::IsVisible() const {
134 return bubble_ && bubble_->GetWidget()->IsVisible(); 130 return bubble_ && bubble_->GetWidget()->IsVisible();
135 } 131 }
136 132
137 views::View* ShelfTooltipManager::GetCurrentAnchorView() const { 133 views::View* ShelfTooltipManager::GetCurrentAnchorView() const {
138 return bubble_ ? bubble_->GetAnchorView() : nullptr; 134 return bubble_ ? bubble_->GetAnchorView() : nullptr;
139 } 135 }
140 136
141 void ShelfTooltipManager::ShowTooltip(views::View* view) { 137 void ShelfTooltipManager::ShowTooltip(views::View* view) {
142 timer_.Stop(); 138 timer_.Stop();
143 if (bubble_) { 139 if (bubble_) {
144 // Cancel the hiding animation to hide the old bubble immediately. 140 // Cancel the hiding animation to hide the old bubble immediately.
145 gfx::NativeView native_view = bubble_->GetWidget()->GetNativeView(); 141 WmLookup::Get()
146 ::wm::SetWindowVisibilityAnimationTransition(native_view, 142 ->GetWindowForWidget(bubble_->GetWidget())
147 ::wm::ANIMATE_NONE); 143 ->SetVisibilityAnimationTransition(::wm::ANIMATE_NONE);
148 Close(); 144 Close();
149 } 145 }
150 146
151 if (!ShouldShowTooltipForView(view)) 147 if (!ShouldShowTooltipForView(view))
152 return; 148 return;
153 149
154 Shelf* shelf = shelf_view_->shelf(); 150 views::BubbleBorder::Arrow arrow = views::BubbleBorder::Arrow::NONE;
155 views::BubbleBorder::Arrow arrow = shelf->SelectValueForShelfAlignment( 151 switch (shelf_view_->wm_shelf()->GetAlignment()) {
156 views::BubbleBorder::BOTTOM_CENTER, views::BubbleBorder::LEFT_CENTER, 152 case SHELF_ALIGNMENT_BOTTOM:
157 views::BubbleBorder::RIGHT_CENTER); 153 case SHELF_ALIGNMENT_BOTTOM_LOCKED:
154 arrow = views::BubbleBorder::BOTTOM_CENTER;
155 break;
156 case SHELF_ALIGNMENT_LEFT:
157 arrow = views::BubbleBorder::LEFT_CENTER;
158 break;
159 case SHELF_ALIGNMENT_RIGHT:
160 arrow = views::BubbleBorder::RIGHT_CENTER;
161 break;
162 }
158 163
159 base::string16 text = shelf_view_->GetTitleForView(view); 164 base::string16 text = shelf_view_->GetTitleForView(view);
160 bubble_ = new ShelfTooltipBubble(view, arrow, text); 165 bubble_ = new ShelfTooltipBubble(view, arrow, text);
161 gfx::NativeView native_view = bubble_->GetWidget()->GetNativeView(); 166 WmWindow* window = WmLookup::Get()->GetWindowForWidget(bubble_->GetWidget());
162 ::wm::SetWindowVisibilityAnimationType( 167 window->SetVisibilityAnimationType(
163 native_view, ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); 168 ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
164 ::wm::SetWindowVisibilityAnimationTransition(native_view, ::wm::ANIMATE_HIDE); 169 window->SetVisibilityAnimationTransition(::wm::ANIMATE_HIDE);
165 bubble_->GetWidget()->Show(); 170 bubble_->GetWidget()->Show();
166 } 171 }
167 172
168 void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) { 173 void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) {
169 if (ShouldShowTooltipForView(view)) { 174 if (ShouldShowTooltipForView(view)) {
170 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_delay_), 175 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_delay_),
171 base::Bind(&ShelfTooltipManager::ShowTooltip, 176 base::Bind(&ShelfTooltipManager::ShowTooltip,
172 weak_factory_.GetWeakPtr(), view)); 177 weak_factory_.GetWeakPtr(), view));
173 } 178 }
174 } 179 }
175 180
176 void ShelfTooltipManager::OnMousePressed(const ui::MouseEvent& event, 181 void ShelfTooltipManager::OnMousePressed(const ui::MouseEvent& event,
177 const gfx::Point& location_in_screen, 182 const gfx::Point& location_in_screen,
178 views::Widget* target) { 183 views::Widget* target) {
179 // Close on any mouse press events inside or outside the tooltip. 184 // Close on any mouse press events inside or outside the tooltip.
180 Close(); 185 Close();
181 } 186 }
182 187
183 void ShelfTooltipManager::OnTouchPressed(const ui::TouchEvent& event, 188 void ShelfTooltipManager::OnTouchPressed(const ui::TouchEvent& event,
184 const gfx::Point& location_in_screen, 189 const gfx::Point& location_in_screen,
185 views::Widget* target) { 190 views::Widget* target) {
186 // Close on any touch press events inside or outside the tooltip. 191 // Close on any touch press events inside or outside the tooltip.
187 Close(); 192 Close();
188 } 193 }
189 194
190 void ShelfTooltipManager::OnEvent(ui::Event* event) { 195 void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) {
191 // Mouse and touch press events are handled via views::PointerWatcher. 196 if (event->type() == ui::ET_MOUSE_EXITED) {
192 if (event->type() == ui::ET_MOUSE_PRESSED || 197 Close();
193 event->type() == ui::ET_TOUCH_PRESSED) {
194 return; 198 return;
195 } 199 }
196 200
197 if (event->type() == ui::ET_MOUSE_EXITED || !event->IsMouseEvent() || 201 if (event->type() != ui::ET_MOUSE_MOVED)
198 event->target() != shelf_view_->GetWidget()->GetNativeWindow()) {
199 if (!event->IsKeyEvent())
200 Close();
201 return; 202 return;
202 }
203 203
204 gfx::Point point = static_cast<ui::LocatedEvent*>(event)->location(); 204 gfx::Point point = event->location();
205 aura::Window::ConvertPointToTarget(
206 static_cast<aura::Window*>(event->target()),
207 shelf_view_->GetWidget()->GetNativeWindow(), &point);
208 views::View::ConvertPointFromWidget(shelf_view_, &point); 205 views::View::ConvertPointFromWidget(shelf_view_, &point);
James Cook 2016/07/26 16:31:14 Just to double-check: Is this still needed? I'm no
msw 2016/07/26 17:43:25 I tested removing this, and it breaks (reverses) t
James Cook 2016/07/26 17:55:37 Ah, RTL. Good to know.
209 views::View* view = shelf_view_->GetTooltipHandlerForPoint(point); 206 views::View* view = shelf_view_->GetTooltipHandlerForPoint(point);
210 const bool should_show = ShouldShowTooltipForView(view); 207 const bool should_show = ShouldShowTooltipForView(view);
211 208
212 timer_.Stop(); 209 timer_.Stop();
213 if (IsVisible() && should_show && bubble_->GetAnchorView() != view) 210 if (IsVisible() && should_show && bubble_->GetAnchorView() != view)
214 ShowTooltip(view); 211 ShowTooltip(view);
215 else if (!IsVisible() && should_show && event->type() == ui::ET_MOUSE_MOVED) 212 else if (!IsVisible() && should_show)
216 ShowTooltipWithDelay(view); 213 ShowTooltipWithDelay(view);
217 else if (IsVisible() && shelf_view_->ShouldHideTooltip(point)) 214 else if (IsVisible() && shelf_view_->ShouldHideTooltip(point))
218 Close(); 215 Close();
219 } 216 }
220 217
221 void ShelfTooltipManager::OnWindowDestroying(aura::Window* window) {
sky 2016/07/26 13:25:20 Is this and the next no loner necessary because of
James Cook 2016/07/26 16:31:14 Same question. I suspect it's OK since you're no l
msw 2016/07/26 17:43:25 Yeah, this is no longer an observer of the aura ro
James Cook 2016/07/26 17:55:37 I think it's fine as you have it.
222 if (window == root_window_) {
223 root_window_->RemoveObserver(this);
224 root_window_->RemovePreTargetHandler(this);
225 root_window_ = nullptr;
226 }
227 }
228
229 void ShelfTooltipManager::WillDeleteShelfLayoutManager() {
230 if (shelf_layout_manager_)
231 shelf_layout_manager_->RemoveObserver(this);
232 shelf_layout_manager_ = nullptr;
233 shelf_view_ = nullptr;
234 }
235
236 void ShelfTooltipManager::WillChangeVisibilityState( 218 void ShelfTooltipManager::WillChangeVisibilityState(
237 ShelfVisibilityState new_state) { 219 ShelfVisibilityState new_state) {
238 if (new_state == SHELF_HIDDEN) 220 if (new_state == SHELF_HIDDEN)
239 Close(); 221 Close();
240 } 222 }
241 223
242 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) { 224 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) {
243 if (new_state == SHELF_AUTO_HIDE_HIDDEN) { 225 if (new_state == SHELF_AUTO_HIDE_HIDDEN) {
244 timer_.Stop(); 226 timer_.Stop();
245 // AutoHide state change happens during an event filter, so immediate close 227 // AutoHide state change happens during an event filter, so immediate close
246 // may cause a crash in the HandleMouseEvent() after the filter. So we just 228 // may cause a crash in the HandleMouseEvent() after the filter. So we just
247 // schedule the Close here. 229 // schedule the Close here.
248 base::ThreadTaskRunnerHandle::Get()->PostTask( 230 base::ThreadTaskRunnerHandle::Get()->PostTask(
249 FROM_HERE, 231 FROM_HERE,
250 base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr())); 232 base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr()));
251 } 233 }
252 } 234 }
253 235
254 bool ShelfTooltipManager::ShouldShowTooltipForView(views::View* view) { 236 bool ShelfTooltipManager::ShouldShowTooltipForView(views::View* view) {
255 return shelf_view_ && shelf_view_->ShouldShowTooltipForView(view) && 237 return shelf_view_ && shelf_view_->ShouldShowTooltipForView(view) &&
256 shelf_layout_manager_ && shelf_layout_manager_->IsVisible(); 238 shelf_view_->wm_shelf()->GetVisibilityState() == SHELF_VISIBLE;
257 } 239 }
258 240
259 } // namespace ash 241 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698