diff --git a/docs/api/browser-view.md b/docs/api/browser-view.md index 7f70a11ac70a..c4a38fce0af6 100644 --- a/docs/api/browser-view.md +++ b/docs/api/browser-view.md @@ -94,6 +94,12 @@ Returns `Boolean` - Whether the view is destroyed. Resizes and moves the view to the supplied bounds relative to the window. +#### `view.getBounds()` _Experimental_ + +Returns [`Rectangle`](structures/rectangle.md) + +The `bounds` of this BrowserView instance as `Object`. + #### `view.setBackgroundColor(color)` _Experimental_ * `color` String - Color in `#aarrggbb` or `#argb` form. The alpha channel is diff --git a/shell/browser/api/atom_api_browser_view.cc b/shell/browser/api/atom_api_browser_view.cc index 618c2ecb06e7..8239cf17fc83 100644 --- a/shell/browser/api/atom_api_browser_view.cc +++ b/shell/browser/api/atom_api_browser_view.cc @@ -120,6 +120,10 @@ void BrowserView::SetBounds(const gfx::Rect& bounds) { view_->SetBounds(bounds); } +gfx::Rect BrowserView::GetBounds() { + return view_->GetBounds(); +} + void BrowserView::SetBackgroundColor(const std::string& color_name) { view_->SetBackgroundColor(ParseHexColor(color_name)); } @@ -140,6 +144,7 @@ void BrowserView::BuildPrototype(v8::Isolate* isolate, .MakeDestroyable() .SetMethod("setAutoResize", &BrowserView::SetAutoResize) .SetMethod("setBounds", &BrowserView::SetBounds) + .SetMethod("getBounds", &BrowserView::GetBounds) .SetMethod("setBackgroundColor", &BrowserView::SetBackgroundColor) .SetProperty("webContents", &BrowserView::GetWebContents) .SetProperty("id", &BrowserView::ID); diff --git a/shell/browser/api/atom_api_browser_view.h b/shell/browser/api/atom_api_browser_view.h index 85398536ad3d..5d02406608ab 100644 --- a/shell/browser/api/atom_api_browser_view.h +++ b/shell/browser/api/atom_api_browser_view.h @@ -59,6 +59,7 @@ class BrowserView : public mate::TrackableObject, void SetAutoResize(AutoResizeFlags flags); void SetBounds(const gfx::Rect& bounds); + gfx::Rect GetBounds(); void SetBackgroundColor(const std::string& color_name); v8::Local GetWebContents(); diff --git a/shell/browser/native_browser_view.h b/shell/browser/native_browser_view.h index d5143f89561a..ac2dff293bbf 100644 --- a/shell/browser/native_browser_view.h +++ b/shell/browser/native_browser_view.h @@ -43,6 +43,7 @@ class NativeBrowserView { 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; // Called when the window needs to update its draggable region. diff --git a/shell/browser/native_browser_view_mac.h b/shell/browser/native_browser_view_mac.h index 3ac72a21ea0c..7149952caf04 100644 --- a/shell/browser/native_browser_view_mac.h +++ b/shell/browser/native_browser_view_mac.h @@ -21,6 +21,7 @@ class NativeBrowserViewMac : public NativeBrowserView { void SetAutoResizeFlags(uint8_t flags) override; void SetBounds(const gfx::Rect& bounds) override; + gfx::Rect GetBounds() override; void SetBackgroundColor(SkColor color) override; void UpdateDraggableRegions( diff --git a/shell/browser/native_browser_view_mac.mm b/shell/browser/native_browser_view_mac.mm index dec69f1ad5ac..4caca846213d 100644 --- a/shell/browser/native_browser_view_mac.mm +++ b/shell/browser/native_browser_view_mac.mm @@ -201,6 +201,17 @@ void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) { bounds.width(), bounds.height()); } +gfx::Rect NativeBrowserViewMac::GetBounds() { + NSView* view = + GetInspectableWebContentsView()->GetNativeView().GetNativeNSView(); + const int superview_height = + (view.superview) ? view.superview.frame.size.height : 0; + return gfx::Rect( + view.frame.origin.x, + superview_height - view.frame.origin.y - view.frame.size.height, + view.frame.size.width, view.frame.size.height); +} + void NativeBrowserViewMac::SetBackgroundColor(SkColor color) { auto* view = GetInspectableWebContentsView()->GetNativeView().GetNativeNSView(); diff --git a/shell/browser/native_browser_view_views.cc b/shell/browser/native_browser_view_views.cc index f9b3a541804d..908aa50a2101 100644 --- a/shell/browser/native_browser_view_views.cc +++ b/shell/browser/native_browser_view_views.cc @@ -97,6 +97,10 @@ void NativeBrowserViewViews::SetBounds(const gfx::Rect& bounds) { ResetAutoResizeProportions(); } +gfx::Rect NativeBrowserViewViews::GetBounds() { + return GetInspectableWebContentsView()->GetView()->bounds(); +} + void NativeBrowserViewViews::SetBackgroundColor(SkColor color) { auto* view = GetInspectableWebContentsView()->GetView(); view->SetBackground(views::CreateSolidBackground(color)); diff --git a/shell/browser/native_browser_view_views.h b/shell/browser/native_browser_view_views.h index b2ec13e52a91..35fdbcbbcafa 100644 --- a/shell/browser/native_browser_view_views.h +++ b/shell/browser/native_browser_view_views.h @@ -24,6 +24,7 @@ class NativeBrowserViewViews : public NativeBrowserView { // NativeBrowserView: void SetAutoResizeFlags(uint8_t flags) override; void SetBounds(const gfx::Rect& bounds) override; + gfx::Rect GetBounds() override; void SetBackgroundColor(SkColor color) override; private: diff --git a/spec-main/api-browser-view-spec.ts b/spec-main/api-browser-view-spec.ts index 9e1800aaaafb..b48eb17d6d45 100644 --- a/spec-main/api-browser-view-spec.ts +++ b/spec-main/api-browser-view-spec.ts @@ -92,6 +92,15 @@ describe('BrowserView module', () => { }) }) + describe('BrowserView.getBounds()', () => { + it('returns the current bounds', () => { + view = new BrowserView() + const bounds = { x: 10, y: 20, width: 30, height: 40 } + view.setBounds(bounds) + expect(view.getBounds()).to.deep.equal(bounds) + }) + }) + describe('BrowserWindow.setBrowserView()', () => { it('does not throw for valid args', () => { view = new BrowserView()