Skip to content

Commit

Permalink
Fix startup issue
Browse files Browse the repository at this point in the history
  • Loading branch information
SRichner committed Feb 26, 2024
1 parent 0e5a59b commit bc37696
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/electron/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { IpcHandler } from '../ipc/IpcHandler';
import { ExperienceSamplingService } from './services/ExperienceSamplingService';
import studyConfig from '../../shared/study.config';
import { SchedulingService } from './services/SchedulingService';
import { is } from './services/utils/helpers';
import { Settings } from './entities/Settings';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down Expand Up @@ -51,7 +53,7 @@ if (!app.requestSingleInstanceLock()) {
process.exit(0);
}

if (process.platform === 'darwin') {
if (is.macOS) {
app.dock.hide();
}

Expand All @@ -66,10 +68,6 @@ app.whenReady().then(async () => {
openAtLogin: true
});

LOG.info(
`hasAccessibilityAndScreenRecordingPermission = ${hasAccessibilityAndScreenRecordingPermission()}, systemPreferences.isTrustedAccessibilityClient(false) = ${systemPreferences.isTrustedAccessibilityClient(false)}, systemPreferences.getMediaAccessStatus('screen') = ${systemPreferences.getMediaAccessStatus('screen')}`
);

try {
await databaseService.init();
await settingsService.init();
Expand All @@ -92,11 +90,19 @@ app.whenReady().then(async () => {
);
}

if (process.platform === 'darwin' && hasAccessibilityAndScreenRecordingPermission() === false) {
LOG.info('Screen recording permission not granted, opening onboarding window...');
const settings: Settings = await Settings.findOneBy({ onlyOneEntityShouldExist: 1 });

if (settings.onboardingShown === false || !hasAccessibilityAndScreenRecordingPermission()) {
LOG.debug(
`Onboarding shown: ${settings.onboardingShown}, hasAccessibilityAndScreenRecordingPermission: ${hasAccessibilityAndScreenRecordingPermission()}, creating onboarding window...`
);
await windowService.createOnboardingWindow();
settings.onboardingShown = true;
await settings.save();
} else {
LOG.info('Screen recording permission granted, starting all trackers...');
LOG.debug(
`Onboarding shown: ${settings.onboardingShown}, hasAccessibilityAndScreenRecordingPermission: ${hasAccessibilityAndScreenRecordingPermission()}, starting all trackers...`
);
await trackers.startAllTrackers();
LOG.info(`Trackers started: ${trackers.getRunningTrackerNames().join(', ')}`);

Expand Down Expand Up @@ -151,6 +157,10 @@ app.on('window-all-closed', () => {
});

function hasAccessibilityAndScreenRecordingPermission(): boolean {
if (!is.macOS) {
return true;
}

return (
systemPreferences.isTrustedAccessibilityClient(false) &&
systemPreferences.getMediaAccessStatus('screen') === 'granted'
Expand Down
4 changes: 3 additions & 1 deletion src/electron/electron/main/services/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import electron from 'electron';

export declare interface Is {
dev: boolean;
macOS: boolean;
}

export const is: Is = {
dev: !electron.app.isPackaged
dev: !electron.app.isPackaged,
macOS: process.platform === 'darwin'
};

export function generateAlphaNumericString(length: number = 0): string {
Expand Down

0 comments on commit bc37696

Please sign in to comment.