From b272af30c7c3c647a83ca058fbc85759f4254e8a Mon Sep 17 00:00:00 2001 From: kyranjamie Date: Mon, 16 Nov 2020 10:41:01 +0100 Subject: [PATCH] feat: wip context menus --- .../transaction-list-context-menu.ts | 78 +++++++++---------- app/main.dev.ts | 16 +++- app/preload.js | 4 +- app/types/index.ts | 2 + 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/app/components/home/transaction-list/transaction-list-context-menu.ts b/app/components/home/transaction-list/transaction-list-context-menu.ts index 7e9f21414..31f7b9065 100644 --- a/app/components/home/transaction-list/transaction-list-context-menu.ts +++ b/app/components/home/transaction-list/transaction-list-context-menu.ts @@ -1,4 +1,3 @@ -// import { remote } from 'electron'; import { useClipboard } from '@blockstack/ui'; import { Transaction } from '@blockstack/stacks-blockchain-api-types'; @@ -29,46 +28,39 @@ interface TxListContextMenu { } export function createTxListContextMenu(event: Event, { tx, copy }: TxListContextMenu) { - // event.preventDefault(); - // const { Menu, MenuItem } = remote; - // const menu = new Menu(); - // const menuItems: Electron.MenuItemConstructorOptions[] = [ - // { - // label: 'Copy to clipboard', - // enabled: false, - // }, - // { type: 'separator' }, - // { - // label: 'Transaction ID', - // click: () => copy.txid.onCopy(), - // }, - // { - // label: 'Recipient address', - // visible: !!getRecipientAddress(tx), - // click: () => copy.recipientAddress.onCopy(), - // }, - // { - // label: 'Memo', - // visible: hasMemo(tx), - // click: () => copy.memo.onCopy(), - // }, - // { - // label: 'Timestamp', - // click: () => copy.date.onCopy(), - // }, - // { - // label: 'Transaction (as JSON)', - // click: () => copy.txDetails.onCopy(), - // }, - // { - // label: 'Explorer link', - // click: () => copy.explorerLink.onCopy(), - // }, - // ]; - // menuItems.forEach(item => menu.append(new MenuItem(item))); - // menu.popup({ window: remote.getCurrentWindow() }); - // menu.once('menu-will-close', () => { - // // `destroy` call untyped - // (menu as any).destroy(); - // }); + event.preventDefault(); + const menuItems: Electron.MenuItemConstructorOptions[] = [ + { + label: 'Copy to clipboard', + enabled: false, + }, + { type: 'separator' }, + { + label: 'Transaction ID', + click: () => copy.txid.onCopy(), + }, + { + label: 'Recipient address', + visible: !!getRecipientAddress(tx), + click: () => copy.recipientAddress.onCopy(), + }, + { + label: 'Memo', + visible: hasMemo(tx), + click: () => copy.memo.onCopy(), + }, + { + label: 'Timestamp', + click: () => copy.date.onCopy(), + }, + { + label: 'Transaction (as JSON)', + click: () => copy.txDetails.onCopy(), + }, + { + label: 'Explorer link', + click: () => copy.explorerLink.onCopy(), + }, + ]; + api.contextMenu(menuItems); } diff --git a/app/main.dev.ts b/app/main.dev.ts index 3cc0c0866..9e62e7e02 100644 --- a/app/main.dev.ts +++ b/app/main.dev.ts @@ -16,7 +16,7 @@ import 'core-js/stable'; import 'regenerator-runtime/runtime'; import path from 'path'; -import { app, BrowserWindow, ipcMain } from 'electron'; +import { app, BrowserWindow, ipcMain, Menu, MenuItem } from 'electron'; import { autoUpdater } from 'electron-updater'; import log from 'electron-log'; import windowState from 'electron-window-state'; @@ -96,7 +96,7 @@ const createWindow = async () => { mainWindowState.manage(mainWindow); if (process.env.NODE_ENV === 'development' && process.env.DEBUG_PROD !== 'true') { - void mainWindow.loadURL(`file://${__dirname}/app-dev.html`); + void mainWindow.loadFile(`app-dev.html`); } if (process.env.NODE_ENV === 'production' || process.env.DEBUG_PROD === 'true') { @@ -182,6 +182,16 @@ ipcMain.handle('derive-key', async (_e, args) => { return deriveKey(args); }); -ipcMain.handle('reload-app', _e => { +ipcMain.handle('reload-app', () => { mainWindow?.reload(); }); + +ipcMain.on('context-menu-open', (_e, { menuItems }) => { + const menu = new Menu(); + menuItems.forEach((item: Electron.MenuItemConstructorOptions) => menu.append(new MenuItem(item))); + menu.popup({ window: mainWindow?.getParentWindow() }); + menu.once('menu-will-close', () => { + // `destroy` call untyped + (menu as any).destroy(); + }); +}); diff --git a/app/preload.js b/app/preload.js index ed601b47a..4d3ed90f7 100644 --- a/app/preload.js +++ b/app/preload.js @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-var-requires */ const fs = require('fs'); -const { contextBridge, ipcRenderer, app, shell, remote } = require('electron'); +const { contextBridge, ipcRenderer, app, shell } = require('electron'); const TransportNodeHid = require('@ledgerhq/hw-transport-node-hid').default; @@ -75,4 +75,6 @@ contextBridge.exposeInMainWorld('api', { }; }, }, + + contextMenu: menuItems => ipcRenderer.send('context-menu-open', { menuItems }), }); diff --git a/app/types/index.ts b/app/types/index.ts index 65db74a86..f03123eae 100644 --- a/app/types/index.ts +++ b/app/types/index.ts @@ -24,6 +24,8 @@ declare const api: { reloadApp(): void; + contextMenu(menuItems: any[]): void; + nodeHid: { listen: typeof import('@ledgerhq/hw-transport').default['listen']; open({