From e29f09d18cb3d665f3cdced68badb297239ab912 Mon Sep 17 00:00:00 2001 From: 4gray Date: Sat, 27 Feb 2021 13:37:43 +0100 Subject: [PATCH] refactor: some improvements in app menu Improved the class structure of menu and added "Settings" as new menu item --- main.ts | 4 +- menu.ts | 171 ++++++++++++++++++++++++--------------- src/app/app.component.ts | 9 +++ 3 files changed, 117 insertions(+), 67 deletions(-) diff --git a/main.ts b/main.ts index afa2f85b2..d057a1dad 100644 --- a/main.ts +++ b/main.ts @@ -94,8 +94,8 @@ try { app.on('ready', () => { // create main window and set menu const win = createWindow(); - const menu = AppMenu.createMenu(win); - Menu.setApplicationMenu(menu); + const menu = new AppMenu(win); + Menu.setApplicationMenu(menu.getMenu()); api.setMainWindow(win); // create hidden window for epg worker diff --git a/menu.ts b/menu.ts index 706872861..9f7ec1177 100644 --- a/menu.ts +++ b/menu.ts @@ -3,80 +3,121 @@ import * as path from 'path'; const openAboutWindow = require('about-window').default; export class AppMenu { + /** Application menu */ + menu: Menu = new Menu(); + + /** Application window */ + window: BrowserWindow; + + constructor(appWindow: BrowserWindow) { + this.window = appWindow; + this.initMenu(); + } + /** * Creates context menu * @param win browser window object */ - static createMenu(win: BrowserWindow): Menu { - const menu = new Menu(); - menu.append( - new MenuItem({ - label: 'File', - submenu: [ - { - label: 'Add playlist', - click: () => win.webContents.send('add-playlist-view'), - }, - { - type: 'separator', - }, - { - label: 'Exit', - click: () => app.quit(), - }, - ], - }) - ); + initMenu(): void { + this.menu.append(this.getFileMenu()); // copy-paste shortcuts workaround for mac os if (process.platform === 'darwin') { - menu.append( - new MenuItem({ - label: 'Edit', - submenu: [ - { role: 'cut' }, - { role: 'copy' }, - { role: 'paste' }, - { role: 'delete' }, - ], - }) - ); + this.menu.append(this.getEditMenu()); } - menu.append( - new MenuItem({ - label: 'Help', - submenu: [ - { - label: 'Report a bug', - click: () => - shell.openExternal( - 'https://github.com/4gray/iptvnator' - ), - }, - { - label: 'Open DevTools', - click: () => win.webContents.openDevTools(), - }, - { - type: 'separator', - }, - { - label: 'About', - click: () => - openAboutWindow({ - icon_path: path.join( - __dirname, - 'dist/assets/icons/icon.png' - ), - copyright: 'Copyright (c) 2020 4gray', - package_json_dir: __dirname, - }), - }, - ], - }) - ); + this.menu.append(this.getHelpMenu()); + } - return menu; + /** + * Return the application menu + */ + getMenu(): Menu { + return this.menu; + } + + /** + * Creates and returns the file menu + * @param win application window + */ + private getFileMenu(): MenuItem { + return new MenuItem({ + label: 'File', + submenu: [ + { + label: 'Add playlist', + click: () => + this.window.webContents.send('add-playlist-view'), + }, + { + type: 'separator', + }, + { + label: 'Settings', + click: () => this.window.webContents.send('settings-view'), + }, + { + type: 'separator', + }, + { + label: 'Exit', + click: () => app.quit(), + }, + ], + }); + } + + /** + * Creates and returns the edit menu + * @param win application window + */ + private getHelpMenu(): MenuItem { + return new MenuItem({ + label: 'Help', + submenu: [ + { + label: 'Report a bug', + click: () => + shell.openExternal( + 'https://github.com/4gray/iptvnator' + ), + }, + { + label: 'Open DevTools', + click: () => this.window.webContents.openDevTools(), + }, + { + type: 'separator', + }, + { + label: 'About', + click: () => + openAboutWindow({ + icon_path: path.join( + __dirname, + 'dist/assets/icons/icon.png' + ), + copyright: 'Copyright (c) 2020-2021 4gray', + package_json_dir: __dirname, + }), + }, + ], + }); + } + + /** + * Creates and returns the help menu + * @param win application window + */ + private getEditMenu(): MenuItem { + return new MenuItem({ + label: 'Edit', + submenu: [ + { role: 'cut' }, + { role: 'copy' }, + { role: 'paste' }, + { role: 'delete' }, + ], + }); } } diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 89a6f3ad5..1de074c06 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -53,6 +53,14 @@ export class AppComponent { }); }); + this.electronService.ipcRenderer.on('settings-view', () => { + this.ngZone.run(() => { + this.router.navigateByUrl('/settings', { + skipLocationChange: true, + }); + }); + }); + this.electronService.ipcRenderer.on(EPG_FETCH_DONE, () => { this.ngZone.run(() => { this.channelStore.setEpgAvailableFlag(true); @@ -120,5 +128,6 @@ export class AppComponent { this.electronService.ipcRenderer.removeAllListeners( 'add-playlist-view' ); + this.electronService.ipcRenderer.removeAllListeners('settings-view'); } }