Skip to content

Commit

Permalink
Cleanup and fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Dec 2, 2016
1 parent 23656f0 commit 939e782
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 77 deletions.
4 changes: 0 additions & 4 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function frameRatio(a: Rectangle, b: Rectangle): FrameRatio {
let widthRatio = b.width / a.width;
let heightRatio = b.height / a.height;

return function ({ width, height, x, y }) {
return ({ width, height, x, y }) => {
width = Math.round(width * widthRatio);
height = Math.round(height * heightRatio);
x = Math.round(b.x + (x - a.x) * widthRatio);
Expand Down
8 changes: 3 additions & 5 deletions src/key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import log from './logger';
import { hyper, hyperShift } from './config';
import log from './logger';

const handlers: Map<string, Key> = new Map();
const hyperHandlers: Map<string, Key> = new Map();
Expand All @@ -12,7 +12,6 @@ onKey('f19', ['shift'], enableHyperBindings);

function enableHyperBindings() {
if (!hyperTimeout) {
log('enable f19');
enableHyperKeys();
hyperTimeout = setTimeout(disableHyperBindings, 510);
} else {
Expand All @@ -22,7 +21,6 @@ function enableHyperBindings() {
}

function disableHyperBindings() {
log('disable f19');
if (hyperTimeout) {
clearTimeout(hyperTimeout);
hyperTimeout = null;
Expand Down Expand Up @@ -75,11 +73,11 @@ function getHandler(key: string, mod: string[]) {
}

function enableHyperKeys() {
hyperHandlers.forEach(h => h.enable());
hyperHandlers.forEach((h) => h.enable());
}

function disableHyperKeys() {
hyperHandlers.forEach(h => h.disable());
hyperHandlers.forEach((h) => h.disable());
}

export { onKey, getHandler, enableHyperKeys, disableHyperKeys };
14 changes: 7 additions & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export default function log(...args: any[]): void {
args = args.map(arg => stringify(arg));
args = args.map((arg) => stringify(arg));
Phoenix.log(args.join(' '));
}

function stringify(value: any) {
switch (typeof value) {
case 'object':
return JSON.stringify(value, null, 2);
case 'function':
return value.toString();
default:
return value;
case 'object':
return JSON.stringify(value, null, 2);
case 'function':
return value.toString();
default:
return value;
}
}
4 changes: 3 additions & 1 deletion src/misc/brightness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ function addBrightness(value: number) {

function showBrightness(value: number) {
clearTimeout(bModalHideHandler);
if (!bModal) bModal = new Modal();
if (!bModal) {
bModal = new Modal();
}

bModal.text = `Brightness: ${value}`;
bModal.showCenterOn(Screen.main());
Expand Down
6 changes: 4 additions & 2 deletions src/misc/coffee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This module starts a timer to notify you when your coffee is done.
*/
import { applyMargin, originOnScreen, Orientation } from '../modal';
import { applyMargin, Orientation, originOnScreen } from '../modal';

export default start;

Expand Down Expand Up @@ -50,7 +50,9 @@ function updater(timer: CoffeTimer) {
timer.timeout--;
let min = timer.timeout ? '~' + String(timer.timeout) : '<1';
timer.modal.text = `Coffee in ${min} min`;
timer.modal.origin = applyMargin(originOnScreen(timer.modal, timer.screen, Orientation.SouthEast), MODAL_MARGIN, MODAL_MARGIN);

const screenOrigin = originOnScreen(timer.modal, timer.screen, Orientation.SouthEast);
timer.modal.origin = applyMargin(screenOrigin, MODAL_MARGIN, MODAL_MARGIN);
timer.modal.show();
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/misc/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const TERMINAL_APP = TERMINAL_NAME.replace(/[0-9]+$/, '');

// Initialize with current app status.
let term = App.get(TERMINAL_NAME);
let termIsActive = (function() {
let termIsActive = (() => {
const win = Window.focused();
if (!win) return false;
if (!win) { return false; }

return isTerminal(win.app());
})();
Expand Down
6 changes: 3 additions & 3 deletions src/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ enum Orientation {
NorthWest,
NorthEast,
SouthWest,
SouthEast
SouthEast,
}

function titleModal(text: string, duration: number = 1, icon?: Phoenix.Icon) {
Expand All @@ -17,11 +17,11 @@ function titleModal(text: string, duration: number = 1, icon?: Phoenix.Icon) {
m.showTitleOn(Screen.main());
}

Modal.prototype.showTitleOn = function(screen: Screen) {
Modal.prototype.showTitleOn = function _showTitleOn(screen: Screen) {
showAt(this, screen, 2, 1 + (1 / 3));
};

Modal.prototype.showCenterOn = function(screen: Screen) {
Modal.prototype.showCenterOn = function _showCenterOn(screen: Screen) {
showAt(this, screen, 2, 2);
};

Expand Down
46 changes: 23 additions & 23 deletions src/phoenix.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import * as _ from 'lodash';

import './window';
import './screen';
import './window';

import { onKey } from './key';
import { frameRatio } from './calc';
import { titleModal } from './modal';
import { hyper, hyperShift } from './config';
import { onKey } from './key';
import log from './logger';
import brightness from './misc/brightness';
import coffeTimer from './misc/coffee';
import { TimerStopper } from './misc/coffee';
import { Scanner } from './scan';
import * as terminal from './misc/terminal';
import { hyper, hyperShift } from './config';
import { titleModal } from './modal';
import { Scanner } from './scan';

let scanner = new Scanner();
let coffee: TimerStopper;

Phoenix.set({
'daemon': true,
'openAtLogin': true,
daemon: true,
openAtLogin: true,
});

Event.on('screensDidChange', () => log('Screens changed'));

onKey('tab', hyper, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let oldScreen = win.screen();
let newScreen = oldScreen.next();

if (oldScreen.isEqual(newScreen)) return;
if (oldScreen.isEqual(newScreen)) {
return;
}

let ratio = frameRatio(oldScreen.flippedVisibleFrame(), newScreen.flippedVisibleFrame());
win.setFrame(ratio(win.frame()));
});

onKey('left', hyper, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height, x, y } = win.screen().flippedVisibleFrame();
width = Math.ceil(width / 2);
Expand All @@ -49,7 +49,7 @@ onKey('left', hyper, () => {

onKey('right', hyper, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height, x, y } = win.screen().flippedVisibleFrame();
width /= 2;
Expand All @@ -62,7 +62,7 @@ onKey('right', hyper, () => {

onKey('up', hyper, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, x } = win.frame();
let { height, y } = win.screen().flippedVisibleFrame();
Expand All @@ -74,12 +74,12 @@ onKey('up', hyper, () => {

onKey('down', hyper, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, x } = win.frame();
let { height, y } = win.screen().flippedVisibleFrame();
height /= 2;
[ height, y ] = [ Math.ceil(height), y + Math.floor(height) ];
[height, y] = [Math.ceil(height), y + Math.floor(height)];

win.setFrame({ height, width, x, y });
win.clearUnmaximized();
Expand All @@ -94,7 +94,7 @@ onKey('return', hyper, () => {

onKey('left', hyperShift, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height, y } = win.frame();
let { x } = win.screen().flippedVisibleFrame();
Expand All @@ -104,7 +104,7 @@ onKey('left', hyperShift, () => {

onKey('right', hyperShift, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height, y } = win.frame();
let { width: sWidth, x } = win.screen().flippedVisibleFrame();
Expand All @@ -117,7 +117,7 @@ onKey('right', hyperShift, () => {

onKey('up', hyperShift, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height, x } = win.frame();
let { y } = win.screen().flippedVisibleFrame();
Expand All @@ -127,7 +127,7 @@ onKey('up', hyperShift, () => {

onKey('down', hyperShift, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height, x } = win.frame();
let { height: sHeight, y } = win.screen().flippedVisibleFrame();
Expand All @@ -140,7 +140,7 @@ onKey('down', hyperShift, () => {

onKey('return', hyperShift, () => {
let win = Window.focused();
if (!win) return;
if (!win) { return; }

let { width, height } = win.frame();
let { width: sWidth, height: sHeight, x, y } = win.screen().flippedVisibleFrame();
Expand All @@ -164,7 +164,7 @@ onKey('delete', hyper, () => {

onKey('m', hyper, () => {
let s = Screen.at(Mouse.location());
if (!s) return;
if (!s) { return; }

log(s.identifier(), Mouse.location());
});
Expand Down Expand Up @@ -202,7 +202,7 @@ onKey('space', hyper, () => {
m.showCenterOn(Screen.main());
});

scanner.scanln(s => {
scanner.scanln((s) => {
m.close();
tab.disable();
}, (s) => {
Expand Down
10 changes: 6 additions & 4 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Scanner {
this.enable();
this.doneCallback = done;
this.updateCallback = (s) => {
if (!s) return; // an update requires a character
if (!s) {
return; // an update requires a character
}
this.disable();
done(s);
};
Expand All @@ -39,7 +41,7 @@ class Scanner {
/**
* scanln scans an entire line (return ends scan).
*/
public scanln(done: ScanCallback, update: ScanCallback = (() => {})) {
public scanln(done: ScanCallback, update: ScanCallback = (() => undefined)) {
this.enable();
this.doneCallback = done;
this.updateCallback = update;
Expand All @@ -65,11 +67,11 @@ class Scanner {
}
}

this.keyHandlers.forEach(h => h.enable()); // make sure all handlers are enabled
this.keyHandlers.forEach((h) => h.enable()); // make sure all handlers are enabled
}

private disable() {
this.keyHandlers.forEach(h => {
this.keyHandlers.forEach((h) => {
h.disable();
const last = getHandler(h.key, h.modifiers);
if (last) {
Expand Down
19 changes: 0 additions & 19 deletions src/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,3 @@ function screenAt(point: Point) {

// Extend ScreenObject
Screen.at = screenAt;

class PositionHandler {
private screenStore: Map<string, Object>;
private store: Map<number, Object>;

constructor() {
this.screenStore = new Map();
this.store = new Map();
}

changed(win: Window) {
let window = Object.assign({}, win.frame());
let screen = Object.assign({}, win.screen().flippedVisibleFrame());
this.store.set(win.hash(), {window, screen});
}
remove(win: Window) {
this.store.delete(win.hash());
}
}
1 change: 0 additions & 1 deletion src/util.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function clearUnmaximized(win: Window) {
}

function unmaximizedFrame(win: Window): Rectangle {
let c = frameCache.get(win.hash());
let c = frameCache.get(win.hash());
let ratio = frameRatio(c.screen, win.screen().flippedVisibleFrame());
return ratio(c.window);
}
Expand All @@ -32,19 +32,19 @@ function toggleMaximized(win: Window) {
win.clearUnmaximized();
} else {
frameCache.set(id, {
window: win.frame(),
screen: win.screen().flippedVisibleFrame(),
window: win.frame(),
});
win.maximize();
}
}

Window.prototype.clearUnmaximized = function () { clearUnmaximized(this); };
Window.prototype.toggleMaximized = function () { toggleMaximized(this); };
Window.prototype.clearUnmaximized = function _clearUnmaximized() { clearUnmaximized(this); };
Window.prototype.toggleMaximized = function _toggleMaximized() { toggleMaximized(this); };

// FIXME: This is too hacky, I'd prefer not to monkey patch built-ins
let setFrameOrig = Window.prototype.setFrame;
Window.prototype.setFrame = function(frame: Rectangle): boolean {
Window.prototype.setFrame = function _setFrame(frame: Rectangle): boolean {
let ret = setFrameOrig.call(this, frame);
if (this.app().bundleIdentifier() === 'com.microsoft.Word') {
// Workaround for Microsoft Word resizing too slowly and thus not
Expand Down

0 comments on commit 939e782

Please sign in to comment.