From c8412478157c9a506927c3b29d9c73f278d431c4 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 17 Jun 2021 15:42:51 +0200 Subject: [PATCH] fix: color select eyedropper not working within DevTools (#29729) --- chromium_src/BUILD.gn | 2 ++ .../browser/api/electron_api_web_contents.cc | 24 +++++++++++++++++++ shell/browser/api/electron_api_web_contents.h | 6 +++++ shell/browser/ui/inspectable_web_contents.cc | 5 +++- .../ui/inspectable_web_contents_delegate.h | 1 + 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/chromium_src/BUILD.gn b/chromium_src/BUILD.gn index 14d287db3d09..4f0968e18313 100644 --- a/chromium_src/BUILD.gn +++ b/chromium_src/BUILD.gn @@ -142,6 +142,8 @@ static_library("chrome") { if (enable_color_chooser) { sources += [ + "//chrome/browser/devtools/devtools_eye_dropper.cc", + "//chrome/browser/devtools/devtools_eye_dropper.h", "//chrome/browser/platform_util.cc", "//chrome/browser/platform_util.h", "//chrome/browser/ui/browser_dialogs.h", diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 7a84186112d8..ad1f130735b6 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3473,6 +3473,30 @@ void WebContents::DevToolsSearchInPath(int request_id, file_system_path)); } +void WebContents::DevToolsSetEyeDropperActive(bool active) { + auto* web_contents = GetWebContents(); + if (!web_contents) + return; + + if (active) { + eye_dropper_ = std::make_unique( + web_contents, base::BindRepeating(&WebContents::ColorPickedInEyeDropper, + base::Unretained(this))); + } else { + eye_dropper_.reset(); + } +} + +void WebContents::ColorPickedInEyeDropper(int r, int g, int b, int a) { + base::DictionaryValue color; + color.SetInteger("r", r); + color.SetInteger("g", g); + color.SetInteger("b", b); + color.SetInteger("a", a); + inspectable_web_contents_->CallClientFunction( + "DevToolsAPI.eyeDropperPickedColor", &color, nullptr, nullptr); +} + #if defined(TOOLKIT_VIEWS) && !defined(OS_MAC) ui::ImageModel WebContents::GetDevToolsWindowIcon() { return owner_window() ? owner_window()->GetWindowAppIcon() : ui::ImageModel{}; diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 91e241c4ca02..1bdfbdea106d 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -14,6 +14,7 @@ #include "base/memory/weak_ptr.h" #include "base/observer_list.h" #include "base/observer_list_types.h" +#include "chrome/browser/devtools/devtools_eye_dropper.h" #include "chrome/browser/devtools/devtools_file_system_indexer.h" #include "content/common/cursors/webcursor.h" #include "content/common/frame.mojom.h" @@ -658,6 +659,7 @@ class WebContents : public gin::Wrappable, void DevToolsSearchInPath(int request_id, const std::string& file_system_path, const std::string& query) override; + void DevToolsSetEyeDropperActive(bool active) override; // InspectableWebContentsViewDelegate: #if defined(TOOLKIT_VIEWS) && !defined(OS_MAC) @@ -668,6 +670,8 @@ class WebContents : public gin::Wrappable, std::string* class_name) override; #endif + void ColorPickedInEyeDropper(int r, int g, int b, int a); + // DevTools index event callbacks. void OnDevToolsIndexingWorkCalculated(int request_id, const std::string& file_system_path, @@ -740,6 +744,8 @@ class WebContents : public gin::Wrappable, scoped_refptr devtools_file_system_indexer_; + std::unique_ptr eye_dropper_; + ElectronBrowserContext* browser_context_; // The stored InspectableWebContents object. diff --git a/shell/browser/ui/inspectable_web_contents.cc b/shell/browser/ui/inspectable_web_contents.cc index 7b422da122ca..6b4a3002ff15 100644 --- a/shell/browser/ui/inspectable_web_contents.cc +++ b/shell/browser/ui/inspectable_web_contents.cc @@ -789,7 +789,10 @@ void InspectableWebContents::SearchInPath(int request_id, void InspectableWebContents::SetWhitelistedShortcuts( const std::string& message) {} -void InspectableWebContents::SetEyeDropperActive(bool active) {} +void InspectableWebContents::SetEyeDropperActive(bool active) { + if (delegate_) + delegate_->DevToolsSetEyeDropperActive(active); +} void InspectableWebContents::ShowCertificateViewer( const std::string& cert_chain) {} diff --git a/shell/browser/ui/inspectable_web_contents_delegate.h b/shell/browser/ui/inspectable_web_contents_delegate.h index 363ef1e20277..fce434421248 100644 --- a/shell/browser/ui/inspectable_web_contents_delegate.h +++ b/shell/browser/ui/inspectable_web_contents_delegate.h @@ -35,6 +35,7 @@ class InspectableWebContentsDelegate { virtual void DevToolsSearchInPath(int request_id, const std::string& file_system_path, const std::string& query) {} + virtual void DevToolsSetEyeDropperActive(bool active) {} }; } // namespace electron