shelf_tooltip_manager.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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
  3. // found in the LICENSE file.
  4. #ifndef ASH_SHELF_SHELF_TOOLTIP_MANAGER_H_
  5. #define ASH_SHELF_SHELF_TOOLTIP_MANAGER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/shelf/shelf_observer.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/timer/timer.h"
  10. #include "ui/events/event_handler.h"
  11. namespace ui {
  12. class LocatedEvent;
  13. }
  14. namespace views {
  15. class View;
  16. }
  17. namespace ash {
  18. class ShelfBubble;
  19. class Shelf;
  20. class ShelfTooltipDelegate;
  21. // ShelfTooltipManager manages the tooltip bubble that appears for shelf items.
  22. class ASH_EXPORT ShelfTooltipManager : public ui::EventHandler,
  23. public ShelfObserver {
  24. public:
  25. explicit ShelfTooltipManager(Shelf* shelf);
  26. ShelfTooltipManager(const ShelfTooltipManager&) = delete;
  27. ShelfTooltipManager& operator=(const ShelfTooltipManager&) = delete;
  28. ~ShelfTooltipManager() override;
  29. // Closes the tooltip; uses an animation if |animate| is true.
  30. void Close(bool animate = true);
  31. // Returns true if the tooltip is currently visible.
  32. bool IsVisible() const;
  33. // Returns the view to which the tooltip bubble is anchored. May be null.
  34. views::View* GetCurrentAnchorView() const;
  35. // Show the tooltip bubble for the specified view.
  36. void ShowTooltip(views::View* view);
  37. void ShowTooltipWithDelay(views::View* view);
  38. // Set the timer delay in ms for testing.
  39. void set_timer_delay_for_test(int timer_delay) { timer_delay_ = timer_delay; }
  40. void set_shelf_tooltip_delegate(
  41. ShelfTooltipDelegate* shelf_tooltip_delegate) {
  42. DCHECK(!shelf_tooltip_delegate_ || !shelf_tooltip_delegate);
  43. shelf_tooltip_delegate_ = shelf_tooltip_delegate;
  44. }
  45. protected:
  46. // ui::EventHandler overrides:
  47. void OnMouseEvent(ui::MouseEvent* event) override;
  48. void OnTouchEvent(ui::TouchEvent* event) override;
  49. void OnScrollEvent(ui::ScrollEvent* event) override;
  50. void OnKeyEvent(ui::KeyEvent* event) override;
  51. // ShelfObserver overrides:
  52. void WillChangeVisibilityState(ShelfVisibilityState new_state) override;
  53. void OnAutoHideStateChanged(ShelfAutoHideState new_state) override;
  54. private:
  55. friend class ShelfViewTest;
  56. friend class ShelfTooltipManagerTest;
  57. // A helper function to check for shelf visibility and view validity.
  58. bool ShouldShowTooltipForView(views::View* view);
  59. // A helper function to close the tooltip on mouse and touch press events.
  60. void ProcessPressedEvent(const ui::LocatedEvent& event);
  61. int timer_delay_;
  62. base::OneShotTimer timer_;
  63. Shelf* shelf_ = nullptr;
  64. ShelfBubble* bubble_ = nullptr;
  65. ShelfTooltipDelegate* shelf_tooltip_delegate_ = nullptr;
  66. base::WeakPtrFactory<ShelfTooltipManager> weak_factory_{this};
  67. };
  68. } // namespace ash
  69. #endif // ASH_SHELF_SHELF_TOOLTIP_MANAGER_H_