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
|
// 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.
#include "chrome/browser/ui/views/performance_controls/performance_intervention_button.h"
#include <memory>
#include <string>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/performance_controls/performance_controls_metrics.h"
#include "chrome/browser/ui/performance_controls/performance_intervention_button_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/performance_controls/performance_intervention_bubble.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"
PerformanceInterventionButton::PerformanceInterventionButton(
BrowserView* browser_view)
: ToolbarButton(
base::BindRepeating(&PerformanceInterventionButton::OnClicked,
base::Unretained(this))),
browser_view_(browser_view) {
button_controller()->set_notify_action(
views::ButtonController::NotifyAction::kOnPress);
SetFlipCanvasOnPaintForRTLUI(false);
GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_PERFORMANCE_INTERVENTION_BUTTON_ACCNAME));
SetTooltipText(
l10n_util::GetStringUTF16(IDS_PERFORMANCE_INTERVENTION_BUTTON_TOOLTIP));
SetProperty(views::kElementIdentifierKey,
kToolbarPerformanceInterventionButtonElementId);
SetVisible(false);
controller_ = std::make_unique<PerformanceInterventionButtonController>(
this, browser_view->browser());
if (menu_model()) {
GetViewAccessibility().SetHasPopup(ax::mojom::HasPopup::kMenu);
}
}
PerformanceInterventionButton::~PerformanceInterventionButton() = default;
void PerformanceInterventionButton::Show() {
is_active_ = true;
SetVisible(true);
PreferredSizeChanged();
CreateBubble();
}
void PerformanceInterventionButton::Hide() {
SetVisible(false);
PreferredSizeChanged();
}
bool PerformanceInterventionButton::IsButtonShowing() {
return GetVisible();
}
bool PerformanceInterventionButton::IsBubbleShowing() {
return bubble_dialog_model_host_ != nullptr;
}
void PerformanceInterventionButton::OnWidgetDestroying(views::Widget* widget) {
InterventionBubbleActionType type = InterventionBubbleActionType::kUnknown;
switch (widget->closed_reason()) {
case views::Widget::ClosedReason::kUnspecified:
type = InterventionBubbleActionType::kUnknown;
break;
case views::Widget::ClosedReason::kEscKeyPressed:
type = InterventionBubbleActionType::kClose;
break;
case views::Widget::ClosedReason::kCloseButtonClicked:
type = InterventionBubbleActionType::kClose;
break;
case views::Widget::ClosedReason::kLostFocus:
type = InterventionBubbleActionType::kIgnore;
break;
case views::Widget::ClosedReason::kCancelButtonClicked:
type = InterventionBubbleActionType::kDismiss;
break;
case views::Widget::ClosedReason::kAcceptButtonClicked:
type = InterventionBubbleActionType::kAccept;
break;
}
RecordInterventionBubbleClosedReason(
performance_manager::user_tuning::PerformanceDetectionManager::
ResourceType::kCpu,
type);
bubble_dialog_model_host_ = nullptr;
scoped_widget_observation_.Reset();
}
void PerformanceInterventionButton::OnThemeChanged() {
ToolbarButton::OnThemeChanged();
UpdateIconColor();
}
void PerformanceInterventionButton::OnClicked() {
is_active_ = false;
UpdateIconColor();
if (IsBubbleShowing()) {
PerformanceInterventionBubble::CloseBubble(bubble_dialog_model_host_);
} else {
CreateBubble();
RecordInterventionToolbarButtonClicked();
}
}
void PerformanceInterventionButton::CreateBubble() {
CHECK(GetWidget());
bubble_dialog_model_host_ = PerformanceInterventionBubble::CreateBubble(
browser_view_->browser(), this, controller_.get());
scoped_widget_observation_.Observe(bubble_dialog_model_host_->GetWidget());
}
void PerformanceInterventionButton::UpdateIconColor() {
const ui::ColorId icon_color =
is_active_ ? kColorPerformanceInterventionButtonIconActive
: kColorPerformanceInterventionButtonIconInactive;
SetImageModel(
ButtonState::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(kPerformanceSpeedometerIcon,
GetColorProvider()->GetColor(icon_color)));
}
BEGIN_METADATA(PerformanceInterventionButton)
END_METADATA
|