-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
customize_chrome_side_panel_controller.cc
139 lines (121 loc) · 5.3 KB
/
customize_chrome_side_panel_controller.cc
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
// 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/side_panel/customize_chrome/customize_chrome_side_panel_controller.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h"
#include "chrome/browser/ui/views/side_panel/side_panel_registry_observer.h"
#include "chrome/browser/ui/views/side_panel/side_panel_web_ui_view.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/web_contents_user_data.h"
#include "ui/base/l10n/l10n_util.h"
CustomizeChromeSidePanelController::CustomizeChromeSidePanelController(
content::WebContents* web_contents)
: web_contents_(web_contents) {}
CustomizeChromeSidePanelController::~CustomizeChromeSidePanelController() =
default;
void CustomizeChromeSidePanelController::CreateAndRegisterEntry() {
const int icon_size = ChromeLayoutProvider::Get()->GetDistanceMetric(
ChromeDistanceMetric::DISTANCE_SIDE_PANEL_HEADER_VECTOR_ICON_SIZE);
auto* registry = SidePanelRegistry::Get(web_contents_);
if (!registry)
return;
auto entry = std::make_unique<SidePanelEntry>(
SidePanelEntry::Id::kCustomizeChrome,
l10n_util::GetStringUTF16(IDS_SIDE_PANEL_CUSTOMIZE_CHROME_TITLE),
ui::ImageModel::FromVectorIcon(vector_icons::kEditIcon, ui::kColorIcon,
icon_size),
base::BindRepeating(
&CustomizeChromeSidePanelController::CreateCustomizeChromeWebView,
base::Unretained(this)));
entry->AddObserver(this);
registry->Register(std::move(entry));
}
void CustomizeChromeSidePanelController::DeregisterEntry() {
auto* registry = SidePanelRegistry::Get(web_contents_);
if (!registry)
return;
if (auto* current_entry = registry->GetEntryForKey(
SidePanelEntry::Key(SidePanelEntry::Id::kCustomizeChrome))) {
current_entry->RemoveObserver(this);
}
registry->Deregister(
SidePanelEntry::Key(SidePanelEntry::Id::kCustomizeChrome));
}
void CustomizeChromeSidePanelController::SetCustomizeChromeSidePanelVisible(
bool visible,
CustomizeChromeSection section) {
auto* side_panel_ui = GetSidePanelUI();
if (!side_panel_ui) {
return;
}
DCHECK(IsCustomizeChromeEntryAvailable());
if (visible) {
side_panel_ui->Show(SidePanelEntry::Id::kCustomizeChrome);
if (customize_chrome_ui_) {
customize_chrome_ui_->ScrollToSection(section);
section_.reset();
} else {
section_ = section;
}
} else {
side_panel_ui->Close();
}
}
bool CustomizeChromeSidePanelController::IsCustomizeChromeEntryShowing() const {
auto* side_panel_ui = GetSidePanelUI();
return side_panel_ui && side_panel_ui->IsSidePanelShowing() &&
(side_panel_ui->GetCurrentEntryId() ==
SidePanelEntry::Id::kCustomizeChrome);
}
bool CustomizeChromeSidePanelController::IsCustomizeChromeEntryAvailable()
const {
auto* registry = SidePanelRegistry::Get(web_contents_);
return registry ? (registry->GetEntryForKey(SidePanelEntry::Key(
SidePanelEntry::Id::kCustomizeChrome)) != nullptr)
: false;
}
void CustomizeChromeSidePanelController::OnEntryShown(SidePanelEntry* entry) {
auto* customize_chrome_tab_helper =
CustomizeChromeTabHelper::FromWebContents(web_contents_);
customize_chrome_tab_helper->EntryStateChanged(true);
}
void CustomizeChromeSidePanelController::OnEntryHidden(SidePanelEntry* entry) {
auto* customize_chrome_tab_helper =
CustomizeChromeTabHelper::FromWebContents(web_contents_);
customize_chrome_tab_helper->EntryStateChanged(false);
}
std::unique_ptr<views::View>
CustomizeChromeSidePanelController::CreateCustomizeChromeWebView() {
auto customize_chrome_web_view =
std::make_unique<SidePanelWebUIViewT<CustomizeChromeUI>>(
base::RepeatingClosure(), base::RepeatingClosure(),
std::make_unique<BubbleContentsWrapperT<CustomizeChromeUI>>(
GURL(chrome::kChromeUICustomizeChromeSidePanelURL),
Profile::FromBrowserContext(web_contents_->GetBrowserContext()),
IDS_SIDE_PANEL_CUSTOMIZE_CHROME_TITLE,
/*webui_resizes_host=*/false,
/*esc_closes_ui=*/false));
customize_chrome_web_view->ShowUI();
customize_chrome_ui_ = customize_chrome_web_view->contents_wrapper()
->GetWebUIController()
->GetWeakPtr();
if (section_.has_value()) {
customize_chrome_ui_->ScrollToSection(*section_);
section_.reset();
}
return customize_chrome_web_view;
}
SidePanelUI* CustomizeChromeSidePanelController::GetSidePanelUI() const {
auto* browser = chrome::FindBrowserWithWebContents(web_contents_);
return browser ? SidePanelUI::GetSidePanelUIForBrowser(browser) : nullptr;
}