mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: enforce size constraints on window creation on Windows and Linux (#50754)
fix: enforce size constraints on window creation on Windows and Linux (#49906) * enforce size constraints on window creation * set constraints after resizing on init * restore conditional centering Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Mitchell Cohen <mitch.cohen@me.com>
This commit is contained in:
@@ -140,24 +140,10 @@ NativeWindow::~NativeWindow() {
|
||||
|
||||
void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
|
||||
// Setup window from options.
|
||||
if (int x, y; options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
|
||||
SetPosition(gfx::Point{x, y});
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
// FIXME(felixrieseberg): Dirty, dirty workaround for
|
||||
// https://github.com/electron/electron/issues/10862
|
||||
// Somehow, we need to call `SetBounds` twice to get
|
||||
// usable results. The root cause is still unknown.
|
||||
SetPosition(gfx::Point{x, y});
|
||||
#endif
|
||||
} else if (bool center; options.Get(options::kCenter, ¢er) && center) {
|
||||
Center();
|
||||
}
|
||||
|
||||
const bool use_content_size =
|
||||
options.ValueOrDefault(options::kUseContentSize, false);
|
||||
|
||||
// On Linux and Window we may already have maximum size defined.
|
||||
// On Linux and Windows we may already have minimum and maximum size defined.
|
||||
extensions::SizeConstraints size_constraints(
|
||||
use_content_size ? GetContentSizeConstraints() : GetSizeConstraints());
|
||||
|
||||
@@ -184,10 +170,32 @@ void NativeWindow::InitFromOptions(const gin_helper::Dictionary& options) {
|
||||
size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
|
||||
|
||||
if (use_content_size) {
|
||||
gfx::Size clamped = size_constraints.ClampSize(GetContentSize());
|
||||
if (clamped != GetContentSize()) {
|
||||
SetContentSize(clamped);
|
||||
}
|
||||
SetContentSizeConstraints(size_constraints);
|
||||
} else {
|
||||
gfx::Size clamped = size_constraints.ClampSize(GetSize());
|
||||
if (clamped != GetSize()) {
|
||||
SetSize(clamped);
|
||||
}
|
||||
SetSizeConstraints(size_constraints);
|
||||
}
|
||||
|
||||
if (int x, y; options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
|
||||
SetPosition(gfx::Point{x, y});
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
// FIXME(felixrieseberg): Dirty, dirty workaround for
|
||||
// https://github.com/electron/electron/issues/10862
|
||||
// Somehow, we need to call `SetBounds` twice to get
|
||||
// usable results. The root cause is still unknown.
|
||||
SetPosition(gfx::Point{x, y});
|
||||
#endif
|
||||
} else if (bool center; options.Get(options::kCenter, ¢er) && center) {
|
||||
Center();
|
||||
}
|
||||
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
|
||||
if (bool val; options.Get(options::kClosable, &val))
|
||||
SetClosable(val);
|
||||
|
||||
@@ -1764,6 +1764,32 @@ describe('BrowserWindow module', () => {
|
||||
expectBoundsEqual(w.getMaximumSize(), [900, 600]);
|
||||
});
|
||||
|
||||
it('creates window at min size when a smaller size is requested', () => {
|
||||
const w1 = new BrowserWindow({
|
||||
show: false,
|
||||
width: 200,
|
||||
height: 200,
|
||||
minWidth: 300,
|
||||
minHeight: 300
|
||||
});
|
||||
const size = w1.getSize();
|
||||
expect(size[0]).to.equal(300);
|
||||
expect(size[1]).to.equal(300);
|
||||
});
|
||||
|
||||
it('creates window at max size when a larger size is requested', () => {
|
||||
const w1 = new BrowserWindow({
|
||||
show: false,
|
||||
width: 300,
|
||||
height: 300,
|
||||
maxWidth: 200,
|
||||
maxHeight: 200
|
||||
});
|
||||
const size = w1.getSize();
|
||||
expect(size[0]).to.equal(200);
|
||||
expect(size[1]).to.equal(200);
|
||||
});
|
||||
|
||||
it('enforces minimum size', async () => {
|
||||
w.setMinimumSize(300, 300);
|
||||
const resize = once(w, 'resize');
|
||||
|
||||
Reference in New Issue
Block a user