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
|
// Copyright (c) 2011 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/tabs/tab_strip_selection_model.h"
#include <algorithm>
#include <valarray>
#include "base/logging.h"
// static
const int TabStripSelectionModel::kUnselectedIndex = -1;
static void IncrementFromImpl(int index, int* value) {
if (*value >= index)
(*value)++;
}
static bool DecrementFromImpl(int index, int* value) {
if (*value == index) {
*value = TabStripSelectionModel::kUnselectedIndex;
return true;
}
if (*value > index)
(*value)--;
return false;
}
TabStripSelectionModel::TabStripSelectionModel()
: active_(kUnselectedIndex),
anchor_(kUnselectedIndex) {
}
TabStripSelectionModel::~TabStripSelectionModel() {
}
void TabStripSelectionModel::IncrementFrom(int index) {
// Shift the selection to account for the newly inserted tab.
for (SelectedIndices::iterator i = selected_indices_.begin();
i != selected_indices_.end(); ++i) {
IncrementFromImpl(index, &(*i));
}
IncrementFromImpl(index, &anchor_);
IncrementFromImpl(index, &active_);
}
void TabStripSelectionModel::DecrementFrom(int index) {
for (SelectedIndices::iterator i = selected_indices_.begin();
i != selected_indices_.end(); ) {
if (DecrementFromImpl(index, &(*i)))
i = selected_indices_.erase(i);
else
++i;
}
DecrementFromImpl(index, &anchor_);
DecrementFromImpl(index, &active_);
}
void TabStripSelectionModel::SetSelectedIndex(int index) {
anchor_ = active_ = index;
SetSelectionFromAnchorTo(index);
}
bool TabStripSelectionModel::IsSelected(int index) const {
return std::find(selected_indices_.begin(), selected_indices_.end(), index) !=
selected_indices_.end();
}
void TabStripSelectionModel::AddIndexToSelection(int index) {
if (!IsSelected(index)) {
selected_indices_.push_back(index);
std::sort(selected_indices_.begin(), selected_indices_.end());
}
}
void TabStripSelectionModel::RemoveIndexFromSelection(int index) {
SelectedIndices::iterator i = std::find(selected_indices_.begin(),
selected_indices_.end(), index);
if (i != selected_indices_.end())
selected_indices_.erase(i);
}
void TabStripSelectionModel::SetSelectionFromAnchorTo(int index) {
if (anchor_ == kUnselectedIndex) {
SetSelectedIndex(index);
} else {
int delta = std::abs(index - anchor_);
SelectedIndices new_selection(delta + 1, 0);
for (int i = 0, min = std::min(index, anchor_); i <= delta; ++i)
new_selection[i] = i + min;
selected_indices_.swap(new_selection);
active_ = index;
}
}
void TabStripSelectionModel::AddSelectionFromAnchorTo(int index) {
if (anchor_ == kUnselectedIndex) {
SetSelectedIndex(index);
} else {
for (int i = std::min(index, anchor_), end = std::max(index, anchor_);
i <= end; ++i) {
if (!IsSelected(i))
selected_indices_.push_back(i);
}
std::sort(selected_indices_.begin(), selected_indices_.end());
active_ = index;
}
}
void TabStripSelectionModel::Move(int from, int to) {
DCHECK_NE(to, from);
bool was_anchor = from == anchor_;
bool was_active = from == active_;
bool was_selected = IsSelected(from);
if (to < from) {
IncrementFrom(to);
DecrementFrom(from + 1);
} else {
DecrementFrom(from);
IncrementFrom(to);
}
if (was_active)
active_ = to;
if (was_anchor)
anchor_ = to;
if (was_selected)
AddIndexToSelection(to);
}
void TabStripSelectionModel::Clear() {
anchor_ = active_ = kUnselectedIndex;
SelectedIndices empty_selection;
selected_indices_.swap(empty_selection);
}
void TabStripSelectionModel::Copy(const TabStripSelectionModel& source) {
selected_indices_ = source.selected_indices_;
active_ = source.active_;
anchor_ = source.anchor_;
}
|