Skip to content

Instantly share code, notes, and snippets.

@my-lalex
Last active April 11, 2024 16:22
Show Gist options
  • Save my-lalex/f81352ad69fba206b84e59341fc469ed to your computer and use it in GitHub Desktop.
Save my-lalex/f81352ad69fba206b84e59341fc469ed to your computer and use it in GitHub Desktop.
// Count in "frames", actually it's more ticks
const DEFAULT_DURATION = 150;
/* Penner easing function
See: http://robertpenner.com/easing/ */
const DEFAULT_EASING = (t, b, c, d) => (t == d ? b + c : c * (-Math.pow(2, (-10 * t) / d) + 1) + b);
type BrowserWindowAnimateOptions = {
duration?: number;
easing?: typeof DEFAULT_EASING;
};
const windowSetSize = BrowserWindow.prototype.setSize;
BrowserWindow.prototype.setSize = function (
this: BrowserWindow,
width: number,
height: number,
animate?: boolean | BrowserWindowAnimateOptions
) {
if (animate) {
const [startWidth, startHeight] = this.getSize();
const [targetWidth, targetHeight] = [width, height];
const animateOptions =
typeof animate === "object" ? animate : { duration: DEFAULT_DURATION, easing: DEFAULT_EASING };
const duration = animateOptions.duration ?? DEFAULT_DURATION;
const easing = animateOptions.easing ?? DEFAULT_EASING;
let currentFrame = 0;
const updateSize = () => {
currentFrame++;
windowSetSize.apply(this, [
Math.round(easing(currentFrame, startWidth, targetWidth - startWidth, duration)),
Math.round(easing(currentFrame, startHeight, targetHeight - startHeight, duration)),
]);
if (currentFrame < duration) setImmediate(updateSize);
};
setImmediate(updateSize);
} else {
windowSetSize.apply(this, [width, height]);
}
};
declare module "electron" {
interface BrowserWindow {
setSize(width: number, height: number, animate?: boolean | BrowserWindowAnimateOptions);
}
}
function createWindow() {
const win = new BrowserWindow({
width: 100,
height: 100,
title: "Eletron App",
webPreferences: {
preload: join(__dirname, "preload.js"),
},
});
win.loadFile("index.html");
win.on("show", () => {
/* Old fashion way still works, but for all platforms
win.setSize(800, 600, true); */
win.setSize(800, 600, { duration: 500 });
});
}
// Whatever's next...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment