From 184ac2b382e8324a0b3db1944fd17cdd6ccffff9 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Mon, 7 Nov 2022 10:15:57 -0800 Subject: [PATCH] refactor: move draggable regions to WebContents (#36230) --- filenames.gni | 1 + shell/browser/api/electron_api_base_window.cc | 3 + .../browser/api/electron_api_browser_view.cc | 25 +++----- shell/browser/api/electron_api_browser_view.h | 10 +-- .../api/electron_api_browser_window.cc | 13 +--- .../browser/api/electron_api_browser_window.h | 6 -- .../browser/api/electron_api_web_contents.cc | 3 +- shell/browser/api/electron_api_web_contents.h | 6 ++ .../api/electron_api_web_contents_view.cc | 11 ++++ .../api/electron_api_web_contents_view.h | 7 ++- shell/browser/draggable_region_provider.h | 13 ++++ shell/browser/native_browser_view_mac.mm | 3 - shell/browser/native_window.cc | 26 +++++++- shell/browser/native_window.h | 31 ++-------- shell/browser/native_window_mac.mm | 6 +- shell/browser/native_window_views.cc | 12 +--- .../ui/inspectable_web_contents_view.h | 10 --- .../ui/inspectable_web_contents_view_mac.h | 2 - .../ui/inspectable_web_contents_view_mac.mm | 61 ------------------- shell/browser/ui/views/frameless_view.cc | 21 ++----- .../inspectable_web_contents_view_views.cc | 21 ------- .../inspectable_web_contents_view_views.h | 7 --- 22 files changed, 96 insertions(+), 202 deletions(-) create mode 100644 shell/browser/draggable_region_provider.h diff --git a/filenames.gni b/filenames.gni index 7f647d70a81e..b18435886ba7 100644 --- a/filenames.gni +++ b/filenames.gni @@ -358,6 +358,7 @@ filenames = { "shell/browser/child_web_contents_tracker.h", "shell/browser/cookie_change_notifier.cc", "shell/browser/cookie_change_notifier.h", + "shell/browser/draggable_region_provider.h", "shell/browser/electron_api_ipc_handler_impl.cc", "shell/browser/electron_api_ipc_handler_impl.h", "shell/browser/electron_autofill_driver.cc", diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index 6ecc2684d106..79648aee3fbe 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -766,6 +766,7 @@ void BaseWindow::AddBrowserView(gin::Handle browser_view) { } window_->AddBrowserView(browser_view->view()); + window_->AddDraggableRegionProvider(browser_view.get()); browser_view->SetOwnerWindow(this); browser_views_[browser_view->ID()].Reset(isolate(), browser_view.ToV8()); } @@ -775,6 +776,7 @@ void BaseWindow::RemoveBrowserView(gin::Handle browser_view) { auto iter = browser_views_.find(browser_view->ID()); if (iter != browser_views_.end()) { window_->RemoveBrowserView(browser_view->view()); + window_->RemoveDraggableRegionProvider(browser_view.get()); browser_view->SetOwnerWindow(nullptr); iter->second.Reset(); browser_views_.erase(iter); @@ -1124,6 +1126,7 @@ void BaseWindow::ResetBrowserViews() { DCHECK_EQ(owner_window, this); browser_view->SetOwnerWindow(nullptr); window_->RemoveBrowserView(browser_view->view()); + window_->RemoveDraggableRegionProvider(browser_view.get()); browser_view->SetOwnerWindow(nullptr); } diff --git a/shell/browser/api/electron_api_browser_view.cc b/shell/browser/api/electron_api_browser_view.cc index 80971de6c479..00317909e763 100644 --- a/shell/browser/api/electron_api_browser_view.cc +++ b/shell/browser/api/electron_api_browser_view.cc @@ -20,6 +20,7 @@ #include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/node_includes.h" #include "shell/common/options_switches.h" +#include "ui/base/hit_test.h" #include "ui/gfx/geometry/rect.h" namespace gin { @@ -105,16 +106,16 @@ void BrowserView::SetOwnerWindow(BaseWindow* window) { if (web_contents()) web_contents()->SetOwnerWindow(window ? window->window() : nullptr); - if (owner_window_.get()) { - owner_window_->window()->remove_inspectable_view( - view_->GetInspectableWebContentsView()); - } - owner_window_ = window ? window->GetWeakPtr() : nullptr; +} - if (owner_window_.get() && view_->GetInspectableWebContentsView()) - owner_window_->window()->add_inspectable_view( - view_->GetInspectableWebContentsView()); +int BrowserView::NonClientHitTest(const gfx::Point& point) { + gfx::Rect bounds = GetBounds(); + gfx::Point local_point(point.x() - bounds.x(), point.y() - bounds.y()); + SkRegion* region = api_web_contents_->draggable_region(); + if (region && region->contains(local_point.x(), local_point.y())) + return HTCAPTION; + return HTNOWHERE; } BrowserView::~BrowserView() { @@ -130,14 +131,6 @@ void BrowserView::WebContentsDestroyed() { Unpin(); } -void BrowserView::OnDraggableRegionsUpdated( - const std::vector& regions) { - InspectableWebContentsView* iwc_view = view_->GetInspectableWebContentsView(); - if (!iwc_view) - return; - iwc_view->UpdateDraggableRegions(regions); -} - // static gin::Handle BrowserView::New(gin_helper::ErrorThrower thrower, gin::Arguments* args) { diff --git a/shell/browser/api/electron_api_browser_view.h b/shell/browser/api/electron_api_browser_view.h index 7d480874cb9b..a0b517c03312 100644 --- a/shell/browser/api/electron_api_browser_view.h +++ b/shell/browser/api/electron_api_browser_view.h @@ -12,6 +12,7 @@ #include "content/public/browser/web_contents_observer.h" #include "gin/handle.h" #include "gin/wrappable.h" +#include "shell/browser/draggable_region_provider.h" #include "shell/browser/extended_web_contents_observer.h" #include "shell/browser/native_browser_view.h" #include "shell/browser/native_window.h" @@ -37,7 +38,8 @@ class BrowserView : public gin::Wrappable, public gin_helper::Constructible, public gin_helper::Pinnable, public content::WebContentsObserver, - public ExtendedWebContentsObserver { + public ExtendedWebContentsObserver, + public DraggableRegionProvider { public: // gin_helper::Constructible static gin::Handle New(gin_helper::ErrorThrower thrower, @@ -58,6 +60,8 @@ class BrowserView : public gin::Wrappable, int32_t ID() const { return id_; } + int NonClientHitTest(const gfx::Point& point) override; + // disable copy BrowserView(const BrowserView&) = delete; BrowserView& operator=(const BrowserView&) = delete; @@ -69,10 +73,6 @@ class BrowserView : public gin::Wrappable, // content::WebContentsObserver: void WebContentsDestroyed() override; - // ExtendedWebContentsObserver: - void OnDraggableRegionsUpdated( - const std::vector& regions) override; - private: void SetAutoResize(AutoResizeFlags flags); void SetBounds(const gfx::Rect& bounds); diff --git a/shell/browser/api/electron_api_browser_window.cc b/shell/browser/api/electron_api_browser_window.cc index 57478700911a..71ee355195d2 100644 --- a/shell/browser/api/electron_api_browser_window.cc +++ b/shell/browser/api/electron_api_browser_window.cc @@ -83,6 +83,7 @@ BrowserWindow::BrowserWindow(gin::Arguments* args, gin::Handle web_contents_view = WebContentsView::Create(isolate, web_preferences); DCHECK(web_contents_view.get()); + window_->AddDraggableRegionProvider(web_contents_view.get()); // Save a reference of the WebContents. gin::Handle web_contents = @@ -146,14 +147,6 @@ void BrowserWindow::OnRendererResponsive(content::RenderProcessHost*) { Emit("responsive"); } -void BrowserWindow::OnDraggableRegionsUpdated( - const std::vector& regions) { - if (window_->has_frame()) - return; - - window_->UpdateDraggableRegions(regions); -} - void BrowserWindow::OnSetContentBounds(const gfx::Rect& rect) { // window.resizeTo(...) // window.moveTo(...) @@ -204,8 +197,8 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) { api_web_contents_->NotifyUserActivation(); // Trigger beforeunload events for associated BrowserViews. - for (InspectableWebContentsView* view : window_->inspectable_views()) { - auto* vwc = view->inspectable_web_contents()->GetWebContents(); + for (NativeBrowserView* view : window_->browser_views()) { + auto* vwc = view->GetInspectableWebContents()->GetWebContents(); auto* api_web_contents = api::WebContents::From(vwc); // Required to make beforeunload handler work. diff --git a/shell/browser/api/electron_api_browser_window.h b/shell/browser/api/electron_api_browser_window.h index 127ac78df46c..7c181ff9ae21 100644 --- a/shell/browser/api/electron_api_browser_window.h +++ b/shell/browser/api/electron_api_browser_window.h @@ -51,8 +51,6 @@ class BrowserWindow : public BaseWindow, // ExtendedWebContentsObserver: void OnCloseContents() override; - void OnDraggableRegionsUpdated( - const std::vector& regions) override; void OnSetContentBounds(const gfx::Rect& rect) override; void OnActivateContents() override; void OnPageTitleUpdated(const std::u16string& title, @@ -90,10 +88,6 @@ class BrowserWindow : public BaseWindow, private: // Helpers. - // Called when the window needs to update its draggable region. - void UpdateDraggableRegions( - const std::vector& regions); - // Schedule a notification unresponsive event. void ScheduleUnresponsiveEvent(int ms); diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 675372b7f38f..27a3c3ba8f63 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1875,8 +1875,7 @@ void WebContents::MessageHost(const std::string& channel, void WebContents::UpdateDraggableRegions( std::vector regions) { - for (ExtendedWebContentsObserver& observer : observers_) - observer.OnDraggableRegionsUpdated(regions); + draggable_region_ = DraggableRegionsToSkRegion(regions); } void WebContents::DidStartNavigation( diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index e838d0a3256f..835610c50e6a 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -77,6 +77,8 @@ class Arguments; class ExclusiveAccessManager; +class SkRegion; + namespace electron { class ElectronBrowserContext; @@ -438,6 +440,8 @@ class WebContents : public ExclusiveAccessContext, // content::RenderWidgetHost::InputEventObserver: void OnInputEvent(const blink::WebInputEvent& event) override; + SkRegion* draggable_region() { return draggable_region_.get(); } + // disable copy WebContents(const WebContents&) = delete; WebContents& operator=(const WebContents&) = delete; @@ -819,6 +823,8 @@ class WebContents : public ExclusiveAccessContext, // Stores the frame thats currently in fullscreen, nullptr if there is none. content::RenderFrameHost* fullscreen_frame_ = nullptr; + std::unique_ptr draggable_region_; + base::WeakPtrFactory weak_factory_{this}; }; diff --git a/shell/browser/api/electron_api_web_contents_view.cc b/shell/browser/api/electron_api_web_contents_view.cc index 7b74cb238cd0..a81f87e18399 100644 --- a/shell/browser/api/electron_api_web_contents_view.cc +++ b/shell/browser/api/electron_api_web_contents_view.cc @@ -14,6 +14,8 @@ #include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/node_includes.h" +#include "third_party/skia/include/core/SkRegion.h" +#include "ui/base/hit_test.h" #if BUILDFLAG(IS_MAC) #include "shell/browser/ui/cocoa/delayed_native_view_host.h" @@ -50,6 +52,15 @@ gin::Handle WebContentsView::GetWebContents(v8::Isolate* isolate) { return gin::CreateHandle(isolate, api_web_contents_); } +int WebContentsView::NonClientHitTest(const gfx::Point& point) { + gfx::Point local_point(point); + views::View::ConvertPointFromWidget(view(), &local_point); + SkRegion* region = api_web_contents_->draggable_region(); + if (region && region->contains(local_point.x(), local_point.y())) + return HTCAPTION; + return HTNOWHERE; +} + void WebContentsView::WebContentsDestroyed() { api_web_contents_ = nullptr; web_contents_.Reset(); diff --git a/shell/browser/api/electron_api_web_contents_view.h b/shell/browser/api/electron_api_web_contents_view.h index 057e31ec18b8..aa2cd00912f9 100644 --- a/shell/browser/api/electron_api_web_contents_view.h +++ b/shell/browser/api/electron_api_web_contents_view.h @@ -7,6 +7,7 @@ #include "content/public/browser/web_contents_observer.h" #include "shell/browser/api/electron_api_view.h" +#include "shell/browser/draggable_region_provider.h" namespace gin_helper { class Dictionary; @@ -16,7 +17,9 @@ namespace electron::api { class WebContents; -class WebContentsView : public View, public content::WebContentsObserver { +class WebContentsView : public View, + public content::WebContentsObserver, + public DraggableRegionProvider { public: // Create a new instance of WebContentsView. static gin::Handle Create( @@ -33,6 +36,8 @@ class WebContentsView : public View, public content::WebContentsObserver { // Public APIs. gin::Handle GetWebContents(v8::Isolate* isolate); + int NonClientHitTest(const gfx::Point& point) override; + protected: // Takes an existing WebContents. WebContentsView(v8::Isolate* isolate, gin::Handle web_contents); diff --git a/shell/browser/draggable_region_provider.h b/shell/browser/draggable_region_provider.h new file mode 100644 index 000000000000..69f9e8c6d121 --- /dev/null +++ b/shell/browser/draggable_region_provider.h @@ -0,0 +1,13 @@ +// Copyright (c) 2022 Salesforce, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ELECTRON_SHELL_BROWSER_DRAGGABLE_REGION_PROVIDER_H_ +#define ELECTRON_SHELL_BROWSER_DRAGGABLE_REGION_PROVIDER_H_ + +class DraggableRegionProvider { + public: + virtual int NonClientHitTest(const gfx::Point& point) = 0; +}; + +#endif // ELECTRON_SHELL_BROWSER_DRAGGABLE_REGION_PROVIDER_H_ diff --git a/shell/browser/native_browser_view_mac.mm b/shell/browser/native_browser_view_mac.mm index 4ed2ebc3f660..63cc06463f32 100644 --- a/shell/browser/native_browser_view_mac.mm +++ b/shell/browser/native_browser_view_mac.mm @@ -73,9 +73,6 @@ void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) { superview_height - bounds.y() - bounds.height() + titlebar_height; view.frame = NSMakeRect(bounds.x(), new_height, bounds.width(), bounds.height()); - - // Ensure draggable regions are properly updated to reflect new bounds. - iwc_view->UpdateDraggableRegions(iwc_view->GetDraggableRegions()); } gfx::Rect NativeBrowserViewMac::GetBounds() { diff --git a/shell/browser/native_window.cc b/shell/browser/native_window.cc index 995419d0a083..fd1be39c6c5f 100644 --- a/shell/browser/native_window.cc +++ b/shell/browser/native_window.cc @@ -21,6 +21,7 @@ #include "shell/common/gin_helper/persistent_dictionary.h" #include "shell/common/options_switches.h" #include "third_party/skia/include/core/SkRegion.h" +#include "ui/base/hit_test.h" #include "ui/views/widget/widget.h" #if BUILDFLAG(IS_WIN) @@ -685,9 +686,28 @@ void NativeWindow::NotifyWindowMessage(UINT message, } #endif -void NativeWindow::UpdateDraggableRegions( - const std::vector& regions) { - draggable_region_ = DraggableRegionsToSkRegion(regions); +int NativeWindow::NonClientHitTest(const gfx::Point& point) { + for (auto* provider : draggable_region_providers_) { + int hit = provider->NonClientHitTest(point); + if (hit != HTNOWHERE) + return hit; + } + return HTNOWHERE; +} + +void NativeWindow::AddDraggableRegionProvider( + DraggableRegionProvider* provider) { + if (std::find(draggable_region_providers_.begin(), + draggable_region_providers_.end(), + provider) == draggable_region_providers_.end()) { + draggable_region_providers_.push_back(provider); + } +} + +void NativeWindow::RemoveDraggableRegionProvider( + DraggableRegionProvider* provider) { + draggable_region_providers_.remove_if( + [&provider](DraggableRegionProvider* p) { return p == provider; }); } views::Widget* NativeWindow::GetWidget() { diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index 6eccbf2a9805..493207833d21 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -17,6 +17,7 @@ #include "content/public/browser/desktop_media_id.h" #include "content/public/browser/web_contents_user_data.h" #include "extensions/browser/app_window/size_constraints.h" +#include "shell/browser/draggable_region_provider.h" #include "shell/browser/native_window_observer.h" #include "shell/browser/ui/inspectable_web_contents_view.h" #include "shell/common/api/api.mojom.h" @@ -351,11 +352,6 @@ class NativeWindow : public base::SupportsUserData, return fullscreen_transition_type_; } - SkRegion const* draggable_region() const { return draggable_region_.get(); } - - void UpdateDraggableRegions( - const std::vector& regions); - views::Widget* widget() const { return widget_.get(); } views::View* content_view() const { return content_view_; } @@ -384,12 +380,12 @@ class NativeWindow : public base::SupportsUserData, std::list browser_views() const { return browser_views_; } - std::list inspectable_views() const { - return inspectable_views_; - } - int32_t window_id() const { return next_id_; } + int NonClientHitTest(const gfx::Point& point); + void AddDraggableRegionProvider(DraggableRegionProvider* provider); + void RemoveDraggableRegionProvider(DraggableRegionProvider* provider); + protected: friend class api::BrowserView; @@ -410,16 +406,6 @@ class NativeWindow : public base::SupportsUserData, [&browser_view](NativeBrowserView* n) { return (n == browser_view); }); } - void add_inspectable_view(InspectableWebContentsView* inspectable_view) { - inspectable_views_.push_back(inspectable_view); - } - void remove_inspectable_view(InspectableWebContentsView* inspectable_view) { - inspectable_views_.remove_if( - [&inspectable_view](InspectableWebContentsView* n) { - return (n == inspectable_view); - }); - } - // The boolean parsing of the "titleBarOverlay" option bool titlebar_overlay_ = false; @@ -483,8 +469,7 @@ class NativeWindow : public base::SupportsUserData, // The browser view layer. std::list browser_views_; - // The inspectable webContents views. - std::list inspectable_views_; + std::list draggable_region_providers_; // Observers of this window. base::ObserverList observers_; @@ -494,10 +479,6 @@ class NativeWindow : public base::SupportsUserData, gfx::Rect overlay_rect_; - // For custom drag, the whole window is non-draggable and the draggable region - // has to been explicitly provided. - std::unique_ptr draggable_region_; // used in custom drag. - base::WeakPtrFactory weak_factory_{this}; }; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index afd3add3ae59..a651feb475a4 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1664,9 +1664,9 @@ class NativeAppWindowFrameViewMac : public views::NativeFrameViewMac { // Check for possible draggable region in the client area for the frameless // window. - SkRegion const* draggable_region = native_window_->draggable_region(); - if (draggable_region && draggable_region->contains(point.x(), point.y())) - return HTCAPTION; + int contents_hit_test = native_window_->NonClientHitTest(point); + if (contents_hit_test != HTNOWHERE) + return contents_hit_test; return HTCLIENT; } diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index de0d63c58781..436f9d9d137b 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -1594,17 +1594,7 @@ bool NativeWindowViews::ShouldDescendIntoChildForEventHandling( const gfx::Point& location) { // App window should claim mouse events that fall within any BrowserViews' // draggable region. - for (auto* view : inspectable_views()) { - auto* inspectable_view = - static_cast(view); - if (inspectable_view->IsContainedInDraggableRegion(content_view(), - location)) - return false; - } - - // App window should claim mouse events that fall within the draggable region. - if (draggable_region() && - draggable_region()->contains(location.x(), location.y())) + if (NonClientHitTest(location) != HTNOWHERE) return false; // And the events on border for dragging resizable frameless window. diff --git a/shell/browser/ui/inspectable_web_contents_view.h b/shell/browser/ui/inspectable_web_contents_view.h index c46e5234d01b..7e6d6845c031 100644 --- a/shell/browser/ui/inspectable_web_contents_view.h +++ b/shell/browser/ui/inspectable_web_contents_view.h @@ -41,10 +41,6 @@ class InspectableWebContentsView { } InspectableWebContentsViewDelegate* GetDelegate() const { return delegate_; } - const std::vector& GetDraggableRegions() const { - return draggable_regions_; - } - #if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC) // Returns the container control, which has devtools view attached. virtual views::View* GetView() = 0; @@ -66,16 +62,10 @@ class InspectableWebContentsView { const DevToolsContentsResizingStrategy& strategy) = 0; virtual void SetTitle(const std::u16string& title) = 0; - // Called when the window needs to update its draggable region. - virtual void UpdateDraggableRegions( - const std::vector& regions) = 0; - protected: // Owns us. InspectableWebContents* inspectable_web_contents_; - std::vector draggable_regions_; - private: InspectableWebContentsViewDelegate* delegate_ = nullptr; // weak references. }; diff --git a/shell/browser/ui/inspectable_web_contents_view_mac.h b/shell/browser/ui/inspectable_web_contents_view_mac.h index f65fe7d75c35..62c1e9440283 100644 --- a/shell/browser/ui/inspectable_web_contents_view_mac.h +++ b/shell/browser/ui/inspectable_web_contents_view_mac.h @@ -34,8 +34,6 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView { void SetContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy) override; void SetTitle(const std::u16string& title) override; - void UpdateDraggableRegions( - const std::vector& regions) override; private: base::scoped_nsobject view_; diff --git a/shell/browser/ui/inspectable_web_contents_view_mac.mm b/shell/browser/ui/inspectable_web_contents_view_mac.mm index 428f09e232a7..10440e0e21c6 100644 --- a/shell/browser/ui/inspectable_web_contents_view_mac.mm +++ b/shell/browser/ui/inspectable_web_contents_view_mac.mm @@ -267,65 +267,4 @@ void InspectableWebContentsViewMac::SetTitle(const std::u16string& title) { [view_ setTitle:base::SysUTF16ToNSString(title)]; } -void InspectableWebContentsViewMac::UpdateDraggableRegions( - const std::vector& regions) { - auto* web_contents = inspectable_web_contents()->GetWebContents(); - NSView* web_view = web_contents->GetNativeView().GetNativeNSView(); - NSView* inspectable_view = GetNativeView().GetNativeNSView(); - NSView* inspectable_superview = inspectable_view.superview; - - NSInteger webViewWidth = NSWidth([web_view bounds]); - NSInteger webViewHeight = NSHeight([web_view bounds]); - - // Draggable regions are implemented by having the whole web view draggable - // and overlaying regions that are not draggable. - if (&draggable_regions_ != ®ions) - draggable_regions_ = mojo::Clone(regions); - - std::vector drag_exclude_rects; - if (draggable_regions_.empty()) { - drag_exclude_rects.emplace_back(0, 0, webViewWidth, webViewHeight); - } else { - drag_exclude_rects = CalculateNonDraggableRegions( - DraggableRegionsToSkRegion(draggable_regions_), webViewWidth, - webViewHeight); - } - - // Remove all DragRegionViews that were added last time. Note that we need - // to copy the `subviews` array to avoid mutation during iteration. - base::scoped_nsobject subviews([[web_view subviews] copy]); - for (NSView* subview in subviews.get()) { - if ([subview isKindOfClass:[DragRegionView class]]) - [subview removeFromSuperview]; - } - - // Create one giant NSView that is draggable. - base::scoped_nsobject drag_region_view( - [[DragRegionView alloc] initWithFrame:web_view.bounds]); - [web_view addSubview:drag_region_view]; - - // Then, on top of that, add "exclusion zones". - const int superview_height = - (inspectable_superview) ? inspectable_superview.frame.size.height : 0; - if (!inspectable_superview) - inspectable_superview = inspectable_view; - const int offset_x = inspectable_view.frame.origin.x; - const int offset_y = superview_height - inspectable_view.frame.origin.y - - inspectable_view.frame.size.height; - for (const auto& rect : drag_exclude_rects) { - const auto x = rect.x() + offset_x; - const auto y = superview_height - (rect.bottom() + offset_y); - const auto exclude_rect = NSMakeRect(x, y, rect.width(), rect.height()); - - const auto drag_region_view_exclude_rect = - [inspectable_superview convertRect:exclude_rect - toView:drag_region_view]; - - base::scoped_nsobject exclude_drag_region_view( - [[ExcludeDragRegionView alloc] - initWithFrame:drag_region_view_exclude_rect]); - [drag_region_view addSubview:exclude_drag_region_view]; - } -} - } // namespace electron diff --git a/shell/browser/ui/views/frameless_view.cc b/shell/browser/ui/views/frameless_view.cc index 8398b9e5a53a..392618dfd8ad 100644 --- a/shell/browser/ui/views/frameless_view.cc +++ b/shell/browser/ui/views/frameless_view.cc @@ -76,30 +76,19 @@ gfx::Rect FramelessView::GetWindowBoundsForClientBounds( return window_bounds; } -int FramelessView::NonClientHitTest(const gfx::Point& cursor) { +int FramelessView::NonClientHitTest(const gfx::Point& point) { if (frame_->IsFullscreen()) return HTCLIENT; - // Check attached BrowserViews for potential draggable areas. - for (auto* view : window_->inspectable_views()) { - auto* inspectable_view = - static_cast(view); - if (inspectable_view->IsContainedInDraggableRegion(window_->content_view(), - cursor)) - return HTCAPTION; - } + int contents_hit_test = window_->NonClientHitTest(point); + if (contents_hit_test != HTNOWHERE) + return contents_hit_test; // Support resizing frameless window by dragging the border. - int frame_component = ResizingBorderHitTest(cursor); + int frame_component = ResizingBorderHitTest(point); if (frame_component != HTNOWHERE) return frame_component; - // Check for possible draggable region in the client area for the frameless - // window. - const SkRegion* draggable_region = window_->draggable_region(); - if (draggable_region && draggable_region->contains(cursor.x(), cursor.y())) - return HTCAPTION; - return HTCLIENT; } diff --git a/shell/browser/ui/views/inspectable_web_contents_view_views.cc b/shell/browser/ui/views/inspectable_web_contents_view_views.cc index 4bf598bdce16..7c76417fa3fb 100644 --- a/shell/browser/ui/views/inspectable_web_contents_view_views.cc +++ b/shell/browser/ui/views/inspectable_web_contents_view_views.cc @@ -105,19 +105,6 @@ InspectableWebContentsViewViews::~InspectableWebContentsViewViews() { devtools_window_->GetWindowBoundsInScreen()); } -bool InspectableWebContentsViewViews::IsContainedInDraggableRegion( - views::View* root_view, - const gfx::Point& location) { - if (!draggable_region_.get()) - return false; - // Draggable regions are defined relative to the web contents. - gfx::Point point_in_contents_web_view_coords(location); - views::View::ConvertPointToTarget(root_view, this, - &point_in_contents_web_view_coords); - return draggable_region_->contains(point_in_contents_web_view_coords.x(), - point_in_contents_web_view_coords.y()); -} - views::View* InspectableWebContentsViewViews::GetView() { return this; } @@ -227,14 +214,6 @@ void InspectableWebContentsViewViews::SetTitle(const std::u16string& title) { } } -void InspectableWebContentsViewViews::UpdateDraggableRegions( - const std::vector& regions) { - if (&draggable_regions_ == ®ions) - return; - draggable_regions_ = mojo::Clone(regions); - draggable_region_ = DraggableRegionsToSkRegion(draggable_regions_); -} - void InspectableWebContentsViewViews::Layout() { if (!devtools_web_view_->GetVisible()) { contents_web_view_->SetBoundsRect(GetContentsBounds()); diff --git a/shell/browser/ui/views/inspectable_web_contents_view_views.h b/shell/browser/ui/views/inspectable_web_contents_view_views.h index d77a97766c87..25f7f075c319 100644 --- a/shell/browser/ui/views/inspectable_web_contents_view_views.h +++ b/shell/browser/ui/views/inspectable_web_contents_view_views.h @@ -29,9 +29,6 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView, InspectableWebContents* inspectable_web_contents); ~InspectableWebContentsViewViews() override; - bool IsContainedInDraggableRegion(views::View* root_view, - const gfx::Point& location); - // InspectableWebContentsView: views::View* GetView() override; views::View* GetWebView() override; @@ -43,8 +40,6 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView, void SetContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy) override; void SetTitle(const std::u16string& title) override; - void UpdateDraggableRegions( - const std::vector& regions) override; // views::View: void Layout() override; @@ -61,8 +56,6 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView, bool devtools_visible_ = false; views::WidgetDelegate* devtools_window_delegate_ = nullptr; std::u16string title_; - - std::unique_ptr draggable_region_; }; } // namespace electron