Skip to content

Instantly share code, notes, and snippets.

@rcarmo
Created June 7, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcarmo/1daccbe9abbbb5c133b7b48d05cc09e7 to your computer and use it in GitHub Desktop.
Save rcarmo/1daccbe9abbbb5c133b7b48d05cc09e7 to your computer and use it in GitHub Desktop.
Phoenix tiling snippet
// Stub of my .phoenix.js with basic XMonad-like tiling
// Amethyst bindings
Key.on("return", ["shift", "control", "option"], () => tile_windows(WEST));
Key.on("return", ["shift", "control"], () => retile());
Key.on("j", ["shift", "control"], () => rotate(-1, false));
Key.on("j", ["shift", "control", "option"], () => rotate(-1, true));
Key.on("k", ["shift", "control"], () => rotate(1, false));
Key.on("k", ["shift", "control", "option"], () => rotate(1, true));
// testing
Key.on("space", ["control", "option", "command"], () => {
tile_windows(COLS);
})
// Tiling
const TILING_MODES = [NONE, EAST, WEST, COLS];
var TILING_MODE = TILING_MODES[0];
var TIMERS = null;
var SCREEN_TILING_MODES = null;
Key.on("space", ["shift", "control"], () => {
if(!SCREEN_TILING_MODES) {
SCREEN_TILING_MODES = Array(Screen.all().length);
TIMERS = Array(Screen.all().length);
SCREEN_TILING_MODES.fill(NONE);
}
var window = Window.focused(),
screen = window.screen(),
CURRENT_SCREEN = Screen.all().indexOf(screen);
TILING_MODE = SCREEN_TILING_MODES[CURRENT_SCREEN];
TILING_MODE = TILING_MODES[(TILING_MODES.indexOf(TILING_MODE)+1) % TILING_MODES.length];
SCREEN_TILING_MODES[CURRENT_SCREEN] = TILING_MODE;
buildhint("Tiling mode: " + TILING_MODE, window, 0.5);
retile()
/*
if(TILING_MODE != NONE) {
if(!TIMERS[CURRENT_SCREEN]) {
TIMERS[CURRENT_SCREEN] = new Timer(1, true, retile);
}
} else {
TIMERS[CURRENT_SCREEN].stop()
TIMERS[CURRENT_SCREEN] = null;
}
*/
})
WINDOW_OFFSET = 0;
function rotate(delta, shuffle) {
var window = Window.focused();
// when nothing is in focus yet
if(typeof window === 'undefined')
return;
var screen = window.screen(),
neighbors = screen.windows({visible:true});
WINDOW_OFFSET+=delta;
if(WINDOW_OFFSET > neighbors.length-1) {
WINDOW_OFFSET = 0;
}
else if(WINDOW_OFFSET < 0) {
WINDOW_OFFSET = neighbors.length-1;
}
neighbors[WINDOW_OFFSET].focus();
if(shuffle)
retile()
}
function retile() {
try {
CURRENT_SCREEN = Screen.all().indexOf(Window.focused().screen());
TILING_MODE = SCREEN_TILING_MODES[CURRENT_SCREEN];
tile_windows(TILING_MODE);
} catch {
// TODO: display modal
}
}
function tile_windows(mode) {
var main_window = Window.focused(),
screen = main_window.screen(),
neighbors = screen.windows({visible:true}),
tiles = neighbors.length - 1,
index = 0;
if ([EAST, WEST, COLS].indexOf(mode) === -1) {
return
}
if ([EAST, WEST].indexOf(mode) >= 0)
main_window.to(mode);
neighbors.forEach((e) => {
if([EAST, WEST].indexOf(mode) >= 0)
if(e.isEqual(main_window)) return;
var height = (screen.height() - PADDING) / tiles,
width = screen.width()/2 - PADDING /2 ;
if(mode === EAST) {
e.setFrame({
x: screen.origin().x,
y: screen.origin().y + (height + PADDING) * index,
height: height,
width: width
});
}
else if(mode===WEST) {
e.setFrame({
x: screen.origin().x + (screen.width() + PADDING) / 2,
y: screen.origin().y + (height + PADDING) * index,
height: height,
width: width
});
}
else if(mode===COLS) {
width = screen.width() / (tiles+1);
e.setFrame({
x: screen.origin().x + ( PADDING/2 + width) * index,
y: screen.origin().y,
height: screen.height() - PADDING/2,
width: width - PADDING/2
});
}
index++;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment