File: glic_button.h

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 (134 lines) | stat: -rw-r--r-- 4,723 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
// Copyright 2024 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_VIEWS_TABS_GLIC_BUTTON_H_
#define CHROME_BROWSER_UI_VIEWS_TABS_GLIC_BUTTON_H_

#include "base/memory/raw_ptr.h"
#include "chrome/browser/glic/browser_ui/glic_button_controller_delegate.h"
#include "chrome/browser/ui/views/tabs/tab_strip_controller.h"
#include "chrome/browser/ui/views/tabs/tab_strip_nudge_button.h"
#include "chrome/common/buildflags.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/controls/menu/menu_model_adapter.h"

#if BUILDFLAG(ENABLE_GLIC)
#include "chrome/browser/glic/fre/glic_fre.mojom.h"
#endif  // BUILDFLAG(ENABLE_GLIC)

class PrefService;

namespace glic {

// GlicButton should leverage the look and feel of the existing
// TabSearchButton for sizing and appropriate theming.

class GlicButton : public TabStripNudgeButton,
                   public views::ContextMenuController,
                   public ui::SimpleMenuModel::Delegate {
  METADATA_HEADER(GlicButton, TabStripNudgeButton)

 public:
  explicit GlicButton(TabStripController* tab_strip_controller,
                      PressedCallback pressed_callback,
                      PressedCallback close_pressed_callback,
                      base::RepeatingClosure hovered_callback,
                      base::RepeatingClosure mouse_down_callback,
                      const gfx::VectorIcon& icon,
                      const std::u16string& tooltip);
  GlicButton(const GlicButton&) = delete;
  GlicButton& operator=(const GlicButton&) = delete;
  ~GlicButton() override;

  // TabStripNudgeButton:
  void SetIsShowingNudge(bool is_showing) override;

  void SetDropToAttachIndicator(bool indicate);

  // GetBoundsInScreen() gives a rect with some padding that extends beyond the
  // visible edges of the button. This function returns a rect without that
  // padding.
  gfx::Rect GetBoundsWithInset() const;

  // TabStripControlButton:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;
  void StateChanged(ButtonState old_state) override;

  // views::ContextMenuController:
  void ShowContextMenuForViewImpl(
      View* source,
      const gfx::Point& point,
      ui::mojom::MenuSourceType source_type) override;

  // ui::SimpleMenuModel::Delegate:
  void ExecuteCommand(int command_id, int event_flags) override;

  // views::View:
  // Note that this is an optimization for fetching zero-state suggestions so
  // that we can load the suggestions in the UI as quickly as possible.
  bool OnMousePressed(const ui::MouseEvent& event) override;

  bool IsContextMenuShowingForTest();

 private:
  // Creates the model for the context menu.
  std::unique_ptr<ui::SimpleMenuModel> CreateMenuModel();

  // Callback when the context menu closes.
  void OnMenuClosed();

  // Called every time the contextual cue is shown to make a screen reader
  // announcement.
  void AnnounceNudgeShown();

  PrefService* profile_prefs() {
    return tab_strip_controller_->GetProfile()->GetPrefs();
  }

#if BUILDFLAG(ENABLE_GLIC)
  void PanelStateChanged(bool active);

  void OnFreWebUiStateChanged(mojom::FreWebUiState new_state);

  // Used to update the tooltip text when the showing states of the Glic
  // window/FRE change.
  void UpdateTooltipText();

  // Callback subscription for listening to changes to the Glic window
  // activation changes.
  base::CallbackListSubscription glic_window_activation_subscription_;

  // Callback subscription for listening to changes to the FRE WebUI state.
  base::CallbackListSubscription fre_subscription_;
#endif  // BUILDFLAG(ENABLE_GLIC)

  // The model adapter for the context menu.
  std::unique_ptr<views::MenuModelAdapter> menu_model_adapter_;

  // Model for the context menu.
  std::unique_ptr<ui::MenuModel> menu_model_;

  // Used to ensure the button remains highlighted while the menu is active.
  std::optional<Button::ScopedAnchorHighlight> menu_anchor_higlight_;

  // Menu runner for the context menu.
  std::unique_ptr<views::MenuRunner> menu_runner_;

  // Tab strip that contains this button.
  raw_ptr<TabStripController> tab_strip_controller_;

  // Callback which is invoked when the button is hovered (i.e., the user is
  // more likely to interact with it soon).
  base::RepeatingClosure hovered_callback_;

  // Callback which is invoked when there is a mouse down event on the button
  // (i.e., the user is very likely to interact with it soon).
  base::RepeatingClosure mouse_down_callback_;
};

}  // namespace glic

#endif  // CHROME_BROWSER_UI_VIEWS_TABS_GLIC_BUTTON_H_