Commit 282af28e authored by Mike Wasserman's avatar Mike Wasserman Committed by Commit Bot

ash: Use EventHandler instead of PointerWatcher in ShelfTooltipManager

PointerWatcher is no longer needed with Ash hosting the Window Service.
Some minor cleanup.

Bug: 872450
Test: No regressions with shelf tooltip behavior.
Change-Id: I8d1b2bf008c54ae921dcabc67dc236d7d5801e11
Reviewed-on: https://chromium-review.googlesource.com/1250048
Commit-Queue: Michael Wasserman <msw@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594954}
parent aa39f30d
......@@ -31,27 +31,29 @@ const int kTooltipAppearanceDelay = 1000; // msec
ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view)
: timer_delay_(kTooltipAppearanceDelay),
shelf_view_(shelf_view),
bubble_(nullptr),
weak_factory_(this) {
shelf_view_->shelf()->AddObserver(this);
Shell::Get()->AddPointerWatcher(this, views::PointerWatcherEventTypes::BASIC);
Shell::Get()->AddPreTargetHandler(this);
}
ShelfTooltipManager::~ShelfTooltipManager() {
Shell::Get()->RemovePointerWatcher(this);
Shell::Get()->RemovePreTargetHandler(this);
shelf_view_->shelf()->RemoveObserver(this);
if (shelf_view_->GetWidget() && shelf_view_->GetWidget()->GetNativeWindow())
shelf_view_->GetWidget()->GetNativeWindow()->RemovePreTargetHandler(this);
}
void ShelfTooltipManager::Init() {
shelf_view_->GetWidget()->GetNativeWindow()->AddPreTargetHandler(this);
}
void ShelfTooltipManager::Close() {
void ShelfTooltipManager::Close(bool animate) {
// Cancel any timer set to show a tooltip after a delay.
timer_.Stop();
if (bubble_)
bubble_->GetWidget()->Close();
if (!bubble_)
return;
if (!animate) {
// Cancel the typical hiding animation to hide the bubble immediately.
::wm::SetWindowVisibilityAnimationTransition(
bubble_->GetWidget()->GetNativeWindow(), ::wm::ANIMATE_NONE);
}
bubble_->GetWidget()->Close();
bubble_ = nullptr;
}
......@@ -64,13 +66,8 @@ views::View* ShelfTooltipManager::GetCurrentAnchorView() const {
}
void ShelfTooltipManager::ShowTooltip(views::View* view) {
timer_.Stop();
if (bubble_) {
// Cancel the hiding animation to hide the old bubble immediately.
::wm::SetWindowVisibilityAnimationTransition(
bubble_->GetWidget()->GetNativeWindow(), ::wm::ANIMATE_NONE);
Close();
}
// Hide the old bubble immediately, skipping the typical closing animation.
Close(false /*animate*/);
if (!ShouldShowTooltipForView(view))
return;
......@@ -115,37 +112,23 @@ void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) {
}
}
void ShelfTooltipManager::OnPointerEventObserved(
const ui::PointerEvent& event,
const gfx::Point& location_in_screen,
gfx::NativeView target) {
if (event.type() != ui::ET_POINTER_DOWN || !bubble_)
return;
// If the click was outside the tooltip, always close it.
if (!bubble_->GetWidget()->GetWindowBoundsInScreen().Contains(
location_in_screen)) {
Close();
void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) {
if (bubble_ && event->type() == ui::ET_MOUSE_PRESSED) {
ProcessPressedEvent(*event);
return;
}
// Close the bubble if appropriate.
if (bubble_->ShouldCloseOnPressDown())
if (bubble_ && event->type() == ui::ET_MOUSE_EXITED &&
bubble_->ShouldCloseOnMouseExit()) {
Close();
}
void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) {
if (event->type() == ui::ET_MOUSE_EXITED) {
if (bubble_ && bubble_->ShouldCloseOnMouseExit())
Close();
// Don't show any tooltip we were planning on showing after a delay.
timer_.Stop();
return;
}
if (event->type() != ui::ET_MOUSE_MOVED)
// The code below handles mouse move events within the shelf window.
if (event->type() != ui::ET_MOUSE_MOVED ||
event->target() != shelf_view_->GetWidget()->GetNativeWindow()) {
return;
}
gfx::Point point = event->location();
views::View::ConvertPointFromWidget(shelf_view_, &point);
......@@ -161,6 +144,11 @@ void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent* event) {
Close();
}
void ShelfTooltipManager::OnTouchEvent(ui::TouchEvent* event) {
if (bubble_ && event->type() == ui::ET_TOUCH_PRESSED)
ProcessPressedEvent(*event);
}
void ShelfTooltipManager::WillChangeVisibilityState(
ShelfVisibilityState new_state) {
if (new_state == SHELF_HIDDEN)
......@@ -168,15 +156,8 @@ void ShelfTooltipManager::WillChangeVisibilityState(
}
void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) {
if (new_state == SHELF_AUTO_HIDE_HIDDEN) {
timer_.Stop();
// AutoHide state change happens during an event filter, so immediate close
// may cause a crash in the HandleMouseEvent() after the filter. So we just
// schedule the Close here.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr()));
}
if (new_state == SHELF_AUTO_HIDE_HIDDEN)
Close();
}
bool ShelfTooltipManager::ShouldShowTooltipForView(views::View* view) {
......@@ -188,4 +169,12 @@ bool ShelfTooltipManager::ShouldShowTooltipForView(views::View* view) {
shelf->GetAutoHideState() == SHELF_AUTO_HIDE_SHOWN));
}
void ShelfTooltipManager::ProcessPressedEvent(const ui::LocatedEvent& event) {
// Always close the tooltip on press events outside the tooltip.
if (bubble_->ShouldCloseOnPressDown() ||
event.target() != bubble_->GetWidget()->GetNativeWindow()) {
Close();
}
}
} // namespace ash
......@@ -7,33 +7,32 @@
#include "ash/ash_export.h"
#include "ash/shelf/shelf_observer.h"
#include "ash/shelf/shelf_tooltip_bubble_base.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "ui/events/event_handler.h"
#include "ui/views/pointer_watcher.h"
namespace ui {
class LocatedEvent;
}
namespace views {
class View;
}
namespace ash {
class ShelfTooltipBubbleBase;
class ShelfView;
// ShelfTooltipManager manages the tooltip bubble that appears for shelf items.
class ASH_EXPORT ShelfTooltipManager : public ui::EventHandler,
public views::PointerWatcher,
public ShelfObserver {
public:
explicit ShelfTooltipManager(ShelfView* shelf_view);
~ShelfTooltipManager() override;
// Initializes the tooltip manager once the shelf is shown.
void Init();
// Closes the tooltip.
void Close();
// Closes the tooltip; uses an animation if |animate| is true.
void Close(bool animate = true);
// Returns true if the tooltip is currently visible.
bool IsVisible() const;
......@@ -51,11 +50,7 @@ class ASH_EXPORT ShelfTooltipManager : public ui::EventHandler,
protected:
// ui::EventHandler overrides:
void OnMouseEvent(ui::MouseEvent* event) override;
// views::PointerWatcher overrides:
void OnPointerEventObserved(const ui::PointerEvent& event,
const gfx::Point& location_in_screen,
gfx::NativeView target) override;
void OnTouchEvent(ui::TouchEvent* event) override;
// ShelfObserver overrides:
void WillChangeVisibilityState(ShelfVisibilityState new_state) override;
......@@ -68,11 +63,13 @@ class ASH_EXPORT ShelfTooltipManager : public ui::EventHandler,
// A helper function to check for shelf visibility and view validity.
bool ShouldShowTooltipForView(views::View* view);
// A helper function to close the tooltip on mouse and touch press events.
void ProcessPressedEvent(const ui::LocatedEvent& event);
int timer_delay_;
base::OneShotTimer timer_;
ShelfView* shelf_view_;
ShelfTooltipBubbleBase* bubble_;
ShelfTooltipBubbleBase* bubble_ = nullptr;
base::WeakPtrFactory<ShelfTooltipManager> weak_factory_;
......
......@@ -9,6 +9,7 @@
#include "ash/public/cpp/shelf_model.h"
#include "ash/shelf/app_list_button.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_tooltip_bubble_base.h"
#include "ash/shelf/shelf_view.h"
#include "ash/shelf/shelf_view_test_api.h"
#include "ash/shell.h"
......@@ -122,10 +123,6 @@ TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsAutoHideHidden) {
ASSERT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS,
GetPrimaryShelf()->auto_hide_behavior());
ASSERT_EQ(SHELF_AUTO_HIDE_HIDDEN, GetPrimaryShelf()->GetAutoHideState());
// Tooltip visibility change for auto hide may take time.
EXPECT_TRUE(tooltip_manager_->IsVisible());
RunAllPendingInMessageLoop();
EXPECT_FALSE(tooltip_manager_->IsVisible());
// Do not show the view if the shelf is hidden.
......
......@@ -1793,12 +1793,6 @@ void ShelfView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
node_data->SetName(l10n_util::GetStringUTF8(IDS_ASH_SHELF_ACCESSIBLE_NAME));
}
void ShelfView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (details.is_add && details.child == this)
tooltip_.Init();
}
void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
// Do not forward events to |shelf_| (which forwards events to the shelf
// layout manager) as we do not want gestures on the overflow to open the app
......
......@@ -392,8 +392,6 @@ class ASH_EXPORT ShelfView : public views::View,
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
FocusTraversable* GetPaneFocusTraversable() override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override;
// Overridden from ui::EventHandler:
void OnGestureEvent(ui::GestureEvent* event) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment