Skip to content

Instantly share code, notes, and snippets.

@michaelsbradleyjr
Created September 16, 2020 15:08
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 michaelsbradleyjr/a8ef81982420d0611878d5f521652c5a to your computer and use it in GitHub Desktop.
Save michaelsbradleyjr/a8ef81982420d0611878d5f521652c5a to your computer and use it in GitHub Desktop.
// adapted from:
// https://gist.github.com/kasper/b7c2a1596a90246c9ccd
// partly inspired by the grid hotkeys for Optimal Layout:
// http://most-advantageous.com/optimal-layout/
'use strict';
var commandOption = [ 'cmd', 'alt' ],
commandOptionShift = [ 'cmd', 'alt', 'shift' ],
keys = [];
/* Window functions */
Window.prototype.reframe = function (position) {
this.setFrame(position(this));
}
/* Window positions */
Window.windows = {
lastPositions: {},
halfFullToggle: {},
halfFullHorizToggle: {},
halfHalfToggle: {}
};
Window.positions = {
centered: function (window, whichCentered) {
var screen = window.screen(),
screenFrame = screen.visibleFrameInRectangle();
Window.windows.lastPositions[ window.hash() ] = whichCentered;
var multiplier = undefined;
switch (whichCentered) {
case 'fourFifthsFullCentered':
multiplier = Window.sizes.fourFifthsFull;
break;
case 'halfFullCentered':
multiplier = Window.sizes.halfFull;
break;
case 'thirdFullCentered':
multiplier = Window.sizes.thirdFull;
break;
case 'twoThirdsFullCentered':
multiplier = Window.sizes.twoThirdsFull;
break;
}
return {
width: screenFrame.width * multiplier.width,
height: screenFrame.height * multiplier.height,
x: screenFrame.x
+ ((screenFrame.width * 1/2)
- (screenFrame.width * multiplier.width * 1/2)),
y: screenFrame.y
};
},
fourFifthsFullCentered: function (window) {
return Window.positions.centered(window, 'fourFifthsFullCentered');
},
full: function (window) {
var screen = window.screen(),
screenFrame = screen.visibleFrameInRectangle();
Window.windows.lastPositions[ window.hash() ] = 'full';
return {
width: screenFrame.width * Window.sizes.full.width,
height: screenFrame.height * Window.sizes.full.height,
x: screenFrame.x,
y: screenFrame.y
};
},
halfFullCentered: function (window) {
return Window.positions.centered(window, 'halfFullCentered');
},
halfFullToggle: function (window) {
var screen = window.screen(),
screenFrame = screen.visibleFrameInRectangle();
Window.windows.lastPositions[ window.hash() ] = 'halfFullToggle';
var windowRef = Window.windows.halfFullToggle[ window.hash() ];
if (!windowRef) {
windowRef = {
currentHalfFullToggle: undefined,
nextHalfFullToggle: [ 'left', 'right' ]
};
Window.windows.halfFullToggle[ window.hash() ] = windowRef;
}
var nextHalfFullToggle = windowRef.nextHalfFullToggle.shift();
windowRef.currentHalfFullToggle = nextHalfFullToggle;
windowRef.nextHalfFullToggle.push(nextHalfFullToggle);
var x = undefined;
switch (windowRef.currentHalfFullToggle) {
case 'left':
x = screenFrame.x;
break;
case 'right':
x = screenFrame.x
+ screenFrame.width
* Window.sizes.halfFull.width;
break;
}
return {
width: screenFrame.width * Window.sizes.halfFull.width,
height: screenFrame.height * Window.sizes.halfFull.height,
x: x,
y: screenFrame.y
};
},
halfFullHorizToggle: function (window) {
var screen = window.screen(),
screenFrame = screen.visibleFrameInRectangle();
Window.windows.lastPositions[ window.hash() ] = 'halfFullHorizToggle';
var windowRef = Window.windows.halfFullHorizToggle[ window.hash() ];
if (!windowRef) {
windowRef = {
currentHalfFullHorizToggle: undefined,
nextHalfFullHorizToggle: [ 'top', 'bottom' ]
};
Window.windows.halfFullHorizToggle[ window.hash() ] = windowRef;
}
var nextHalfFullHorizToggle = windowRef.nextHalfFullHorizToggle.shift();
windowRef.currentHalfFullHorizToggle = nextHalfFullHorizToggle;
windowRef.nextHalfFullHorizToggle.push(nextHalfFullHorizToggle);
var y = undefined;
switch (windowRef.currentHalfFullHorizToggle) {
case 'top':
y = screenFrame.y;
break;
case 'bottom':
y = screenFrame.y
+ screenFrame.height
* 1/2;
// * Window.sizes.halfFull.height;
break;
}
return {
width: screenFrame.width * Window.sizes.full.width,
height: screenFrame.height * 1/2, // screenFrame.height * Window.sizes.halfFull.height,
x: screenFrame.x,
y: y
};
},
halfHalfToggle: function (window) {
var screen = window.screen(),
screenFrame = screen.visibleFrameInRectangle();
Window.windows.lastPositions[ window.hash() ] = 'halfHalfToggle';
var windowRef = Window.windows.halfHalfToggle[ window.hash() ];
if (!windowRef) {
windowRef = {
currentHalfHalfToggle: undefined,
nextHalfHalfToggle: [
'top-left',
'top-right',
'bottom-right',
'bottom-left'
]
};
Window.windows.halfHalfToggle[ window.hash() ] = windowRef;
}
var nextHalfHalfToggle = windowRef.nextHalfHalfToggle.shift();
windowRef.currentHalfHalfToggle = nextHalfHalfToggle;
windowRef.nextHalfHalfToggle.push(nextHalfHalfToggle);
var x = undefined,
y = undefined;
switch (windowRef.currentHalfHalfToggle) {
case 'top-left':
x = screenFrame.x;
y = screenFrame.y;
break;
case 'top-right':
x = screenFrame.x
+ screenFrame.width
* Window.sizes.halfHalf.width;
y = screenFrame.y;
break;
case 'bottom-right':
x = screenFrame.x
+ screenFrame.width
* Window.sizes.halfHalf.width;
y = screenFrame.y
+ screenFrame.height
* Window.sizes.halfHalf.height;
break;
case 'bottom-left':
x = screenFrame.x;
y = screenFrame.y
+ screenFrame.height
* Window.sizes.halfHalf.height;
break;
}
return {
width: screenFrame.width * Window.sizes.halfHalf.width,
height: screenFrame.height * Window.sizes.halfHalf.height,
x: x,
y: y
};
},
thirdFullCentered: function (window) {
return Window.positions.centered(window, 'thirdFullCentered');
},
twoThirdsFullCentered: function (window) {
return Window.positions.centered(window, 'twoThirdsFullCentered');
}
};
/* Window sizes */
Window.sizes = Object.freeze({
fourFifthsFull: Object.freeze({
width: 4/5,
height: 1
}),
full: Object.freeze({
width: 1,
height: 1
}),
halfFull: Object.freeze({
width: 1/2,
height: 1
}),
halfHalf: Object.freeze({
width: 1/2,
height: 1/2
}),
thirdFull: Object.freeze({
width: 1/3,
height: 1
}),
twoThirdsFull: Object.freeze({
width: 2/3,
height: 1
})
});
/* Key bindings */
keys.push(Key.on('1', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.full);
}));
keys.push(Key.on('2', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.halfFullToggle);
}));
keys.push(Key.on('2', commandOptionShift, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.halfFullHorizToggle);
}));
keys.push(Key.on('3', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.thirdFullCentered);
}));
keys.push(Key.on('4', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.halfHalfToggle);
}));
keys.push(Key.on('5', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.halfFullCentered);
}));
keys.push(Key.on('6', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.twoThirdsFullCentered);
}));
keys.push(Key.on('7', commandOption, function () {
Window.focused()
&& Window.focused()
.reframe(Window.positions.fourFifthsFullCentered);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment