@niyamulahsan/create-deskapp 1.2.3 → 1.2.4
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/package.json +1 -1
- package/template/package.json +1 -2
- package/template/src/config/winsetup.ts +1 -1
- package/template/src/main.ts +1 -3
- package/template/src/window/app-menu.ts +6 -0
- package/template/src/window/context-menu.ts +7 -0
- package/template/src/window/manager.ts +31 -3
- package/template/src/window/native.ts +8 -3
- package/template/src/window/tray.ts +64 -13
package/package.json
CHANGED
package/template/package.json
CHANGED
package/template/src/main.ts
CHANGED
|
@@ -76,9 +76,7 @@ if (!gotLock) {
|
|
|
76
76
|
|
|
77
77
|
if (isMain) {
|
|
78
78
|
const chromeParts = chrome.resolveChrome(preset.chrome);
|
|
79
|
-
|
|
80
|
-
chrome.setupDesktopChrome(created, chromeParts);
|
|
81
|
-
}
|
|
79
|
+
chrome.setupDesktopChrome(created, chromeParts);
|
|
82
80
|
}
|
|
83
81
|
isMain = false;
|
|
84
82
|
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Menu } from "electron";
|
|
2
2
|
import { type DesktopWindow, win as winApi } from "@/window/manager";
|
|
3
3
|
|
|
4
|
+
export function removeAppMenu(win: DesktopWindow): void {
|
|
5
|
+
win.setMenu(null);
|
|
6
|
+
// On Windows/Linux this controls the application menu bar.
|
|
7
|
+
Menu.setApplicationMenu(null);
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
export interface MenuRole {
|
|
5
11
|
role: string;
|
|
6
12
|
}
|
|
@@ -12,3 +12,10 @@ export function installContextMenu(win: DesktopWindow): void {
|
|
|
12
12
|
menu.popup({ window: win });
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
|
+
|
|
16
|
+
export function disableContextMenu(win: DesktopWindow): void {
|
|
17
|
+
// Suppress default/custom context menu when explicitly disabled.
|
|
18
|
+
win.webContents.on("context-menu", (event) => {
|
|
19
|
+
event.preventDefault();
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -1,8 +1,34 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import { app, BrowserWindow } from "electron";
|
|
2
|
+
import { app, BrowserWindow, nativeImage } from "electron";
|
|
3
3
|
import { closeDatabase } from "@/core/database/connection";
|
|
4
4
|
import { type WindowPreset, windowConfig, windows as windowPresets } from "@/window/config";
|
|
5
5
|
|
|
6
|
+
function resolveWindowIcon(): Electron.NativeImage | string | undefined {
|
|
7
|
+
const bases = app.isPackaged
|
|
8
|
+
? [app.getAppPath()]
|
|
9
|
+
: [path.resolve(app.getAppPath(), "..", ".."), app.getAppPath(), process.cwd()];
|
|
10
|
+
|
|
11
|
+
const candidates: string[] = [];
|
|
12
|
+
for (const root of Array.from(new Set(bases))) {
|
|
13
|
+
const base = path.join(root, "src", "icons");
|
|
14
|
+
candidates.push(
|
|
15
|
+
path.join(base, "favicon.ico"),
|
|
16
|
+
path.join(base, "web-app-manifest-512x512.png"),
|
|
17
|
+
path.join(base, "web-app-manifest-192x192.png"),
|
|
18
|
+
path.join(base, "favicon-96x96.png"),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
for (const iconPath of candidates) {
|
|
23
|
+
try {
|
|
24
|
+
const icon = nativeImage.createFromPath(iconPath);
|
|
25
|
+
if (!icon.isEmpty()) return icon;
|
|
26
|
+
} catch {}
|
|
27
|
+
}
|
|
28
|
+
// Fallback: no custom icon found (Electron default will be used).
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
6
32
|
// We map your custom options to Electron's expected options
|
|
7
33
|
export interface BrowserWindowOptions extends WindowPreset {}
|
|
8
34
|
|
|
@@ -37,6 +63,7 @@ function createWindow(options?: BrowserWindowOptions, name?: string, preloadPath
|
|
|
37
63
|
const resolvedPreload = resolvePreloadPath(preloadPath);
|
|
38
64
|
if (resolvedPreload && !defaultPreloadPath) defaultPreloadPath = resolvedPreload;
|
|
39
65
|
|
|
66
|
+
const icon = resolveWindowIcon();
|
|
40
67
|
const win = new BrowserWindow({
|
|
41
68
|
title: preset.title,
|
|
42
69
|
width: preset.width,
|
|
@@ -48,12 +75,13 @@ function createWindow(options?: BrowserWindowOptions, name?: string, preloadPath
|
|
|
48
75
|
frame: !preset.frameless,
|
|
49
76
|
transparent: preset.transparentTitlebar ?? false,
|
|
50
77
|
show: false,
|
|
78
|
+
...(icon ? { icon } : {}),
|
|
51
79
|
webPreferences: {
|
|
52
80
|
preload: resolvedPreload,
|
|
53
81
|
contextIsolation: true,
|
|
54
82
|
nodeIntegration: false,
|
|
55
|
-
sandbox: false
|
|
56
|
-
}
|
|
83
|
+
sandbox: false,
|
|
84
|
+
},
|
|
57
85
|
});
|
|
58
86
|
|
|
59
87
|
const winId = win.id;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ChromeParts } from "@config/winsetup";
|
|
2
2
|
import { resolveChrome } from "@config/winsetup";
|
|
3
|
-
import { defaultMenuItems, installAppMenu } from "./app-menu";
|
|
4
|
-
import { installContextMenu } from "./context-menu";
|
|
3
|
+
import { defaultMenuItems, installAppMenu, removeAppMenu } from "./app-menu";
|
|
4
|
+
import { disableContextMenu, installContextMenu } from "./context-menu";
|
|
5
5
|
import { alertDialog, confirmDialog } from "./dialogs";
|
|
6
6
|
import type { DesktopWindow } from "./manager";
|
|
7
7
|
import { createAppTray, createHeadlessTray, createTray, hideDock, isTrayAvailable } from "./tray";
|
|
@@ -14,9 +14,14 @@ export type { ChromeParts };
|
|
|
14
14
|
* Install desktop chrome for a window. Pass `parts` for granular control;
|
|
15
15
|
* defaults to all enabled (backward compatible).
|
|
16
16
|
*/
|
|
17
|
-
function setupDesktopChrome(
|
|
17
|
+
function setupDesktopChrome(
|
|
18
|
+
win: DesktopWindow,
|
|
19
|
+
parts: Required<ChromeParts> = { appMenu: true, contextMenu: true, tray: true },
|
|
20
|
+
): void {
|
|
18
21
|
if (parts.appMenu) installAppMenu(win);
|
|
22
|
+
else removeAppMenu(win);
|
|
19
23
|
if (parts.contextMenu) installContextMenu(win);
|
|
24
|
+
else disableContextMenu(win);
|
|
20
25
|
if (parts.tray) createAppTray();
|
|
21
26
|
}
|
|
22
27
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
1
2
|
import path from "node:path";
|
|
2
3
|
import { app, Menu, nativeImage, Tray } from "electron";
|
|
3
4
|
import { config } from "@/core/config";
|
|
@@ -7,18 +8,68 @@ export interface DesktopTray extends Tray {}
|
|
|
7
8
|
|
|
8
9
|
let tray: Tray | null = null;
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
// 16x16 red dot (known-good data URL) as guaranteed fallback so tray can still appear.
|
|
12
|
+
const FALLBACK_TRAY_ICON =
|
|
13
|
+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAABYSURBVHgB7ZMxDoAgDEXfQI7gERzFQw5u5BWMg4maNB8WBgeTPqHJb9qfAjgQCLZYkGIgnSWmmOMcRZ6LzF1k65s6UKMo15W1X9mHe3vK+3xM4QcM0wUogx/2yq6l8gAAAABJRU5ErkJggg==";
|
|
14
|
+
|
|
15
|
+
function getTrayIconCandidates(): string[] {
|
|
16
|
+
// In dev, main bundle runs from out/main so app.getAppPath() can resolve to out/main;
|
|
17
|
+
// project assets live at the project root (../../ from out/main). In packaged builds,
|
|
18
|
+
// app.getAppPath() points to the app root (or asar root) where src/icons is packaged.
|
|
19
|
+
const bases = app.isPackaged
|
|
20
|
+
? [app.getAppPath()]
|
|
21
|
+
: [path.resolve(app.getAppPath(), "..", ".."), app.getAppPath(), process.cwd()];
|
|
22
|
+
|
|
23
|
+
const candidates: string[] = [];
|
|
24
|
+
for (const root of Array.from(new Set(bases))) {
|
|
25
|
+
const base = path.join(root, "src", "icons");
|
|
26
|
+
candidates.push(
|
|
27
|
+
path.join(base, "tray.png"),
|
|
28
|
+
path.join(base, "favicon-96x96.png"),
|
|
29
|
+
path.join(base, "favicon.ico"),
|
|
30
|
+
path.join(base, "web-app-manifest-192x192.png"),
|
|
31
|
+
path.join(base, "web-app-manifest-512x512.png"),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
// Last resort: executable icon (Electron icon in dev, app icon when packaged).
|
|
35
|
+
candidates.push(process.execPath);
|
|
36
|
+
return candidates;
|
|
13
37
|
}
|
|
14
38
|
|
|
15
39
|
export function createTray(): Tray | undefined {
|
|
40
|
+
if (tray && !(tray as any).isDestroyed?.()) return tray;
|
|
16
41
|
try {
|
|
17
|
-
const iconPath
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
42
|
+
for (const iconPath of getTrayIconCandidates()) {
|
|
43
|
+
try {
|
|
44
|
+
// Prefer passing a file path string directly (supported by Electron Tray).
|
|
45
|
+
if (fs.existsSync(iconPath)) {
|
|
46
|
+
try {
|
|
47
|
+
tray = new Tray(iconPath);
|
|
48
|
+
return tray;
|
|
49
|
+
} catch {}
|
|
50
|
+
}
|
|
51
|
+
const icon = nativeImage.createFromPath(iconPath);
|
|
52
|
+
if (!icon.isEmpty()) {
|
|
53
|
+
const size = icon.getSize();
|
|
54
|
+
const trayIcon = size.width > 32 ? icon.resize({ width: 16, height: 16 }) : icon;
|
|
55
|
+
tray = new Tray(trayIcon);
|
|
56
|
+
return tray;
|
|
57
|
+
}
|
|
58
|
+
} catch {
|
|
59
|
+
// try next candidate
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Guaranteed fallback so tray still appears even if files/exe icon can't be loaded.
|
|
64
|
+
try {
|
|
65
|
+
const fallback = nativeImage.createFromDataURL(FALLBACK_TRAY_ICON);
|
|
66
|
+
if (!fallback.isEmpty()) {
|
|
67
|
+
tray = new Tray(fallback);
|
|
68
|
+
return tray;
|
|
69
|
+
}
|
|
70
|
+
} catch {}
|
|
71
|
+
|
|
72
|
+
return undefined;
|
|
22
73
|
} catch {
|
|
23
74
|
return undefined;
|
|
24
75
|
}
|
|
@@ -40,16 +91,16 @@ export function createAppTray(): Tray | undefined {
|
|
|
40
91
|
click: () => {
|
|
41
92
|
winApi.getWindow()?.show();
|
|
42
93
|
winApi.getWindow()?.focus();
|
|
43
|
-
}
|
|
94
|
+
},
|
|
44
95
|
},
|
|
45
96
|
{
|
|
46
97
|
label: "Open Settings",
|
|
47
98
|
click: () => {
|
|
48
99
|
void winApi.openWindow({ title: "Settings", width: 420, height: 320 });
|
|
49
|
-
}
|
|
100
|
+
},
|
|
50
101
|
},
|
|
51
102
|
{ type: "separator" },
|
|
52
|
-
{ label: "Quit", click: () => app.quit() }
|
|
103
|
+
{ label: "Quit", click: () => app.quit() },
|
|
53
104
|
]);
|
|
54
105
|
|
|
55
106
|
t.setContextMenu(contextMenu);
|
|
@@ -65,8 +116,8 @@ export function createHeadlessTray(): Tray | undefined {
|
|
|
65
116
|
Menu.buildFromTemplate([
|
|
66
117
|
{ label: `${config.appName} is running - no UI`, enabled: false },
|
|
67
118
|
{ type: "separator" },
|
|
68
|
-
{ label: "Quit", click: () => app.quit() }
|
|
69
|
-
])
|
|
119
|
+
{ label: "Quit", click: () => app.quit() },
|
|
120
|
+
]),
|
|
70
121
|
);
|
|
71
122
|
|
|
72
123
|
return t;
|