1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/tabs/touch_tab_strip_layout.h"
#include <stdio.h>
#include "base/logging.h"
#include "base/string_number_conversions.h"
TouchTabStripLayout::TouchTabStripLayout(const gfx::Size& size,
int padding,
int stacked_padding,
int max_stacked_count,
views::ViewModel* view_model)
: size_(size),
padding_(padding),
stacked_padding_(stacked_padding),
max_stacked_count_(max_stacked_count),
view_model_(view_model),
x_(0),
width_(0),
mini_tab_count_(0),
active_index_(-1) {
}
TouchTabStripLayout::~TouchTabStripLayout() {
}
void TouchTabStripLayout::SetXAndMiniCount(int x, int mini_tab_count) {
x_ = x;
mini_tab_count_ = mini_tab_count;
if (!requires_stacking() || tab_count() == mini_tab_count) {
ResetToIdealState();
return;
}
SetIdealBoundsAt(active_index(), ConstrainActiveX(ideal_x(active_index())));
LayoutByTabOffsetAfter(active_index());
LayoutByTabOffsetBefore(active_index());
}
void TouchTabStripLayout::SetWidth(int width) {
if (width_ == width)
return;
width_ = width;
if (!requires_stacking()) {
ResetToIdealState();
return;
}
SetActiveBoundsAndLayoutFromActiveTab();
}
void TouchTabStripLayout::SetActiveIndex(int index) {
int old = active_index();
active_index_ = index;
if (old == active_index() || !requires_stacking())
return;
SetIdealBoundsAt(active_index(), ConstrainActiveX(ideal_x(active_index())));
LayoutByTabOffsetBefore(active_index());
LayoutByTabOffsetAfter(active_index());
AdjustStackedTabs();
}
void TouchTabStripLayout::DragActiveTab(int delta) {
if (delta == 0 || !requires_stacking())
return;
int initial_x = ideal_x(active_index());
// If we're at a particular edge and start dragging, reset to ideal state.
if ((delta > 0 && initial_x == GetMinX(active_index())) ||
(delta < 0 && initial_x == GetMaxX(active_index()))) {
ResetToIdealState();
}
int x = delta > 0 ?
std::min(initial_x + delta, GetMaxX(active_index())) :
std::max(initial_x + delta, GetMinX(active_index()));
if (x != initial_x) {
SetIdealBoundsAt(active_index(), x);
LayoutByTabOffsetAfter(active_index());
LayoutByTabOffsetBefore(active_index());
delta -= (x - initial_x);
}
if (delta > 0)
ExpandTabsBefore(active_index(), delta);
else if (delta < 0)
ExpandTabsAfter(active_index(), -delta);
AdjustStackedTabs();
}
void TouchTabStripLayout::AddTab(int index,
int add_types,
int start_x) {
if (add_types & kAddTypeActive)
active_index_ = index;
else if (active_index_ >= index)
active_index_++;
if (add_types & kAddTypeMini)
mini_tab_count_++;
x_ = start_x;
if (!requires_stacking() || normal_tab_count() <= 1) {
ResetToIdealState();
return;
}
int active_x = (index + 1 == tab_count()) ?
width_ - size_.width() : ideal_x(index + 1);
SetIdealBoundsAt(active_index(), ConstrainActiveX(active_x));
LayoutByTabOffsetAfter(active_index());
LayoutByTabOffsetBefore(active_index());
AdjustStackedTabs();
if ((add_types & kAddTypeActive) == 0)
MakeVisible(index);
}
void TouchTabStripLayout::RemoveTab(int index, int start_x, int old_x) {
if (index == active_index_)
active_index_ = std::min(active_index_, tab_count() - 1);
else if (index < active_index_)
active_index_--;
bool removed_mini_tab = index < mini_tab_count_;
if (removed_mini_tab) {
mini_tab_count_--;
DCHECK_GE(mini_tab_count_, 0);
}
int delta = start_x - x_;
x_ = start_x;
if (!requires_stacking()) {
ResetToIdealState();
return;
}
if (removed_mini_tab) {
for (int i = mini_tab_count_; i < tab_count(); ++i)
SetIdealBoundsAt(i, ideal_x(i) + delta);
}
SetActiveBoundsAndLayoutFromActiveTab();
AdjustStackedTabs();
}
void TouchTabStripLayout::MoveTab(int from,
int to,
int new_active_index,
int start_x,
int mini_tab_count) {
x_ = start_x;
mini_tab_count_ = mini_tab_count;
active_index_ = new_active_index;
if (!requires_stacking() || tab_count() == mini_tab_count_) {
ResetToIdealState();
return;
}
SetIdealBoundsAt(active_index(), ConstrainActiveX(ideal_x(active_index())));
LayoutByTabOffsetAfter(active_index());
LayoutByTabOffsetBefore(active_index());
AdjustStackedTabs();
}
bool TouchTabStripLayout::IsStacked(int index) const {
if (index == active_index() || tab_count() == mini_tab_count_ ||
index < mini_tab_count_)
return false;
if (index > active_index())
return ideal_x(index) != ideal_x(index - 1) + tab_offset();
return ideal_x(index + 1) != ideal_x(index) + tab_offset();
}
#if !defined(NDEBUG)
std::string TouchTabStripLayout::BoundsString() const {
std::string result;
for (int i = 0; i < view_model_->view_size(); ++i) {
if (!result.empty())
result += " ";
if (i == active_index())
result += "[";
result += base::IntToString(view_model_->ideal_bounds(i).x());
if (i == active_index())
result += "]";
}
return result;
}
#endif
void TouchTabStripLayout::Reset(int x,
int width,
int mini_tab_count,
int active_index) {
x_ = x;
width_ = width;
mini_tab_count_ = mini_tab_count;
active_index_ = active_index;
ResetToIdealState();
}
void TouchTabStripLayout::ResetToIdealState() {
if (tab_count() == mini_tab_count_)
return;
if (!requires_stacking()) {
SetIdealBoundsAt(mini_tab_count_, x_);
LayoutByTabOffsetAfter(mini_tab_count_);
return;
}
if (normal_tab_count() == 1) {
// TODO: might want to shrink the tab here.
SetIdealBoundsAt(mini_tab_count_, 0);
return;
}
int available_width = width_ - x_;
int leading_count = active_index() - mini_tab_count_;
int trailing_count = tab_count() - active_index();
if (width_for_count(leading_count + 1) + max_stacked_width() <
available_width) {
SetIdealBoundsAt(mini_tab_count_, x_);
LayoutByTabOffsetAfter(mini_tab_count_);
} else if (width_for_count(trailing_count) + max_stacked_width() <
available_width) {
SetIdealBoundsAt(tab_count() - 1, width_ - size_.width());
LayoutByTabOffsetBefore(tab_count() - 1);
} else {
int index = active_index();
do {
int stacked_padding = stacked_padding_for_count(index - mini_tab_count_);
SetIdealBoundsAt(index, x_ + stacked_padding);
LayoutByTabOffsetAfter(index);
LayoutByTabOffsetBefore(index);
index--;
} while (index >= mini_tab_count_ && ideal_x(mini_tab_count_) != x_ &&
ideal_x(tab_count() - 1) != width_ - size_.width());
}
AdjustStackedTabs();
}
void TouchTabStripLayout::MakeVisible(int index) {
// Currently no need to support tabs openning before |index| visible.
if (index <= active_index() || !requires_stacking() || !IsStacked(index))
return;
int ideal_delta = width_for_count(index - active_index()) + padding_;
if (ideal_x(index) - ideal_x(active_index()) == ideal_delta)
return;
// First push active index as far to the left as it'll go.
int active_x = std::max(GetMinX(active_index()),
std::min(ideal_x(index) - ideal_delta,
ideal_x(active_index())));
SetIdealBoundsAt(active_index(), active_x);
LayoutUsingCurrentBefore(active_index());
LayoutUsingCurrentAfter(active_index());
AdjustStackedTabs();
if (ideal_x(index) - ideal_x(active_index()) == ideal_delta)
return;
// If we get here active_index() is left aligned. Push |index| as far to
// the right as possible.
int x = std::min(GetMaxX(index), active_x + ideal_delta);
SetIdealBoundsAt(index, x);
LayoutByTabOffsetAfter(index);
for (int next_x = x, i = index - 1; i > active_index(); --i) {
next_x = std::max(GetMinXCompressed(i), next_x - tab_offset());
SetIdealBoundsAt(i, next_x);
}
LayoutUsingCurrentAfter(active_index());
AdjustStackedTabs();
}
int TouchTabStripLayout::ConstrainActiveX(int x) const {
return std::min(GetMaxX(active_index()),
std::max(GetMinX(active_index()), x));
}
void TouchTabStripLayout::SetActiveBoundsAndLayoutFromActiveTab() {
int x = ConstrainActiveX(ideal_x(active_index()));
SetIdealBoundsAt(active_index(), x);
LayoutUsingCurrentBefore(active_index());
LayoutUsingCurrentAfter(active_index());
AdjustStackedTabs();
}
void TouchTabStripLayout::LayoutByTabOffsetAfter(int index) {
for (int i = index + 1; i < tab_count(); ++i) {
int max_x = width_ - size_.width() -
stacked_padding_for_count(tab_count() - i - 1);
int x = std::min(max_x,
view_model_->ideal_bounds(i - 1).x() + tab_offset());
SetIdealBoundsAt(i, x);
}
}
void TouchTabStripLayout::LayoutByTabOffsetBefore(int index) {
for (int i = index - 1; i >= mini_tab_count_; --i) {
int min_x = x_ + stacked_padding_for_count(i - mini_tab_count_);
int x = std::max(min_x, ideal_x(i + 1) - (tab_offset()));
SetIdealBoundsAt(i, x);
}
}
void TouchTabStripLayout::LayoutUsingCurrentAfter(int index) {
for (int i = index + 1; i < tab_count(); ++i) {
int min_x = width_ - width_for_count(tab_count() - i);
int x = std::max(min_x,
std::min(ideal_x(i), ideal_x(i - 1) + tab_offset()));
x = std::min(GetMaxX(i), x);
SetIdealBoundsAt(i, x);
}
}
void TouchTabStripLayout::LayoutUsingCurrentBefore(int index) {
for (int i = index - 1; i >= mini_tab_count_; --i) {
int max_x = x_ + width_for_count(i - mini_tab_count_);
if (i > mini_tab_count_)
max_x += padding_;
max_x = std::min(max_x, ideal_x(i + 1) - stacked_padding_);
SetIdealBoundsAt(
i, std::min(max_x,
std::max(ideal_x(i), ideal_x(i + 1) - tab_offset())));
}
}
void TouchTabStripLayout::ExpandTabsBefore(int index, int delta) {
if (index == mini_tab_count_ + 1)
return; // Nothing to expand.
for (int i = index - 1; i > mini_tab_count_ && delta > 0; --i) {
int to_resize = std::min(delta, GetMaxXCompressed(i) - ideal_x(i));
if (to_resize <= 0)
continue;
SetIdealBoundsAt(i, ideal_x(i) + to_resize);
delta -= to_resize;
LayoutByTabOffsetBefore(i);
}
}
void TouchTabStripLayout::ExpandTabsAfter(int index, int delta) {
if (index == tab_count() - 1)
return; // Nothing to expand.
for (int i = index + 1; i < tab_count() - 1 && delta > 0; ++i) {
int to_resize = std::min(ideal_x(i) - GetMinXCompressed(i), delta);
if (to_resize <= 0)
continue;
SetIdealBoundsAt(i, ideal_x(i) - to_resize);
delta -= to_resize;
LayoutByTabOffsetAfter(i);
}
}
void TouchTabStripLayout::AdjustStackedTabs() {
if (!requires_stacking() || tab_count() <= mini_tab_count_ + 1)
return;
AdjustLeadingStackedTabs();
AdjustTrailingStackedTabs();
}
void TouchTabStripLayout::AdjustLeadingStackedTabs() {
int index = mini_tab_count_ + 1;
while (index < active_index() &&
ideal_x(index) - ideal_x(index - 1) <= stacked_padding_ &&
ideal_x(index) <= x_ + max_stacked_width()) {
index++;
}
if (ideal_x(index) - ideal_x(index - 1) <= stacked_padding_ &&
ideal_x(index) <= x_ + max_stacked_width()) {
index++;
}
if (index <= mini_tab_count_ + max_stacked_count_ - 1)
return;
int max_stacked = index;
int x = x_;
index = mini_tab_count_;
for (; index < max_stacked - max_stacked_count_ - 1; ++index)
SetIdealBoundsAt(index, x);
for (; index < max_stacked; ++index, x += stacked_padding_)
SetIdealBoundsAt(index, x);
}
void TouchTabStripLayout::AdjustTrailingStackedTabs() {
int index = tab_count() - 1;
int max_stacked_x = width_ - size_.width() - max_stacked_width();
while (index > active_index() &&
ideal_x(index) - ideal_x(index - 1) <= stacked_padding_ &&
ideal_x(index - 1) >= max_stacked_x) {
index--;
}
if (index > active_index() &&
ideal_x(index) - ideal_x(index - 1) <= stacked_padding_ &&
ideal_x(index - 1) >= max_stacked_x) {
index--;
}
if (index >= tab_count() - max_stacked_count_)
return;
int first_stacked = index;
int x = width_ - size_.width() -
std::min(tab_count() - first_stacked, max_stacked_count_) *
stacked_padding_;
for (; index < first_stacked + max_stacked_count_;
++index, x += stacked_padding_) {
SetIdealBoundsAt(index, x);
}
for (; index < tab_count(); ++index)
SetIdealBoundsAt(index, x);
}
void TouchTabStripLayout::SetIdealBoundsAt(int index, int x) {
view_model_->set_ideal_bounds(index, gfx::Rect(gfx::Point(x, 0), size_));
}
int TouchTabStripLayout::GetMinX(int index) const {
int leading_count = index - mini_tab_count_;
int trailing_count = tab_count() - index;
return std::max(x_ + stacked_padding_for_count(leading_count),
width_ - width_for_count(trailing_count));
}
int TouchTabStripLayout::GetMaxX(int index) const {
int leading_count = index - mini_tab_count_;
int trailing_count = tab_count() - index - 1;
int trailing_offset = stacked_padding_for_count(trailing_count);
int leading_size = width_for_count(leading_count) + x_;
if (leading_count > 0)
leading_size += padding_;
return std::min(width_ - trailing_offset - size_.width(), leading_size);
}
int TouchTabStripLayout::GetMaxXCompressed(int index) const {
DCHECK_LT(index, active_index());
DCHECK_GT(index, mini_tab_count_);
int trailing = active_index() - index;
return std::min(
x_ + width_for_count(index - mini_tab_count_) + padding_,
ideal_x(active_index()) - stacked_padding_for_count(trailing));
}
int TouchTabStripLayout::GetMinXCompressed(int index) const {
DCHECK_GT(index, active_index());
return std::max(
width_ - width_for_count(tab_count() - index),
ideal_x(active_index()) +
stacked_padding_for_count(index - active_index()));
}
|