File: toolbar_action_hover_card_controller.h

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 (108 lines) | stat: -rw-r--r-- 4,430 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
107
108
// Copyright 2022 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_TOOLBAR_TOOLBAR_ACTION_HOVER_CARD_CONTROLLER_H_
#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_HOVER_CARD_CONTROLLER_H_

#include <memory>

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/ui/toolbar/toolbar_action_hover_card_types.h"
#include "ui/views/animation/bubble_slide_animator.h"
#include "ui/views/animation/widget_fade_animator.h"
#include "ui/views/view.h"
#include "ui/views/view_observer.h"

class ToolbarActionHoverCardBubbleView;
class ExtensionsToolbarContainer;
class ToolbarActionView;

// Controls how hover cards are shown and hidden for toolbar actions.
class ToolbarActionHoverCardController : public views::ViewObserver {
 public:
  explicit ToolbarActionHoverCardController(
      ExtensionsToolbarContainer* extensions_container);
  ~ToolbarActionHoverCardController() override;

  // Returns whether hover card animations should be shown on the current
  // device.
  static bool UseAnimations();

  bool IsHoverCardVisible() const;
  bool IsHoverCardShowingForAction(ToolbarActionView* action_view) const;
  void UpdateHoverCard(ToolbarActionView* action_view,
                       ToolbarActionHoverCardUpdateType update_type);

 private:
  friend class ToolbarActionHoverCardBubbleViewUITest;

  class EventSniffer;

  void UpdateOrShowHoverCard(ToolbarActionView* action_view,
                             ToolbarActionHoverCardUpdateType update_type);
  void UpdateHoverCardContent(ToolbarActionView* action_view);

  void CreateHoverCard(ToolbarActionView* action_view);
  void ShowHoverCard(bool is_initial, const ToolbarActionView* action_view);
  void HideHoverCard();

  bool ShouldShowImmediately(const ToolbarActionView* action_view) const;

  const views::View* GetTargetAnchorView() const;

  // Determines if `target_action_view` is still valid. Call this when entering
  // ToolbarActionHoverCardController from an asynchronous callback.
  bool TargetActionViewIsValid() const;

  // Animator events:
  void OnFadeAnimationEnded(views::WidgetFadeAnimator* animator,
                            views::WidgetFadeAnimator::FadeType fade_type);
  void OnSlideAnimationProgressed(views::BubbleSlideAnimator* animator,
                                  double value);
  void OnSlideAnimationComplete(views::BubbleSlideAnimator* animator);

  // views::ViewObserver:
  void OnViewIsDeleting(views::View* observed_view) override;
  void OnViewVisibilityChanged(views::View* observed_view,
                               views::View* starting_view) override;

  // Timestamp of the last time the hover card is hidden by the mouse leaving
  // the tab strip. This is used for reshowing the hover card without delay if
  // the mouse reenters within a given amount of time.
  base::TimeTicks last_mouse_exit_timestamp_;

  raw_ptr<ToolbarActionView> target_action_view_ = nullptr;
  const raw_ptr<ExtensionsToolbarContainer> extensions_container_;
  raw_ptr<ToolbarActionHoverCardBubbleView> hover_card_ = nullptr;

  base::ScopedObservation<views::View, views::ViewObserver>
      hover_card_observation_{this};
  base::ScopedObservation<views::View, views::ViewObserver>
      target_action_view_observation_{this};
  std::unique_ptr<EventSniffer> event_sniffer_;

  // Used to animate the tab hover card's opacity when visible or not.
  std::unique_ptr<views::WidgetFadeAnimator> fade_animator_;
  // Fade animations interfere with browser tests so we disable them in tests.
  static bool disable_animations_for_testing_;

  // Used to animate the tab hover card's movement between tabs.
  std::unique_ptr<views::BubbleSlideAnimator> slide_animator_;

  base::CallbackListSubscription fade_complete_subscription_;
  base::CallbackListSubscription slide_progressed_subscription_;
  base::CallbackListSubscription slide_complete_subscription_;

  // Ensure that this timer is destroyed before anything else is cleaned up.
  base::OneShotTimer delayed_show_timer_;
  base::WeakPtrFactory<ToolbarActionHoverCardController> weak_ptr_factory_{
      this};
};

#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_ACTION_HOVER_CARD_CONTROLLER_H_