Skip to content

Commit

Permalink
Added keyboard shortcuts for reopen closed window, and switching windows
Browse files Browse the repository at this point in the history
in electron
Closes #1229.
Closes #1235.
  • Loading branch information
imolorhe committed May 2, 2020
1 parent 53ff5ae commit 83c6392
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/altair-app/src/app/actions/windows-meta/windows-meta.ts
@@ -1,6 +1,8 @@
import { Action as NGRXAction } from '@ngrx/store';

export const SET_ACTIVE_WINDOW_ID = 'SET_ACTIVE_WINDOW_ID';
export const SET_NEXT_WINDOW_ACTIVE = 'SET_NEXT_WINDOW_ACTIVE';
export const SET_PREVIOUS_WINDOW_ACTIVE = 'SET_PREVIOUS_WINDOW_ACTIVE';
export const SET_WINDOW_IDS = 'SET_WINDOW_IDS';
export const REPOSITION_WINDOW = 'REPOSITION_WINDOW';

Expand All @@ -16,6 +18,18 @@ export class SetActiveWindowIdAction implements NGRXAction {
constructor(public payload: { windowId: string }) {}
}

export class SetNextWindowActiveAction implements NGRXAction {
readonly type = SET_NEXT_WINDOW_ACTIVE;

constructor(public payload?: any) {}
}

export class SetPreviousWindowAction implements NGRXAction {
readonly type = SET_PREVIOUS_WINDOW_ACTIVE;

constructor(public payload?: any) {}
}

export class SetWindowIdsAction implements NGRXAction {
readonly type = SET_WINDOW_IDS;

Expand Down Expand Up @@ -60,6 +74,8 @@ export class ShowPluginManagerAction implements NGRXAction {

export type Action =
| SetActiveWindowIdAction
| SetNextWindowActiveAction
| SetPreviousWindowAction
| SetWindowIdsAction
| RepositionWindowAction
| ShowImportCurlDialogAction
Expand Down
22 changes: 22 additions & 0 deletions packages/altair-app/src/app/reducers/windows-meta/windows-meta.ts
Expand Up @@ -26,6 +26,28 @@ export const getInitialState = (): State => {

export function windowsMetaReducer(state = getInitialState(), action: windowsMeta.Action): State {
switch (action.type) {
case windowsMeta.SET_ACTIVE_WINDOW_ID:
return { ...state, activeWindowId: action.payload.windowId };
case windowsMeta.SET_NEXT_WINDOW_ACTIVE: {
const idx = state.windowIds.findIndex(id => id === state.activeWindowId);
let newActiveWindowId = '';
if (idx >= state.windowIds.length - 1) {
newActiveWindowId = state.windowIds[0];
} else {
newActiveWindowId = state.windowIds[idx + 1];
}
return { ...state, activeWindowId: newActiveWindowId };
}
case windowsMeta.SET_PREVIOUS_WINDOW_ACTIVE: {
const idx = state.windowIds.findIndex(id => id === state.activeWindowId);
let newActiveWindowId = '';
if (idx <= 0) {
newActiveWindowId = state.windowIds[state.windowIds.length - 1];
} else {
newActiveWindowId = state.windowIds[idx - 1];
}
return { ...state, activeWindowId: newActiveWindowId };
}
case windowsMeta.SET_ACTIVE_WINDOW_ID:
return { ...state, activeWindowId: action.payload.windowId };
case windowsMeta.SET_WINDOW_IDS:
Expand Down
Expand Up @@ -13,6 +13,7 @@ import * as fromHeader from '../../reducers/headers/headers';
import * as queryActions from '../../actions/query/query';
import * as docsActions from '../../actions/docs/docs';
import * as windowsMetaActions from '../../actions/windows-meta/windows-meta';
import * as windowsActions from '../../actions/windows/windows';
import { debug } from 'app/utils/logger';

@Injectable()
Expand Down Expand Up @@ -62,6 +63,18 @@ export class ElectronAppService {
});
});

this.ipc.on('next-tab', () => {
this.zone.run(() => this.store.dispatch(new windowsMetaActions.SetNextWindowActiveAction()));
});

this.ipc.on('previous-tab', () => {
this.zone.run(() => this.store.dispatch(new windowsMetaActions.SetPreviousWindowAction()));
});

this.ipc.on('reopen-closed-tab', () => {
this.zone.run(() => this.store.dispatch(new windowsActions.ReopenClosedWindowAction()));
});

this.ipc.on('send-request', () => {
this.zone.run(() => this.store.dispatch(new queryActions.SendQueryRequestAction(this.activeWindowId)));
});
Expand Down
15 changes: 15 additions & 0 deletions packages/altair-electron/src/menu.js
Expand Up @@ -15,6 +15,11 @@ const createMenu = (actions) => {
accelerator: 'CmdOrCtrl+W',
click: actions.closeTab
},
{
label: 'Reopen Closed Tab',
accelerator: 'CmdOrCtrl+Shift+T',
click: actions.reopenClosedTab
},
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
Expand All @@ -29,6 +34,16 @@ const createMenu = (actions) => {
{
label: "View",
submenu: [
{
label: 'Next Tab',
accelerator: 'CmdOrCtrl+Tab',
click: actions.nextTab
},
{
label: 'Previous Tab',
accelerator: 'CmdOrCtrl+Shift+Tab',
click: actions.previousTab
},
{ role: "reload" },
{ role: "forcereload" },
{ role: "toggledevtools" },
Expand Down
11 changes: 9 additions & 2 deletions packages/altair-electron/src/window.js
Expand Up @@ -29,13 +29,20 @@ let requestHeaders = {};

const actions = {
createTab: () => {
console.log('Create tab.');
instance.webContents.send('create-tab', true);
},
closeTab: () => {
console.log('Close tab.');
instance.webContents.send('close-tab', true);
},
nextTab: () => {
instance.webContents.send('next-tab', true);
},
previousTab: () => {
instance.webContents.send('previous-tab', true);
},
reopenClosedTab: () => {
instance.webContents.send('reopen-closed-tab', true);
},
sendRequest: () => {
instance.webContents.send('send-request', true);
},
Expand Down

0 comments on commit 83c6392

Please sign in to comment.