New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce a aspect ratio when resizing for Windows #8036
Comments
Possible workaround/implementation? (16:9 aspect ratio example) // 'win' as the BrowserWindow instance
win.on('resize', function () {
setTimeout(function () {
var size = win.getSize();
win.setSize(size[0], parseInt(size[0] * 9 / 16));
}, 0);
}); |
@Cleod9 did you try this one? We have tried something similar but it causes blinking of window and strange behaviour as it interferes with native resize events. It worked well but just for one edge |
@szymonc Yes, I did try it and it works for my purposes on Windows 7. Doesn't look pretty while resizing but not a deal-breaker for me. Also I had to account for the top/bottom windows borders using |
Almost 1 year and I can't believe this issue hasn't been fixed yet for Windows. @Cleod9's suggestion works, but it flickers and it looks ugly as heck. |
After 1 year of nobody watching this... here's my fix. It works and it doesn't flicker as much (it still does, but it's hardly noticeable), somebody please PR this :) 750 = Original height Taken from: https://stackoverflow.com/questions/48043469/how-to-lock-aspect-ratio-while-resizing-the-window
|
Would be nice to see win.setAspectRatio added for Windows? Any plans for this? |
This solution is based on the one proposed by @Cleod9 but with a 'debounce' type function worked well for me, just a little jarring that the height can jump when you're finally done resizing. Not perfect but an alternate solution that feels a little smoother
|
Based upon the upper answers, I made a npm package for this reason! |
@Jacobboogiebear Cool, I'll give this a try in the next couple days and report back! |
@Jacobboogiebear Do you know if there's a way to prevent 'resize' from triggering when just moving the window? I don't want to stop the ability to move the window, but for some reason, Electron triggers a resize on move. It's bizarre. |
@HaulinOats I don't know how to deal with that but I will put some time in to study it and report back |
@Jacobboogiebear Turns out doing a window move actually triggers a resize, by default, in Electron. I opened a separate thread about this but it hasn't be resolved or addressed yet. However, I found this solution on another thread that prevents the weird incremental resize glitch when moving the window (stays exact same size) while also removing the timeout I put, making the resize process a little bit faster. Basically, a window move attempts a resize by an increment of one pixel, so comparing the previous
|
I'm using this approach to enforce 1:1 ratio on Windows. It works quite nicely for the right and bottom sides. The other two are a bit weird :P
|
This one works better for resizing from the top and left sides. Still not perfect because the window may start to move when resizing from those sides.
|
@nornagon @codebytere @MadfishDT I hope #18306 will patch and merge to master. |
After spend some time I found this solution. enum Directions {
LEFT = 1,
RIGHT = 2,
TOP = 3,
LEFTTOP = 4,
RIGHTTOP = 5,
BOTTOM = 6,
BOTTOMLEFT = 7,
BOTTOMRIGHT = 8
}
const aspectRatio = 16 / 9;
const extraWidth = 16;
const extraHeight = 48;
function getHeight(aspectRatio: number, width: number): number {
return Math.max(
MIN_HEIGHT,
Math.floor((width - extraWidth) / aspectRatio + extraHeight)
);
}
function getWidth(aspectRatio: number, height: number): number {
return Math.max(
MIN_WIDTH,
Math.floor((height - extraHeight) * aspectRatio + extraWidth)
);
}
if (process.platform === 'win32') {
let resizeDirection: Directions;
window.hookWindowMessage(0x0214, (wParam: Buffer) => {
resizeDirection = wParam.readUIntBE(0, 1);
});
window?.on('will-resize', (event, newBounds) => {
if (aspectRatio && window) {
event.preventDefault();
let temp_width;
let temp_height;
const toBounds = { ...newBounds };
switch (resizeDirection) {
case Directions.LEFT:
case Directions.RIGHT:
toBounds.height = getHeight(
aspectRatio,
newBounds.width
);
break;
case Directions.TOP:
case Directions.BOTTOM:
toBounds.width = getWidth(aspectRatio, newBounds.height);
break;
case Directions.BOTTOMLEFT:
case Directions.BOTTOMRIGHT:
case Directions.LEFTTOP:
case Directions.RIGHTTOP:
toBounds.width = getWidth(aspectRatio, newBounds.height);
temp_width = newBounds.width;
temp_height = getHeight(aspectRatio, temp_width);
if (
temp_width * temp_height >
toBounds.width * toBounds.height
) {
toBounds.width = temp_width;
toBounds.height = temp_height;
}
break;
default:
}
switch (resizeDirection) {
case Directions.BOTTOMLEFT:
toBounds.x = newBounds.x + newBounds.width - toBounds.width;
break;
case Directions.LEFTTOP:
toBounds.x = newBounds.x + newBounds.width - toBounds.width;
toBounds.y = newBounds.y + newBounds.height - toBounds.height;
break;
case Directions.RIGHTTOP:
toBounds.y = newBounds.y + newBounds.height - toBounds.height;
break;
default:
}
window.setBounds(toBounds);
}
});
}
|
@asc0910 Great and Awesome!!! |
Plenty of great answers in here, thought I would give my two cents anyway. You could use this RatioWindow wrapper of BrowserWindow instead of BrowserWindow to maintain aspect ratio. RatioWindow.js const { BrowserWindow } = require('electron');
function __handleWillResize(event, newSize) {
event.preventDefault();
event.sender.setSize(newSize.width, parseInt(newSize.width * this.height / this.width));
}
class RatioWindow extends BrowserWindow {
constructor(options) {
super(options);
this.on('will-resize', __handleWillResize.bind(options));
}
}
module.exports = RatioWindow; app.js const { app } = require('electron');
const RatioWindow = require('./RatioWindow');
app.whenReady().then(function() {
let win = new RatioWindow({
width: 1280,
height: 720,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile(/* Some File */);
}); |
I've combined @asc0910 and @swimauger approaches (thanks!), added support for different screen scales on Windows (100-125-150%), and published as a drop-in replacement module for |
As in topic. API currently offers fixed ratio only for Mac but not for Windows
The text was updated successfully, but these errors were encountered: