File: pinned_action_toolbar_button_menu_model.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (281 lines) | stat: -rw-r--r-- 10,457 bytes parent folder | download | duplicates (3)
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
// 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.

#include "chrome/browser/ui/views/toolbar/pinned_action_toolbar_button_menu_model.h"

#include <optional>
#include <string>
#include <string_view>

#include "base/check.h"
#include "base/containers/fixed_flat_map.h"
#include "base/metrics/user_metrics.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/customize_chrome/side_panel_controller.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/tabs/public/tab_interface.h"
#include "ui/actions/action_id.h"
#include "ui/actions/action_utils.h"
#include "ui/actions/actions.h"
#include "ui/menus/simple_menu_model.h"

namespace {
// Returns true if the button pin state is managed through prefs instead of
// the PinnedToolbarActionsModel.
bool IsPinStateManagedByPrefs(actions::ActionId action_id) {
  return action_id == kActionSplitTab || action_id == kActionForward ||
         action_id == kActionHome;
}

// Helper function to get the pref name for a given action id.
// Only valid for actions that have their pin state managed by prefs.
std::string_view GetPrefForActionId(actions::ActionId action_id) {
  static constexpr base::fixed_flat_map<actions::ActionId, std::string_view, 3>
      action_id_to_pref =
          base::MakeFixedFlatMap<actions::ActionId, std::string_view>(
              {{kActionSplitTab, prefs::kPinSplitTabButton},
               {kActionForward, prefs::kShowForwardButton},
               {kActionHome, prefs::kShowHomeButton}});

  return action_id_to_pref.at(action_id);
}
}  // namespace

DEFINE_UI_CLASS_PROPERTY_KEY(actions::ActionId, kActionIdKey, -1)

PinnedActionToolbarButtonMenuModel::PinnedActionToolbarButtonMenuModel(
    BrowserWindowInterface* browser_interface,
    actions::ActionId action_id)
    : browser_(browser_interface), action_id_(action_id) {
  AddActionSpecificItems();
  // Add the pin/unpin and customize toolbar items.
  items_.emplace_back(kActionPinActionToToolbar, TYPE_COMMAND);
  items_.emplace_back(kActionUnpinActionFromToolbar, TYPE_COMMAND);
  items_.emplace_back(kActionSidePanelShowCustomizeChromeToolbar, TYPE_COMMAND);
}

PinnedActionToolbarButtonMenuModel::~PinnedActionToolbarButtonMenuModel() =
    default;

base::WeakPtr<ui::MenuModel> PinnedActionToolbarButtonMenuModel::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

size_t PinnedActionToolbarButtonMenuModel::GetItemCount() const {
  return items_.size();
}

ui::MenuModel::ItemType PinnedActionToolbarButtonMenuModel::GetTypeAt(
    size_t index) const {
  if (items_[index].type == TYPE_SEPARATOR) {
    return items_[index].type;
  }

  return GetActionItemFor(items_[index].action_id)->GetChecked()
             ? TYPE_CHECK
             : items_[index].type;
}

ui::MenuSeparatorType PinnedActionToolbarButtonMenuModel::GetSeparatorTypeAt(
    size_t index) const {
  return ui::NORMAL_SEPARATOR;
}

int PinnedActionToolbarButtonMenuModel::GetCommandIdAt(size_t index) const {
  return static_cast<int>(index);
}

std::u16string PinnedActionToolbarButtonMenuModel::GetLabelAt(
    size_t index) const {
  if (GetTypeAt(index) == TYPE_SEPARATOR) {
    return std::u16string();
  }

  return std::u16string(GetActionItemFor(items_[index].action_id)->GetText());
}

bool PinnedActionToolbarButtonMenuModel::IsItemDynamicAt(size_t index) const {
  return false;
}

bool PinnedActionToolbarButtonMenuModel::GetAcceleratorAt(
    size_t index,
    ui::Accelerator* accelerator) const {
  return false;
}

bool PinnedActionToolbarButtonMenuModel::IsItemCheckedAt(size_t index) const {
  if (GetTypeAt(index) == TYPE_SEPARATOR) {
    return false;
  }

  return GetActionItemFor(items_[index].action_id)->GetChecked();
}

int PinnedActionToolbarButtonMenuModel::GetGroupIdAt(size_t index) const {
  return false;
}

ui::ImageModel PinnedActionToolbarButtonMenuModel::GetIconAt(
    size_t index) const {
  if (GetTypeAt(index) == TYPE_SEPARATOR) {
    return ui::ImageModel();
  }

  const ui::ImageModel& image =
      GetActionItemFor(items_[index].action_id)->GetImage();
  // Update the icon size for a context menu if possible.
  if (!image.IsEmpty() && image.IsVectorIcon()) {
    ui::VectorIconModel vector_icon_model = image.GetVectorIcon();
    return ui::ImageModel::FromVectorIcon(
        *vector_icon_model.vector_icon(), vector_icon_model.color(),
        ui::SimpleMenuModel::kDefaultIconSize);
  }
  return image;
}

ui::ButtonMenuItemModel*
PinnedActionToolbarButtonMenuModel::GetButtonMenuItemAt(size_t index) const {
  return nullptr;
}

bool PinnedActionToolbarButtonMenuModel::IsEnabledAt(size_t index) const {
  if (GetTypeAt(index) == TYPE_SEPARATOR) {
    return true;
  }
  if (items_[index].action_id == kActionPinActionToToolbar ||
      items_[index].action_id == kActionUnpinActionFromToolbar) {
    const bool is_pinnable = IsPinnable();
    return browser_->GetProfile()->IsRegularProfile() && is_pinnable;
  }
  if (items_[index].action_id == kActionSidePanelShowCustomizeChromeToolbar) {
    // TODO(https://crbug.com/365591184) This check should become a static
    // method on customize chrome's side panel controller once the
    // abstract-base-class is removed and should be used to update the
    // ActionItem's enabled property.
    tabs::TabInterface* tab = browser_->GetTabStripModel()->GetActiveTab();
    customize_chrome::SidePanelController* side_panel_controller =
        tab->GetTabFeatures()->customize_chrome_side_panel_controller();
    return side_panel_controller &&
           side_panel_controller->IsCustomizeChromeEntryAvailable();
  }
  return GetActionItemFor(items_[index].action_id)->GetEnabled();
}

bool PinnedActionToolbarButtonMenuModel::IsVisibleAt(size_t index) const {
  if (GetTypeAt(index) == TYPE_SEPARATOR) {
    return true;
  }
  const bool is_pinned = IsPinned();
  if (is_pinned && items_[index].action_id == kActionPinActionToToolbar) {
    return false;
  }
  if (!is_pinned && items_[index].action_id == kActionUnpinActionFromToolbar) {
    return false;
  }
  return GetActionItemFor(items_[index].action_id)->GetVisible();
}

ui::MenuModel* PinnedActionToolbarButtonMenuModel::GetSubmenuModelAt(
    size_t index) const {
  return nullptr;
}

void PinnedActionToolbarButtonMenuModel::ActivatedAt(size_t index) {
  ActivatedAt(index, 0);
}

void PinnedActionToolbarButtonMenuModel::ActivatedAt(size_t index,
                                                     int event_flags) {
  DCHECK(GetTypeAt(index) != TYPE_SEPARATOR);
  auto action_id = items_[index].action_id;
  if (action_id == kActionPinActionToToolbar ||
      action_id == kActionUnpinActionFromToolbar) {
    UpdatePinState(action_id);
  } else {
    GetActionItemFor(action_id)->InvokeAction();
  }
}

actions::ActionId PinnedActionToolbarButtonMenuModel::GetActionIdAtForTesting(
    size_t index) {
  return items_[index].action_id;
}

PinnedActionToolbarButtonMenuModel::Item::Item(Item&&) = default;
PinnedActionToolbarButtonMenuModel::Item::Item(actions::ActionId action_id,
                                               ItemType type)
    : action_id(action_id), type(type) {}
PinnedActionToolbarButtonMenuModel::Item::Item(ItemType type) : type(type) {}
PinnedActionToolbarButtonMenuModel::Item&
PinnedActionToolbarButtonMenuModel::Item::operator=(Item&&) = default;
PinnedActionToolbarButtonMenuModel::Item::~Item() = default;

void PinnedActionToolbarButtonMenuModel::AddActionSpecificItems() {
  if (!IsPinStateManagedByPrefs(action_id_)) {
    // If the action has child actions add those first followed by a separator.
    actions::ActionItem* action_item = GetActionItemFor(action_id_);
    CHECK(action_item);
    if (!action_item->GetChildren().empty()) {
      for (const auto& child_item : action_item->GetChildren().children()) {
        // Adding all ActionItems as Command types here, if the ActionItem
        // should be displayed as Checked that is handled in `GetTypeAt` which
        // will evaluated the ActionItem's checked state when the menu is run.
        items_.emplace_back(*child_item->GetActionId(), TYPE_COMMAND);
      }
      items_.emplace_back(TYPE_SEPARATOR);
    }
  }
}

actions::ActionItem* PinnedActionToolbarButtonMenuModel::GetActionItemFor(
    actions::ActionId id) const {
  return actions::ActionManager::Get().FindAction(
      id, browser_->GetActions()->root_action_item());
}

bool PinnedActionToolbarButtonMenuModel::IsPinnable() const {
  return IsPinStateManagedByPrefs(action_id_) ||
         GetActionItemFor(action_id_)
                 ->GetProperty(actions::kActionItemPinnableKey) ==
             std::underlying_type_t<actions::ActionPinnableState>(
                 actions::ActionPinnableState::kPinnable);
}

bool PinnedActionToolbarButtonMenuModel::IsPinned() const {
  if (IsPinStateManagedByPrefs(action_id_)) {
    return browser_->GetProfile()->GetPrefs()->GetBoolean(
        GetPrefForActionId(action_id_));
  } else {
    return PinnedToolbarActionsModel::Get(browser_->GetProfile())
        ->Contains(action_id_);
  }
}

void PinnedActionToolbarButtonMenuModel::UpdatePinState(
    actions::ActionId pin_unpin_action) {
  const bool should_pin = pin_unpin_action == kActionPinActionToToolbar;
  if (IsPinStateManagedByPrefs(action_id_)) {
    PrefService* const pref_service = browser_->GetProfile()->GetPrefs();
    pref_service->SetBoolean(GetPrefForActionId(action_id_), should_pin);
  } else {
    GetActionItemFor(pin_unpin_action)
        ->InvokeAction(actions::ActionInvocationContext::Builder()
                           .SetProperty(kActionIdKey, action_id_)
                           .Build());
    const std::optional<std::string> metrics_name =
        actions::ActionIdMap::ActionIdToString(action_id_);
    CHECK(metrics_name.has_value());
    base::RecordComputedAction(base::StrCat(
        {"Actions.PinnedToolbarButton.", should_pin ? "Pinned" : "Unpinned",
         ".ByContextMenu.", metrics_name.value()}));
  }
}