Home | History | Annotate | Download | only in aura
      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_AURA_WINDOW_TREE_HOST_H_
      6 #define UI_AURA_WINDOW_TREE_HOST_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/event_types.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "ui/aura/aura_export.h"
     13 #include "ui/base/cursor/cursor.h"
     14 #include "ui/events/event_source.h"
     15 #include "ui/gfx/native_widget_types.h"
     16 
     17 namespace gfx {
     18 class Insets;
     19 class Point;
     20 class Rect;
     21 class Size;
     22 class Transform;
     23 }
     24 
     25 namespace ui {
     26 class Compositor;
     27 class EventProcessor;
     28 class ViewProp;
     29 }
     30 
     31 namespace aura {
     32 namespace test {
     33 class WindowTreeHostTestApi;
     34 }
     35 
     36 class WindowEventDispatcher;
     37 class WindowTreeHostObserver;
     38 
     39 // WindowTreeHost bridges between a native window and the embedded RootWindow.
     40 // It provides the accelerated widget and maps events from the native os to
     41 // aura.
     42 class AURA_EXPORT WindowTreeHost {
     43  public:
     44   virtual ~WindowTreeHost();
     45 
     46   // Creates a new WindowTreeHost. The caller owns the returned value.
     47   static WindowTreeHost* Create(const gfx::Rect& bounds);
     48 
     49   // Returns the WindowTreeHost for the specified accelerated widget, or NULL
     50   // if there is none associated.
     51   static WindowTreeHost* GetForAcceleratedWidget(gfx::AcceleratedWidget widget);
     52 
     53   void InitHost();
     54 
     55   void InitCompositor();
     56 
     57   void AddObserver(WindowTreeHostObserver* observer);
     58   void RemoveObserver(WindowTreeHostObserver* observer);
     59 
     60   Window* window() { return window_; }
     61   const Window* window() const { return window_; }
     62 
     63   ui::EventProcessor* event_processor();
     64 
     65   WindowEventDispatcher* dispatcher() {
     66     return const_cast<WindowEventDispatcher*>(
     67         const_cast<const WindowTreeHost*>(this)->dispatcher());
     68   }
     69   const WindowEventDispatcher* dispatcher() const { return dispatcher_.get(); }
     70 
     71   ui::Compositor* compositor() { return compositor_.get(); }
     72 
     73   // Gets/Sets the root window's transform.
     74   virtual gfx::Transform GetRootTransform() const;
     75   virtual void SetRootTransform(const gfx::Transform& transform);
     76   virtual gfx::Transform GetInverseRootTransform() const;
     77 
     78   // Updates the root window's size using |host_size|, current
     79   // transform and insets.
     80   virtual void UpdateRootWindowSize(const gfx::Size& host_size);
     81 
     82   // Returns the actual size of the screen.
     83   // (gfx::Screen only reports on the virtual desktop exposed by Aura.)
     84   static gfx::Size GetNativeScreenSize();
     85 
     86   // Converts |point| from the root window's coordinate system to native
     87   // screen's.
     88   void ConvertPointToNativeScreen(gfx::Point* point) const;
     89 
     90   // Converts |point| from native screen coordinate system to the root window's.
     91   void ConvertPointFromNativeScreen(gfx::Point* point) const;
     92 
     93   // Converts |point| from the root window's coordinate system to the
     94   // host window's.
     95   void ConvertPointToHost(gfx::Point* point) const;
     96 
     97   // Converts |point| from the host window's coordinate system to the
     98   // root window's.
     99   void ConvertPointFromHost(gfx::Point* point) const;
    100 
    101   // Cursor.
    102   // Sets the currently-displayed cursor. If the cursor was previously hidden
    103   // via ShowCursor(false), it will remain hidden until ShowCursor(true) is
    104   // called, at which point the cursor that was last set via SetCursor() will be
    105   // used.
    106   void SetCursor(gfx::NativeCursor cursor);
    107 
    108   // Invoked when the cursor's visibility has changed.
    109   void OnCursorVisibilityChanged(bool visible);
    110 
    111   // Moves the cursor to the specified location relative to the root window.
    112   void MoveCursorTo(const gfx::Point& location);
    113 
    114   // Moves the cursor to the |host_location| given in host coordinates.
    115   void MoveCursorToHostLocation(const gfx::Point& host_location);
    116 
    117   gfx::NativeCursor last_cursor() const { return last_cursor_; }
    118 
    119   // Returns the EventSource responsible for dispatching events to the window
    120   // tree.
    121   virtual ui::EventSource* GetEventSource() = 0;
    122 
    123   // Returns the accelerated widget.
    124   virtual gfx::AcceleratedWidget GetAcceleratedWidget() = 0;
    125 
    126   // Shows the WindowTreeHost.
    127   virtual void Show() = 0;
    128 
    129   // Hides the WindowTreeHost.
    130   virtual void Hide() = 0;
    131 
    132   // Gets/Sets the size of the WindowTreeHost.
    133   virtual gfx::Rect GetBounds() const = 0;
    134   virtual void SetBounds(const gfx::Rect& bounds) = 0;
    135 
    136   // Sets the OS capture to the root window.
    137   virtual void SetCapture() = 0;
    138 
    139   // Releases OS capture of the root window.
    140   virtual void ReleaseCapture() = 0;
    141 
    142   // Posts |native_event| to the platform's event queue.
    143   virtual void PostNativeEvent(const base::NativeEvent& native_event) = 0;
    144 
    145   // Called when the device scale factor of the root window has chagned.
    146   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) = 0;
    147 
    148  protected:
    149   friend class TestScreen;  // TODO(beng): see if we can remove/consolidate.
    150 
    151   WindowTreeHost();
    152   void DestroyCompositor();
    153   void DestroyDispatcher();
    154 
    155   void CreateCompositor(gfx::AcceleratedWidget accelerated_widget);
    156 
    157   // Returns the location of the RootWindow on native screen.
    158   virtual gfx::Point GetLocationOnNativeScreen() const = 0;
    159 
    160   void OnHostMoved(const gfx::Point& new_location);
    161   void OnHostResized(const gfx::Size& new_size);
    162   void OnHostCloseRequested();
    163   void OnHostActivated();
    164   void OnHostLostWindowCapture();
    165 
    166   // Sets the currently displayed cursor.
    167   virtual void SetCursorNative(gfx::NativeCursor cursor) = 0;
    168 
    169   // Moves the cursor to the specified location relative to the root window.
    170   virtual void MoveCursorToNative(const gfx::Point& location) = 0;
    171 
    172   // kCalled when the cursor visibility has changed.
    173   virtual void OnCursorVisibilityChangedNative(bool show) = 0;
    174 
    175  private:
    176   friend class test::WindowTreeHostTestApi;
    177 
    178   // Moves the cursor to the specified location. This method is internally used
    179   // by MoveCursorTo() and MoveCursorToHostLocation().
    180   void MoveCursorToInternal(const gfx::Point& root_location,
    181                             const gfx::Point& host_location);
    182 
    183   // We don't use a scoped_ptr for |window_| since we need this ptr to be valid
    184   // during its deletion. (Window's dtor notifies observers that may attempt to
    185   // reach back up to access this object which will be valid until the end of
    186   // the dtor).
    187   Window* window_;  // Owning.
    188 
    189   ObserverList<WindowTreeHostObserver> observers_;
    190 
    191   scoped_ptr<WindowEventDispatcher> dispatcher_;
    192 
    193   scoped_ptr<ui::Compositor> compositor_;
    194 
    195   // Last cursor set.  Used for testing.
    196   gfx::NativeCursor last_cursor_;
    197   gfx::Point last_cursor_request_position_in_host_;
    198 
    199   scoped_ptr<ui::ViewProp> prop_;
    200 
    201   DISALLOW_COPY_AND_ASSIGN(WindowTreeHost);
    202 };
    203 
    204 }  // namespace aura
    205 
    206 #endif  // UI_AURA_WINDOW_TREE_HOST_H_
    207