Skip to content

Commit

Permalink
Support Catalina (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
karaggeorge authored and sindresorhus committed Nov 13, 2019
1 parent aa1326f commit fba30c6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
57 changes: 46 additions & 11 deletions main/common/system-permissions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const {systemPreferences, shell, dialog} = require('electron');
const {systemPreferences, shell, dialog, app} = require('electron');
const {hasScreenCapturePermission, hasPromptedForPermission} = require('mac-screen-capture-permissions');

const getMicrophoneAccess = () => systemPreferences.getMediaAccessStatus('microphone');

const promptSystemPreferences = async ({hasAsked} = {}) => {
const promptSystemPreferences = options => async ({hasAsked} = {}) => {
if (hasAsked) {
return false;
}
Expand All @@ -11,19 +10,32 @@ const promptSystemPreferences = async ({hasAsked} = {}) => {
type: 'warning',
buttons: ['Open System Preferences', 'Cancel'],
defaultId: 0,
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, relaunch Kap for the changes to take effect.',
message: options.message,
detail: options.detail,
cancelId: 1
});

if (response === 0) {
openSystemPreferences();
await openSystemPreferences(options.systemPreferencesPath);
app.quit();
}

return false;
};

const ensureMicrophonePermissions = async (fallback = promptSystemPreferences) => {
const openSystemPreferences = path => 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'
});

const ensureMicrophonePermissions = async (fallback = microphoneFallback) => {
const access = getMicrophoneAccess();

if (access === 'granted') {
Expand All @@ -43,12 +55,35 @@ const ensureMicrophonePermissions = async (fallback = promptSystemPreferences) =
return fallback();
};

const openSystemPreferences = () => shell.openExternal('x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone');

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'
});

const ensureScreenCapturePermissions = (fallback = screenCaptureFallback) => {
const hadAsked = hasPromptedForPermission();

const hasAccess = hasScreenCapturePermission();

if (hasAccess) {
return true;
}

fallback({hasAsked: !hadAsked});
return false;
};

const hasScreenCaptureAccess = () => hasScreenCapturePermission();

module.exports = {
ensureMicrophonePermissions,
hasMicrophoneAccess,
openSystemPreferences
openSystemPreferences,
ensureScreenCapturePermissions,
hasScreenCaptureAccess
};
6 changes: 5 additions & 1 deletion main/cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const electron = require('electron');
const delay = require('delay');

const settings = require('./common/settings');
const {hasMicrophoneAccess, ensureMicrophonePermissions, openSystemPreferences} = require('./common/system-permissions');
const {hasMicrophoneAccess, ensureMicrophonePermissions, openSystemPreferences, ensureScreenCapturePermissions} = require('./common/system-permissions');
const loadRoute = require('./utils/routes');
const {checkForAnyBlockingEditors} = require('./editor');

Expand Down Expand Up @@ -87,6 +87,10 @@ const openCropperWindow = async () => {
return;
}

if (!ensureScreenCapturePermissions()) {
return;
}

const recordAudio = settings.get('recordAudio');

if (recordAudio && !hasMicrophoneAccess()) {
Expand Down
3 changes: 2 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {setApplicationMenu} = require('./menus');
const openFiles = require('./utils/open-files');
const {initializeExportOptions} = require('./export-options');
const settings = require('./common/settings');
const {hasMicrophoneAccess} = require('./common/system-permissions');
const {hasMicrophoneAccess, ensureScreenCapturePermissions} = require('./common/system-permissions');

require('./utils/sentry');

Expand Down Expand Up @@ -90,6 +90,7 @@ const checkForUpdates = () => {
openFiles(...filesToOpen);
} else if (
!app.getLoginItemSettings().wasOpenedAtLogin &&
ensureScreenCapturePermissions() &&
(!settings.get('recordAudio') || hasMicrophoneAccess())
) {
openCropperWindow();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"insight": "^0.10.3",
"lodash": "^4.17.15",
"mac-open-with": "^1.2.2",
"mac-screen-capture-permissions": "^1.0.2",
"mac-windows": "^0.7.1",
"macos-version": "^5.2.0",
"make-dir": "^3.0.0",
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5169,6 +5169,15 @@ mac-open-with@^1.2.2:
execa "^2.0.4"
macos-version "^5.2.0"

mac-screen-capture-permissions@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/mac-screen-capture-permissions/-/mac-screen-capture-permissions-1.0.2.tgz#d85f6041bec0910e12694e65de00205484a0734f"
integrity sha512-foz6w6ZGJbAHfHrd56xawt3WvswfmUK4PurWlXH4LsUMq69P0XY523YJ3srR0okpUivUhVQmzIk0LOOO/u7agQ==
dependencies:
electron-util "^0.12.1"
execa "^2.0.4"
macos-version "^5.2.0"

mac-windows@^0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/mac-windows/-/mac-windows-0.7.1.tgz#3309ef0c7c508725cc3e17a3ea1687de0680c78b"
Expand Down

0 comments on commit fba30c6

Please sign in to comment.