File: tab_strip_model_selection_state.h

package info (click to toggle)
chromium 141.0.7390.65-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,246,236 kB
  • sloc: cpp: 35,264,825; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,623; asm: 950,788; xml: 751,717; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (90 lines) | stat: -rw-r--r-- 3,383 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_TABS_TAB_STRIP_MODEL_SELECTION_STATE_H_
#define CHROME_BROWSER_UI_TABS_TAB_STRIP_MODEL_SELECTION_STATE_H_

#include <unordered_set>

#include "base/memory/raw_ptr.h"

class TabStripModel;

namespace tabs {

class TabInterface;

// Stores the selection state for a TabStripModel. This is an internal
// implementation detail of TabStripModel and should not be used by other
// classes. It stores pointers to TabInterface objects, which are owned by
// TabStripModel's TabStripCollection.
class TabStripModelSelectionState final {
 public:
  TabStripModelSelectionState();
  TabStripModelSelectionState(
      std::unordered_set<raw_ptr<TabInterface>> selected_tabs,
      raw_ptr<TabInterface> active_tab,
      raw_ptr<TabInterface> anchor_tab);
  TabStripModelSelectionState(const TabStripModelSelectionState&) = delete;
  TabStripModelSelectionState& operator=(const TabStripModelSelectionState&) =
      delete;
  ~TabStripModelSelectionState();

  bool operator==(const TabStripModelSelectionState& other) const;

  TabInterface* active_tab() const { return active_tab_; }
  TabInterface* anchor_tab() const { return anchor_tab_; }
  const std::unordered_set<raw_ptr<TabInterface>>& selected_tabs() const {
    return selected_tabs_;
  }
  bool empty() const { return selected_tabs_.empty(); }
  size_t size() const { return selected_tabs_.size(); }

  // Returns whether the tab is in the selected_tabs_.
  bool IsSelected(TabInterface* tab) const;

  // Adds the tab to the selected_tabs_ if `tab` is non-null.
  void AddTabToSelection(TabInterface* tab);

  // Removes tabs from the selected_tabs_ if `tab` is non-null.
  void RemoveTabFromSelection(TabInterface* tab);

  // Sets the active tab to the given tab interface. If that tab is not part of
  // the selection model, then it's added to the list of selected tabs.
  void SetActiveTab(TabInterface* tab);

  // Sets the anchor tab to the given tab interface. If that tab is not part of
  // the selection model, then it's added to the list of selected tabs.
  void SetAnchorTab(TabInterface* tab);

  // Adds tabs to the selection model, does not update the active tab, or the
  // anchor tab.
  bool AppendTabsToSelection(std::unordered_set<TabInterface*> tabs);

  // Updates the set of selected tabs with the new TabInterface ptrs. If the
  // active/anchor tabs are provided, then they will be CHECKed to make sure
  // they're part of the new list of tabs. If not provided, then the active tab
  // and anchor tab will be set to the "first" tab in the set.
  void SetSelectedTabs(std::unordered_set<TabInterface*> tabs,
                       TabInterface* active_tab = nullptr,
                       TabInterface* anchor_tab = nullptr);

  // Returns true if the selection model has at least 1 selected tab, an anchor
  // and an active tab. Otherwise returns false.
  bool Valid();

 private:
  // The selected tabs in the tabstrip.
  std::unordered_set<raw_ptr<TabInterface>> selected_tabs_;

  // The active tab.
  raw_ptr<TabInterface> active_tab_ = nullptr;

  // The anchor tab for selection.
  raw_ptr<TabInterface> anchor_tab_ = nullptr;
};

}  // namespace tabs

#endif  // CHROME_BROWSER_UI_TABS_TAB_STRIP_MODEL_SELECTION_STATE_H_