Skip to content

Commit

Permalink
fix: draggable regions calculation in BrowserWindow/BrowserView (elec…
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored Dec 1, 2020
1 parent 4e3e3d4 commit acfbbe9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
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 @@ -309,10 +309,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 @@ -71,25 +71,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,13 +40,20 @@ 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) {}
Expand All @@ -57,6 +64,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 @@ -238,7 +238,7 @@ - (void)drawRect:(NSRect)aRect {
}

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 @@ -248,25 +248,6 @@ - (void)drawRect:(NSRect)aRect {
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 @@ -297,6 +278,32 @@ - (void)drawRect:(NSRect)aRect {
}
}

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

0 comments on commit acfbbe9

Please sign in to comment.