From 1829849a966cae4797917a1ee12616ef7904a012 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 9 Oct 2024 18:43:11 +0200 Subject: [PATCH] added menu and contextMenu --- package.json | 4 +- src/main/const.ts | 6 ++- src/main/index.ts | 21 +++++++++++ src/main/menu.ts | 82 +++++++++++++++++++++++++++++++++++++++++ src/main/windows.ts | 2 - yarn.lock | 89 ++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 src/main/menu.ts diff --git a/package.json b/package.json index 283ff9e..f67ecba 100644 --- a/package.json +++ b/package.json @@ -27,11 +27,14 @@ "@holochain/hc-spin-rust-utils": "0.300.1", "@lightningrodlabs/we-rust-utils": "0.300.2", "@msgpack/msgpack": "^2.8.0", + "adm-zip": "0.5.14", "bufferutil": "4.0.8", + "electron-context-menu": "3.6.1", "get-port": "7.0.0", "nanoid": "5.0.4", "split": "1.0.1", "ts-node": "10.9.2", + "typescript": "^5.2.2", "utf-8-validate": "^6.0.3", "winston": "3.11.0" }, @@ -52,7 +55,6 @@ "octokit": "4.0.2", "png2icons": "2.0.1", "rimraf": "^5.0.1", - "typescript": "^5.2.2", "vite": "^4.4.9" } } \ No newline at end of file diff --git a/src/main/const.ts b/src/main/const.ts index 1dbe1f1..248b229 100644 --- a/src/main/const.ts +++ b/src/main/const.ts @@ -36,4 +36,8 @@ export const HAPP_APP_ID = 'kangaroo.happ'; export const UI_DIRECTORY = path.join(RESOURCES_DIRECTORY, 'ui'); -export const ICON_PATH = path.join(RESOURCES_DIRECTORY, 'ui', 'icon.png'); \ No newline at end of file +export const ICON_PATH = path.join(RESOURCES_DIRECTORY, 'ui', 'icon.png'); + +export const isMac = process.platform === 'darwin'; +export const isWindows = process.platform === 'win32'; +export const isLinux = process.platform === 'linux'; \ No newline at end of file diff --git a/src/main/index.ts b/src/main/index.ts index f66a4c8..dccd1ff 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,6 +3,7 @@ import { BrowserWindow, ipcMain, IpcMainInvokeEvent, + Menu, protocol, } from "electron"; import childProcess from "child_process"; @@ -11,6 +12,7 @@ import { ZomeCallSigner, ZomeCallUnsignedNapi, } from "@holochain/hc-spin-rust-utils"; +import contextMenu from 'electron-context-menu'; import { encode } from "@msgpack/msgpack"; import { CallZomeRequest, @@ -33,6 +35,7 @@ import { UI_DIRECTORY, } from "./const"; import { initializeLairKeystore, launchLairKeystore } from "./lairKeystore"; +import { kangarooMenu } from "./menu"; // Read CLI options @@ -48,6 +51,22 @@ if (!app.isPackaged) { app.setName(KANGAROO_CONFIG.appId + '-dev'); } +contextMenu({ + showSaveImageAs: true, + showSearchWithGoogle: false, + showInspectElement: true, + append: (_defaultActions, _parameters, browserWindow) => [ + { + label: 'Reload', + click: () => (browserWindow as BrowserWindow).reload(), + }, + { + label: 'Quit Launcher', + click: () => app.quit(), + }, + ], +}); + const KANGAROO_FILESYSTEM = KangarooFileSystem.connect(app); const KANGAROO_EMITTER = new KangarooEmitter(); @@ -106,6 +125,8 @@ let LAIR_HANDLE: childProcess.ChildProcessWithoutNullStreams | undefined; let MAIN_WINDOW: BrowserWindow | undefined | null; let SPLASH_SCREEN_WINDOW: BrowserWindow | undefined; +Menu.setApplicationMenu(kangarooMenu(KANGAROO_FILESYSTEM)); + app.whenReady().then(async () => { SPLASH_SCREEN_WINDOW = createSplashWindow(); ipcMain.handle("sign-zome-call", handleSignZomeCall); diff --git a/src/main/menu.ts b/src/main/menu.ts new file mode 100644 index 0000000..66db8b2 --- /dev/null +++ b/src/main/menu.ts @@ -0,0 +1,82 @@ +import { Menu, app, dialog, shell } from 'electron'; +import { KangarooFileSystem } from './filesystem'; +import { isMac, KANGAROO_CONFIG } from './const'; +import AdmZip from 'adm-zip'; + +// extending from electron's default menu: https://github.com/electron/electron/blob/398dde9dfbdfcfd7757ead9a30785c01de9f0808/lib/browser/default-menu.ts#L12 +export const kangarooMenu = (kangarooFs: KangarooFileSystem) => { + const macAppMenu: Electron.MenuItemConstructorOptions = { role: 'appMenu' }; + const helpMenu: Electron.MenuItemConstructorOptions = { + role: 'help', + submenu: [ + { + label: 'Open Logs', + async click() { + try { + await shell.openPath(kangarooFs.appLogsDir); + } catch (e) { + dialog.showErrorBox('Failed to open logs folder', (e as any).toString()); + } + }, + }, + { + label: 'Export Logs', + async click() { + try { + const zip = new AdmZip(); + zip.addLocalFolder(kangarooFs.appLogsDir); + const exportToPathResponse = await dialog.showSaveDialog({ + title: 'Export Logs', + buttonLabel: 'Export', + defaultPath: `Moss_${app.getVersion()}_logs_${new Date().toISOString()}.zip`, + }); + if (exportToPathResponse.filePath) { + zip.writeZip(exportToPathResponse.filePath); + shell.showItemInFolder(exportToPathResponse.filePath); + } + } catch (e) { + dialog.showErrorBox('Failed to export logs', (e as any).toString()); + } + }, + }, + ], + }; + + const applicationMenu: Electron.MenuItemConstructorOptions = { + label: KANGAROO_CONFIG.productName, + submenu: [ + { + label: 'Restart', + click() { + const options: Electron.RelaunchOptions = { + args: process.argv, + }; + // https://github.com/electron-userland/electron-builder/issues/1727#issuecomment-769896927 + if (process.env.APPIMAGE) { + console.log('process.execPath: ', process.execPath); + options.args?.unshift('--appimage-extract-and-run'); + options.execPath = process.env.APPIMAGE; + } + app.relaunch(options); + app.quit(); + }, + }, + { + label: 'Quit', + type: 'normal', + click() { + app.quit(); + }, + }, + ], + }; + + return Menu.buildFromTemplate([ + ...(isMac ? [macAppMenu] : []), + applicationMenu, + { role: 'editMenu' }, + { role: 'viewMenu' }, + { role: 'windowMenu' }, + helpMenu, + ]); +}; diff --git a/src/main/windows.ts b/src/main/windows.ts index 49935df..cd5ff71 100644 --- a/src/main/windows.ts +++ b/src/main/windows.ts @@ -147,8 +147,6 @@ export const createHappWindow = async ( throw new Error(`Unsupported uiSource type: ${(uiSource as any).type}`); } - console.log("Showing browser window"); - happWindow.show(); return happWindow; diff --git a/yarn.lock b/yarn.lock index 0c5cd8b..c34159a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1365,6 +1365,11 @@ acorn@^8.11.0, acorn@^8.4.1, acorn@^8.9.0: resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.12.1.tgz#71616bdccbe25e27a54439e0046e89ca76df2248" integrity sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg== +adm-zip@0.5.14: + version "0.5.14" + resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.5.14.tgz#2c557c0bf12af4311cf6d32970f4060cf8133b2a" + integrity sha512-DnyqqifT4Jrcvb8USYjp6FHtBpEIz1mnXu6pTRHZ0RL69LbQYiO+0lDFg5+OKA7U29oWSs3a/i8fhn8ZcceIWg== + agent-base@6: version "6.0.2" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" @@ -2143,6 +2148,29 @@ electron-builder@^24.6.3: simple-update-notifier "2.0.0" yargs "^17.6.2" +electron-context-menu@3.6.1: + version "3.6.1" + resolved "https://registry.yarnpkg.com/electron-context-menu/-/electron-context-menu-3.6.1.tgz#42f117e15309687b22283e6f8f7a0d95a19afe84" + integrity sha512-lcpO6tzzKUROeirhzBjdBWNqayEThmdW+2I2s6H6QMrwqTVyT3EK47jW3Nxm60KTxl5/bWfEoIruoUNn57/QkQ== + dependencies: + cli-truncate "^2.1.0" + electron-dl "^3.2.1" + electron-is-dev "^2.0.0" + +electron-dl@^3.2.1: + version "3.5.2" + resolved "https://registry.yarnpkg.com/electron-dl/-/electron-dl-3.5.2.tgz#9e83d624473ec90682928b19ae9221a0e0b83a39" + integrity sha512-i104cl+u8yJ0lhpRAtUWfeGuWuL1PL6TBiw2gLf0MMIBjfgE485Ags2mcySx4uWU9P9uj/vsD3jd7X+w1lzZxw== + dependencies: + ext-name "^5.0.0" + pupa "^2.0.1" + unused-filename "^2.1.0" + +electron-is-dev@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-2.0.0.tgz#833487a069b8dad21425c67a19847d9064ab19bd" + integrity sha512-3X99K852Yoqu9AcW50qz3ibYBWY79/pBhlMCab8ToEWS48R0T9tyxRiQhwylE7zQdXrMnx2JKqUJyMPmt5FBqA== + electron-publish@24.13.1: version "24.13.1" resolved "https://registry.yarnpkg.com/electron-publish/-/electron-publish-24.13.1.tgz#57289b2f7af18737dc2ad134668cdd4a1b574a0c" @@ -2389,6 +2417,11 @@ escalade@^3.1.1, escalade@^3.2.0: resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.2.0.tgz#011a3f69856ba189dffa7dc8fcce99d2a87903e5" integrity sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA== +escape-goat@^2.0.0: + version "2.1.1" + resolved "https://registry.yarnpkg.com/escape-goat/-/escape-goat-2.1.1.tgz#1b2dc77003676c457ec760b2dc68edb648188675" + integrity sha512-8/uIhbG12Csjy2JEW7D9pHbreaVaS/OpN3ycnyvElTdwM5n6GY6W6e2IPemfvGZeUMqZ9A/3GqIZMgKnBhAw/Q== + escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" @@ -2566,6 +2599,21 @@ events@^3.3.0: resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q== +ext-list@^2.0.0: + version "2.2.2" + resolved "https://registry.yarnpkg.com/ext-list/-/ext-list-2.2.2.tgz#0b98e64ed82f5acf0f2931babf69212ef52ddd37" + integrity sha512-u+SQgsubraE6zItfVA0tBuCBhfU9ogSRnsvygI7wht9TS510oLkBRXBsqopeUG/GBOIQyKZO9wjTqIu/sf5zFA== + dependencies: + mime-db "^1.28.0" + +ext-name@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/ext-name/-/ext-name-5.0.0.tgz#70781981d183ee15d13993c8822045c506c8f0a6" + integrity sha512-yblEwXAbGv1VQDmow7s38W77hzAgJAO50ztBLMcUyUBfxv1HC+LGwtiEN+Co6LtlqT/5uwVOxsD4TNIilWhwdQ== + dependencies: + ext-list "^2.0.0" + sort-keys-length "^1.0.0" + extract-zip@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/extract-zip/-/extract-zip-2.0.1.tgz#663dca56fe46df890d5f131ef4a06d22bb8ba13a" @@ -3185,7 +3233,7 @@ is-path-inside@^3.0.3: resolved "https://registry.yarnpkg.com/is-path-inside/-/is-path-inside-3.0.3.tgz#d231362e53a07ff2b0e0ea7fed049161ffd16283" integrity sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ== -is-plain-obj@^1.1.0: +is-plain-obj@^1.0.0, is-plain-obj@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e" integrity sha512-yvkRyxmFKEOQ4pNXCmJG5AEQNlXJS5LaONXo5/cLdTZdWvsZ1ioJEonLGAosKlMWE8lwUy/bJzMjcw8az73+Fg== @@ -3542,6 +3590,11 @@ mime-db@1.52.0: resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== +mime-db@^1.28.0: + version "1.53.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.53.0.tgz#3cb63cd820fc29896d9d4e8c32ab4fcd74ccb447" + integrity sha512-oHlN/w+3MQ3rba9rqFr6V/ypF10LSkdwUysQL7GkXoTgIWeV+tcXGA852TBxH+gsh8UWoyhR1hKcoMJTuWflpg== + mime-types@^2.1.12: version "2.1.35" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" @@ -3641,6 +3694,11 @@ mkdirp@^1.0.3: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +modify-filename@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/modify-filename/-/modify-filename-1.1.0.tgz#9a2dec83806fbb2d975f22beec859ca26b393aa1" + integrity sha512-EickqnKq3kVVaZisYuCxhtKbZjInCuwgwZWyAmRIp1NTMhri7r3380/uqwrUHfaDiPzLVTuoNy4whX66bxPVog== + ms@^2.1.1, ms@^2.1.3: version "2.1.3" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" @@ -3961,6 +4019,13 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5" integrity sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg== +pupa@^2.0.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/pupa/-/pupa-2.1.1.tgz#f5e8fd4afc2c5d97828faa523549ed8744a20d62" + integrity sha512-l1jNAspIBSFqbT+y+5FosojNpVpF94nlI+wDUpqP9enwOTfHx9f0gh5nB96vl+6yTpsJsypeNrwfzPrKuHB41A== + dependencies: + escape-goat "^2.0.0" + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -4271,6 +4336,20 @@ smart-buffer@^4.0.2: resolved "https://registry.yarnpkg.com/smart-buffer/-/smart-buffer-4.2.0.tgz#6e1d71fa4f18c05f7d0ff216dd16a481d0e8d9ae" integrity sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg== +sort-keys-length@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/sort-keys-length/-/sort-keys-length-1.0.1.tgz#9cb6f4f4e9e48155a6aa0671edd336ff1479a188" + integrity sha512-GRbEOUqCxemTAk/b32F2xa8wDTs+Z1QHOkbhJDQTvv/6G3ZkbJ+frYWsTcc7cBB3Fu4wy4XlLCuNtJuMn7Gsvw== + dependencies: + sort-keys "^1.0.0" + +sort-keys@^1.0.0: + version "1.1.2" + resolved "https://registry.yarnpkg.com/sort-keys/-/sort-keys-1.1.2.tgz#441b6d4d346798f1b4e49e8920adfba0e543f9ad" + integrity sha512-vzn8aSqKgytVik0iwdBEi+zevbTYZogewTUM6dtpmGwEcdzbub/TX4bCzRhebDCRC3QzXgJsLRKB2V/Oof7HXg== + dependencies: + is-plain-obj "^1.0.0" + source-map-js@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.1.tgz#1ce5650fddd87abc099eda37dcff024c2667ae46" @@ -4704,6 +4783,14 @@ universalify@^2.0.0: resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.1.tgz#168efc2180964e6386d061e094df61afe239b18d" integrity sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw== +unused-filename@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/unused-filename/-/unused-filename-2.1.0.tgz#33719c4e8d9644f32d2dec1bc8525c6aaeb4ba51" + integrity sha512-BMiNwJbuWmqCpAM1FqxCTD7lXF97AvfQC8Kr/DIeA6VtvhJaMDupZ82+inbjl5yVP44PcxOuCSxye1QMS0wZyg== + dependencies: + modify-filename "^1.1.0" + path-exists "^4.0.0" + update-browserslist-db@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.1.1.tgz#80846fba1d79e82547fb661f8d141e0945755fe5"