Home | History | Annotate | Download | only in wm
      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 ASH_WM_WINDOW_RESIZER_H_
      6 #define ASH_WM_WINDOW_RESIZER_H_
      7 
      8 #include "ash/ash_export.h"
      9 #include "base/basictypes.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "ui/aura/client/window_move_client.h"
     12 #include "ui/gfx/rect.h"
     13 
     14 namespace aura {
     15 class Window;
     16 }
     17 
     18 namespace ash {
     19 
     20 // WindowResizer is used by ToplevelWindowEventFilter to handle dragging, moving
     21 // or resizing a window. All coordinates passed to this are in the parent
     22 // windows coordinates.
     23 class ASH_EXPORT WindowResizer {
     24  public:
     25   // Constants to identify the type of resize.
     26   static const int kBoundsChange_None;
     27   static const int kBoundsChange_Repositions;
     28   static const int kBoundsChange_Resizes;
     29 
     30   // Used to indicate which direction the resize occurs in.
     31   static const int kBoundsChangeDirection_None;
     32   static const int kBoundsChangeDirection_Horizontal;
     33   static const int kBoundsChangeDirection_Vertical;
     34 
     35   WindowResizer();
     36   virtual ~WindowResizer();
     37 
     38   // Returns a bitmask of the kBoundsChange_ values.
     39   static int GetBoundsChangeForWindowComponent(int component);
     40 
     41   // Invoked to drag/move/resize the window. |location| is in the coordinates
     42   // of the window supplied to the constructor. |event_flags| is the event
     43   // flags from the event.
     44   virtual void Drag(const gfx::Point& location, int event_flags) = 0;
     45 
     46   // Invoked to complete the drag.
     47   virtual void CompleteDrag(int event_flags) = 0;
     48 
     49   // Reverts the drag.
     50   virtual void RevertDrag() = 0;
     51 
     52   // Returns the target window the resizer was created for.
     53   virtual aura::Window* GetTarget() = 0;
     54 
     55   // See comment for |Details::initial_location_in_parent|.
     56   virtual const gfx::Point& GetInitialLocation() const = 0;
     57 
     58  protected:
     59   struct Details {
     60     Details();
     61     Details(aura::Window* window,
     62             const gfx::Point& location,
     63             int window_component,
     64             aura::client::WindowMoveSource source);
     65     ~Details();
     66 
     67     // The window we're resizing.
     68     aura::Window* window;
     69 
     70     // Initial bounds of the window in parent coordinates.
     71     gfx::Rect initial_bounds_in_parent;
     72 
     73     // Restore bounds (in screen coordinates) of the window before the drag
     74     // started. Only set if the window is normal and is being dragged.
     75     gfx::Rect restore_bounds;
     76 
     77     // Location passed to the constructor, in |window->parent()|'s coordinates.
     78     gfx::Point initial_location_in_parent;
     79 
     80     // Initial opacity of the window.
     81     float initial_opacity;
     82 
     83     // The component the user pressed on.
     84     int window_component;
     85 
     86     // Bitmask of the |kBoundsChange_| constants.
     87     int bounds_change;
     88 
     89     // Bitmask of the |kBoundsChangeDirection_| constants.
     90     int position_change_direction;
     91 
     92     // Bitmask of the |kBoundsChangeDirection_| constants.
     93     int size_change_direction;
     94 
     95     // Will the drag actually modify the window?
     96     bool is_resizable;
     97 
     98     // Source of the event initiating the drag.
     99     aura::client::WindowMoveSource source;
    100   };
    101 
    102   static gfx::Rect CalculateBoundsForDrag(const Details& details,
    103                                           const gfx::Point& location);
    104 
    105   static gfx::Rect AdjustBoundsToGrid(const gfx::Rect& bounds,
    106                                       int grid_size);
    107 
    108   static bool IsBottomEdge(int component);
    109 
    110  private:
    111   // In case of touch resizing, adjusts deltas so that the border is positioned
    112   // just under the touch point.
    113   static void AdjustDeltaForTouchResize(const Details& details,
    114                                         int* delta_x,
    115                                         int* delta_y);
    116 
    117   // Returns the new origin of the window. The arguments are the difference
    118   // between the current location and the initial location.
    119   static gfx::Point GetOriginForDrag(const Details& details,
    120                                      int delta_x,
    121                                      int delta_y);
    122 
    123   // Returns the size of the window for the drag.
    124   static gfx::Size GetSizeForDrag(const Details& details,
    125                                   int* delta_x,
    126                                   int* delta_y);
    127 
    128   // Returns the width of the window.
    129   static int GetWidthForDrag(const Details& details,
    130                              int min_width,
    131                              int* delta_x);
    132 
    133   // Returns the height of the drag.
    134   static int GetHeightForDrag(const Details& details,
    135                               int min_height,
    136                               int* delta_y);
    137 };
    138 
    139 // Creates a WindowResizer for |window|. This can return a scoped_ptr
    140 // initialized with NULL if |window| should not be resized nor dragged.
    141 ASH_EXPORT scoped_ptr<WindowResizer> CreateWindowResizer(
    142     aura::Window* window,
    143     const gfx::Point& point_in_parent,
    144     int window_component,
    145     aura::client::WindowMoveSource source);
    146 
    147 }  // namespace ash
    148 
    149 #endif  // ASH_WM_WINDOW_RESIZER_H_
    150