Skip to content

Commit

Permalink
enable basic context menu #11
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Feb 6, 2018
1 parent 41e589f commit a970a2e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/electron/main/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ test.serial("workflow", async (t: TestContext) => {
const makeSingleInstanceSpy: sinon.SinonSpy = mocks.electron.app.makeSingleInstance;
const initBrowserWindowSpy: sinon.SinonSpy = mocks["./window"].initBrowserWindow;
const initTraySpy: sinon.SinonSpy = mocks["./tray"].initTray;
const initWebContentContextMenuSpy: sinon.SinonSpy = mocks["./web-content-context-menu"].initWebContentContextMenu;
const initAutoUpdateSpy: sinon.SinonSpy = mocks["./app-update"].initAutoUpdate;

t.true(electronUnhandledSpy.calledWithExactly(sinon.match.hasOwn("logger")), `"electronUnhandled" called`);
t.true(mocks[`./util`].initContext.calledWithExactly(), `"initContext" called`);
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(initAutoUpdateSpy.calledWithExactly(), `"initAutoUpdate" called`);
});

Expand Down Expand Up @@ -55,6 +57,9 @@ test.beforeEach(async (t: TestContext) => {
"./tray": {
initTray: sinon.spy(),
},
"./web-content-context-menu": {
initWebContentContextMenu: sinon.spy(),
},
"./app-update": {
initAutoUpdate: sinon.spy(),
},
Expand Down Expand Up @@ -88,6 +93,8 @@ test.beforeEach(async (t: TestContext) => {
.with(mocks["./window"]);
mock("./tray")
.with(mocks["./tray"]);
mock("./web-content-context-menu")
.with(mocks["./web-content-context-menu"]);
mock("./app-update")
.with(mocks["./app-update"]);
mock("keytar")
Expand Down
3 changes: 3 additions & 0 deletions src/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {initContext} from "./util";
import {initBrowserWindow} from "./window";
import {initTray} from "./tray";
import {initAutoUpdate} from "./app-update";
import {initWebContentContextMenu} from "./web-content-context-menu";

electronUnhandled({logger: logger.error});

Expand All @@ -34,6 +35,8 @@ export function initApp(ctx: Context) {
tray: await initTray(ctx, endpoints),
};

initWebContentContextMenu(uiContext);

ctx.on("toggleBrowserWindow", (forcedState?: boolean) => {
const needsToBeVisible = typeof forcedState !== "undefined"
? forcedState
Expand Down
48 changes: 48 additions & 0 deletions src/electron/main/web-content-context-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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);
}
});
});
}

0 comments on commit a970a2e

Please sign in to comment.