mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-03-01 06:22:44 -08:00
refactor: extract overlay visibility and ipc helper services
This commit is contained in:
55
src/core/utils/notification.ts
Normal file
55
src/core/utils/notification.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Notification, nativeImage } from "electron";
|
||||
import * as fs from "fs";
|
||||
|
||||
export function showDesktopNotification(
|
||||
title: string,
|
||||
options: { body?: string; icon?: string },
|
||||
): void {
|
||||
const notificationOptions: {
|
||||
title: string;
|
||||
body?: string;
|
||||
icon?: Electron.NativeImage | string;
|
||||
} = { title };
|
||||
|
||||
if (options.body) {
|
||||
notificationOptions.body = options.body;
|
||||
}
|
||||
|
||||
if (options.icon) {
|
||||
const isFilePath =
|
||||
typeof options.icon === "string" &&
|
||||
(options.icon.startsWith("/") || /^[a-zA-Z]:[\\/]/.test(options.icon));
|
||||
|
||||
if (isFilePath) {
|
||||
if (fs.existsSync(options.icon)) {
|
||||
notificationOptions.icon = options.icon;
|
||||
} else {
|
||||
console.warn("Notification icon file not found:", options.icon);
|
||||
}
|
||||
} else if (
|
||||
typeof options.icon === "string" &&
|
||||
options.icon.startsWith("data:image/")
|
||||
) {
|
||||
const base64Data = options.icon.replace(/^data:image\/\w+;base64,/, "");
|
||||
try {
|
||||
const image = nativeImage.createFromBuffer(
|
||||
Buffer.from(base64Data, "base64"),
|
||||
);
|
||||
if (image.isEmpty()) {
|
||||
console.warn(
|
||||
"Notification icon created from base64 is empty - image format may not be supported by Electron",
|
||||
);
|
||||
} else {
|
||||
notificationOptions.icon = image;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to create notification icon from base64:", err);
|
||||
}
|
||||
} else {
|
||||
notificationOptions.icon = options.icon;
|
||||
}
|
||||
}
|
||||
|
||||
const notification = new Notification(notificationOptions);
|
||||
notification.show();
|
||||
}
|
||||
Reference in New Issue
Block a user