File: bubble_menu_item_factory.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 4,088 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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/bubble_menu_item_factory.h"

#include <memory>
#include <string>
#include <utility>

#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/controls/hover_button.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/color/color_id.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_host.h"
#include "ui/views/border.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h"

namespace {

constexpr gfx::Insets kDefaultBorderInsets = gfx::Insets(12);

class BubbleMenuItemButton : public HoverButton {
  METADATA_HEADER(BubbleMenuItemButton, HoverButton)

 public:
  BubbleMenuItemButton(PressedCallback callback,
                       const ui::ImageModel& icon,
                       const std::u16string& text)
      : HoverButton(std::move(callback), icon, text) {}

  // HoverButton:
  void StateChanged(ButtonState old_state) override {
    // Explicitly override HoverButton::StateChanged so focus is not taken from
    // other elements within the same view as this button when it is hovered.
    // Ex: In the TabGroupEditorBubbleView users should be able to hover over
    // the menu items without losing focus on the title text box.
    LabelButton::StateChanged(old_state);
  }
};

BEGIN_METADATA(BubbleMenuItemButton)
END_METADATA

}  // namespace

void ConfigureBubbleMenuItem(views::Button* button, int button_id) {
  // Items within a menu should not show focus rings.
  button->SetInstallFocusRingOnFocus(false);
  views::InkDrop::Get(button)->SetMode(views::InkDropHost::InkDropMode::ON);
  views::InkDrop::Get(button)->GetInkDrop()->SetShowHighlightOnHover(true);
  views::InkDrop::Get(button)->GetInkDrop()->SetHoverHighlightFadeDuration(
      base::TimeDelta());
  views::InstallRectHighlightPathGenerator(button);
  button->SetID(button_id);
  button->SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
  button->SetBorder(views::CreateEmptyBorder(kDefaultBorderInsets));
}

std::unique_ptr<HoverButton> CreateBubbleMenuItem(
    int button_id,
    const std::u16string& name,
    views::Button::PressedCallback callback,
    const ui::ImageModel& icon,
    const std::u16string& accelerator_text) {
  auto button =
      std::make_unique<BubbleMenuItemButton>(std::move(callback), icon, name);
  ConfigureBubbleMenuItem(button.get(), button_id);

  if (!accelerator_text.empty()) {
    // Add an accelerator label to the button if it has one. The accelerator
    // should be right aligned in Left-to-Right mode and left aligned in
    // Right-to-Left mode.

    // Use BoxLayout for the button's content.
    views::BoxLayout* layout =
        button->SetLayoutManager(std::make_unique<views::BoxLayout>(
            views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
            ChromeLayoutProvider::Get()->GetDistanceMetric(
                views::DISTANCE_RELATED_LABEL_HORIZONTAL)));
    layout->set_cross_axis_alignment(
        views::BoxLayout::CrossAxisAlignment::kCenter);

    // Add a spacer view to push the accelerator to the end of the view. This
    // ensures all accelerators are aligned with each other.
    button->AddChildView(std::make_unique<views::View>());
    layout->SetFlexForView(button->children().back(), 1);

    auto accelerator_label = std::make_unique<views::Label>(accelerator_text);
    accelerator_label->SetEnabledColor(ui::kColorLabelForegroundSecondary);
    accelerator_label->SetTextStyle(views::style::STYLE_BODY_4);
    accelerator_label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
    button->AddChildView(std::move(accelerator_label));

    button->SetAccessibleName(name + u" " + accelerator_text);
  }

  return button;
}