Scope SearchResultImageViewDelegate to image list
Instead of using a global SearchResultImageViewDelegate instance in
SearchResultImageViews, scope the delegate to an
SearchResultImageListView, and pass it to SearchResultImageViews in
their constructors. Using a global SearchResultImageViewDelegate had an
invalid assumption that at most one instance could exist at the time -
this did not take into account that both bubble and tablet mode launcher
have their own SearchResultImageListView, and thus their own
SearchResultImageViewDelegate.
Change-Id: I4ed0257262a792679e130ca08d83a903b237b229
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5181716
Reviewed-by: Wen-Chien Wang <wcwang@chromium.org>
Commit-Queue: Toni Barzic <tbarzic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1245050}
diff --git a/ash/app_list/views/search_result_image_list_view.cc b/ash/app_list/views/search_result_image_list_view.cc
index 03765dc9..767034e 100644
--- a/ash/app_list/views/search_result_image_list_view.cc
+++ b/ash/app_list/views/search_result_image_list_view.cc
@@ -118,7 +118,7 @@
for (size_t i = 0;
i < SharedAppListConfig::instance().image_search_max_results(); ++i) {
auto* image_view = image_view_container_->AddChildView(
- std::make_unique<SearchResultImageView>(/*index=*/1, this));
+ std::make_unique<SearchResultImageView>(/*index=*/1, this, &delegate_));
image_view->SetPaintToLayer();
image_view->layer()->SetFillsBoundsOpaquely(false);
image_views_.push_back(image_view);
diff --git a/ash/app_list/views/search_result_image_list_view.h b/ash/app_list/views/search_result_image_list_view.h
index 590670e..13cd2f5 100644
--- a/ash/app_list/views/search_result_image_list_view.h
+++ b/ash/app_list/views/search_result_image_list_view.h
@@ -66,9 +66,8 @@
views::View* GetTitleLabel() override;
std::vector<views::View*> GetViewsToAnimate() override;
- // The singleton delegate for search result image views that implements
- // support for context menu and drag-and-drop operations. This delegate needs
- // to be a singleton to support multi-selection which requires a shared state.
+ // Delegate for search result image views that implements support for context
+ // menu and drag-and-drop operations.
SearchResultImageViewDelegate delegate_;
// Owned by views hierarchy.
diff --git a/ash/app_list/views/search_result_image_view.cc b/ash/app_list/views/search_result_image_view.cc
index ef87dd09..e1bf234b 100644
--- a/ash/app_list/views/search_result_image_view.cc
+++ b/ash/app_list/views/search_result_image_view.cc
@@ -81,7 +81,8 @@
SearchResultImageView::SearchResultImageView(
int index,
- SearchResultImageListView* list_view)
+ SearchResultImageListView* list_view,
+ SearchResultImageViewDelegate* image_view_delegate)
: index_(index), list_view_(list_view) {
SetLayoutManager(std::make_unique<views::FillLayout>());
result_image_ = AddChildView(std::make_unique<ImagePreviewView>());
@@ -106,7 +107,7 @@
SetCallback(base::BindRepeating(&SearchResultImageView::OnImageViewPressed,
base::Unretained(this)));
- set_drag_controller(SearchResultImageViewDelegate::Get());
+ set_drag_controller(image_view_delegate);
}
void SearchResultImageView::OnImageViewPressed(const ui::Event& event) {
diff --git a/ash/app_list/views/search_result_image_view.h b/ash/app_list/views/search_result_image_view.h
index 7a06a6b..d46683b 100644
--- a/ash/app_list/views/search_result_image_view.h
+++ b/ash/app_list/views/search_result_image_view.h
@@ -17,12 +17,15 @@
namespace ash {
class PulsingBlockView;
class SearchResultImageListView;
+class SearchResultImageViewDelegate;
// Displays a search result in the form of an unlabeled image.
class ASH_EXPORT SearchResultImageView : public SearchResultBaseView {
public:
METADATA_HEADER(SearchResultImageView);
- SearchResultImageView(int index, SearchResultImageListView* list_view);
+ SearchResultImageView(int index,
+ SearchResultImageListView* list_view,
+ SearchResultImageViewDelegate* image_view_delegate);
SearchResultImageView(const SearchResultImageView&) = delete;
SearchResultImageView& operator=(const SearchResultImageView&) = delete;
~SearchResultImageView() override;
diff --git a/ash/app_list/views/search_result_image_view_delegate.cc b/ash/app_list/views/search_result_image_view_delegate.cc
index 82816cf..1fcf14b 100644
--- a/ash/app_list/views/search_result_image_view_delegate.cc
+++ b/ash/app_list/views/search_result_image_view_delegate.cc
@@ -14,24 +14,10 @@
#include "ui/views/view_utils.h"
namespace ash {
-namespace {
-SearchResultImageViewDelegate* g_instance = nullptr;
-} // namespace
-// static
-SearchResultImageViewDelegate* SearchResultImageViewDelegate::Get() {
- return g_instance;
-}
+SearchResultImageViewDelegate::SearchResultImageViewDelegate() = default;
-SearchResultImageViewDelegate::SearchResultImageViewDelegate() {
- DCHECK_EQ(nullptr, g_instance);
- g_instance = this;
-}
-
-SearchResultImageViewDelegate::~SearchResultImageViewDelegate() {
- DCHECK_EQ(g_instance, this);
- g_instance = nullptr;
-}
+SearchResultImageViewDelegate::~SearchResultImageViewDelegate() = default;
bool SearchResultImageViewDelegate::CanStartDragForView(
views::View* sender,
diff --git a/ash/app_list/views/search_result_image_view_delegate.h b/ash/app_list/views/search_result_image_view_delegate.h
index fd394f79..fe77eca 100644
--- a/ash/app_list/views/search_result_image_view_delegate.h
+++ b/ash/app_list/views/search_result_image_view_delegate.h
@@ -13,15 +13,9 @@
namespace ash {
class SearchResultImageView;
-// A delegate for `SearchResultImageView` which implements drag and drop. Only a
-// single delegate instance exists at a time and is shared by all existing
-// search result image views in order to support multiselection which requires a
-// shared state.
+// A delegate for `SearchResultImageView` which implements drag and drop.
class ASH_EXPORT SearchResultImageViewDelegate : public views::DragController {
public:
- // Returns the singleton instance.
- static SearchResultImageViewDelegate* Get();
-
SearchResultImageViewDelegate();
SearchResultImageViewDelegate(const SearchResultImageViewDelegate&) = delete;
SearchResultImageViewDelegate& operator=(