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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_TABS_STACKED_TAB_STRIP_LAYOUT_H_
6 #define CHROME_BROWSER_UI_VIEWS_TABS_STACKED_TAB_STRIP_LAYOUT_H_
10 #include "base/basictypes.h"
11 #include "ui/gfx/geometry/size.h"
12 #include "ui/views/view_model.h"
16 // StackedTabStripLayout is used by TabStrip in touch
17 // mode. StackedTabStripLayout is responsible for managing the bounds of the
18 // tabs. StackedTabStripLayout differs from the normal layout in that it stacks
19 // tabs. Stacked tabs are tabs placed nearly on top of each other, and if enough
20 // consecutive stacked tabs exist they are placed on top of each other. Normally
21 // stacked tabs are placed after pinned tabs, or at the end of the tabstrip, but
22 // during dragging tabs may be stacked before or after the active tab.
23 class StackedTabStripLayout
{
25 static const int kAddTypePinned
= 1 << 0;
26 static const int kAddTypeActive
= 1 << 1;
28 // |size| is the size for tabs, |padding| the padding between consecutive
29 // tabs, |stacked_padding| the padding between stacked tabs,
30 // |max_stacked_count| the maximum number of consecutive tabs that can be
31 // stacked before they are placed on top of each other, |view_model| is the
32 // ViewModel the bounds of the tabs are placed in.
33 StackedTabStripLayout(const gfx::Size
& size
,
36 int max_stacked_count
,
37 views::ViewModelBase
* view_model
);
38 ~StackedTabStripLayout();
40 // Sets the x-coordinate the normal tabs start at as well as the pinned tab
41 // count. This is only useful if the pinned tab count or x-coordinate change.
42 void SetXAndPinnedCount(int x
, int pinned_tab_count
);
44 // Sets the width available for sizing the tabs to.
45 void SetWidth(int width
);
47 int width() const { return width_
; }
49 // Sets the index of the active tab.
50 void SetActiveIndex(int index
);
52 // Drags the active tab.
53 void DragActiveTab(int delta
);
55 // Makes sure the tabs fill the available width. Used after a drag operation
59 // Adds a new tab at the specified index. |add_types| is a bitmask of
60 // kAddType*. |start_x| is the new x-coordinate non-pinned tabs start at.
61 void AddTab(int index
, int add_types
, int start_x
);
63 // Removes the tab at the specified index. |start_x| is the new x-coordinate
64 // normal tabs start at, and |old_x| the old x-coordinate of the tab. It is
65 // expected that the ViewModel hash been updated at the time this is invoked.
66 void RemoveTab(int index
, int start_x
, int old_x
);
68 // Moves the tab from |from| to |to|. |new_active_index| is the index of the
69 // currently active tab.
70 void MoveTab(int from
,
74 int pinned_tab_count
);
76 // Returns the active index as used by this class. The active index dictates
77 // stacking and what tabs are visible. As pinned tabs are never stacked,
78 // StackedTabStripLayout forces the active index to be in the normal tabs.
79 int active_index() const {
80 return active_index_
< pinned_tab_count_
? pinned_tab_count_
84 int pinned_tab_count() const { return pinned_tab_count_
; }
86 // Returns true if the tab at index is stacked.
87 bool IsStacked(int index
) const;
89 // Sets the location of the active tab as close to |x| as possible.
90 void SetActiveTabLocation(int x
);
93 std::string
BoundsString() const;
97 friend class StackedTabStripLayoutTest
;
99 // Sets the x-coordinate normal tabs start at, width pinned tab count and
100 // active index at once.
101 void Reset(int x
, int width
, int pinned_tab_count
, int active_index
);
103 // Resets to an ideal layout state.
104 void ResetToIdealState();
106 // Makes |index| visible. This is used when a new tab is added that isn't
108 void MakeVisible(int index
);
110 // Returns the x-coordinate for the active tab constrained by the current tab
112 int ConstrainActiveX(int x
) const;
114 // Reset the bounds of the active tab (based on ConstrainActiveX()) and resets
115 // the bounds of the remaining tabs by way of LayoutUsingCurrent*.
116 void SetActiveBoundsAndLayoutFromActiveTab();
118 // Sets the bounds of the tabs after |index| relative to the position of the
119 // tab at |index|. Each tab is placed |tab_offset()| pixels after the previous
120 // tab, stacking as necessary.
121 void LayoutByTabOffsetAfter(int index
);
123 // Same as LayoutByTabOffsetAfter(), but iterates toward
124 // |pinned_tab_count_|.
125 void LayoutByTabOffsetBefore(int index
);
127 // Similar to LayoutByTabOffsetAfter(), but uses the current x-coordinate
129 void LayoutUsingCurrentAfter(int index
);
130 void LayoutUsingCurrentBefore(int index
);
132 void PushTabsAfter(int index
, int delta
);
133 void PushTabsBefore(int index
, int delta
);
135 // Does a layout for drag. Similar to LayoutUsingCurrentXXX() but does not
136 // contrain. Used when dragging the active tab.
137 void LayoutForDragAfter(int index
);
138 void LayoutForDragBefore(int index
);
140 // Used when the tabs are stacked at one side. The remaining tabs are stacked
141 // against the |active_index()|. |delta| is the amount of space to resize the
143 void ExpandTabsBefore(int index
, int delta
);
144 void ExpandTabsAfter(int index
, int delta
);
146 // Adjusts the stacked tabs so that if there are more than
147 // |max_stacked_count_| tabs, the set > max_stacked_count_ have an
148 // x-coordinate of |x_|. Similarly those at the end have the same x-coordinate
149 // and are pushed all the way to the right.
150 void AdjustStackedTabs();
151 void AdjustLeadingStackedTabs();
152 void AdjustTrailingStackedTabs();
154 // Sets the bounds of the tab at |index|.
155 void SetIdealBoundsAt(int index
, int x
);
157 // Returns the min x-coordinate for the sepcified index. This is calculated
158 // assuming all the tabs before |index| are stacked.
159 int GetMinX(int index
) const;
161 // Returns the max x-coordinate for the speficifed index. This is calculated
162 // assuming all the tabs after |index| are stacked.
163 int GetMaxX(int index
) const;
165 // Used when dragging to get the min/max coodinate.
166 int GetMinDragX(int index
) const;
167 int GetMaxDragX(int index
) const;
169 // Returns the min x-coordinate for the tab at |index|. This is relative
170 // to the |active_index()| and is only useful when the active tab is pushed
171 // against the left side.
172 int GetMinXCompressed(int index
) const;
174 // Width needed to display |count| tabs.
175 int width_for_count(int count
) const {
176 return (count
* size_
.width()) + (std::max(count
- 1, 0) * padding_
);
179 // Padding needed for |count| stacked tabs.
180 int stacked_padding_for_count(int count
) const {
181 return std::min(count
, max_stacked_count_
) * stacked_padding_
;
184 // Max stacked padding.
185 int max_stacked_width() const {
186 return stacked_padding_
* max_stacked_count_
;
189 int ideal_x(int index
) const { return view_model_
->ideal_bounds(index
).x(); }
191 // Returns true if some of the tabs need to be stacked.
192 bool requires_stacking() const {
193 return tab_count() != pinned_tab_count_
&&
194 x_
+ width_for_count(tab_count() - pinned_tab_count_
) > width_
;
198 int tab_count() const { return view_model_
->view_size(); }
200 // Number of normal (non-pinned) tabs.
201 int normal_tab_count() const { return tab_count() - pinned_tab_count_
; }
203 // Distance between one tab to the next.
204 int tab_offset() const { return size_
.width() + padding_
; }
207 const gfx::Size size_
;
209 // Padding between tabs.
212 // Padding between stacked tabs.
213 const int stacked_padding_
;
215 // Max number of stacked tabs.
216 const int max_stacked_count_
;
218 // Where bounds are placed. This is owned by TabStrip.
219 // (Note: This is a ViewModelBase, not a ViewModelT<Tab>, because the tests do
220 // not populate the model with Tab views.)
221 views::ViewModelBase
* view_model_
;
223 // x-coordinate normal tabs start at.
229 // Number of pinned tabs.
230 int pinned_tab_count_
;
232 // Distance from the last pinned tab to the first non-pinned tab.
233 int pinned_tab_to_non_pinned_tab_
;
235 // Index of the active tab.
238 // X-coordinate of the first tab. This is either |x_| if there are no
239 // pinned tabs, or the x-coordinate of the first pinned tab.
242 DISALLOW_COPY_AND_ASSIGN(StackedTabStripLayout
);
245 #endif // CHROME_BROWSER_UI_VIEWS_TABS_STACKED_TAB_STRIP_LAYOUT_H_