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#ifndef UI_VIEWS_COREWM_TOOLTIP_CONTROLLER_H_
6#define UI_VIEWS_COREWM_TOOLTIP_CONTROLLER_H_
7
8#include <map>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/strings/string16.h"
12#include "base/timer/timer.h"
13#include "ui/aura/window_observer.h"
14#include "ui/events/event_handler.h"
15#include "ui/gfx/point.h"
16#include "ui/views/views_export.h"
17#include "ui/wm/public/tooltip_client.h"
18
19namespace aura {
20class Window;
21}
22
23namespace views {
24namespace corewm {
25
26class Tooltip;
27
28namespace test {
29class TooltipControllerTestHelper;
30}  // namespace test
31
32// TooltipController provides tooltip functionality for aura.
33class VIEWS_EXPORT TooltipController : public aura::client::TooltipClient,
34                                       public ui::EventHandler,
35                                       public aura::WindowObserver {
36 public:
37  explicit TooltipController(scoped_ptr<Tooltip> tooltip);
38  virtual ~TooltipController();
39
40  // Overridden from aura::client::TooltipClient.
41  virtual void UpdateTooltip(aura::Window* target) OVERRIDE;
42  virtual void SetTooltipShownTimeout(aura::Window* target,
43                                      int timeout_in_ms) OVERRIDE;
44  virtual void SetTooltipsEnabled(bool enable) OVERRIDE;
45
46  // Overridden from ui::EventHandler.
47  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
48  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
49  virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
50  virtual void OnCancelMode(ui::CancelModeEvent* event) OVERRIDE;
51
52  // Overridden from aura::WindowObserver.
53  virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
54
55  const gfx::Point& mouse_location() const { return curr_mouse_loc_; }
56
57 private:
58  friend class test::TooltipControllerTestHelper;
59
60  void TooltipTimerFired();
61  void TooltipShownTimerFired();
62
63  // Updates the tooltip if required (if there is any change in the tooltip
64  // text, tooltip id or the aura::Window).
65  void UpdateIfRequired();
66
67  // Only used in tests.
68  bool IsTooltipVisible();
69
70  bool IsDragDropInProgress();
71
72  // Returns true if the cursor is visible.
73  bool IsCursorVisible();
74
75  int GetTooltipShownTimeout();
76
77  // Sets tooltip window to |target| if it is different from existing window.
78  // Calls RemoveObserver on the existing window if it is not NULL.
79  // Calls AddObserver on the new window if it is not NULL.
80  void SetTooltipWindow(aura::Window* target);
81
82  aura::Window* tooltip_window_;
83  base::string16 tooltip_text_;
84  const void* tooltip_id_;
85
86  // These fields are for tracking state when the user presses a mouse button.
87  aura::Window* tooltip_window_at_mouse_press_;
88  base::string16 tooltip_text_at_mouse_press_;
89
90  scoped_ptr<Tooltip> tooltip_;
91
92  base::RepeatingTimer<TooltipController> tooltip_timer_;
93
94  // Timer to timeout the life of an on-screen tooltip. We hide the tooltip when
95  // this timer fires.
96  base::OneShotTimer<TooltipController> tooltip_shown_timer_;
97
98  // Location of the last event in |tooltip_window_|'s coordinates.
99  gfx::Point curr_mouse_loc_;
100
101  bool tooltips_enabled_;
102
103  std::map<aura::Window*, int> tooltip_shown_timeout_map_;
104
105  DISALLOW_COPY_AND_ASSIGN(TooltipController);
106};
107
108}  // namespace corewm
109}  // namespace views
110
111#endif  // UI_VIEWS_COREWM_TOOLTIP_CONTROLLER_H_
112