Skip to content

Commit

Permalink
enable context menu for the browser window #11
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Feb 7, 2018
1 parent a970a2e commit 8430d7e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 94 deletions.
82 changes: 41 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "protonmail-desktop-app",
"description": "Unofficial ProtonMail Desktop App",
"version": "0.3.0",
"version": "0.3.1",
"author": "Vladimir Yakovlev <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/vladimiry/protonmail-desktop-app",
Expand Down Expand Up @@ -80,7 +80,6 @@
"jimp": "0.2.28",
"keepasshttp-client": "2.2.5",
"keytar": "4.1.0",
"otpauth": "3.1.3",
"rxjs": "5.5.6",
"stacktrace-js": "2.0.0",
"valid-url": "1.0.9"
Expand All @@ -105,7 +104,7 @@
"@ngtools/webpack": "1.9.7",
"@types/keytar": "4.0.1",
"@types/mkdirp": "0.5.2",
"@types/node": "9.4.0",
"@types/node": "9.4.1",
"@types/randomstring": "1.1.6",
"@types/sinon": "4.1.3",
"@types/stacktrace-js": "0.0.32",
Expand All @@ -127,7 +126,7 @@
"css-loader": "0.28.9",
"cssnano": "3.10.0",
"devtron": "1.4.0",
"electron": "1.8.2-beta.5",
"electron": "1.8.2",
"electron-builder": "19.56.0",
"electron-builder-squirrel-windows": "20.0.1",
"electron-rebuild": "1.7.3",
Expand All @@ -145,6 +144,7 @@
"ngx-bootstrap": "2.0.2",
"node-sass": "4.7.2",
"npm-run-all": "4.1.2",
"otpauth": "3.1.3",
"postcss-custom-properties": "6.2.0",
"postcss-loader": "2.1.0",
"postcss-url": "7.3.0",
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 @@ -27,7 +27,7 @@ test.serial("workflow", async (t: TestContext) => {
t.true(makeSingleInstanceSpy.calledWithExactly(t.context.mocked.index.activateBrowserWindow), `"makeSingleInstance" called`);
t.true(initBrowserWindowSpy.calledWithExactly(t.context.ctx), `"initBrowserWindow" called`);
t.true(initTraySpy.calledWithExactly(t.context.ctx, t.context.endpoints), `"initTray" called`);
t.true(initWebContentContextMenuSpy.calledWithExactly(t.context.ctx.uiContext), `"initWebContentContextMenu" called`);
t.true(initWebContentContextMenuSpy.calledWithExactly(t.context.ctx), `"initWebContentContextMenu" called`);
t.true(initAutoUpdateSpy.calledWithExactly(), `"initAutoUpdate" called`);
});

Expand Down
4 changes: 2 additions & 2 deletions src/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export function initApp(ctx: Context) {

await readConfigApi.process(undefined);

initWebContentContextMenu(ctx);

uiContext = ctx.uiContext = {
browserWindow: await initBrowserWindow(ctx),
tray: await initTray(ctx, endpoints),
};

initWebContentContextMenu(uiContext);

ctx.on("toggleBrowserWindow", (forcedState?: boolean) => {
const needsToBeVisible = typeof forcedState !== "undefined"
? forcedState
Expand Down
102 changes: 56 additions & 46 deletions src/electron/main/web-content-context-menu.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
import {app, clipboard, Menu} from "electron";

import {UIContext} from "./model";

export function initWebContentContextMenu(uiContext: UIContext) {
app.on("web-contents-created", (webContentsCreatedEvent, contents) => {
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"},
]);

contents.on("context-menu", (e, props) => {
const {selectionText, isEditable, linkURL} = props;

if (isEditable) {
inputMenu.popup(uiContext.browserWindow);
return;
}

if (linkURL) {
Menu
.buildFromTemplate([{
label: "Copy Link Address",
click() {
clipboard.writeText(linkURL);
},
}])
.popup(uiContext.browserWindow);
return;
}

if (selectionText && selectionText.trim()) {
selectionMenu.popup(uiContext.browserWindow);
}
});
import {app, clipboard, ContextMenuParams, Event, Menu} from "electron";

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 contextMenuEvenHandler = (e: Event, props: ContextMenuParams) => {
const {selectionText, isEditable, linkURL} = props;
const browserWindow = ctx.uiContext && ctx.uiContext.browserWindow;

if (!browserWindow) {
return;
}

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

if (linkURL) {
Menu
.buildFromTemplate([{
label: "Copy Link Address",
click() {
clipboard.writeText(linkURL);
},
}])
.popup(browserWindow);
return;
}

if (selectionText && selectionText.trim()) {
selectionMenu.popup(browserWindow);
}
};

app.on("browser-window-created", (event, {webContents}) => {
webContents.on("context-menu", contextMenuEvenHandler);
});
app.on("web-contents-created", (webContentsCreatedEvent, webContents) => {
webContents.on("context-menu", contextMenuEvenHandler);
});
}

0 comments on commit 8430d7e

Please sign in to comment.