14
14
#include " chrome/browser/history/history_service_factory.h"
15
15
#include " chrome/browser/search/search.h"
16
16
#include " chrome/browser/search_engines/template_url_service_factory.h"
17
+ #include " chrome/browser/ui/side_panel/companion/companion_tab_helper.h"
18
+ #include " chrome/browser/ui/side_panel/companion/companion_utils.h"
17
19
#include " chrome/browser/ui/ui_features.h"
18
20
#include " chrome/browser/ui/views/frame/browser_view.h"
19
21
#include " chrome/browser/ui/views/side_panel/lens/lens_unified_side_panel_view.h"
30
32
#include " components/search_engines/util.h"
31
33
#include " components/vector_icons/vector_icons.h"
32
34
#include " ui/base/l10n/l10n_util.h"
35
+ #include " ui/base/ui_base_features.h"
33
36
#include " ui/gfx/image/image.h"
34
37
#include " ui/views/vector_icons.h"
35
38
@@ -69,8 +72,27 @@ LensSidePanelCoordinator::~LensSidePanelCoordinator() {
69
72
70
73
void LensSidePanelCoordinator::DeregisterLensFromSidePanel () {
71
74
lens_side_panel_view_ = nullptr ;
72
- SidePanelCoordinator::GetGlobalSidePanelRegistry (&GetBrowser ())
73
- ->Deregister (SidePanelEntry::Key (SidePanelEntry::Id::kLens ));
75
+ auto * active_web_contents = GetBrowserView ()->GetActiveWebContents ();
76
+ const bool is_companion_enabled =
77
+ companion::IsSearchInCompanionSidePanelSupported (&GetBrowser ());
78
+
79
+ // Delete contextual lens view if applicable.
80
+ if (is_companion_enabled && active_web_contents) {
81
+ auto * companion_helper =
82
+ companion::CompanionTabHelper::FromWebContents (active_web_contents);
83
+ if (companion_helper) {
84
+ companion_helper->RemoveContextualLensView ();
85
+ }
86
+ }
87
+
88
+ // Remove entry from side panel entry if it exists.
89
+ auto * registry =
90
+ is_companion_enabled
91
+ ? SidePanelRegistry::Get (active_web_contents)
92
+ : SidePanelCoordinator::GetGlobalSidePanelRegistry (&GetBrowser ());
93
+ if (registry) {
94
+ registry->Deregister (SidePanelEntry::Key (SidePanelEntry::Id::kLens ));
95
+ }
74
96
}
75
97
76
98
void LensSidePanelCoordinator::OnSidePanelDidClose () {
@@ -80,13 +102,16 @@ void LensSidePanelCoordinator::OnSidePanelDidClose() {
80
102
}
81
103
82
104
void LensSidePanelCoordinator::OnFaviconFetched (const gfx::Image& favicon) {
83
- auto * global_registry =
84
- SidePanelCoordinator::GetGlobalSidePanelRegistry (&GetBrowser ());
85
- if (global_registry == nullptr )
105
+ auto * registry =
106
+ companion::IsSearchInCompanionSidePanelSupported (&GetBrowser ())
107
+ ? SidePanelRegistry::Get (GetBrowserView ()->GetActiveWebContents ())
108
+ : SidePanelCoordinator::GetGlobalSidePanelRegistry (&GetBrowser ());
109
+ if (registry == nullptr ) {
86
110
return ;
111
+ }
87
112
88
- auto * lens_side_panel_entry = global_registry-> GetEntryForKey (
89
- SidePanelEntry::Key (SidePanelEntry::Id::kLens ));
113
+ auto * lens_side_panel_entry =
114
+ registry-> GetEntryForKey ( SidePanelEntry::Key (SidePanelEntry::Id::kLens ));
90
115
if (lens_side_panel_entry == nullptr )
91
116
return ;
92
117
@@ -121,6 +146,12 @@ void LensSidePanelCoordinator::OnEntryHidden(SidePanelEntry* entry) {
121
146
}
122
147
123
148
bool LensSidePanelCoordinator::IsLaunchButtonEnabledForTesting () {
149
+ if (companion::IsSearchInCompanionSidePanelSupported (&GetBrowser ())) {
150
+ auto * companion_helper = companion::CompanionTabHelper::FromWebContents (
151
+ GetBrowserView ()->GetActiveWebContents ());
152
+ return companion_helper->IsLensLaunchButtonEnabledForTesting (); // IN-TEST
153
+ }
154
+
124
155
DCHECK (lens_side_panel_view_);
125
156
return lens_side_panel_view_->IsLaunchButtonEnabledForTesting ();
126
157
}
@@ -130,6 +161,13 @@ bool LensSidePanelCoordinator::IsDefaultSearchProviderGoogle() {
130
161
}
131
162
132
163
std::u16string LensSidePanelCoordinator::GetComboboxLabel () {
164
+ // If this panel was opened while the companion feature is enabled, then we
165
+ // want this panel to be labelled like the companion panel.
166
+ if (companion::IsSearchInCompanionSidePanelSupportedForProfile (
167
+ GetBrowser ().profile ())) {
168
+ return l10n_util::GetStringUTF16 (IDS_SIDE_PANEL_COMPANION_TITLE);
169
+ }
170
+
133
171
if (IsDefaultSearchProviderGoogle ()) {
134
172
return l10n_util::GetStringUTF16 (IDS_GOOGLE_LENS_TITLE);
135
173
}
@@ -140,6 +178,17 @@ std::u16string LensSidePanelCoordinator::GetComboboxLabel() {
140
178
}
141
179
142
180
const ui::ImageModel LensSidePanelCoordinator::GetFaviconImage () {
181
+ // If this panel was opened while the companion feature is enabled, then we
182
+ // want this panel to use the companion panel favicon.
183
+ if (companion::IsSearchInCompanionSidePanelSupportedForProfile (
184
+ GetBrowser ().profile ())) {
185
+ return ui::ImageModel::FromVectorIcon (
186
+ features::IsChromeRefresh2023 ()
187
+ ? vector_icons::
188
+ kGoogleSearchCompanionMonochromeLogoChromeRefreshIcon
189
+ : vector_icons::kGoogleSearchCompanionMonochromeLogoIcon );
190
+ }
191
+
143
192
// If google is search engine, return checked-in lens icon.
144
193
if (IsDefaultSearchProviderGoogle ())
145
194
return ui::ImageModel::FromVectorIcon (vector_icons::kGoogleLensLogoIcon );
@@ -172,28 +221,46 @@ const ui::ImageModel LensSidePanelCoordinator::GetFaviconImage() {
172
221
void LensSidePanelCoordinator::RegisterEntryAndShow (
173
222
const content::OpenURLParams& params) {
174
223
base::RecordAction (base::UserMetricsAction (" LensUnifiedSidePanel.LensQuery" ));
175
- auto * global_registry =
176
- SidePanelCoordinator::GetGlobalSidePanelRegistry (&GetBrowser ());
224
+ const bool is_companion_enabled =
225
+ companion::IsSearchInCompanionSidePanelSupported (&GetBrowser ());
226
+ auto * companion_helper = companion::CompanionTabHelper::FromWebContents (
227
+ GetBrowserView ()->GetActiveWebContents ());
228
+ auto * registry =
229
+ is_companion_enabled
230
+ ? SidePanelRegistry::Get (GetBrowserView ()->GetActiveWebContents ())
231
+ : SidePanelCoordinator::GetGlobalSidePanelRegistry (&GetBrowser ());
177
232
178
233
// check if the view is already registered
179
- if (global_registry ->GetEntryForKey (
234
+ if (registry ->GetEntryForKey (
180
235
SidePanelEntry::Key (SidePanelEntry::Id::kLens )) != nullptr &&
181
- lens_side_panel_view_ != nullptr ) {
236
+ ( lens_side_panel_view_ != nullptr || is_companion_enabled) ) {
182
237
// The user issued a follow-up Lens query.
183
238
base::RecordAction (
184
239
base::UserMetricsAction (" LensUnifiedSidePanel.LensQuery_Followup" ));
185
- lens_side_panel_view_->OpenUrl (params);
240
+ if (is_companion_enabled) {
241
+ companion_helper->OpenContextualLensView (params);
242
+ } else {
243
+ lens_side_panel_view_->OpenUrl (params);
244
+ }
186
245
} else {
187
246
base::RecordAction (
188
247
base::UserMetricsAction (" LensUnifiedSidePanel.LensQuery_New" ));
189
- auto entry = std::make_unique<SidePanelEntry>(
190
- SidePanelEntry::Id::kLens , GetComboboxLabel (), GetFaviconImage (),
191
- base::BindRepeating (&LensSidePanelCoordinator::CreateLensWebView,
192
- base::Unretained (this ), params),
193
- base::BindRepeating (&LensSidePanelCoordinator::GetOpenInNewTabURL,
194
- base::Unretained (this )));
195
- entry->AddObserver (this );
196
- global_registry->Register (std::move (entry));
248
+ if (is_companion_enabled) {
249
+ // Side panel entry needs to be created and registered
250
+ // in the companion side panel controller that exists per web contents in
251
+ // order to prevent a dependency on views on CompanionTabHelper.
252
+ companion_helper->CreateAndRegisterLensEntry (params, GetComboboxLabel (),
253
+ GetFaviconImage ());
254
+ } else {
255
+ auto entry = std::make_unique<SidePanelEntry>(
256
+ SidePanelEntry::Id::kLens , GetComboboxLabel (), GetFaviconImage (),
257
+ base::BindRepeating (&LensSidePanelCoordinator::CreateLensWebView,
258
+ base::Unretained (this ), params),
259
+ base::BindRepeating (&LensSidePanelCoordinator::GetOpenInNewTabURL,
260
+ base::Unretained (this )));
261
+ entry->AddObserver (this );
262
+ registry->Register (std::move (entry));
263
+ }
197
264
}
198
265
199
266
auto * side_panel_coordinator = GetSidePanelCoordinator ();
@@ -206,7 +273,6 @@ void LensSidePanelCoordinator::RegisterEntryAndShow(
206
273
base::RecordAction (base::UserMetricsAction (
207
274
" LensUnifiedSidePanel.LensQuery_SidePanelOpenNonLens" ));
208
275
}
209
-
210
276
side_panel_coordinator->Show (SidePanelEntry::Id::kLens ,
211
277
SidePanelOpenTrigger::kLensContextMenu );
212
278
} else {
@@ -216,11 +282,22 @@ void LensSidePanelCoordinator::RegisterEntryAndShow(
216
282
}
217
283
218
284
content::WebContents* LensSidePanelCoordinator::GetViewWebContentsForTesting () {
285
+ if (companion::IsSearchInCompanionSidePanelSupported (&GetBrowser ())) {
286
+ auto * companion_helper = companion::CompanionTabHelper::FromWebContents (
287
+ GetBrowserView ()->GetActiveWebContents ());
288
+ return companion_helper->GetLensViewWebContentsForTesting (); // IN-TEST
289
+ }
219
290
return lens_side_panel_view_ ? lens_side_panel_view_->GetWebContents ()
220
291
: nullptr ;
221
292
}
222
293
223
294
bool LensSidePanelCoordinator::OpenResultsInNewTabForTesting () {
295
+ if (companion::IsSearchInCompanionSidePanelSupported (&GetBrowser ())) {
296
+ auto * companion_helper = companion::CompanionTabHelper::FromWebContents (
297
+ GetBrowserView ()->GetActiveWebContents ());
298
+ return companion_helper->OpenLensResultsInNewTabForTesting (); // IN-TEST
299
+ }
300
+
224
301
if (lens_side_panel_view_ == nullptr )
225
302
return false ;
226
303
0 commit comments