Skip to content

Commit 0d5bce7

Browse files
committed
fix: notification not set correctly / 修复: 通知无法正常设置
1 parent da10ce7 commit 0d5bce7

6 files changed

Lines changed: 49 additions & 17 deletions

File tree

src/electron/events/event-handlers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import getLogger from "../logger.js";
99
import { modifyGlobalBringUpWindowShortcut } from "./glabol-event-handlers.js";
1010
import { LinkTarget } from "../types.js";
11+
import { showWindow } from "../actions/window-actions.js";
1112
const logger = getLogger(import.meta.url);
1213

1314
export function registerWindowEventHandlers(window: BrowserWindow) {
@@ -192,10 +193,15 @@ export function registerDataEventHandlers(noteService: NoteService) {
192193

193194
//通知事件处理器
194195
export function registerNotificationEventHandlers(
196+
window: BrowserWindow,
195197
notificationService: NotificationService
196198
) {
197199
ipcMain.handle("notification:set", async (event, delay, name, content?) => {
198200
logger.info(`设置的通知延迟: ${delay}`);
199-
notificationService.enqueue(new NotificationItem(content, name, delay));
201+
notificationService.enqueue(
202+
new NotificationItem(content, name, delay, (e) => {
203+
showWindow(window);
204+
})
205+
);
200206
});
201207
}

src/electron/io/notification-service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class NotificationItem {
88
private _title: string;
99
private _content: string;
1010
private _delay: number;
11+
private _clickCallback?: (e: Electron.Event) => void;
1112
public get title() {
1213
return this._title;
1314
}
@@ -17,10 +18,19 @@ export class NotificationItem {
1718
public get delay() {
1819
return this._delay;
1920
}
20-
public constructor(title: string, source: string, delay: number) {
21+
public get clickCallback() {
22+
return this._clickCallback;
23+
}
24+
public constructor(
25+
title: string,
26+
source: string,
27+
delay: number,
28+
clickCallback?: (e: Electron.Event) => void
29+
) {
2130
this._title = title;
2231
this._content = source;
2332
this._delay = delay;
33+
this._clickCallback = clickCallback;
2434
}
2535
}
2636

src/electron/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ app.on("ready", async () => {
7474
registerDataEventHandlers(noteService);
7575

7676
//注册通知处理事件监听
77-
registerNotificationEventHandlers(notificationService);
77+
registerNotificationEventHandlers(mainWindow, notificationService);
7878

7979
//注册全局快捷键
8080
registerGlobalBringUpWindowShortCut(() => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { Notification } from "electron";
22
import { NotificationItem } from "../io/notification-service.js";
3+
import GetLogger from "../logger.js";
4+
const logger = GetLogger(import.meta.url);
35

46
export function nativeSendNotification(item: NotificationItem) {
57
const notification = new Notification({
68
title: item.content,
79
body: item.title,
810
});
11+
if (item.clickCallback) {
12+
notification.on("click", (e) => {
13+
logger.info("用户点击通知");
14+
item.clickCallback!(e);
15+
notification.close();
16+
});
17+
}
918
notification.show();
1019
}

src/ui/components/appBar/appBar.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,15 @@ export default function AppBar({
226226
setIsTimePickerOpen(!isTimePickerOpen);
227227
};
228228

229-
const handleScheduleNotification = async () => {
230-
if (!timePickerRef.current || !contentUseRef.current) {
231-
return;
232-
}
233-
const targetTime = (timePickerRef!.current as HTMLInputElement).value;
229+
const handleScheduleNotification = async (
230+
targetTime: string,
231+
useContent: boolean
232+
) => {
234233
console.log(targetTime);
235-
const useContentAsNotification = (
236-
contentUseRef!.current as HTMLInputElement
237-
).checked;
238234
await scheduleNotification(
239235
targetTime,
240236
`来自笔记: ${title}`,
241-
useContentAsNotification ? content : "你有新的提醒"
237+
useContent ? content : "你有新的提醒"
242238
);
243239
setIsTimePickerOpen(false);
244240
};
@@ -277,9 +273,6 @@ export default function AppBar({
277273
};
278274
}, [isDisplay]);
279275

280-
const timePickerRef = useRef(null);
281-
const contentUseRef = useRef(null);
282-
283276
return (
284277
<header
285278
className={clsx(isDisplay ? styles.header : styles["header-collapse"])}

src/ui/components/dialogs/NotificationSettingWindow.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function NotificationSettingWindow({
1515
onSet,
1616
onCancel,
1717
}: {
18-
onSet: React.MouseEventHandler;
18+
onSet: (time: string, useContent: boolean) => void;
1919
onCancel: React.MouseEventHandler;
2020
}) {
2121
const timePickerRef = useRef(null);
@@ -31,6 +31,20 @@ export default function NotificationSettingWindow({
3131
[onCancel]
3232
);
3333

34+
const handleOnSet = useCallback(
35+
(e: React.MouseEvent) => {
36+
e.preventDefault();
37+
const timePickerInput = timePickerRef.current;
38+
const useContentInput = contentUseRef.current;
39+
if (timePickerInput && useContentInput) {
40+
const time = (timePickerInput as HTMLInputElement).value;
41+
const useContent = (useContentInput as HTMLInputElement).checked;
42+
onSet(time, useContent);
43+
}
44+
},
45+
[onSet]
46+
);
47+
3448
useEffect(() => {
3549
document.addEventListener("keydown", handleOnQuit);
3650
return () => {
@@ -73,7 +87,7 @@ export default function NotificationSettingWindow({
7387
</MdOutlinedButton>
7488
<MdFilledButton
7589
className={styles["dialog-action-button-small"]}
76-
onClick={onSet}
90+
onClick={handleOnSet}
7791
>
7892
确定
7993
</MdFilledButton>

0 commit comments

Comments
 (0)