Skip to content

Commit

Permalink
Add option to hide dock icon (#15)
Browse files Browse the repository at this point in the history
* Add Yarn, upgrade dependencies

* Add option to hide dock icon

- Add hide dock icon option
- Split up entry file into testable modules

* Modularize source

- Separate source into testable modules
- Remove seemingly unnecessary listeners
- Clean up syntax

* Revert removal of focus listener

Re-instate focus listener that managed window stack order.

* Add Travis CI config

* Upgrade hyperterm-register-shortcut

Pull in latest version that supports additional passed arguments.

* Fix test titles
  • Loading branch information
dcalhoun authored Apr 15, 2017
1 parent 984cdd2 commit 527ba38
Show file tree
Hide file tree
Showing 8 changed files with 2,123 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "stable"
script: yarn test
3 changes: 3 additions & 0 deletions __mocks__/hyperterm-register-shortcut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
registerShortcut = () => jest.fn()

module.exports = registerShortcut
59 changes: 5 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,9 @@
const registerShortcut = require('hyperterm-register-shortcut');
const windowSet = new Set([]);

function showWindows (windows, app) {
windows.forEach((win, index) => {
if (index === windowSet.length-1) {
win.focus();
} else {
win.show();
}
});
if (process.platform !== 'win32') app.show();
}
const addWindow = require('./modules/windows').addWindow;
const setup = require('./modules/setup');

function hideWindows (windows, app) {
windows.forEach(win => {
if (win.isFullScreen()) return;
if (process.platform === 'win32') {
win.minimize();
} else {
win.hide();
}
});
if (process.platform !== 'win32') app.hide(); // Mac OS only (re-focuses the last active app)
}

function toggleWindowVisibility (app) {
const windows = [...windowSet];
const focusedWindows = windows.filter(window => window.isFocused());
if (focusedWindows.length > 0) {
hideWindows(windows, app);
} else {
showWindows(windows, app);
}
}

function addWindow (window) {
windowSet.add(window);
window.on('focus', () => {
windowSet.delete(window);
windowSet.add(window);
});
window.on('close', () => {
windowSet.delete(window);
});
}

function setup (app) {
registerShortcut('summon', toggleWindowVisibility)(app);
app.on('activate', () => {
showWindows([...windowSet], app);
});
}
const windowSet = new Set([]);

module.exports = {
onApp: setup,
onWindow: addWindow
onApp: app => setup(app, windowSet),
onWindow: window => addWindow(window, windowSet)
}
20 changes: 20 additions & 0 deletions modules/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const registerShortcut = require('hyperterm-register-shortcut');
const toggleWindowVisibility = require('./windows').toggleWindowVisibility;

const DEFAULTS = {
hideDock: false
}

module.exports = function setup (app, windowSet) {
const config = Object.assign({}, DEFAULTS, app.config.getConfig().summon);

if (config.hideDock) {
app.dock.hide();
}

registerShortcut('summon', toggleWindowVisibility)(app, windowSet);

app.on('activate', () => {
showWindows([...windowSet], app);
});
}
40 changes: 40 additions & 0 deletions modules/setup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const setup = require('./setup');

jest.mock('hyperterm-register-shortcut');

let app = {
config: {
getConfig: jest.fn(() => ({}))
},
dock: {
hide: jest.fn()
},
on: jest.fn()
};

describe('setup', () => {
describe('with default config', () => {
beforeEach(() => {
setup(app);
});

it('does not hide the dock', () => {
expect(app.dock.hide).not.toHaveBeenCalled();
});
});

describe('with hideDock config enabled', () => {
beforeEach(() => {
app.config.getConfig.mockReturnValueOnce({
summon: {
hideDock: true
}
})
setup(app);
});

it('hides the dock', () => {
expect(app.dock.hide).toHaveBeenCalled();
});
});
})
60 changes: 60 additions & 0 deletions modules/windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function showWindows (windows, app) {
windows.forEach((win, index) => {
if (index === windows.length - 1) {
win.show();
win.focus();
} else {
win.show();
}
});

if (process.platform !== 'win32') {
app.show();
}
}

function hideWindows (windows, app) {
windows.forEach(win => {
if (win.isFullScreen()) {
return;
}

process.platform === 'win32'
? win.minimize()
: win.hide();
});

// Re-focuses the last active app for macOS only
if (process.platform !== 'win32') {
app.hide();
}
}

function toggleWindowVisibility (app, windowSet) {
const windows = [...windowSet];
const focusedWindows = windows.filter(window => window.isFocused());

focusedWindows.length > 0
? hideWindows(windows, app)
: showWindows(windows, app);
}

function addWindow (window, windowSet) {
windowSet.add(window);

window.on('focus', () => {
windowSet.delete(window);
windowSet.add(window);
});

window.on('close', () => {
windowSet.delete(window);
});
}

module.exports = {
addWindow,
hideWindows,
showWindows,
toggleWindowVisibility
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/Soutar/hyperterm-summon.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"keywords": [
"hyperterm",
Expand All @@ -23,6 +23,12 @@
"author": "John Soutar <[email protected]>",
"license": "ISC",
"dependencies": {
"hyperterm-register-shortcut": "^1.0.1"
"hyperterm-register-shortcut": "^1.2.0"
},
"devDependencies": {
"jest": "^19.0.2"
},
"peerDependencies": {
"electron": "^1.6.2"
}
}
Loading

0 comments on commit 527ba38

Please sign in to comment.