desktop: refactor to clean up desktop integration logic

This commit is contained in:
Abdullah Atta
2023-01-26 11:12:34 +05:00
committed by Abdullah Atta
parent dde276f77e
commit 43c5c67bfa
4 changed files with 215 additions and 131 deletions

80
apps/web/desktop/cli.ts Normal file
View File

@@ -0,0 +1,80 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
export type CLIOptions = {
note?: boolean | string;
notebook?: boolean | string;
reminder?: boolean | string;
};
export async function parseArguments(): Promise<CLIOptions> {
const result: CLIOptions = {
note: false,
notebook: false,
reminder: false
};
await yargs(hideBin(process.argv))
.command("new", "Create a new item", (yargs) => {
return yargs
.command("note", "Create a new note", {}, () => {
result.note = true;
})
.command("notebook", "Create a new notebook", {}, () => {
result.notebook = true;
})
.command("reminder", "Add a new reminder", {}, () => {
result.reminder = true;
});
})
.command("open", "Open a specific item", (yargs) => {
return yargs
.command(
"note",
"Open a note",
{ id: { string: true, description: "Id of the note" } },
(args) => {
result.note = args.id;
}
)
.command(
"notebook",
"Open a notebook",
{ id: { string: true, description: "Id of the notebook" } },
(args) => {
result.notebook = args.id;
}
)
.command(
"topic",
"Open a topic",
{
id: { string: true, description: "Id of the topic" },
notebookId: { string: true, description: "Id of the notebook" }
},
(args) => {
result.notebook = `${args.notebookId}/${args.id}`;
}
);
})
.parse();
return result;
}

View File

@@ -39,6 +39,9 @@ import { hideBin } from "yargs/helpers";
import { getDesktopIntegration } from "./config/desktopIntegration";
import { AutoLaunch } from "./autolaunch";
import bringToFront from "./ipc/actions/bringToFront";
import { setupJumplist } from "./jumplist";
import { setupTray } from "./tray";
import { parseArguments } from "./cli";
if (!RELEASE) {
require("electron-reloader")(module);
@@ -56,6 +59,8 @@ if (process.platform === "win32") {
var mainWindowState;
async function createWindow() {
const cliOptions = await parseArguments();
mainWindowState = new WindowState({});
setTheme(getTheme());
@@ -96,7 +101,6 @@ async function createWindow() {
setPrivacyMode({ privacyMode: getPrivacyMode() });
}
const cliOptions = await parseCli();
try {
await mainWindow.loadURL(`${createURL(cliOptions)}`);
} catch (e) {
@@ -142,6 +146,9 @@ app.on("activate", () => {
}
});
/**
* @param {import("./cli").CLIOptions} options
*/
function createURL(options) {
const url = new URL(isDevelopment() ? "http://localhost:3000" : PROTOCOL_URL);
@@ -156,136 +163,6 @@ function createURL(options) {
return url;
}
async function parseCli() {
const result = {
note: false,
notebook: false,
reminder: false
};
await yargs(hideBin(process.argv))
.command("new", "Create a new item", (yargs) => {
return yargs
.command("note", "Create a new note", {}, () => (result.note = true))
.command(
"notebook",
"Create a new notebook",
{},
() => (result.notebook = true)
)
.command(
"reminder",
"Add a new reminder",
{},
() => (result.reminder = true)
);
})
.command("open", "Open a specific item", (yargs) => {
return yargs
.command(
"note",
"Open a note",
{ id: { string: true, description: "Id of the note" } },
(args) => (result.note = args.id)
)
.command(
"notebook",
"Open a notebook",
{ id: { string: true, description: "Id of the notebook" } },
(args) => (result.notebook = args.id)
)
.command(
"topic",
"Open a topic",
{
id: { string: true, description: "Id of the topic" },
notebookId: { string: true, description: "Id of the notebook" }
},
(args) => (result.notebook = `${args.notebookId}/${args.id}`)
);
})
.parse();
return result;
}
function setupJumplist() {
if (process.platform === "win32") {
app.setJumpList([
{ type: "frequent" },
{
type: "tasks",
items: [
{
program: process.execPath,
iconPath: process.execPath,
args: "new note",
description: "Create a new note",
title: "New note",
type: "task"
},
{
program: process.execPath,
iconPath: process.execPath,
args: "new notebook",
description: "Create a new notebook",
title: "New notebook",
type: "task"
},
{
program: process.execPath,
iconPath: process.execPath,
args: "new reminder",
description: "Add a new reminder",
title: "New reminder",
type: "task"
}
]
}
]);
}
}
function setupTray() {
const tray = new Tray(APP_ICON_PATH);
const contextMenu = Menu.buildFromTemplate([
{
label: "Show Notesnook",
type: "normal",
icon: APP_ICON_PATH,
click: bringToFront
},
{ type: "separator" },
{
label: "New note",
type: "normal",
click: () => {
bringToFront();
sendMessageToRenderer(EVENTS.createItem, { itemType: "note" });
}
},
{
label: "New notebook",
type: "normal",
click: () => {
bringToFront();
sendMessageToRenderer(EVENTS.createItem, { itemType: "notebook" });
}
},
{ type: "separator" },
{
label: "Quit Notesnook",
type: "normal",
click: () => {
app.exit(0);
}
}
]);
tray.on("double-click", bringToFront);
tray.on("click", bringToFront);
tray.setToolTip("Notesnook");
tray.setContextMenu(contextMenu);
}
function setupDesktopIntegration() {
const desktopIntegration = getDesktopIntegration();

View File

@@ -0,0 +1,61 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { app } from "electron";
export function setupJumplist() {
if (process.platform === "win32") {
windows();
}
}
function windows() {
app.setJumpList([
{ type: "frequent" },
{
type: "tasks",
items: [
{
program: process.execPath,
iconPath: process.execPath,
args: "new note",
description: "Create a new note",
title: "New note",
type: "task"
},
{
program: process.execPath,
iconPath: process.execPath,
args: "new notebook",
description: "Create a new notebook",
title: "New notebook",
type: "task"
},
{
program: process.execPath,
iconPath: process.execPath,
args: "new reminder",
description: "Add a new reminder",
title: "New reminder",
type: "task"
}
]
}
]);
}

66
apps/web/desktop/tray.ts Normal file
View File

@@ -0,0 +1,66 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { app, Menu, Tray } from "electron";
import { EVENTS } from "./events";
import bringToFront from "./ipc/actions/bringToFront";
import { sendMessageToRenderer } from "./ipc/utils";
import { APP_ICON_PATH } from "./utils";
export function setupTray() {
const tray = new Tray(APP_ICON_PATH);
const contextMenu = Menu.buildFromTemplate([
{
label: "Show Notesnook",
type: "normal",
icon: APP_ICON_PATH,
click: bringToFront
},
{ type: "separator" },
{
label: "New note",
type: "normal",
click: () => {
bringToFront();
sendMessageToRenderer(EVENTS.createItem, { itemType: "note" });
}
},
{
label: "New notebook",
type: "normal",
click: () => {
bringToFront();
sendMessageToRenderer(EVENTS.createItem, { itemType: "notebook" });
}
},
{ type: "separator" },
{
label: "Quit Notesnook",
type: "normal",
click: () => {
app.exit(0);
}
}
]);
tray.on("double-click", bringToFront);
tray.on("click", bringToFront);
tray.setToolTip("Notesnook");
tray.setContextMenu(contextMenu);
}