| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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_CONTROLS_SINGLE_SPLIT_VIEW_H_ | |
| 6 #define UI_VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ | |
| 7 | |
| 8 #include "base/gtest_prod_util.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "ui/views/view.h" | |
| 11 | |
| 12 namespace views { | |
| 13 | |
| 14 class SingleSplitViewListener; | |
| 15 | |
| 16 // SingleSplitView lays out two views next to each other, either horizontally | |
| 17 // or vertically. A splitter exists between the two views that the user can | |
| 18 // drag around to resize the views. | |
| 19 // SingleSplitViewListener's SplitHandleMoved notification helps to monitor user | |
| 20 // initiated layout changes. | |
| 21 class VIEWS_EXPORT SingleSplitView : public View { | |
| 22 public: | |
| 23 enum Orientation { | |
| 24 HORIZONTAL_SPLIT, | |
| 25 VERTICAL_SPLIT | |
| 26 }; | |
| 27 | |
| 28 static const char kViewClassName[]; | |
| 29 | |
| 30 SingleSplitView(View* leading, | |
| 31 View* trailing, | |
| 32 Orientation orientation, | |
| 33 SingleSplitViewListener* listener); | |
| 34 | |
| 35 void Layout() override; | |
| 36 const char* GetClassName() const override; | |
| 37 | |
| 38 void GetAccessibleState(ui::AXViewState* state) override; | |
| 39 | |
| 40 // SingleSplitView's preferred size is the sum of the preferred widths | |
| 41 // and the max of the heights. | |
| 42 gfx::Size GetPreferredSize() const override; | |
| 43 | |
| 44 // Overriden to return a resize cursor when over the divider. | |
| 45 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override; | |
| 46 | |
| 47 Orientation orientation() const { | |
| 48 return is_horizontal_ ? HORIZONTAL_SPLIT : VERTICAL_SPLIT; | |
| 49 } | |
| 50 | |
| 51 void set_orientation(Orientation orientation) { | |
| 52 is_horizontal_ = orientation == HORIZONTAL_SPLIT; | |
| 53 } | |
| 54 | |
| 55 void set_divider_offset(int divider_offset) { | |
| 56 divider_offset_ = divider_offset; | |
| 57 } | |
| 58 int divider_offset() const { return divider_offset_; } | |
| 59 | |
| 60 int GetDividerSize() const; | |
| 61 | |
| 62 void set_resize_disabled(bool resize_disabled) { | |
| 63 resize_disabled_ = resize_disabled; | |
| 64 } | |
| 65 bool is_resize_disabled() const { return resize_disabled_; } | |
| 66 | |
| 67 // Sets whether the leading component is resized when the split views size | |
| 68 // changes. The default is true. A value of false results in the trailing | |
| 69 // component resizing on a bounds change. | |
| 70 void set_resize_leading_on_bounds_change(bool resize) { | |
| 71 resize_leading_on_bounds_change_ = resize; | |
| 72 } | |
| 73 | |
| 74 // Calculates ideal leading and trailing view bounds according to the given | |
| 75 // split view |bounds|, current divider offset and children visiblity. | |
| 76 // Does not change children view bounds. | |
| 77 void CalculateChildrenBounds(const gfx::Rect& bounds, | |
| 78 gfx::Rect* leading_bounds, | |
| 79 gfx::Rect* trailing_bounds) const; | |
| 80 | |
| 81 void SetAccessibleName(const base::string16& name); | |
| 82 | |
| 83 protected: | |
| 84 // View overrides. | |
| 85 bool OnMousePressed(const ui::MouseEvent& event) override; | |
| 86 bool OnMouseDragged(const ui::MouseEvent& event) override; | |
| 87 void OnMouseCaptureLost() override; | |
| 88 void OnBoundsChanged(const gfx::Rect& previous_bounds) override; | |
| 89 | |
| 90 private: | |
| 91 // This test calls OnMouse* functions. | |
| 92 FRIEND_TEST_ALL_PREFIXES(SingleSplitViewTest, MouseDrag); | |
| 93 | |
| 94 // Returns true if |x| or |y| is over the divider. | |
| 95 bool IsPointInDivider(const gfx::Point& p); | |
| 96 | |
| 97 // Calculates the new |divider_offset| based on the changes of split view | |
| 98 // bounds. | |
| 99 int CalculateDividerOffset(int divider_offset, | |
| 100 const gfx::Rect& previous_bounds, | |
| 101 const gfx::Rect& new_bounds) const; | |
| 102 | |
| 103 // Returns divider offset within primary axis size range for given split | |
| 104 // view |bounds|. | |
| 105 int NormalizeDividerOffset(int divider_offset, const gfx::Rect& bounds) const; | |
| 106 | |
| 107 // Returns width in case of horizontal split and height otherwise. | |
| 108 int GetPrimaryAxisSize() const { | |
| 109 return GetPrimaryAxisSize(width(), height()); | |
| 110 } | |
| 111 | |
| 112 int GetPrimaryAxisSize(int h, int v) const { | |
| 113 return is_horizontal_ ? h : v; | |
| 114 } | |
| 115 | |
| 116 // Used to track drag info. | |
| 117 struct DragInfo { | |
| 118 // The initial coordinate of the mouse when the user started the drag. | |
| 119 int initial_mouse_offset; | |
| 120 // The initial position of the divider when the user started the drag. | |
| 121 int initial_divider_offset; | |
| 122 }; | |
| 123 | |
| 124 DragInfo drag_info_; | |
| 125 | |
| 126 // Orientation of the split view. | |
| 127 bool is_horizontal_; | |
| 128 | |
| 129 // Position of the divider. | |
| 130 int divider_offset_; | |
| 131 | |
| 132 bool resize_leading_on_bounds_change_; | |
| 133 | |
| 134 // Whether resizing is disabled. | |
| 135 bool resize_disabled_; | |
| 136 | |
| 137 // Listener to notify about user initiated handle movements. Not owned. | |
| 138 SingleSplitViewListener* listener_; | |
| 139 | |
| 140 // The accessible name of this view. | |
| 141 base::string16 accessible_name_; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(SingleSplitView); | |
| 144 }; | |
| 145 | |
| 146 } // namespace views | |
| 147 | |
| 148 #endif // UI_VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ | |
| OLD | NEW |