@niyamulahsan/create-deskapp 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/bin/create.js +1 -1
- package/package.json +1 -1
- package/template/src/config/winsetup.ts +91 -0
- package/template/src/core/facade.ts +3 -1
- package/template/src/main.ts +10 -6
- package/template/src/window/app-menu.ts +81 -0
- package/template/src/window/config.ts +10 -69
- package/template/src/window/context-menu.ts +14 -0
- package/template/src/window/dialogs.ts +23 -0
- package/template/src/window/manager.ts +5 -5
- package/template/src/window/native.ts +40 -215
- package/template/src/window/tray.ts +79 -0
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://github.com/niyamulahsan/deskapp">
|
|
3
|
+
<img alt="deskapp" src="https://raw.githubusercontent.com/niyamulahsan/deskapp/main/logo-favicon/deskapp.png" width="300">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
1
7
|
<p align="center">
|
|
2
8
|
<a href="https://niyamulahsan.github.io/deskapp"><img src="https://img.shields.io/badge/docs%20%7C%20deskapp-3b8eed" alt="Documentation"></a>
|
|
3
9
|
<a href="https://www.npmjs.com/package/@niyamulahsan/create-deskapp"><img src="https://img.shields.io/npm/v/@niyamulahsan/create-deskapp" alt="npm version"></a>
|
package/bin/create.js
CHANGED
|
@@ -54,7 +54,7 @@ async function main() {
|
|
|
54
54
|
console.log(`create-deskapp v${pkg.version}`);
|
|
55
55
|
console.log();
|
|
56
56
|
console.log("Usage:");
|
|
57
|
-
console.log(" npm create deskapp@latest <project-name|.> [--force]");
|
|
57
|
+
console.log(" npm create @niyamulahsan/deskapp@latest <project-name|.> [--force]");
|
|
58
58
|
console.log(" npx create-deskapp <project-name|.> [--force]");
|
|
59
59
|
console.log();
|
|
60
60
|
console.log("Options:");
|
package/package.json
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Window setup/config — edit this file to configure app windows (0 hassle).
|
|
3
|
+
* Each entry in `windows` becomes a native BrowserWindow at boot.
|
|
4
|
+
* The first entry ("main") is the primary window.
|
|
5
|
+
*
|
|
6
|
+
* For runtime windows (opened on demand), pass a `WindowPreset` to `win.openWindow()`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { config } from "@/core/config";
|
|
10
|
+
|
|
11
|
+
export type DevToolsMode = "bottom" | "right" | "left" | "undocked" | "detach";
|
|
12
|
+
|
|
13
|
+
export const windowDev = {
|
|
14
|
+
/**
|
|
15
|
+
* Auto-open DevTools in development only (never in production/packaged).
|
|
16
|
+
* Set mode: "bottom" | "right" | "left" | "undocked" | "detach", or "none" to disable.
|
|
17
|
+
*/
|
|
18
|
+
devTools: "bottom" as DevToolsMode | "none"
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface ChromeParts {
|
|
22
|
+
/** Application/window menu. */
|
|
23
|
+
appMenu?: boolean;
|
|
24
|
+
/** Right-click context menu. */
|
|
25
|
+
contextMenu?: boolean;
|
|
26
|
+
/** System tray icon/menu (windowed). */
|
|
27
|
+
tray?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface WindowPreset {
|
|
31
|
+
/** Window title. Falls back to the app name if unset. */
|
|
32
|
+
title: string;
|
|
33
|
+
/** Initial width in logical pixels. */
|
|
34
|
+
width: number;
|
|
35
|
+
/** Initial height in logical pixels. */
|
|
36
|
+
height: number;
|
|
37
|
+
/** Initial horizontal position; centered if omitted. */
|
|
38
|
+
x?: number;
|
|
39
|
+
/** Initial vertical position; centered if omitted. */
|
|
40
|
+
y?: number;
|
|
41
|
+
/** Whether the user can resize the window. */
|
|
42
|
+
resizable?: boolean;
|
|
43
|
+
/** Keep the window above others. */
|
|
44
|
+
alwaysOnTop?: boolean;
|
|
45
|
+
/** Remove the title bar and window chrome (creation-only). */
|
|
46
|
+
frameless?: boolean;
|
|
47
|
+
/** Floating, non-activating panel that doesn't steal focus (creation-only). */
|
|
48
|
+
noActivate?: boolean;
|
|
49
|
+
/** Blend the title bar into the content (creation-only). */
|
|
50
|
+
transparentTitlebar?: boolean;
|
|
51
|
+
/** In-app path the window loads on startup (frontend route). */
|
|
52
|
+
route?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Desktop chrome for the FIRST (main) boot window only.
|
|
55
|
+
* - `true` / omitted: app menu + context menu + tray (default)
|
|
56
|
+
* - `false`: none
|
|
57
|
+
* - object: granular control, e.g. `{ appMenu: true, contextMenu: true, tray: false }`
|
|
58
|
+
* (omitted parts default to `false` when an object is provided)
|
|
59
|
+
*/
|
|
60
|
+
chrome?: boolean | ChromeParts;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Normalize chrome config to explicit part flags. */
|
|
64
|
+
export function resolveChrome(chrome: WindowPreset["chrome"]): Required<ChromeParts> {
|
|
65
|
+
if (chrome === false) return { appMenu: false, contextMenu: false, tray: false };
|
|
66
|
+
if (chrome === true || chrome === undefined) return { appMenu: true, contextMenu: true, tray: true };
|
|
67
|
+
return {
|
|
68
|
+
appMenu: !!chrome.appMenu,
|
|
69
|
+
contextMenu: !!chrome.contextMenu,
|
|
70
|
+
tray: !!chrome.tray,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Named window presets. Only these open at boot. The "main" entry is always
|
|
76
|
+
* created first; every extra entry also opens as a fresh window at startup.
|
|
77
|
+
*/
|
|
78
|
+
export const windows: Record<string, WindowPreset> = {
|
|
79
|
+
main: {
|
|
80
|
+
title: config.appName,
|
|
81
|
+
width: 900,
|
|
82
|
+
height: 600,
|
|
83
|
+
resizable: true,
|
|
84
|
+
alwaysOnTop: false,
|
|
85
|
+
route: "/",
|
|
86
|
+
chrome: true
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** The main (first-created) window preset. */
|
|
91
|
+
export const windowConfig: WindowPreset = windows.main;
|
|
@@ -35,8 +35,10 @@ export { type CronHandle, cron } from "@/core/utils/scheduler";
|
|
|
35
35
|
export { type Disk, type FileData, files, storage } from "@/core/utils/storage";
|
|
36
36
|
// Validation (zod schemas + runner: `validate.z`, `validate.run`)
|
|
37
37
|
export { validate } from "@/core/utils/validation";
|
|
38
|
-
// Native Windows file/folder pickers (Explorer-style Open / Save As / folder)
|
|
38
|
+
// Native Windows file/folder pickers and message dialogs (Explorer-style Open / Save As / folder, confirm/alert)
|
|
39
39
|
export {
|
|
40
|
+
alertDialog,
|
|
41
|
+
confirmDialog,
|
|
40
42
|
type PickFolderOptions,
|
|
41
43
|
type PickOpenOptions,
|
|
42
44
|
type PickSaveOptions,
|
package/template/src/main.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { windowDev, windows } from "@config/winsetup";
|
|
3
4
|
import { app, BrowserWindow, dialog } from "electron";
|
|
4
5
|
import { bindAll } from "@/core/api/registry";
|
|
5
6
|
import { config } from "@/core/config";
|
|
6
7
|
import { loadEnv } from "@/core/env";
|
|
7
8
|
import { chrome, cron, db, win } from "@/core/facade";
|
|
8
9
|
import { storage } from "@/core/utils/storage";
|
|
9
|
-
import { windows } from "@/window/config";
|
|
10
10
|
|
|
11
11
|
function logFatal(err: unknown): void {
|
|
12
12
|
const msg = err instanceof Error ? `${err.stack || err.message}` : String(err);
|
|
@@ -74,20 +74,24 @@ if (!gotLock) {
|
|
|
74
74
|
if (!created.isDestroyed()) created.show();
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
if (isMain
|
|
78
|
-
chrome.
|
|
77
|
+
if (isMain) {
|
|
78
|
+
const chromeParts = chrome.resolveChrome(preset.chrome);
|
|
79
|
+
if (chromeParts.appMenu || chromeParts.contextMenu || chromeParts.tray) {
|
|
80
|
+
chrome.setupDesktopChrome(created, chromeParts);
|
|
81
|
+
}
|
|
79
82
|
}
|
|
80
83
|
isMain = false;
|
|
81
84
|
|
|
82
85
|
if (devUrl) {
|
|
83
86
|
const route = preset.route || "/";
|
|
84
87
|
await created.loadURL(`${devUrl}${route}`);
|
|
85
|
-
if (isDev) {
|
|
86
|
-
created.webContents.openDevTools({ mode: "bottom" });
|
|
87
|
-
}
|
|
88
88
|
} else {
|
|
89
89
|
await created.loadFile(getRendererIndexPath());
|
|
90
90
|
}
|
|
91
|
+
|
|
92
|
+
if (isDev && windowDev.devTools !== "none") {
|
|
93
|
+
created.webContents.openDevTools({ mode: windowDev.devTools });
|
|
94
|
+
}
|
|
91
95
|
}
|
|
92
96
|
} else {
|
|
93
97
|
// Headless mode: No windows, just a tray icon.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Menu } from "electron";
|
|
2
|
+
import { type DesktopWindow, win as winApi } from "@/window/manager";
|
|
3
|
+
|
|
4
|
+
export interface MenuRole {
|
|
5
|
+
role: string;
|
|
6
|
+
}
|
|
7
|
+
export interface MenuActionItem {
|
|
8
|
+
item: { label: string; id?: string; accelerator?: string; enabled?: boolean };
|
|
9
|
+
}
|
|
10
|
+
export interface MenuSubmenu {
|
|
11
|
+
submenu: { label: string; items: MenuItem[] };
|
|
12
|
+
}
|
|
13
|
+
export type MenuItem = MenuActionItem | MenuSubmenu | "separator" | { role: MenuRole };
|
|
14
|
+
|
|
15
|
+
// Map our custom MenuItem types to Electron's MenuItemConstructorOptions
|
|
16
|
+
export function toElectronMenu(items: MenuItem[]): Electron.MenuItemConstructorOptions[] {
|
|
17
|
+
return items.map((item) => {
|
|
18
|
+
if (item === "separator") return { type: "separator" };
|
|
19
|
+
if ("role" in item) return { role: item.role.role as any };
|
|
20
|
+
if ("item" in item) {
|
|
21
|
+
return {
|
|
22
|
+
label: item.item.label,
|
|
23
|
+
id: item.item.id,
|
|
24
|
+
accelerator: item.item.accelerator,
|
|
25
|
+
enabled: item.item.enabled,
|
|
26
|
+
click: () => {
|
|
27
|
+
// Forward clicks to the window's custom event if needed,
|
|
28
|
+
// or handle globally
|
|
29
|
+
const mainWin = winApi.getWindow();
|
|
30
|
+
if (mainWin && item.item.id) {
|
|
31
|
+
// Emulate Deno's event by executing custom JS or sending IPC
|
|
32
|
+
mainWin.webContents.send("menu-click", item.item.id);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if ("submenu" in item) {
|
|
38
|
+
return {
|
|
39
|
+
label: item.submenu.label,
|
|
40
|
+
submenu: toElectronMenu(item.submenu.items)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {};
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function defaultMenuItems(): MenuItem[] {
|
|
48
|
+
return [
|
|
49
|
+
{
|
|
50
|
+
submenu: {
|
|
51
|
+
label: "File",
|
|
52
|
+
items: [
|
|
53
|
+
{ item: { label: "New", id: "new", accelerator: "CmdOrCtrl+N", enabled: true } },
|
|
54
|
+
{ item: { label: "Open Settings", id: "openSettings", accelerator: "CmdOrCtrl+,", enabled: true } },
|
|
55
|
+
"separator",
|
|
56
|
+
{ role: { role: "quit" } }
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
submenu: {
|
|
62
|
+
label: "Edit",
|
|
63
|
+
items: [
|
|
64
|
+
{ role: { role: "undo" } },
|
|
65
|
+
{ role: { role: "redo" } },
|
|
66
|
+
"separator",
|
|
67
|
+
{ role: { role: "cut" } },
|
|
68
|
+
{ role: { role: "copy" } },
|
|
69
|
+
{ role: { role: "paste" } }
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function installAppMenu(win: DesktopWindow): void {
|
|
77
|
+
// Set menu for the main window (also sets application menu on Windows/Linux).
|
|
78
|
+
const menu = Menu.buildFromTemplate(toElectronMenu(defaultMenuItems()));
|
|
79
|
+
win.setMenu(menu);
|
|
80
|
+
Menu.setApplicationMenu(menu);
|
|
81
|
+
}
|
|
@@ -1,71 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Tune windows here instead of touching src/main.ts. Each entry in `windows`
|
|
5
|
-
* becomes a native BrowserWindow. The first entry is the main window.
|
|
6
|
-
*
|
|
7
|
-
* `route` is the in-app path each window loads on startup (frontend router).
|
|
8
|
-
* Omit it to stay on "/".
|
|
2
|
+
* Backward-compat re-export. Window base setup lives in src/config/winsetup.ts.
|
|
9
3
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
height: number;
|
|
20
|
-
/** Initial horizontal position; centered if omitted. */
|
|
21
|
-
x?: number;
|
|
22
|
-
/** Initial vertical position; centered if omitted. */
|
|
23
|
-
y?: number;
|
|
24
|
-
/** Whether the user can resize the window. */
|
|
25
|
-
resizable?: boolean;
|
|
26
|
-
/** Keep the window above others. */
|
|
27
|
-
alwaysOnTop?: boolean;
|
|
28
|
-
/** Remove the title bar and window chrome (creation-only). */
|
|
29
|
-
frameless?: boolean;
|
|
30
|
-
/** Floating, non-activating panel that doesn't steal focus (creation-only). */
|
|
31
|
-
noActivate?: boolean;
|
|
32
|
-
/** Blend the title bar into the content (creation-only). */
|
|
33
|
-
transparentTitlebar?: boolean;
|
|
34
|
-
/** In-app path the window loads on startup (frontend route). */
|
|
35
|
-
route?: string;
|
|
36
|
-
/**
|
|
37
|
-
* Install the default desktop chrome (app menu, context menu, tray) at
|
|
38
|
-
* startup. Only honoured for the FIRST (main) window — openWindow() windows
|
|
39
|
-
* never get it. Omit (or set `true`) to keep it, `false` to disable.
|
|
40
|
-
*/
|
|
41
|
-
chrome?: boolean;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Named window presets. Only these open at boot. The "main" entry is always
|
|
46
|
-
* created first (it adopts the shutdown window); every extra entry also opens
|
|
47
|
-
* as a fresh window at startup.
|
|
48
|
-
*
|
|
49
|
-
* Windows for runtime use (opened on a button click, a menu item, etc.) are
|
|
50
|
-
* NOT declared here — pass a `WindowPreset` straight to `win.openWindow()`:
|
|
51
|
-
*
|
|
52
|
-
* import { win } from "@/core/facade.ts";
|
|
53
|
-
* await win.openWindow({ title: "Help", width: 760, height: 600, route: "/help" });
|
|
54
|
-
*
|
|
55
|
-
* Every such window is individual: its own close (×) button closes only that
|
|
56
|
-
* window. The app exits when the last window is closed.
|
|
57
|
-
*/
|
|
58
|
-
export const windows: Record<string, WindowPreset> = {
|
|
59
|
-
main: {
|
|
60
|
-
title: config.appName,
|
|
61
|
-
width: 900,
|
|
62
|
-
height: 600,
|
|
63
|
-
resizable: true,
|
|
64
|
-
alwaysOnTop: false,
|
|
65
|
-
route: "/",
|
|
66
|
-
chrome: true
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/** The main (first-created) window preset. */
|
|
71
|
-
export const windowConfig: WindowPreset = windows.main;
|
|
4
|
+
export {
|
|
5
|
+
type ChromeParts,
|
|
6
|
+
type DevToolsMode,
|
|
7
|
+
resolveChrome,
|
|
8
|
+
type WindowPreset,
|
|
9
|
+
windowConfig,
|
|
10
|
+
windowDev,
|
|
11
|
+
windows
|
|
12
|
+
} from "@config/winsetup";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Menu } from "electron";
|
|
2
|
+
import type { DesktopWindow } from "@/window/manager";
|
|
3
|
+
|
|
4
|
+
export function installContextMenu(win: DesktopWindow): void {
|
|
5
|
+
win.webContents.on("context-menu", (_event, _params) => {
|
|
6
|
+
const menu = Menu.buildFromTemplate([
|
|
7
|
+
{ role: "copy" },
|
|
8
|
+
{ role: "paste" },
|
|
9
|
+
{ type: "separator" },
|
|
10
|
+
{ label: "Reload", click: () => win.reload() }
|
|
11
|
+
]);
|
|
12
|
+
menu.popup({ window: win });
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import { BrowserWindow, dialog } from "electron";
|
|
2
2
|
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// Message dialogs (confirm / alert / prompt)
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
export async function confirmDialog(message: string): Promise<boolean> {
|
|
8
|
+
const result = await dialog.showMessageBox({
|
|
9
|
+
type: "question",
|
|
10
|
+
buttons: ["Cancel", "OK"],
|
|
11
|
+
title: "Confirm",
|
|
12
|
+
message: message
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return result.response === 1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function alertDialog(message: string): Promise<void> {
|
|
19
|
+
await dialog.showMessageBox({
|
|
20
|
+
type: "info",
|
|
21
|
+
title: "Alert",
|
|
22
|
+
message: message
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
3
26
|
export interface PickOpenOptions {
|
|
4
27
|
title?: string;
|
|
5
28
|
filter?: string; // Electron uses extensions array, but we can parse your string
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import { app, BrowserWindow } from "electron";
|
|
3
|
-
import { type WindowPreset, windowConfig, windows as windowPresets } from "@/window/config";
|
|
4
3
|
import { closeDatabase } from "@/core/database/connection";
|
|
4
|
+
import { type WindowPreset, windowConfig, windows as windowPresets } from "@/window/config";
|
|
5
5
|
|
|
6
6
|
// We map your custom options to Electron's expected options
|
|
7
7
|
export interface BrowserWindowOptions extends WindowPreset {}
|
|
@@ -52,8 +52,8 @@ function createWindow(options?: BrowserWindowOptions, name?: string, preloadPath
|
|
|
52
52
|
preload: resolvedPreload,
|
|
53
53
|
contextIsolation: true,
|
|
54
54
|
nodeIntegration: false,
|
|
55
|
-
sandbox: false
|
|
56
|
-
}
|
|
55
|
+
sandbox: false
|
|
56
|
+
}
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
const winId = win.id;
|
|
@@ -135,5 +135,5 @@ export const win = {
|
|
|
135
135
|
getWindow,
|
|
136
136
|
getWindowCount,
|
|
137
137
|
openWindow,
|
|
138
|
-
watchFrontend
|
|
139
|
-
};
|
|
138
|
+
watchFrontend
|
|
139
|
+
};
|
|
@@ -1,215 +1,40 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
label: item.submenu.label,
|
|
43
|
-
submenu: toElectronMenu(item.submenu.items)
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
return {};
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let tray: Tray | null = null;
|
|
51
|
-
|
|
52
|
-
function getTrayIconPath(): string {
|
|
53
|
-
// Bundle-safe: resolve from the app root (works in dev and packaged builds).
|
|
54
|
-
return path.join(app.getAppPath(), "src", "icons", "tray.png");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function createTray(): Tray | undefined {
|
|
58
|
-
try {
|
|
59
|
-
const iconPath = getTrayIconPath();
|
|
60
|
-
const icon = nativeImage.createFromPath(iconPath);
|
|
61
|
-
if (icon.isEmpty()) return undefined;
|
|
62
|
-
tray = new Tray(icon);
|
|
63
|
-
return tray;
|
|
64
|
-
} catch {
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function isTrayAvailable(): boolean {
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function confirmDialog(message: string): Promise<boolean> {
|
|
74
|
-
const result = await dialog.showMessageBox({
|
|
75
|
-
type: "question",
|
|
76
|
-
buttons: ["Cancel", "OK"],
|
|
77
|
-
title: "Confirm",
|
|
78
|
-
message: message
|
|
79
|
-
});
|
|
80
|
-
return result.response === 1;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function alertDialog(message: string): Promise<void> {
|
|
84
|
-
await dialog.showMessageBox({
|
|
85
|
-
type: "info",
|
|
86
|
-
title: "Alert",
|
|
87
|
-
message: message
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async function promptDialog(message: string, defaultValue?: string): Promise<string | null> {
|
|
92
|
-
// Electron doesn't have a native prompt dialog, so we'd need a custom HTML modal.
|
|
93
|
-
// For now, we fallback to confirm as a stub.
|
|
94
|
-
console.warn("promptDialog not natively supported in Electron");
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function defaultMenuItems(): MenuItem[] {
|
|
99
|
-
return [
|
|
100
|
-
{
|
|
101
|
-
submenu: {
|
|
102
|
-
label: "File",
|
|
103
|
-
items: [
|
|
104
|
-
{ item: { label: "New", id: "new", accelerator: "CmdOrCtrl+N", enabled: true } },
|
|
105
|
-
{ item: { label: "Open Settings", id: "openSettings", accelerator: "CmdOrCtrl+,", enabled: true } },
|
|
106
|
-
"separator",
|
|
107
|
-
{ role: { role: "quit" } }
|
|
108
|
-
]
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
submenu: {
|
|
113
|
-
label: "Edit",
|
|
114
|
-
items: [
|
|
115
|
-
{ role: { role: "undo" } },
|
|
116
|
-
{ role: { role: "redo" } },
|
|
117
|
-
"separator",
|
|
118
|
-
{ role: { role: "cut" } },
|
|
119
|
-
{ role: { role: "copy" } },
|
|
120
|
-
{ role: { role: "paste" } }
|
|
121
|
-
]
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function installAppMenu(win: DesktopWindow): void {
|
|
128
|
-
// Set menu for the main window (also sets application menu on Windows/Linux).
|
|
129
|
-
const menu = Menu.buildFromTemplate(toElectronMenu(defaultMenuItems()));
|
|
130
|
-
win.setMenu(menu);
|
|
131
|
-
Menu.setApplicationMenu(menu);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function createAppTray(): Tray | undefined {
|
|
135
|
-
const t = createTray();
|
|
136
|
-
if (!t) return undefined;
|
|
137
|
-
|
|
138
|
-
t.setToolTip(config.appName);
|
|
139
|
-
|
|
140
|
-
const contextMenu = Menu.buildFromTemplate([
|
|
141
|
-
{
|
|
142
|
-
label: "Show window",
|
|
143
|
-
click: () => {
|
|
144
|
-
winApi.getWindow()?.show();
|
|
145
|
-
winApi.getWindow()?.focus();
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
{
|
|
149
|
-
label: "Open Settings",
|
|
150
|
-
click: () => {
|
|
151
|
-
void winApi.openWindow({ title: "Settings", width: 420, height: 320 });
|
|
152
|
-
}
|
|
153
|
-
},
|
|
154
|
-
{ type: "separator" },
|
|
155
|
-
{ label: "Quit", click: () => app.quit() }
|
|
156
|
-
]);
|
|
157
|
-
|
|
158
|
-
t.setContextMenu(contextMenu);
|
|
159
|
-
return t;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function installContextMenu(win: DesktopWindow): void {
|
|
163
|
-
win.webContents.on("context-menu", (event, params) => {
|
|
164
|
-
const menu = Menu.buildFromTemplate([
|
|
165
|
-
{ role: "copy" },
|
|
166
|
-
{ role: "paste" },
|
|
167
|
-
{ type: "separator" },
|
|
168
|
-
{ label: "Reload", click: () => win.reload() }
|
|
169
|
-
]);
|
|
170
|
-
menu.popup({ window: win });
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function setupDesktopChrome(win: DesktopWindow): void {
|
|
175
|
-
installAppMenu(win);
|
|
176
|
-
installContextMenu(win);
|
|
177
|
-
createAppTray();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function createHeadlessTray(): Tray | undefined {
|
|
181
|
-
const t = createTray();
|
|
182
|
-
if (!t) return undefined;
|
|
183
|
-
|
|
184
|
-
t.setToolTip(`${config.appName} (headless)`);
|
|
185
|
-
t.setContextMenu(
|
|
186
|
-
Menu.buildFromTemplate([
|
|
187
|
-
{ label: `${config.appName} is running - no UI`, enabled: false },
|
|
188
|
-
{ type: "separator" },
|
|
189
|
-
{ label: "Quit", click: () => app.quit() }
|
|
190
|
-
])
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
return t;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function hideDock(): void {
|
|
197
|
-
if (process.platform === "darwin" && app.dock) {
|
|
198
|
-
app.dock.hide();
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export const chrome = {
|
|
203
|
-
createTray,
|
|
204
|
-
isTrayAvailable,
|
|
205
|
-
confirmDialog,
|
|
206
|
-
alertDialog,
|
|
207
|
-
promptDialog,
|
|
208
|
-
defaultMenuItems,
|
|
209
|
-
installAppMenu,
|
|
210
|
-
createAppTray,
|
|
211
|
-
installContextMenu,
|
|
212
|
-
setupDesktopChrome,
|
|
213
|
-
createHeadlessTray,
|
|
214
|
-
hideDock
|
|
215
|
-
};
|
|
1
|
+
import type { ChromeParts } from "@config/winsetup";
|
|
2
|
+
import { resolveChrome } from "@config/winsetup";
|
|
3
|
+
import { defaultMenuItems, installAppMenu } from "./app-menu";
|
|
4
|
+
import { installContextMenu } from "./context-menu";
|
|
5
|
+
import { alertDialog, confirmDialog } from "./dialogs";
|
|
6
|
+
import type { DesktopWindow } from "./manager";
|
|
7
|
+
import { createAppTray, createHeadlessTray, createTray, hideDock, isTrayAvailable } from "./tray";
|
|
8
|
+
|
|
9
|
+
export type { MenuActionItem, MenuItem, MenuRole, MenuSubmenu } from "@/window/app-menu";
|
|
10
|
+
export type { DesktopTray } from "@/window/tray";
|
|
11
|
+
export type { ChromeParts };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Install desktop chrome for a window. Pass `parts` for granular control;
|
|
15
|
+
* defaults to all enabled (backward compatible).
|
|
16
|
+
*/
|
|
17
|
+
function setupDesktopChrome(win: DesktopWindow, parts: Required<ChromeParts> = { appMenu: true, contextMenu: true, tray: true }): void {
|
|
18
|
+
if (parts.appMenu) installAppMenu(win);
|
|
19
|
+
if (parts.contextMenu) installContextMenu(win);
|
|
20
|
+
if (parts.tray) createAppTray();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const chrome = {
|
|
24
|
+
// Tray
|
|
25
|
+
createTray,
|
|
26
|
+
isTrayAvailable,
|
|
27
|
+
createAppTray,
|
|
28
|
+
createHeadlessTray,
|
|
29
|
+
hideDock,
|
|
30
|
+
// Dialogs
|
|
31
|
+
confirmDialog,
|
|
32
|
+
alertDialog,
|
|
33
|
+
// Menu
|
|
34
|
+
defaultMenuItems,
|
|
35
|
+
installAppMenu,
|
|
36
|
+
installContextMenu,
|
|
37
|
+
// Orchestration
|
|
38
|
+
setupDesktopChrome,
|
|
39
|
+
resolveChrome
|
|
40
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { app, Menu, nativeImage, Tray } from "electron";
|
|
3
|
+
import { config } from "@/core/config";
|
|
4
|
+
import { win as winApi } from "@/window/manager";
|
|
5
|
+
|
|
6
|
+
export interface DesktopTray extends Tray {}
|
|
7
|
+
|
|
8
|
+
let tray: Tray | null = null;
|
|
9
|
+
|
|
10
|
+
function getTrayIconPath(): string {
|
|
11
|
+
// Bundle-safe: resolve from the app root (works in dev and packaged builds).
|
|
12
|
+
return path.join(app.getAppPath(), "src", "icons", "tray.png");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createTray(): Tray | undefined {
|
|
16
|
+
try {
|
|
17
|
+
const iconPath = getTrayIconPath();
|
|
18
|
+
const icon = nativeImage.createFromPath(iconPath);
|
|
19
|
+
if (icon.isEmpty()) return undefined;
|
|
20
|
+
tray = new Tray(icon);
|
|
21
|
+
return tray;
|
|
22
|
+
} catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isTrayAvailable(): boolean {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createAppTray(): Tray | undefined {
|
|
32
|
+
const t = createTray();
|
|
33
|
+
if (!t) return undefined;
|
|
34
|
+
|
|
35
|
+
t.setToolTip(config.appName);
|
|
36
|
+
|
|
37
|
+
const contextMenu = Menu.buildFromTemplate([
|
|
38
|
+
{
|
|
39
|
+
label: "Show window",
|
|
40
|
+
click: () => {
|
|
41
|
+
winApi.getWindow()?.show();
|
|
42
|
+
winApi.getWindow()?.focus();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
label: "Open Settings",
|
|
47
|
+
click: () => {
|
|
48
|
+
void winApi.openWindow({ title: "Settings", width: 420, height: 320 });
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{ type: "separator" },
|
|
52
|
+
{ label: "Quit", click: () => app.quit() }
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
t.setContextMenu(contextMenu);
|
|
56
|
+
return t;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function createHeadlessTray(): Tray | undefined {
|
|
60
|
+
const t = createTray();
|
|
61
|
+
if (!t) return undefined;
|
|
62
|
+
|
|
63
|
+
t.setToolTip(`${config.appName} (headless)`);
|
|
64
|
+
t.setContextMenu(
|
|
65
|
+
Menu.buildFromTemplate([
|
|
66
|
+
{ label: `${config.appName} is running - no UI`, enabled: false },
|
|
67
|
+
{ type: "separator" },
|
|
68
|
+
{ label: "Quit", click: () => app.quit() }
|
|
69
|
+
])
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return t;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function hideDock(): void {
|
|
76
|
+
if (process.platform === "darwin" && app.dock) {
|
|
77
|
+
app.dock.hide();
|
|
78
|
+
}
|
|
79
|
+
}
|