-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathsystem-permissions.ts
More file actions
89 lines (66 loc) · 2.72 KB
/
Copy pathsystem-permissions.ts
File metadata and controls
89 lines (66 loc) · 2.72 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
84
85
86
87
88
89
import {systemPreferences, shell, dialog, app} from 'electron';
import {t} from './localization';
const {hasScreenCapturePermission, hasPromptedForPermission} = require('mac-screen-capture-permissions');
const {ensureDockIsShowing} = require('../utils/dock');
let isDialogShowing = false;
const promptSystemPreferences = (options: {message: string; detail: string; systemPreferencesPath: string}) => async ({hasAsked}: {hasAsked?: boolean} = {}) => {
if (hasAsked || isDialogShowing) {
return false;
}
isDialogShowing = true;
await ensureDockIsShowing(async () => {
const {response} = await dialog.showMessageBox({
type: 'warning',
buttons: [t('Open System Preferences'), t('Cancel')],
defaultId: 0,
message: t(options.message),
detail: t(options.detail),
cancelId: 1
});
isDialogShowing = false;
if (response === 0) {
await openSystemPreferences(options.systemPreferencesPath);
app.quit();
}
});
return false;
};
export const openSystemPreferences = async (path: string) => shell.openExternal(`x-apple.systempreferences:com.apple.preference.security?${path}`);
// Microphone
const getMicrophoneAccess = () => systemPreferences.getMediaAccessStatus('microphone');
const microphoneFallback = promptSystemPreferences({
message: 'Kap cannot access the microphone.',
detail: 'Kap requires microphone access to be able to record audio. You can grant this in the System Preferences. Afterwards, launch Kap for the changes to take effect.',
systemPreferencesPath: 'Privacy_Microphone'
});
export const ensureMicrophonePermissions = async (fallback = microphoneFallback) => {
const access = getMicrophoneAccess();
if (access === 'granted') {
return true;
}
if (access !== 'denied') {
const granted = await systemPreferences.askForMediaAccess('microphone');
if (granted) {
return true;
}
return fallback({hasAsked: true});
}
return fallback();
};
export const hasMicrophoneAccess = () => getMicrophoneAccess() === 'granted';
// Screen Capture (10.15 and newer)
const screenCaptureFallback = promptSystemPreferences({
message: 'Kap cannot record the screen.',
detail: 'Kap requires screen capture access to be able to record the screen. You can grant this in the System Preferences. Afterwards, launch Kap for the changes to take effect.',
systemPreferencesPath: 'Privacy_ScreenCapture'
});
export const ensureScreenCapturePermissions = (fallback = screenCaptureFallback) => {
const hadAsked = hasPromptedForPermission();
const hasAccess = hasScreenCapturePermission();
if (hasAccess) {
return true;
}
fallback({hasAsked: !hadAsked});
return false;
};
export const hasScreenCaptureAccess = () => hasScreenCapturePermission();