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.
5 #ifndef UI_VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_
6 #define UI_VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_
8 #include "base/gtest_prod_util.h"
9 #include "ui/views/view.h"
13 class SingleSplitViewListener
;
15 // SingleSplitView lays out two views next to each other, either horizontally
16 // or vertically. A splitter exists between the two views that the user can
17 // drag around to resize the views.
18 // SingleSplitViewListener's SplitHandleMoved notification helps to monitor user
19 // initiated layout changes.
20 class VIEWS_EXPORT SingleSplitView
: public View
{
27 static const char kViewClassName
[];
29 SingleSplitView(View
* leading
,
31 Orientation orientation
,
32 SingleSplitViewListener
* listener
);
34 void Layout() override
;
35 const char* GetClassName() const override
;
37 void GetAccessibleState(ui::AXViewState
* state
) override
;
39 // SingleSplitView's preferred size is the sum of the preferred widths
40 // and the max of the heights.
41 gfx::Size
GetPreferredSize() const override
;
43 // Overriden to return a resize cursor when over the divider.
44 gfx::NativeCursor
GetCursor(const ui::MouseEvent
& event
) override
;
46 Orientation
orientation() const {
47 return is_horizontal_
? HORIZONTAL_SPLIT
: VERTICAL_SPLIT
;
50 void set_orientation(Orientation orientation
) {
51 is_horizontal_
= orientation
== HORIZONTAL_SPLIT
;
54 void set_divider_offset(int divider_offset
) {
55 divider_offset_
= divider_offset
;
57 int divider_offset() const { return divider_offset_
; }
59 int GetDividerSize() const;
61 void set_resize_disabled(bool resize_disabled
) {
62 resize_disabled_
= resize_disabled
;
64 bool is_resize_disabled() const { return resize_disabled_
; }
66 // Sets whether the leading component is resized when the split views size
67 // changes. The default is true. A value of false results in the trailing
68 // component resizing on a bounds change.
69 void set_resize_leading_on_bounds_change(bool resize
) {
70 resize_leading_on_bounds_change_
= resize
;
73 // Calculates ideal leading and trailing view bounds according to the given
74 // split view |bounds|, current divider offset and children visiblity.
75 // Does not change children view bounds.
76 void CalculateChildrenBounds(const gfx::Rect
& bounds
,
77 gfx::Rect
* leading_bounds
,
78 gfx::Rect
* trailing_bounds
) const;
80 void SetAccessibleName(const base::string16
& name
);
84 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
85 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
86 void OnMouseCaptureLost() override
;
87 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
90 // This test calls OnMouse* functions.
91 FRIEND_TEST_ALL_PREFIXES(SingleSplitViewTest
, MouseDrag
);
93 // Returns true if |x| or |y| is over the divider.
94 bool IsPointInDivider(const gfx::Point
& p
);
96 // Calculates the new |divider_offset| based on the changes of split view
98 int CalculateDividerOffset(int divider_offset
,
99 const gfx::Rect
& previous_bounds
,
100 const gfx::Rect
& new_bounds
) const;
102 // Returns divider offset within primary axis size range for given split
104 int NormalizeDividerOffset(int divider_offset
, const gfx::Rect
& bounds
) const;
106 // Returns width in case of horizontal split and height otherwise.
107 int GetPrimaryAxisSize() const {
108 return GetPrimaryAxisSize(width(), height());
111 int GetPrimaryAxisSize(int h
, int v
) const {
112 return is_horizontal_
? h
: v
;
115 // Used to track drag info.
117 // The initial coordinate of the mouse when the user started the drag.
118 int initial_mouse_offset
;
119 // The initial position of the divider when the user started the drag.
120 int initial_divider_offset
;
125 // Orientation of the split view.
128 // Position of the divider.
131 bool resize_leading_on_bounds_change_
;
133 // Whether resizing is disabled.
134 bool resize_disabled_
;
136 // Listener to notify about user initiated handle movements. Not owned.
137 SingleSplitViewListener
* listener_
;
139 // The accessible name of this view.
140 base::string16 accessible_name_
;
142 DISALLOW_COPY_AND_ASSIGN(SingleSplitView
);
147 #endif // UI_VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_