Skip to content

Commit 06fcf2c

Browse files
committed
Add support for BrowserView autoresizing
1 parent 638eae1 commit 06fcf2c

File tree

9 files changed

+121
-4
lines changed

9 files changed

+121
-4
lines changed

atom/browser/api/atom_api_browser_view.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@
1616
#include "native_mate/dictionary.h"
1717
#include "ui/gfx/geometry/rect.h"
1818

19+
namespace mate {
20+
21+
template <>
22+
struct Converter<atom::AutoResizeFlags> {
23+
static bool FromV8(v8::Isolate* isolate,
24+
v8::Local<v8::Value> val,
25+
atom::AutoResizeFlags* auto_resize_flags) {
26+
mate::Dictionary params;
27+
if (!ConvertFromV8(isolate, val, &params)) {
28+
return false;
29+
}
30+
31+
uint8_t flags = 0;
32+
bool width = false;
33+
if (params.Get("width", &width) && width) {
34+
flags |= atom::kAutoResizeWidth;
35+
}
36+
bool height = false;
37+
if (params.Get("height", &height) && height) {
38+
flags |= atom::kAutoResizeHeight;
39+
}
40+
41+
*auto_resize_flags = static_cast<atom::AutoResizeFlags>(flags);
42+
return true;
43+
}
44+
};
45+
46+
} // namespace mate
47+
1948
namespace atom {
2049

2150
namespace api {
@@ -73,6 +102,10 @@ int32_t BrowserView::ID() const {
73102
return weak_map_id();
74103
}
75104

105+
void BrowserView::SetAutoResize(AutoResizeFlags flags) {
106+
view_->SetAutoResizeFlags(flags);
107+
}
108+
76109
void BrowserView::SetBounds(const gfx::Rect& bounds) {
77110
view_->SetBounds(bounds);
78111
}
@@ -95,6 +128,7 @@ void BrowserView::BuildPrototype(v8::Isolate* isolate,
95128
prototype->SetClassName(mate::StringToV8(isolate, "BrowserView"));
96129
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
97130
.MakeDestroyable()
131+
.SetMethod("setAutoResize", &BrowserView::SetAutoResize)
98132
.SetMethod("setBounds", &BrowserView::SetBounds)
99133
.SetMethod("setBackgroundColor", &BrowserView::SetBackgroundColor)
100134
.SetProperty("webContents", &BrowserView::WebContents)

atom/browser/api/atom_api_browser_view.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010

1111
#include "atom/browser/api/trackable_object.h"
12+
#include "atom/browser/native_browser_view.h"
1213
#include "native_mate/handle.h"
1314

1415
namespace gfx {
@@ -50,6 +51,7 @@ class BrowserView : public mate::TrackableObject<BrowserView> {
5051
v8::Local<v8::Object> wrapper,
5152
const mate::Dictionary& options);
5253

54+
void SetAutoResize(AutoResizeFlags flags);
5355
void SetBounds(const gfx::Rect& bounds);
5456
void SetBackgroundColor(const std::string& color_name);
5557

atom/browser/native_browser_view.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace api {
2222
class WebContents;
2323
}
2424

25+
enum AutoResizeFlags {
26+
kAutoResizeWidth = 0x1,
27+
kAutoResizeHeight = 0x2,
28+
};
29+
2530
class NativeBrowserView {
2631
public:
2732
virtual ~NativeBrowserView();
@@ -33,6 +38,7 @@ class NativeBrowserView {
3338
return web_contents_view_;
3439
}
3540

41+
virtual void SetAutoResizeFlags(uint8_t flags) = 0;
3642
virtual void SetBounds(const gfx::Rect& bounds) = 0;
3743
virtual void SetBackgroundColor(SkColor color) = 0;
3844

atom/browser/native_browser_view_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class NativeBrowserViewMac : public NativeBrowserView {
1717
brightray::InspectableWebContentsView* web_contents_view);
1818
~NativeBrowserViewMac() override;
1919

20+
void SetAutoResizeFlags(uint8_t flags) override;
2021
void SetBounds(const gfx::Rect& bounds) override;
2122
void SetBackgroundColor(SkColor color) override;
2223

atom/browser/native_browser_view_mac.mm

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,34 @@
88
#include "skia/ext/skia_utils_mac.h"
99
#include "ui/gfx/geometry/rect.h"
1010

11+
// Match view::Views behavior where the view sticks to the top-left origin.
12+
const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
13+
NSViewMaxXMargin | NSViewMinYMargin;
14+
1115
namespace atom {
1216

1317
NativeBrowserViewMac::NativeBrowserViewMac(
1418
brightray::InspectableWebContentsView* web_contents_view)
15-
: NativeBrowserView(web_contents_view) {}
19+
: NativeBrowserView(web_contents_view) {
20+
auto* view = GetInspectableWebContentsView()->GetNativeView();
21+
view.autoresizingMask = kDefaultAutoResizingMask;
22+
}
1623

1724
NativeBrowserViewMac::~NativeBrowserViewMac() {}
1825

26+
void NativeBrowserViewMac::SetAutoResizeFlags(uint8_t flags) {
27+
NSAutoresizingMaskOptions autoresizing_mask = kDefaultAutoResizingMask;
28+
if (flags & kAutoResizeWidth) {
29+
autoresizing_mask |= NSViewWidthSizable;
30+
}
31+
if (flags & kAutoResizeHeight) {
32+
autoresizing_mask |= NSViewHeightSizable;
33+
}
34+
35+
auto* view = GetInspectableWebContentsView()->GetNativeView();
36+
view.autoresizingMask = autoresizing_mask;
37+
}
38+
1939
void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) {
2040
auto* view = GetInspectableWebContentsView()->GetNativeView();
2141
auto* superview = view.superview;

atom/browser/native_browser_view_views.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ class NativeBrowserViewViews : public NativeBrowserView {
1515
brightray::InspectableWebContentsView* web_contents_view);
1616
~NativeBrowserViewViews() override;
1717

18+
uint8_t GetAutoResizeFlags() { return auto_resize_flags_; }
19+
void SetAutoResizeFlags(uint8_t flags) override {
20+
auto_resize_flags_ = flags;
21+
}
1822
void SetBounds(const gfx::Rect& bounds) override;
1923
void SetBackgroundColor(SkColor color) override;
2024

2125
private:
26+
uint8_t auto_resize_flags_;
27+
2228
DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewViews);
2329
};
2430

atom/browser/native_window_views.cc

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string>
88
#include <vector>
99

10-
#include "atom/browser/native_browser_view.h"
10+
#include "atom/browser/native_browser_view_views.h"
1111
#include "atom/browser/ui/views/menu_bar.h"
1212
#include "atom/browser/window_list.h"
1313
#include "atom/common/color_util.h"
@@ -895,16 +895,20 @@ void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
895895

896896
void NativeWindowViews::SetBrowserView(NativeBrowserView* browser_view) {
897897
if (browser_view_) {
898-
RemoveChildView(browser_view_->GetInspectableWebContentsView()->GetView());
898+
web_view_->RemoveChildView(
899+
browser_view_->GetInspectableWebContentsView()->GetView());
899900
browser_view_ = nullptr;
900901
}
901902

902903
if (!browser_view) {
903904
return;
904905
}
905906

907+
// Add as child of the main web view to avoid (0, 0) origin from overlapping
908+
// with menu bar.
906909
browser_view_ = browser_view;
907-
AddChildView(browser_view->GetInspectableWebContentsView()->GetView());
910+
web_view_->AddChildView(
911+
browser_view->GetInspectableWebContentsView()->GetView());
908912
}
909913

910914
void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
@@ -1292,11 +1296,32 @@ void NativeWindowViews::Layout() {
12921296
menu_bar_->SetBoundsRect(menu_bar_bounds);
12931297
}
12941298

1299+
const auto old_web_view_size = web_view_ ? web_view_->size() : gfx::Size();
12951300
if (web_view_) {
12961301
web_view_->SetBoundsRect(
12971302
gfx::Rect(0, menu_bar_bounds.height(), size.width(),
12981303
size.height() - menu_bar_bounds.height()));
12991304
}
1305+
const auto new_web_view_size = web_view_ ? web_view_->size() : gfx::Size();
1306+
1307+
if (browser_view_) {
1308+
const auto flags = static_cast<NativeBrowserViewViews*>(browser_view_)
1309+
->GetAutoResizeFlags();
1310+
int width_delta = 0;
1311+
int height_delta = 0;
1312+
if (flags & kAutoResizeWidth) {
1313+
width_delta = new_web_view_size.width() - old_web_view_size.width();
1314+
}
1315+
if (flags & kAutoResizeHeight) {
1316+
height_delta = new_web_view_size.height() - old_web_view_size.height();
1317+
}
1318+
1319+
auto* view = browser_view_->GetInspectableWebContentsView()->GetView();
1320+
auto new_view_size = view->size();
1321+
new_view_size.set_width(new_view_size.width() + width_delta);
1322+
new_view_size.set_height(new_view_size.height() + height_delta);
1323+
view->SetSize(new_view_size);
1324+
}
13001325
}
13011326

13021327
gfx::Size NativeWindowViews::GetMinimumSize() {

docs/api/browser-view.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ A `Integer` representing the unique ID of the view.
5050

5151
Objects created with `new BrowserWindow` have the following instance methods:
5252

53+
#### `win.setAutoResize(options)` _Experimental_
54+
55+
* `options` Object
56+
* `width`: If `true`, the view's width will grow and shrink together with
57+
the window. `false` by default.
58+
* `height`: If `true`, the view's height will grow and shrink together with
59+
the window. `false` by default.
60+
5361
#### `win.setBounds(bounds)` _Experimental_
5462

5563
* `bounds` [Rectangle](structures/rectangle.md)

spec/api-browser-view-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ describe('View module', function () {
3838
})
3939
})
4040

41+
describe('BrowserView.setAutoResize()', function () {
42+
it('does not throw for valid args', function () {
43+
const view = new BrowserView()
44+
view.setAutoResize({})
45+
view.setAutoResize({ width: true, height: false })
46+
})
47+
48+
it('throws for invalid args', function () {
49+
const view = new BrowserView()
50+
assert.throws(function () {
51+
view.setAutoResize(null)
52+
}, /conversion failure/)
53+
})
54+
})
55+
4156
describe('BrowserView.setBounds()', function () {
4257
it('does not throw for valid args', function () {
4358
const view = new BrowserView()

0 commit comments

Comments
 (0)