Home | History | Annotate | Download | only in workspace
      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_WORKSPACE_SNAP_SIZER_H_
      6 #define ASH_WM_WORKSPACE_SNAP_SIZER_H_
      7 
      8 #include <vector>
      9 
     10 #include "ash/ash_export.h"
     11 #include "base/basictypes.h"
     12 #include "base/time/time.h"
     13 #include "ui/gfx/rect.h"
     14 
     15 namespace aura {
     16 class Window;
     17 }
     18 
     19 namespace ash {
     20 namespace internal {
     21 
     22 // SnapSizer is responsible for determining the resulting bounds of a window
     23 // that is being snapped to the left or right side of the screen.
     24 // The bounds used in this class are in the container's coordinates.
     25 class ASH_EXPORT SnapSizer {
     26  public:
     27   enum Edge {
     28     LEFT_EDGE,
     29     RIGHT_EDGE
     30   };
     31 
     32   enum InputType {
     33     TOUCH_MAXIMIZE_BUTTON_INPUT,
     34     OTHER_INPUT
     35   };
     36 
     37   // Set |input_type| to |TOUCH_MAXIMIZE_BUTTON_INPUT| when called by a touch
     38   // operation by the maximize button. This will allow the user to snap resize
     39   // the window beginning close to the border.
     40   SnapSizer(aura::Window* window,
     41             const gfx::Point& start,
     42             Edge edge,
     43             InputType input_type);
     44   virtual ~SnapSizer();
     45 
     46   // Snaps a window left or right.
     47   static void SnapWindow(aura::Window* window, Edge edge);
     48 
     49   // Updates the target bounds based on a mouse move.
     50   void Update(const gfx::Point& location);
     51 
     52   // Bounds to position the window at.
     53   const gfx::Rect& target_bounds() const { return target_bounds_; }
     54 
     55   // Returns the appropriate snap bounds (e.g. if a window is already snapped,
     56   // then it returns the next snap-bounds).
     57   gfx::Rect GetSnapBounds(const gfx::Rect& bounds);
     58 
     59   // Set the snap sizer to the button press default size and prevent resizing.
     60   void SelectDefaultSizeAndDisableResize();
     61 
     62   // Returns the target bounds based on the edge and the provided |size_index|.
     63   // For unit test purposes this function is not private.
     64   gfx::Rect GetTargetBoundsForSize(size_t size_index) const;
     65 
     66  private:
     67   // Calculates the amount to increment by. This returns one of -1, 0 or 1 and
     68   // is intended to by applied to |size_index_|. |x| is the current
     69   // x-coordinate, and |reference_x| is used to determine whether to increase
     70   // or decrease the position. It's one of |last_adjust_x_| or |last_update_x_|.
     71   int CalculateIncrement(int x, int reference_x) const;
     72 
     73   // Changes the bounds. |x| is the current x-coordinate and |delta| the amount
     74   // to increase by. |delta| comes from CalculateIncrement() and is applied
     75   // to |size_index_|.
     76   void ChangeBounds(int x, int delta);
     77 
     78   // Returns the target bounds based on the edge and |size_index_|.
     79   gfx::Rect GetTargetBounds() const;
     80 
     81   // Returns true if the specified point is along the edge of the screen.
     82   bool AlongEdge(int x) const;
     83 
     84   // Window being snapped.
     85   aura::Window* window_;
     86 
     87   const Edge edge_;
     88 
     89   // Current target bounds for the snap.
     90   gfx::Rect target_bounds_;
     91 
     92   // Time Update() was last invoked.
     93   base::TimeTicks time_last_update_;
     94 
     95   // Index into |kSizes| that dictates the width of the screen the target
     96   // bounds should get.
     97   int size_index_;
     98 
     99   // If set, |size_index_| will get ignored and the single button default
    100   // setting will be used instead.
    101   bool resize_disabled_;
    102 
    103   // Number of times Update() has been invoked since last ChangeBounds().
    104   int num_moves_since_adjust_;
    105 
    106   // X-coordinate the last time ChangeBounds() was invoked.
    107   int last_adjust_x_;
    108 
    109   // X-coordinate last supplied to Update().
    110   int last_update_x_;
    111 
    112   // Initial x-coordinate.
    113   const int start_x_;
    114 
    115   // |TOUCH_MAXIMIZE_BUTTON_INPUT| if the snap sizer was created through a
    116   // touch & drag operation of the maximizer button. It changes the behavior of
    117   // the drag / resize behavior when the dragging starts close to the border.
    118   const InputType input_type_;
    119 
    120   // A list of usable window widths for size. This gets created when the
    121   // sizer gets created.
    122   const std::vector<int> usable_width_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(SnapSizer);
    125 };
    126 
    127 }  // namespace internal
    128 }  // namespace ash
    129 
    130 #endif  // ASH_WM_WORKSPACE_SNAP_SIZER_H_
    131