Skip to content

Instantly share code, notes, and snippets.

@pluma
Created September 29, 2017 16:01
Show Gist options
  • Save pluma/22bd93821d77c64432bf56191562a7a1 to your computer and use it in GitHub Desktop.
Save pluma/22bd93821d77c64432bf56191562a7a1 to your computer and use it in GitHub Desktop.
Prevent window from closing when last tab is closed (works great with autohide and visor)
'use strict';
const seen = new Set();
const $KNOWS_TO_HIDE = Symbol('knowsToHide');
exports.onWindow = window => {
if (window[$KNOWS_TO_HIDE]) return;
window[$KNOWS_TO_HIDE] = true;
window.rpc.on('hide', () => setTimeout(() => window.hide(), 0));
};
exports.middleware = store => next => action => {
switch (action.type) {
case 'SESSION_USER_EXIT':
case 'SESSION_PTY_EXIT':
if (Object.keys(store.getState().sessions.sessions).length === 1) {
const {ui} = store.getState();
const {cols, rows, cwd} = ui;
store.dispatch({
type: 'TERM_GROUP_REQUEST',
effect: () => {
if (seen.has(action.uid)) return;
seen.add(action.uid);
global.rpc.emit('new', {
isNewGroup: true,
cols,
rows,
cwd,
});
global.rpc.emit('hide', {});
setTimeout(() => next(action), 100);
setTimeout(() => seen.delete(action.uid), 5000);
},
});
return;
}
break;
}
next(action);
};
@pluma
Copy link
Author

pluma commented Sep 29, 2017

This is pretty hacky but works fine as long as you don't spam "close tab" commands. Sometimes it opens two tabs instead of one. No idea why that is.

There's a bug when using multiple windows so don't do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment