Skip to content

Commit fd4789a

Browse files
committed
refactor: use early return guard for mb.window across src/main
1 parent 970d4cb commit fd4789a

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/main/events.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ export function sendRendererEvent(
4646
event: EventType,
4747
data?: string,
4848
) {
49-
mb.window?.webContents.send(event, data);
49+
if (!mb.window) {
50+
return;
51+
}
52+
53+
mb.window.webContents.send(event, data);
5054
}

src/main/handlers/system.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export function registerSystemHandlers(mb: Menubar): void {
2424
let lastRegisteredAccelerator: string | null = null;
2525

2626
const toggleWindow = () => {
27-
if (mb.window?.isVisible()) {
27+
if (!mb.window) {
28+
return;
29+
}
30+
31+
if (mb.window.isVisible()) {
2832
mb.hideWindow();
2933
} else {
3034
mb.showWindow();

src/main/lifecycle/window.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ export function configureWindowEvents(mb: Menubar): void {
2626
* When DevTools is opened, resize and center the window for better visibility and allow resizing.
2727
*/
2828
mb.window.webContents.on('devtools-opened', () => {
29-
mb.window?.setSize(800, 600);
30-
mb.window?.center();
31-
if (mb.window) {
32-
mb.window.resizable = true;
29+
if (!mb.window) {
30+
return;
3331
}
34-
mb.window?.setAlwaysOnTop(true);
32+
33+
mb.window.setSize(800, 600);
34+
mb.window.center();
35+
mb.window.resizable = true;
36+
mb.window.setAlwaysOnTop(true);
3537
});
3638

3739
/**
3840
* When DevTools is closed, restore the window to its original size and position it centered on the tray icon.
3941
*/
4042
mb.window.webContents.on('devtools-closed', () => {
43+
if (!mb.window) {
44+
return;
45+
}
46+
4147
const trayBounds = mb.tray.getBounds();
42-
mb.window?.setSize(WindowConfig.width ?? 500, WindowConfig.height ?? 400);
48+
mb.window.setSize(WindowConfig.width!, WindowConfig.height!);
4349
mb.positioner.move('trayCenter', trayBounds);
44-
if (mb.window) {
45-
mb.window.resizable = false;
46-
}
50+
mb.window.resizable = false;
4751
});
4852
}

src/main/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export function takeScreenshot(mb: Menubar) {
3030
`${dateStr}-${APPLICATION.NAME}-screenshot.png`,
3131
);
3232

33-
mb.window?.capturePage().then((img) => {
33+
if (!mb.window) {
34+
return;
35+
}
36+
37+
mb.window.capturePage().then((img) => {
3438
fs.writeFile(capturedPicFilePath, img.toPNG(), () =>
3539
logInfo('takeScreenshot', `Screenshot saved ${capturedPicFilePath}`),
3640
);

0 commit comments

Comments
 (0)