Skip to content

Commit

Permalink
chore: Move draggable regions implementation from NativeBrowserView i…
Browse files Browse the repository at this point in the history
…nto InspectableWebContentsView (#35007)

* hore: Move draggable regions implementation from NativeBrowserView into InspectableWebContentsView

The draggable regions implementation is related to WebView, so
InspectableWebContentsView is a more appropriate place to put it there.
Also, this refactoring will allow the subsequent extension of the
WebContentsView API, which will eventually replace BrowserView API.

* fix: Lint error

* fix: Adjusted owner-window
  • Loading branch information
daniel-koc committed Oct 17, 2022
1 parent f2c341b commit 23d4a25
Show file tree
Hide file tree
Showing 18 changed files with 397 additions and 360 deletions.
1 change: 1 addition & 0 deletions filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ filenames = {
"shell/browser/ui/inspectable_web_contents.cc",
"shell/browser/ui/inspectable_web_contents.h",
"shell/browser/ui/inspectable_web_contents_delegate.h",
"shell/browser/ui/inspectable_web_contents_view.cc",
"shell/browser/ui/inspectable_web_contents_view.h",
"shell/browser/ui/inspectable_web_contents_view_delegate.cc",
"shell/browser/ui/inspectable_web_contents_view_delegate.h",
Expand Down
14 changes: 13 additions & 1 deletion shell/browser/api/electron_api_browser_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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());
}

BrowserView::~BrowserView() {
Expand All @@ -123,7 +132,10 @@ void BrowserView::WebContentsDestroyed() {

void BrowserView::OnDraggableRegionsUpdated(
const std::vector<mojom::DraggableRegionPtr>& regions) {
view_->UpdateDraggableRegions(regions);
InspectableWebContentsView* iwc_view = view_->GetInspectableWebContentsView();
if (!iwc_view)
return;
iwc_view->UpdateDraggableRegions(regions);
}

// static
Expand Down
4 changes: 2 additions & 2 deletions shell/browser/api/electron_api_browser_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) {
api_web_contents_->NotifyUserActivation();

// Trigger beforeunload events for associated BrowserViews.
for (NativeBrowserView* view : window_->browser_views()) {
auto* vwc = view->web_contents();
for (InspectableWebContentsView* view : window_->inspectable_views()) {
auto* vwc = view->inspectable_web_contents()->GetWebContents();
auto* api_web_contents = api::WebContents::From(vwc);

// Required to make beforeunload handler work.
Expand Down
13 changes: 0 additions & 13 deletions shell/browser/native_browser_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "shell/common/api/api.mojom.h"
#include "third_party/skia/include/core/SkColor.h"

namespace gfx {
Expand Down Expand Up @@ -43,31 +42,19 @@ class NativeBrowserView : public content::WebContentsObserver {
return inspectable_web_contents_;
}

const std::vector<mojom::DraggableRegionPtr>& GetDraggableRegions() const {
return draggable_regions_;
}

InspectableWebContentsView* GetInspectableWebContentsView();

virtual void SetAutoResizeFlags(uint8_t flags) = 0;
virtual void SetBounds(const gfx::Rect& bounds) = 0;
virtual gfx::Rect GetBounds() = 0;
virtual void SetBackgroundColor(SkColor color) = 0;

virtual void UpdateDraggableRegions(
const std::vector<gfx::Rect>& drag_exclude_rects) {}

// Called when the window needs to update its draggable region.
virtual void UpdateDraggableRegions(
const std::vector<mojom::DraggableRegionPtr>& regions) {}

protected:
explicit NativeBrowserView(InspectableWebContents* inspectable_web_contents);
// content::WebContentsObserver:
void WebContentsDestroyed() override;

InspectableWebContents* inspectable_web_contents_;
std::vector<mojom::DraggableRegionPtr> draggable_regions_;
};

} // namespace electron
Expand Down
6 changes: 0 additions & 6 deletions shell/browser/native_browser_view_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class NativeBrowserViewMac : public NativeBrowserView {
void SetBounds(const gfx::Rect& bounds) override;
gfx::Rect GetBounds() override;
void SetBackgroundColor(SkColor color) override;

void UpdateDraggableRegions(
const std::vector<mojom::DraggableRegionPtr>& regions) override;

void UpdateDraggableRegions(
const std::vector<gfx::Rect>& drag_exclude_rects) override;
};

} // namespace electron
Expand Down

0 comments on commit 23d4a25

Please sign in to comment.