Skip to content

Commit

Permalink
fix context menu, closes #49
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Nov 8, 2018
1 parent 2bb070e commit 9491f45
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "email-securely-app",
"description": "Unofficial desktop app for E2E encrypted email providers",
"version": "1.5.3",
"version": "1.5.4",
"author": "Vladimir Yakovlev <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/vladimiry/email-securely-app",
Expand Down
2 changes: 1 addition & 1 deletion src/electron-main/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test.serial("workflow", async (t) => {
t.true(m["./api"].initApi.calledWithExactly(t.context.ctx), `"initApi" called`);
t.true(m["./api"].initApi.calledAfter(m["./session"].clearDefaultSessionCaches), `"initApi" called after "clearDefaultSessionCaches"`);

t.true(m["./web-content-context-menu"].initWebContentContextMenu.calledWithExactly(t.context.ctx), `"initWebContentContextMenu" called`);
t.true(m["./web-content-context-menu"].initWebContentContextMenu.calledWithExactly(), `"initWebContentContextMenu" called`);
t.true(m["./web-content-context-menu"].initWebContentContextMenu.calledBefore(m["./window"].initBrowserWindow), `"initWebContentContextMenu" called before "initBrowserWindow"`);
t.true(m["./web-content-context-menu"].initWebContentContextMenu.calledBefore(m["./tray"].initTray), `"initWebContentContextMenu" called before "initTray"`);

Expand Down
2 changes: 1 addition & 1 deletion src/electron-main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ app.on("ready", async () => {
const endpoints = await initApi(ctx);
const {checkForUpdatesAndNotify} = await endpoints.readConfig().toPromise();

initWebContentContextMenu(ctx);
initWebContentContextMenu();

const uiContext = ctx.uiContext = {
browserWindow: await initBrowserWindow(ctx, endpoints),
Expand Down
110 changes: 49 additions & 61 deletions src/electron-main/web-content-context-menu.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,54 @@
import {ContextMenuParams, Event, Menu, PopupOptions, WebContents, app, clipboard} from "electron";
import {ContextMenuParams, Event, Menu, MenuItemConstructorOptions, WebContents, app, clipboard} from "electron";
import {platform} from "os";

import {Context} from "./model";

const selectionMenu = Menu.buildFromTemplate([
{role: "copy"},
{type: "separator"},
{role: "selectall"},
]);
const inputMenu = Menu.buildFromTemplate([
{role: "undo"},
{role: "redo"},
{type: "separator"},
{role: "cut"},
{role: "copy"},
{role: "paste"},
{type: "separator"},
{role: "selectall"},
]);

export function initWebContentContextMenu(ctx: Context) {
const contextMenuEventArgs = [
"context-menu",
(e: Event, props: ContextMenuParams) => {
const {selectionText, isEditable, linkURL, linkText} = props;
const popupOptions: PopupOptions = {window: ctx.uiContext && ctx.uiContext.browserWindow};

if (!popupOptions.window) {
return;
}

if (isEditable) {
inputMenu.popup(popupOptions);
return;
}

if (linkURL) {
const linkMenu = Menu.buildFromTemplate([{
label: isEmailHref(linkURL) ? "Copy Email Address" : "Copy Link Address",
click() {
const url = extractEmailIfEmailHref(linkURL);
if (platform() === "darwin") {
clipboard.writeBookmark(linkText, url);
} else {
clipboard.writeText(url);
}
},
}]);
linkMenu.popup(popupOptions);
return;
}

if (selectionText && selectionText.trim()) {
selectionMenu.popup(popupOptions);
}
},
];
const webContentsCreatedHandler = (webContents: WebContents) => {
webContents.removeListener.apply(webContents, contextMenuEventArgs);
webContents.on.apply(webContents, contextMenuEventArgs);
};

const emptyArray = Object.freeze([]);

const contextMenuEventSubscriptionArgs = [
"context-menu",
({sender: webContents}: Event, {editFlags, linkURL, linkText}: ContextMenuParams) => {
const template: MenuItemConstructorOptions[] = [];

if (linkURL) {
template.push({
label: isEmailHref(linkURL) ? "Copy Email Address" : "Copy Link Address",
click() {
if (platform() === "darwin") {
clipboard.writeBookmark(linkText, extractEmailIfEmailHref(linkURL));
} else {
clipboard.writeText(extractEmailIfEmailHref(linkURL));
}
},
});
} else {
template.push(...[
// TODO use "role" based "cut/copy/paste" actions, currently these actions don't work properly
// track the respective issue https://github.com/electron/electron/issues/15219
...(editFlags.canCut ? [{label: "Cut", click: () => webContents.cut()}] : emptyArray),
...(editFlags.canCopy ? [{label: "Copy", click: () => webContents.copy()}] : emptyArray),
...(editFlags.canPaste ? [{label: "Paste", click: () => webContents.paste()}] : emptyArray),
]);

// TODO "selectall" context menu action doesn't work properly (works well only on login page)
// if (editFlags.canSelectAll) {
// if (template.length) {
// template.push(separatorItem);
// }
// template.push({role: "selectall"});
// }
}

if (template.length) {
Menu.buildFromTemplate(template).popup({});
}
},
];

const webContentsCreatedHandler = (webContents: WebContents) => {
webContents.removeListener.apply(webContents, contextMenuEventSubscriptionArgs);
webContents.on.apply(webContents, contextMenuEventSubscriptionArgs);
};

export function initWebContentContextMenu() {
app.on("browser-window-created", (event, {webContents}) => webContentsCreatedHandler(webContents));
app.on("web-contents-created", (webContentsCreatedEvent, webContents) => webContentsCreatedHandler(webContents));
}
Expand Down

0 comments on commit 9491f45

Please sign in to comment.