feat: add getBounds() method for BrowserView (#19370)

* implement getBounds on mac

* add Linux/Win impl

* add test

* add docs
This commit is contained in:
Micha Hanselmann 2019-07-29 19:43:05 -07:00 committed by Shelley Vohr
parent c06007175f
commit 42a483ad27
9 changed files with 39 additions and 0 deletions

View file

@ -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

View file

@ -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);

View file

@ -59,6 +59,7 @@ class BrowserView : public mate::TrackableObject<BrowserView>,
void SetAutoResize(AutoResizeFlags flags);
void SetBounds(const gfx::Rect& bounds);
gfx::Rect GetBounds();
void SetBackgroundColor(const std::string& color_name);
v8::Local<v8::Value> GetWebContents();

View file

@ -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.

View file

@ -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(

View file

@ -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();

View file

@ -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));

View file

@ -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:

View file

@ -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()