Skip to content

Commit

Permalink
Frame cache improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Nov 17, 2019
1 parent 55f9440 commit 8a66f00
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export {toggleMaximized, clearUnmaximized};
interface FrameCache {
screen: Rectangle;
window: Rectangle;
maximized?: {
screen: Rectangle;
window: Rectangle;
};
}

const frameCache: Map<number, FrameCache> = new Map();
Expand All @@ -31,18 +35,74 @@ function unmaximizedFrame(win: Window): Rectangle {
return ratio(c.window);
}

function objEqual(a: {[key: string]: any}, b: {[key: string]: any}): boolean {
if (typeof a !== 'object') {
return a === b;
}
if (Object.keys(a).length !== Object.keys(b).length) {
return false;
}
for (const key of Object.keys(a)) {
if (typeof a[key] === 'object') {
if (!objEqual(a[key], b[key])) {
return false;
}
}
if (a[key] !== b[key]) {
return false;
}
}
return true;
}

function isMaximized(win: Window): boolean {
const cache = frameCache.get(win.hash());
if (!cache || !cache.maximized) {
return false;
}

log(win.frame(), cache.maximized.window);

return (
objEqual(win.screen().flippedVisibleFrame(), cache.maximized.screen) &&
objEqual(win.frame(), cache.maximized.window)
);
}

function toggleMaximized(win: Window) {
const id = win.hash();
if (frameCache.has(id)) {
if (isMaximized(win)) {
win.setFrame(unmaximizedFrame(win));
win.clearUnmaximized();
return;
}
frameCache.set(id, {
const previous = {
screen: win.screen().flippedVisibleFrame(),
window: win.frame(),
});
};
win.maximize();

const id = win.hash();
frameCache.set(id, {
...previous,
maximized: {
screen: win.screen().flippedVisibleFrame(),
window: win.frame(),
},
});
}

function setFrame(win: Window, frame: Rectangle): boolean {
const ok = win.setFrame(frame);
if (ok) {
// Invalidate cache.
// No maximized.
}
return ok;
}

function maximize(win: Window): boolean {
const ok = win.maximize();
return ok;
}

Window.prototype.clearUnmaximized = function _clearUnmaximized() {
Expand Down

0 comments on commit 8a66f00

Please sign in to comment.