Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: draggable regions calculation in BrowserWindow/BrowserView #26753

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions shell/browser/api/electron_api_browser_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,8 @@ void BrowserWindow::OnWindowResize() {
if (!draggable_regions_.empty()) {
UpdateDraggableRegions(draggable_regions_);
} else {
// Ensure draggable bounds are recalculated for BrowserViews if any exist.
auto browser_views = window_->browser_views();
for (NativeBrowserView* view : browser_views) {
view->UpdateDraggableRegions(draggable_regions_);
for (NativeBrowserView* view : window_->browser_views()) {
view->UpdateDraggableRegions(view->GetDraggableRegions());
}
}
#endif
Expand Down
18 changes: 7 additions & 11 deletions shell/browser/api/electron_api_browser_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,21 @@

// Draggable regions are implemented by having the whole web view draggable
// and overlaying regions that are not draggable.
if (&draggable_regions_ != &regions) {
draggable_regions_.clear();
for (const auto& r : regions)
draggable_regions_.push_back(r.Clone());
}

auto browser_views = window_->browser_views();
for (NativeBrowserView* view : browser_views) {
view->UpdateDraggableRegions(draggable_regions_);
}
if (&draggable_regions_ != &regions)
draggable_regions_ = mojo::Clone(regions);

std::vector<gfx::Rect> drag_exclude_rects;
if (regions.empty()) {
drag_exclude_rects.push_back(gfx::Rect(0, 0, webViewWidth, webViewHeight));
drag_exclude_rects.emplace_back(0, 0, webViewWidth, webViewHeight);
} else {
drag_exclude_rects = CalculateNonDraggableRegions(
DraggableRegionsToSkRegion(regions), webViewWidth, webViewHeight);
}

for (NativeBrowserView* view : window_->browser_views()) {
view->UpdateDraggableRegions(drag_exclude_rects);
}

// Create and add a ControlRegionView for each region that needs to be
// excluded from the dragging.
for (const auto& rect : drag_exclude_rects) {
Expand Down
8 changes: 8 additions & 0 deletions shell/browser/native_browser_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class NativeBrowserView : public content::WebContentsObserver {
return inspectable_web_contents_;
}

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

InspectableWebContentsView* GetInspectableWebContentsView();
content::WebContents* GetWebContents();

Expand All @@ -48,6 +52,9 @@ class NativeBrowserView : public content::WebContentsObserver {
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) {}
Expand All @@ -58,6 +65,7 @@ class NativeBrowserView : public content::WebContentsObserver {
void WebContentsDestroyed() override;

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

private:
DISALLOW_COPY_AND_ASSIGN(NativeBrowserView);
Expand Down
4 changes: 2 additions & 2 deletions shell/browser/native_browser_view_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class NativeBrowserViewMac : public NativeBrowserView {
void UpdateDraggableRegions(
const std::vector<mojom::DraggableRegionPtr>& regions) override;

private:
std::vector<mojom::DraggableRegionPtr> draggable_regions_;
void UpdateDraggableRegions(
const std::vector<gfx::Rect>& drag_exclude_rects) override;

DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewMac);
};
Expand Down
47 changes: 27 additions & 20 deletions shell/browser/native_browser_view_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ - (BOOL)mouseDownCanMoveWindow {
}

void NativeBrowserViewMac::UpdateDraggableRegions(
const std::vector<mojom::DraggableRegionPtr>& regions) {
const std::vector<gfx::Rect>& drag_exclude_rects) {
if (!inspectable_web_contents_)
return;
auto* web_contents = inspectable_web_contents_->GetWebContents();
Expand All @@ -246,25 +246,6 @@ - (BOOL)mouseDownCanMoveWindow {
NSView* window_content_view = inspectable_view.superview;
const auto window_content_view_height = NSHeight(window_content_view.bounds);

NSInteger webViewWidth = NSWidth([web_view bounds]);
NSInteger webViewHeight = NSHeight([web_view bounds]);

std::vector<gfx::Rect> drag_exclude_rects;
if (regions.empty()) {
drag_exclude_rects.push_back(gfx::Rect(0, 0, webViewWidth, webViewHeight));
} else {
drag_exclude_rects = CalculateNonDraggableRegions(
DraggableRegionsToSkRegion(regions), webViewWidth, webViewHeight);
}

// Draggable regions are implemented by having the whole web view draggable
// and overlaying regions that are not draggable.
if (&draggable_regions_ != &regions) {
draggable_regions_.clear();
for (const auto& r : regions)
draggable_regions_.push_back(r.Clone());
}

// 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<NSArray> subviews([[web_view subviews] copy]);
Expand Down Expand Up @@ -295,6 +276,32 @@ - (BOOL)mouseDownCanMoveWindow {
}
}

void NativeBrowserViewMac::UpdateDraggableRegions(
const std::vector<mojom::DraggableRegionPtr>& regions) {
if (!inspectable_web_contents_)
return;
auto* web_contents = inspectable_web_contents_->GetWebContents();
NSView* web_view = web_contents->GetNativeView().GetNativeNSView();

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_ != &regions)
draggable_regions_ = mojo::Clone(regions);

std::vector<gfx::Rect> drag_exclude_rects;
if (regions.empty()) {
drag_exclude_rects.emplace_back(0, 0, webViewWidth, webViewHeight);
} else {
drag_exclude_rects = CalculateNonDraggableRegions(
DraggableRegionsToSkRegion(regions), webViewWidth, webViewHeight);
}

UpdateDraggableRegions(drag_exclude_rects);
}

// static
NativeBrowserView* NativeBrowserView::Create(
InspectableWebContents* inspectable_web_contents) {
Expand Down