Home | History | Annotate | Download | only in corewm
      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/client/tooltip_client.h"
     14 #include "ui/aura/window_observer.h"
     15 #include "ui/base/events/event_handler.h"
     16 #include "ui/gfx/point.h"
     17 #include "ui/gfx/screen_type_delegate.h"
     18 #include "ui/views/views_export.h"
     19 
     20 namespace aura {
     21 class Window;
     22 }
     23 
     24 namespace views {
     25 namespace corewm {
     26 
     27 namespace test {
     28 class TooltipControllerTestHelper;
     29 }  // namespace test
     30 
     31 // TooltipController provides tooltip functionality for aura shell.
     32 class VIEWS_EXPORT TooltipController : public aura::client::TooltipClient,
     33                                        public ui::EventHandler,
     34                                        public aura::WindowObserver {
     35  public:
     36   explicit TooltipController(gfx::ScreenType screen_type);
     37   virtual ~TooltipController();
     38 
     39   // Overridden from aura::client::TooltipClient.
     40   virtual void UpdateTooltip(aura::Window* target) OVERRIDE;
     41   virtual void SetTooltipShownTimeout(aura::Window* target,
     42                                       int timeout_in_ms) OVERRIDE;
     43   virtual void SetTooltipsEnabled(bool enable) OVERRIDE;
     44 
     45   // Overridden from ui::EventHandler.
     46   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
     47   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
     48   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
     49   virtual void OnCancelMode(ui::CancelModeEvent* event) OVERRIDE;
     50 
     51   // Overridden from aura::WindowObserver.
     52   virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
     53 
     54   const gfx::Point& mouse_location() const { return curr_mouse_loc_; }
     55 
     56  private:
     57   friend class test::TooltipControllerTestHelper;
     58 
     59   class Tooltip;
     60 
     61   // Returns the max width of the tooltip when shown at the specified location.
     62   int GetMaxWidth(const gfx::Point& location) const;
     63 
     64   // Returns the bounds to fit the tooltip in.
     65   gfx::Rect GetBoundsForTooltip(const gfx::Point& origin) const;
     66 
     67   // Trims the tooltip to fit in the width |max_width|, setting |text| to the
     68   // clipped result, |width| to the width (in pixels) of the clipped text
     69   // and |line_count| to the number of lines of text in the tooltip. |x| and |y|
     70   // give the location of the tooltip in screen coordinates. |max_width| comes
     71   // from GetMaxWidth().
     72   static void TrimTooltipToFit(int max_width,
     73                                string16* text,
     74                                int* width,
     75                                int* line_count);
     76 
     77   void TooltipTimerFired();
     78   void TooltipShownTimerFired();
     79 
     80   // Updates the tooltip if required (if there is any change in the tooltip
     81   // text or the aura::Window.
     82   void UpdateIfRequired();
     83 
     84   // Only used in tests.
     85   bool IsTooltipVisible();
     86 
     87   bool IsDragDropInProgress();
     88 
     89   // This lazily creates the Tooltip instance so that the tooltip window will
     90   // be initialized with appropriate drop shadows.
     91   Tooltip* GetTooltip();
     92 
     93   // Returns true if the cursor is visible.
     94   bool IsCursorVisible();
     95 
     96   int GetTooltipShownTimeout();
     97 
     98   const gfx::ScreenType screen_type_;
     99 
    100   aura::Window* tooltip_window_;
    101   string16 tooltip_text_;
    102 
    103   // These fields are for tracking state when the user presses a mouse button.
    104   aura::Window* tooltip_window_at_mouse_press_;
    105   string16 tooltip_text_at_mouse_press_;
    106   bool mouse_pressed_;
    107 
    108   scoped_ptr<Tooltip> tooltip_;
    109 
    110   base::RepeatingTimer<TooltipController> tooltip_timer_;
    111 
    112   // Timer to timeout the life of an on-screen tooltip. We hide the tooltip when
    113   // this timer fires.
    114   base::OneShotTimer<TooltipController> tooltip_shown_timer_;
    115 
    116   gfx::Point curr_mouse_loc_;
    117 
    118   bool tooltips_enabled_;
    119 
    120   std::map<aura::Window*, int> tooltip_shown_timeout_map_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(TooltipController);
    123 };
    124 
    125 }  // namespace corewm
    126 }  // namespace views
    127 
    128 #endif  // UI_VIEWS_COREWM_TOOLTIP_CONTROLLER_H_
    129