Skip to content

Instantly share code, notes, and snippets.

@possan
Last active January 13, 2023 10:26
Show Gist options
  • Save possan/ed7a2ebb0baf4d0e42c328964ea6c6d3 to your computer and use it in GitHub Desktop.
Save possan/ed7a2ebb0baf4d0e42c328964ea6c6d3 to your computer and use it in GitHub Desktop.
phoenix.js window manager configuration
//
// Phoenix configuration, updated 2023-01-13
//
// Use Cmd-Option-Command + Arrow keys to move/size windows, Cmd-Option-Command + Space for fullscreen
//
// Show debug logs: log stream --process Phoenix
//
var FULL = "full";
var FIRST = "0-1/3";
var LAST = "2-3/3";
var ALL_SIZES = [
"0-1/3",
"lefthalf",
"0-2/3",
"full",
"1-3/3",
"righthalf",
"2-3/3",
];
var RANGE_ORDER = ["prev", ...ALL_SIZES, "next"];
Phoenix.set({
openAtLogin: true,
});
function notifySize(fr) {
Phoenix.notify(
"Resized to " + fr.width + "x" + fr.height + " at " + fr.x + "," + fr.y
);
}
function getKeyInfo(key, totalsize, offset) {
var start = 0;
var end = totalsize - 1;
if (key === "0-1/3") {
return {
start: offset,
end: offset + Math.round((1 * (totalsize - 1)) / 3),
};
}
if (key === "0-2/3") {
return {
start: offset,
end: offset + Math.round((2 * (totalsize - 1)) / 3),
};
}
if (key === "lefthalf") {
return {
start: offset,
end: offset + Math.round((totalsize - 1) / 2),
};
}
if (key === "full") {
return {
start: offset,
end: offset + Math.round((3 * (totalsize - 1)) / 3),
};
}
if (key === "righthalf") {
return {
start: offset + Math.round((totalsize - 1) / 2),
end: offset + (totalsize - 1),
};
}
if (key === "1-3/3") {
return {
start: offset + Math.round((1 * (totalsize - 1)) / 3),
end: offset + Math.round((3 * (totalsize - 1)) / 3),
};
}
if (key === "2-3/3") {
return {
start: offset + Math.round((2 * (totalsize - 1)) / 3),
end: offset + Math.round((3 * (totalsize - 1)) / 3),
};
}
return {
start,
end,
};
}
function closeEnough(a, b) {
return Math.abs(a - b) < 10;
}
function getClosestRangeKey(start, end, rangeoffset, rangesize) {
for (const checkKey of ALL_SIZES) {
const ki = getKeyInfo(checkKey, rangesize, rangeoffset);
Phoenix.log(
"Compare " +
start +
"+" +
end +
" (" +
checkKey +
") to " +
JSON.stringify(ki)
);
if (closeEnough(ki.start, start) && closeEnough(ki.end, end)) {
Phoenix.log("^ Match");
return checkKey;
}
}
Phoenix.log("No match, return FULL");
return FULL;
}
function changeRange(currentKey, delta) {
var idx = RANGE_ORDER.indexOf(currentKey);
if (idx === -1) {
idx = RANGE_ORDER.indexOf(FULL);
}
idx += delta;
if (idx < 0) {
idx = 0;
}
if (idx >= RANGE_ORDER.length - 1) {
idx = RANGE_ORDER.length - 1;
}
return RANGE_ORDER[idx];
}
function setWindowSize(window, wkey, hkey, screen) {
// var w = Window.focused();
var oldframe = window.frame();
var newframe = { ...oldframe };
var screenframe = screen.flippedVisibleFrame();
var wi = getKeyInfo(wkey, screenframe.width, screenframe.x);
var hi = getKeyInfo(hkey, screenframe.height, screenframe.y);
newframe.x = wi.start;
newframe.width = wi.end - wi.start;
newframe.y = hi.start;
newframe.height = hi.end - hi.start;
Phoenix.notify(
"old size: " +
JSON.stringify(oldframe) +
" new size: " +
JSON.stringify(oldframe)
);
window.setFrame(newframe);
notifySize(newframe);
}
function resizeHorizontally(dx) {
var window = Window.focused();
var screen = window.screen();
var screenframe = screen.flippedVisibleFrame();
var frame = window.frame();
var oldkey = getClosestRangeKey(
frame.x,
frame.x + frame.width,
screenframe.x,
screenframe.width
);
var newkey = changeRange(oldkey, dx);
if (newkey == "next") {
setWindowSize(window, FIRST, FULL, screen.previous());
} else if (newkey == "prev") {
setWindowSize(window, LAST, FULL, screen.next());
} else {
setWindowSize(window, newkey, FULL, screen);
}
}
function resizeVertically(dy) {
var window = Window.focused();
var screen = window.screen();
var screenframe = screen.flippedVisibleFrame();
var frame = window.frame();
var oldkey = getClosestRangeKey(
frame.y,
frame.y + frame.height,
screenframe.y,
screenframe.height
);
var newkey = changeRange(oldkey, dy);
if (newkey == "next") {
setWindowSize(window, FULL, FIRST, screen.previous());
} else if (newkey == "prev") {
setWindowSize(window, FULL, LAST, screen.next());
} else {
setWindowSize(window, FULL, newkey, screen);
}
}
function moveCurrentWindowToNextScreen() {
var window = Window.focused();
var nextscreen = window.screen().next();
var nextframe = nextscreen.flippedVisibleFrame();
window.setFrame(nextframe);
}
Key.on("i", ["ctrl", "alt", "cmd"], function () {
var window = Window.focused();
var screenframe = window.screen().flippedVisibleFrame();
var frame = window.frame();
var wk = getClosestRangeKey(
frame.x,
frame.x + frame.width,
screenframe.x,
screenframe.width
);
var hk = getClosestRangeKey(
frame.y,
frame.y + frame.height,
screenframe.y,
screenframe.height
);
var lines = [];
lines.push(
"pos: " +
frame.x +
"," +
frame.y +
", size: " +
frame.width +
"x" +
frame.height +
", screen: " +
screenframe.width +
"x" +
screenframe.height
);
lines.push("keys: " + wk + "," + hk);
Phoenix.notify(lines.join("\n"));
});
Key.on("space", ["ctrl", "alt", "cmd"], function () {
var window = Window.focused();
setWindowSize(window, FULL, FULL, window.screen());
});
Key.on("n", ["ctrl", "alt", "cmd"], function () {
var window = Window.focused();
setWindowSize(window, FULL, FULL, window.screen().next());
});
Key.on("up", ["ctrl", "alt", "cmd"], function () {
resizeVertically(-1);
});
Key.on("down", ["ctrl", "alt", "cmd"], function () {
resizeVertically(1);
});
Key.on("left", ["ctrl", "alt", "cmd"], function () {
resizeHorizontally(-1);
});
Key.on("right", ["ctrl", "alt", "cmd"], function () {
resizeHorizontally(1);
});
Phoenix.notify("Configuration loaded.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment