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
|
// Copyright 2019 The Chromium Authors
// 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/tab_strip_layout_helper.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <set>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_style.h"
#include "chrome/browser/ui/tabs/tab_types.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_group_header.h"
#include "chrome/browser/ui/views/tabs/tab_layout_state.h"
#include "chrome/browser/ui/views/tabs/tab_slot_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip_layout_types.h"
#include "chrome/browser/ui/views/tabs/tab_style_views.h"
#include "components/tabs/public/split_tab_id.h"
#include "tab_container_controller.h"
#include "ui/gfx/range/range.h"
#include "ui/views/view_model.h"
struct TabStripLayoutHelper::TabSlot {
static TabStripLayoutHelper::TabSlot CreateForTabSlotView(TabSlotView* view,
TabPinned pinned) {
TabStripLayoutHelper::TabSlot slot;
slot.type = view->GetTabSlotViewType();
slot.view = view;
slot.state = TabLayoutState(TabOpen::kOpen, pinned, TabActive::kInactive,
view->split());
return slot;
}
TabSlotView::ViewType type;
raw_ptr<TabSlotView, DanglingUntriaged> view;
TabLayoutState state;
};
TabStripLayoutHelper::TabStripLayoutHelper(
const TabContainerController& controller,
GetTabsCallback get_tabs_callback)
: controller_(controller),
get_tabs_callback_(get_tabs_callback),
tab_strip_layout_domain_(LayoutDomain::kInactiveWidthEqualsActiveWidth) {}
TabStripLayoutHelper::~TabStripLayoutHelper() = default;
std::vector<Tab*> TabStripLayoutHelper::GetTabs() const {
std::vector<Tab*> tabs;
for (const TabSlot& slot : slots_) {
if (slot.type == TabSlotView::ViewType::kTab) {
tabs.push_back(static_cast<Tab*>(slot.view));
}
}
return tabs;
}
std::vector<TabSlotView*> TabStripLayoutHelper::GetTabSlotViews() const {
std::vector<TabSlotView*> views;
for (const TabSlot& slot : slots_) {
views.push_back(slot.view);
}
return views;
}
size_t TabStripLayoutHelper::GetPinnedTabCount() const {
size_t pinned_count = 0;
for (const TabSlot& slot : slots_) {
if (slot.state.pinned() == TabPinned::kPinned && !slot.state.IsClosed()) {
pinned_count++;
}
}
return pinned_count;
}
void TabStripLayoutHelper::InsertTabAt(int model_index,
Tab* tab,
TabPinned pinned) {
const int slot_index =
GetSlotInsertionIndexForNewTab(model_index, tab->group());
slots_.insert(slots_.begin() + slot_index,
TabSlot::CreateForTabSlotView(tab, pinned));
}
void TabStripLayoutHelper::MarkTabAsClosing(int model_index, Tab* tab) {
const int slot_index = GetSlotIndexForExistingTab(model_index);
slots_[slot_index].state.set_open(TabOpen::kClosed);
}
void TabStripLayoutHelper::RemoveTab(Tab* tab) {
auto it = std::ranges::find_if(slots_, [tab](const TabSlot& slot) {
return slot.type == TabSlotView::ViewType::kTab && slot.view == tab;
});
if (it != slots_.end()) {
slots_.erase(it);
}
}
void TabStripLayoutHelper::MoveTab(
std::optional<tab_groups::TabGroupId> moving_tab_group,
int prev_index,
int new_index) {
const int prev_slot_index = GetSlotIndexForExistingTab(prev_index);
TabSlot moving_tab = slots_[prev_slot_index];
slots_.erase(slots_.begin() + prev_slot_index);
const int new_slot_index =
GetSlotInsertionIndexForNewTab(new_index, moving_tab_group);
slots_.insert(slots_.begin() + new_slot_index, moving_tab);
if (moving_tab_group.has_value()) {
UpdateGroupHeaderIndex(moving_tab_group.value());
}
}
void TabStripLayoutHelper::SetTabPinned(int model_index, TabPinned pinned) {
const int slot_index = GetSlotIndexForExistingTab(model_index);
slots_[slot_index].state.set_pinned(pinned);
}
void TabStripLayoutHelper::InsertGroupHeader(tab_groups::TabGroupId group,
TabGroupHeader* header) {
gfx::Range tabs_in_group = controller_->ListTabsInGroup(group);
const int header_slot_index =
GetSlotInsertionIndexForNewTab(tabs_in_group.start(), group);
slots_.insert(slots_.begin() + header_slot_index,
TabSlot::CreateForTabSlotView(header, TabPinned::kUnpinned));
// Set the starting location of the header to something reasonable for the
// animation.
slots_[header_slot_index].view->SetBoundsRect(
GetTabs()[tabs_in_group.start()]->bounds());
}
void TabStripLayoutHelper::RemoveGroupHeader(tab_groups::TabGroupId group) {
const int slot_index = GetSlotIndexForGroupHeader(group);
slots_.erase(slots_.begin() + slot_index);
}
void TabStripLayoutHelper::UpdateGroupHeaderIndex(
tab_groups::TabGroupId group) {
if (!GetFirstTabSlotForGroup(group).has_value()) {
return;
}
const int slot_index = GetSlotIndexForGroupHeader(group);
TabSlot header_slot = slots_[slot_index];
slots_.erase(slots_.begin() + slot_index);
// Recalculate the first tab index after removing the header as it
// helps with the header insertion index calculation.
std::optional<int> first_tab = GetFirstTabSlotForGroup(group);
slots_.insert(slots_.begin() + first_tab.value(), header_slot);
}
void TabStripLayoutHelper::SetActiveTab(
std::optional<size_t> prev_active_index,
std::optional<size_t> new_active_index) {
if (prev_active_index.has_value()) {
const int prev_slot_index =
GetSlotIndexForExistingTab(prev_active_index.value());
slots_[prev_slot_index].state.set_active(TabActive::kInactive);
// We are assuming that when a tabs active state is changed, it can't be in
// the middle of a move or something that would make them non-contiguous.
std::optional<int> adjacent_split = GetAdjacentSplitTab(prev_slot_index);
if (adjacent_split.has_value()) {
slots_[adjacent_split.value()].state.set_active(TabActive::kInactive);
}
}
if (new_active_index.has_value()) {
const int new_slot_index =
GetSlotIndexForExistingTab(new_active_index.value());
slots_[new_slot_index].state.set_active(TabActive::kActive);
std::optional<int> adjacent_split = GetAdjacentSplitTab(new_slot_index);
if (adjacent_split.has_value()) {
slots_[adjacent_split.value()].state.set_active(TabActive::kActive);
}
}
}
int TabStripLayoutHelper::CalculateMinimumWidth() {
auto [bounds, layout_domain] = CalculateIdealBounds(0);
return bounds.empty() ? 0 : bounds.back().right();
}
int TabStripLayoutHelper::CalculatePreferredWidth() {
auto [bounds, layout_domain] = CalculateIdealBounds(std::nullopt);
return bounds.empty() ? 0 : bounds.back().right();
}
int TabStripLayoutHelper::UpdateIdealBounds(int available_width) {
auto [bounds, layout_domain] = CalculateIdealBounds(available_width);
DCHECK_EQ(slots_.size(), bounds.size());
tab_strip_layout_domain_ = layout_domain;
views::ViewModelT<Tab>* tabs = get_tabs_callback_.Run();
const std::optional<int> active_tab_model_index =
controller_->GetActiveIndex();
const std::optional<int> active_tab_slot_index =
active_tab_model_index.has_value()
? std::optional<int>(
GetSlotIndexForExistingTab(active_tab_model_index.value()))
: std::nullopt;
// Store the active split (if applicable) for determining whether other tabs
// in the split should be active.
std::optional<split_tabs::SplitTabId> active_split_id = std::nullopt;
if (active_tab_slot_index.has_value()) {
const TabSlot& active_slot = slots_[active_tab_slot_index.value()];
active_split_id = active_slot.view->split();
}
int current_tab_model_index = 0;
for (int i = 0; i < static_cast<int>(bounds.size()); ++i) {
const TabSlot& slot = slots_[i];
switch (slot.type) {
case TabSlotView::ViewType::kTab:
if (!slot.state.IsClosed()) {
tabs->set_ideal_bounds(current_tab_model_index, bounds[i]);
++current_tab_model_index;
}
break;
case TabSlotView::ViewType::kTabGroupHeader:
group_header_ideal_bounds_[slot.view->group().value()] = bounds[i];
break;
}
}
return bounds.back().right();
}
std::pair<std::vector<gfx::Rect>, LayoutDomain>
TabStripLayoutHelper::CalculateIdealBounds(std::optional<int> available_width) {
const std::optional<int> active_tab_model_index =
controller_->GetActiveIndex();
const std::optional<int> active_tab_slot_index =
active_tab_model_index.has_value()
? std::optional<int>(
GetSlotIndexForExistingTab(active_tab_model_index.value()))
: std::nullopt;
const std::optional<int> active_split_tab_slot_index =
active_tab_slot_index.has_value()
? GetAdjacentSplitTab(active_tab_model_index.value())
: std::nullopt;
const int pinned_tab_count = GetPinnedTabCount();
const std::optional<int> last_pinned_tab_slot_index =
pinned_tab_count > 0
? std::optional<int>(GetSlotIndexForExistingTab(pinned_tab_count - 1))
: std::nullopt;
std::vector<TabWidthConstraints> tab_widths;
for (int i = 0; i < static_cast<int>(slots_.size()); i++) {
auto active =
(i == active_tab_slot_index || i == active_split_tab_slot_index)
? TabActive::kActive
: TabActive::kInactive;
auto pinned = last_pinned_tab_slot_index.has_value() &&
i <= last_pinned_tab_slot_index
? TabPinned::kPinned
: TabPinned::kUnpinned;
// A collapsed tab animates closed like a closed tab.
auto open = (slots_[i].state.IsClosed() || SlotIsCollapsedTab(i))
? TabOpen::kClosed
: TabOpen::kOpen;
TabLayoutState state =
TabLayoutState(open, pinned, active, slots_[i].view->split());
TabSizeInfo size_info = slots_[i].view->GetTabSizeInfo();
tab_widths.emplace_back(state, size_info);
}
return CalculateTabBounds(tab_widths, available_width);
}
int TabStripLayoutHelper::GetSlotIndexForExistingTab(int model_index) const {
const int original_slot_index =
GetFirstSlotIndexForTabModelIndex(model_index);
CHECK_LT(original_slot_index, static_cast<int>(slots_.size()))
<< "model_index = " << model_index
<< " does not represent an existing tab";
int slot_index = original_slot_index;
if (slots_[slot_index].type == TabSlotView::ViewType::kTab) {
CHECK(!slots_[slot_index].state.IsClosed());
return slot_index;
}
// If `slot_index` is a group header we must return the next slot that
// is not animating closed.
if (slots_[slot_index].type == TabSlotView::ViewType::kTabGroupHeader) {
// Skip all slots animating closed.
do {
slot_index += 1;
} while (slot_index < static_cast<int>(slots_.size()) &&
slots_[slot_index].state.IsClosed());
// Double check we arrived at a tab.
CHECK_LT(slot_index, static_cast<int>(slots_.size()))
<< "group header at " << original_slot_index
<< " not followed by an open tab";
CHECK_EQ(slots_[slot_index].type, TabSlotView::ViewType::kTab);
}
return slot_index;
}
int TabStripLayoutHelper::GetSlotInsertionIndexForNewTab(
int new_model_index,
std::optional<tab_groups::TabGroupId> group) const {
int slot_index = GetFirstSlotIndexForTabModelIndex(new_model_index);
if (slot_index == static_cast<int>(slots_.size())) {
return slot_index;
}
// If `slot_index` points to a group header and the new tab's `group`
// matches, the tab goes to the right of the header to keep it
// contiguous.
if (slots_[slot_index].type == TabSlotView::ViewType::kTabGroupHeader &&
static_cast<const TabGroupHeader*>(slots_[slot_index].view)->group() ==
group) {
return slot_index + 1;
}
return slot_index;
}
std::optional<int> TabStripLayoutHelper::GetFirstTabSlotForGroup(
tab_groups::TabGroupId group) const {
for (int slot_index = 0; slot_index < static_cast<int>(slots_.size());
++slot_index) {
if (slots_[slot_index].state.IsClosed()) {
continue;
}
if (slots_[slot_index].type == TabSlotView::ViewType::kTab &&
slots_[slot_index].view->group().has_value() &&
slots_[slot_index].view->group().value() == group) {
return slot_index;
}
}
return std::nullopt;
}
int TabStripLayoutHelper::GetFirstSlotIndexForTabModelIndex(
int model_index) const {
int current_model_index = 0;
// Conceptually we assign a model index to each slot equal to the
// number of open tabs preceeding it. Group headers will have the same
// index as the tab before it, and each open tab will have the index
// of the previous slot plus 1. Closing tabs are not counted, and are
// skipped altogether.
//
// We simply return the first slot that has a matching model index.
for (int slot_index = 0; slot_index < static_cast<int>(slots_.size());
++slot_index) {
if (slots_[slot_index].state.IsClosed()) {
continue;
}
if (model_index == current_model_index) {
return slot_index;
}
if (slots_[slot_index].type == TabSlotView::ViewType::kTab) {
current_model_index += 1;
}
}
// If there's no slot in `slots_` corresponding to `model_index`, then
// `model_index` may represent the first tab past the end of the
// tabstrip. In this case we should return the first-past-the-end
// index in `slots_`.
CHECK_EQ(current_model_index, model_index) << "model_index is too large";
return slots_.size();
}
int TabStripLayoutHelper::GetSlotIndexForGroupHeader(
tab_groups::TabGroupId group) const {
const auto it = std::ranges::find_if(slots_, [group](const auto& slot) {
return slot.type == TabSlotView::ViewType::kTabGroupHeader &&
static_cast<TabGroupHeader*>(slot.view)->group() == group;
});
CHECK(it != slots_.end());
return it - slots_.begin();
}
std::optional<int> TabStripLayoutHelper::GetAdjacentSplitTab(
int tab_index) const {
const std::optional<split_tabs::SplitTabId> split_id =
slots_[tab_index].view->split();
if (!split_id.has_value()) {
return std::nullopt;
} else if (tab_index > 0 && slots_[tab_index - 1].view->split() == split_id) {
return tab_index - 1;
} else if (tab_index < static_cast<int>(slots_.size()) - 1 &&
slots_[tab_index + 1].view->split() == split_id) {
return tab_index + 1;
}
return std::nullopt;
}
bool TabStripLayoutHelper::SlotIsCollapsedTab(int i) const {
// The slot can only be collapsed if it is a tab and in a collapsed group.
// If the slot is indeed a tab and in a group, check the collapsed state of
// the group to determine if it is collapsed.
const std::optional<tab_groups::TabGroupId> id = slots_[i].view->group();
return slots_[i].type == TabSlotView::ViewType::kTab && id.has_value() &&
controller_->IsGroupCollapsed(id.value());
}
|