Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMD + W closes currently opened Realm window #1733

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/src/main/helpers/shortcut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BrowserWindow, Event, Input, ipcMain, Menu } from 'electron';

import { isMac } from './env';

const registerListeners = (mainWindow: BrowserWindow) => {
if (isMac) {
registerDarwinListeners(mainWindow);
} else {
// registerDefaultListeners(mainWindow);
}

ipcMain.handle('shortcut-enabled', (_, id: string, enabled: boolean) => {
const menu = Menu.getApplicationMenu();
if (!menu) return;
const menuItem = menu.getMenuItemById(id);
if (!menuItem) return;
menuItem.enabled = enabled;
});
};

const registerDarwinListeners = (mainWindow: BrowserWindow) => {
mainWindow.webContents.on(
'before-input-event',
(event: Event, input: Input) => {
// CMD on Mac
if (input.meta && input.type === 'keyDown') {
if (input.key === 'w') {
mainWindow.webContents.send('shortcut-event', 'close-current-window');
event.preventDefault();
}
}
}
);
};

export const ShortcutHelper = { registerListeners };
2 changes: 2 additions & 0 deletions app/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { KeyHelper } from './helpers/key';
import { MediaHelper } from './helpers/media';
import { MouseHelper } from './helpers/mouse';
import { PowerHelper } from './helpers/power';
import { ShortcutHelper } from './helpers/shortcut';
import { WebViewHelper } from './helpers/webview';
import { MenuBuilder } from './menu';
import { resolveHtmlPath } from './util';
Expand Down Expand Up @@ -119,6 +120,7 @@ const createWindow = async () => {
PowerHelper.registerListeners(mainWindow);
KeyHelper.registerListeners(mainWindow);
DeepLinkHelper.registerListeners(mainWindow);
ShortcutHelper.registerListeners(mainWindow);

mainWindow.loadURL(resolveHtmlPath('index.html'));

Expand Down
87 changes: 55 additions & 32 deletions app/src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ export class MenuBuilder {
},
],
};
const subMenuFile: DarwinMenuItemConstructorOptions = {
label: 'File',
submenu: [
{
label: 'Close Current Window',
accelerator: 'CommandOrControl+W',
enabled: false,
click: () => {
this.mainWindow.webContents.send(
'shortcut-event',
'close-current-window'
);
},
},
],
};
const subMenuEdit: DarwinMenuItemConstructorOptions = {
label: 'Edit',
submenu: [
Expand Down Expand Up @@ -165,37 +181,37 @@ export class MenuBuilder {
{ label: 'Bring All to Front', selector: 'arrangeInFront:' },
],
};
const subMenuHelp: MenuItemConstructorOptions = {
label: 'Help',
submenu: [
{
label: 'Learn More',
click() {
shell.openExternal('https://electronjs.org');
},
},
{
label: 'Documentation',
click() {
shell.openExternal(
'https://github.com/electron/electron/tree/main/docs#readme'
);
},
},
{
label: 'Community Discussions',
click() {
shell.openExternal('https://www.electronjs.org/community');
},
},
{
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/electron/electron/issues');
},
},
],
};
// const subMenuHelp: MenuItemConstructorOptions = {
// label: 'Help',
// submenu: [
// {
// label: 'Learn More',
// click() {
// shell.openExternal('https://electronjs.org');
// },
// },
// {
// label: 'Documentation',
// click() {
// shell.openExternal(
// 'https://github.com/electron/electron/tree/main/docs#readme'
// );
// },
// },
// {
// label: 'Community Discussions',
// click() {
// shell.openExternal('https://www.electronjs.org/community');
// },
// },
// {
// label: 'Search Issues',
// click() {
// shell.openExternal('https://github.com/electron/electron/issues');
// },
// },
// ],
// };

// const subMenuView =
// process.env.NODE_ENV === 'development' ||
Expand All @@ -205,7 +221,14 @@ export class MenuBuilder {

const subMenuView = subMenuViewDev;

return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp];
return [
subMenuAbout,
subMenuFile,
subMenuEdit,
subMenuView,
subMenuWindow,
// subMenuHelp,
];
}

buildDefaultTemplate() {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ const appPreload = {
callback(hex);
});
},
onShortcutEvent(callback: (shortcut: string) => void) {
ipcRenderer.on('shortcut-event', (_, shortcut: string) => {
callback(shortcut);
});
},
onKeyDown(callback: (key: string, isFocused: boolean) => void) {
ipcRenderer.on('key-down', (_, key: string, isFocused: boolean) => {
callback(key, isFocused);
Expand Down Expand Up @@ -160,6 +165,9 @@ const appPreload = {
removeOnJoinSpace() {
ipcRenderer.removeAllListeners('join-space');
},
removeOnShortcutEvent() {
ipcRenderer.removeAllListeners('shortcut-event');
},
};

export type AppPreloadType = typeof appPreload;
Expand Down
1 change: 1 addition & 0 deletions app/src/renderer/stores/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const SpacesIPC = window.spacesService;
export const BazaarIPC = window.bazaarService;
export const AppInstallIPC = window.appInstallService;
export const AppRecentsIPC = window.appRecentsService;
// export const ShellIPC = window.shellService;
20 changes: 20 additions & 0 deletions app/src/renderer/stores/models/shell.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export const ShellModel = types
this.setActive(newWindow.appId);
if (self.homePaneOpen) self.homePaneOpen = false;

// ipcRenderer.invoke('shortcut-enabled', 'close-current-window', true);
return newWindow;
},
openBookmark(bookmark: Omit<Bookmark, 'favicon'>) {
Expand Down Expand Up @@ -279,6 +280,16 @@ export const ShellModel = types
toggleDevTools() {
return window.electron.app.toggleDevTools();
},
closeCurrentWindow() {
const windows = Array.from(self.windows.values());
const activeWindow = windows.find(
(app: AppWindowMobxType) => app.isActive
);
// not optimal
if (activeWindow) {
this.closeWindow(activeWindow.appId);
}
},
closeWindow(appId: string) {
const windows = Array.from(self.windows.values());
self.nativeConfig.delete(appId);
Expand All @@ -291,6 +302,8 @@ export const ShellModel = types
)[0];
if (nextWindow) {
this.setActive(nextWindow.appId);
} else {
// ipcRenderer.invoke('shortcut-enabled', 'close-current-window', false);
}
}
self.windows.delete(appId);
Expand All @@ -301,6 +314,13 @@ export const ShellModel = types
toggleMultiplayer() {
self.multiplayerEnabled = !self.multiplayerEnabled;
},
handleShortcutEvent(shortcut: string) {
switch (shortcut) {
case 'close-current-window':
this.closeCurrentWindow();
break;
}
},
}));

export type ShellModelType = Instance<typeof ShellModel>;
10 changes: 10 additions & 0 deletions app/src/renderer/system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ const ShellPresenter = () => {
[shellStore.dialogId, shellStore.dialogProps]
);

useEffect(() => {
window.electron.app.onShortcutEvent((shortcut: string) => {
shellStore.handleShortcutEvent(shortcut);
});

return () => {
window.electron.app.removeOnShortcutEvent();
};
}, []);

useEffect(() => {
// Sync Electron with MobX state.
if (shellStore.isIsolationMode) {
Expand Down