-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathsystem.ts
More file actions
83 lines (71 loc) · 2.19 KB
/
system.ts
File metadata and controls
83 lines (71 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { app, globalShortcut, shell } from 'electron';
import type { Menubar } from 'menubar';
import {
EVENTS,
type EventData,
type IAutoLaunch,
type IKeyboardShortcut,
type IKeyboardShortcutResult,
type IOpenExternal,
} from '../../shared/events';
import { handleMainEvent, onMainEvent } from '../events';
/**
* Register IPC handlers for OS-level system operations.
*
* @param mb - The menubar instance used for show/hide on keyboard shortcut activation.
*/
export function registerSystemHandlers(mb: Menubar): void {
/**
* Currently registered accelerator for the global shortcut, or `null` when none.
*/
let lastRegisteredAccelerator: string | null = null;
const toggleWindow = () => {
if (!mb.window) {
return;
}
if (mb.window.isVisible()) {
mb.hideWindow();
} else {
mb.showWindow();
}
};
/**
* Open the given URL in the user's default browser, with an option to activate the app.
*/
onMainEvent(EVENTS.OPEN_EXTERNAL, (_, { url, activate }: IOpenExternal) =>
shell.openExternal(url, { activate }),
);
/**
* Register or unregister a global keyboard shortcut that toggles the menubar window visibility.
*/
handleMainEvent(
EVENTS.UPDATE_KEYBOARD_SHORTCUT,
(_, data: EventData): IKeyboardShortcutResult => {
const { enabled, keyboardShortcut } = data as IKeyboardShortcut;
const previous = lastRegisteredAccelerator;
if (lastRegisteredAccelerator) {
globalShortcut.unregister(lastRegisteredAccelerator);
lastRegisteredAccelerator = null;
}
if (!enabled) {
return { success: true };
}
const ok = globalShortcut.register(keyboardShortcut, toggleWindow);
if (ok) {
lastRegisteredAccelerator = keyboardShortcut;
return { success: true };
}
if (previous) {
globalShortcut.register(previous, toggleWindow);
lastRegisteredAccelerator = previous;
}
return { success: false };
},
);
/**
* Update the application's auto-launch setting based on the provided configuration.
*/
onMainEvent(EVENTS.UPDATE_AUTO_LAUNCH, (_, settings: IAutoLaunch) => {
app.setLoginItemSettings(settings);
});
}