Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.