File: battery_saver_button.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 (135 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download | duplicates (5)
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
// 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.

#include "chrome/browser/ui/views/performance_controls/battery_saver_button.h"

#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/user_education/browser_user_education_interface.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/performance_controls/battery_saver_bubble_view.h"
#include "chrome/grit/generated_resources.h"
#include "components/feature_engagement/public/event_constants.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ui_base_features.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/view_class_properties.h"

BatterySaverButton::BatterySaverButton(BrowserView* browser_view)
    : ToolbarButton(base::BindRepeating(&BatterySaverButton::OnClicked,
                                        base::Unretained(this))),
      browser_view_(browser_view) {
  SetVectorIcon(kBatterySaverRefreshIcon);
  button_controller()->set_notify_action(
      views::ButtonController::NotifyAction::kOnPress);

  // Do not flip the battery saver icon for RTL languages.
  SetFlipCanvasOnPaintForRTLUI(false);

  GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_BATTERY_SAVER_BUTTON_ACCNAME));
  SetTooltipText(l10n_util::GetStringUTF16(IDS_BATTERY_SAVER_BUTTON_TOOLTIP));
  GetViewAccessibility().SetHasPopup(ax::mojom::HasPopup::kDialog);
  SetProperty(views::kElementIdentifierKey,
              kToolbarBatterySaverButtonElementId);

  // We start hidden and only show once |controller_| tells us to.
  SetVisible(false);

  // Initialize the |controller_| which will update the correct visible state.
  controller_.Init(this);
}

BatterySaverButton::~BatterySaverButton() {
  if (IsBubbleShowing()) {
    BatterySaverBubbleView::CloseBubble(bubble_);
  }
}

bool BatterySaverButton::IsBubbleShowing() const {
  return bubble_ != nullptr;
}

void BatterySaverButton::Show() {
  bool was_visible = GetVisible();
  SetVisible(true);
  PreferredSizeChanged();

  // Wait until the view is properly laid out before triggering the promo
  // The promo will be triggered in |OnBoundsChanged| if the flag is set
  if (!was_visible) {
    pending_promo_ = true;
  }
}

void BatterySaverButton::Hide() {
  CloseFeaturePromo(/*engaged=*/false);

  if (IsBubbleShowing()) {
    // The bubble is closed sync and will be cleared in OnBubbleHidden
    BatterySaverBubbleView::CloseBubble(bubble_);
  }

  SetVisible(false);
  PreferredSizeChanged();
}

void BatterySaverButton::OnBubbleHidden() {
  bubble_ = nullptr;
}

bool BatterySaverButton::ShouldShowInkdropAfterIphInteraction() {
  return false;
}

void BatterySaverButton::OnBoundsChanged(const gfx::Rect& previous_bounds) {
  ToolbarButton::OnBoundsChanged(previous_bounds);

  if (!GetVisible() || size().IsEmpty()) {
    return;
  }

  if (pending_promo_) {
    MaybeShowFeaturePromo();
  }
}

void BatterySaverButton::OnClicked() {
  if (IsBubbleShowing()) {
    // The bubble is closed sync and will be cleared in OnBubbleHidden
    BatterySaverBubbleView::CloseBubble(bubble_);
  } else {
    CloseFeaturePromo(/*engaged=*/true);
    bubble_ = BatterySaverBubbleView::CreateBubble(
        browser_view_->browser(), this, views::BubbleBorder::TOP_RIGHT, this);
  }
}

void BatterySaverButton::MaybeShowFeaturePromo() {
  pending_promo_ = false;
  browser_view_->MaybeShowStartupFeaturePromo(
      feature_engagement::kIPHBatterySaverModeFeature);
}

void BatterySaverButton::CloseFeaturePromo(bool engaged) {
  // CloseFeaturePromo checks if the promo is active for the feature before
  // attempting to close the promo bubble
  pending_promo_ = false;
  if (engaged) {
    browser_view_->NotifyFeaturePromoFeatureUsed(
        feature_engagement::kIPHBatterySaverModeFeature,
        FeaturePromoFeatureUsedAction::kClosePromoIfPresent);
  } else {
    browser_view_->AbortFeaturePromo(
        feature_engagement::kIPHBatterySaverModeFeature);
  }
}

BEGIN_METADATA(BatterySaverButton)
END_METADATA