Skip to content

Commit

Permalink
Update onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
SRichner committed Feb 23, 2024
1 parent 56db8a4 commit 4c9f9ee
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 81 deletions.
33 changes: 31 additions & 2 deletions src/electron/electron/ipc/IpcHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExperienceSamplingService } from '../main/services/ExperienceSamplingService';
import { app, ipcMain, IpcMainInvokeEvent } from 'electron';
import { app, ipcMain, IpcMainInvokeEvent, systemPreferences } from 'electron';
import { WindowService } from '../main/services/WindowService';
import { getLogger } from '../shared/Logger';
import { TypedIpcMain } from '../../src/utils/TypedIpcMain';
Expand Down Expand Up @@ -31,7 +31,11 @@ export class IpcHandler {
this.actions = {
createExperienceSample: this.createExperienceSample,
closeExperienceSamplingWindow: this.closeExperienceSamplingWindow,
getStudyInfo: this.getStudyInfo
closeOnboardingWindow: this.closeOnboardingWindow,
getStudyInfo: this.getStudyInfo,
startAllTrackers: this.startAllTrackers,
triggerPermissionCheckAccessibility: this.triggerPermissionCheckAccessibility,
triggerPermissionCheckScreenRecording: this.triggerPermissionCheckScreenRecording
};
}

Expand All @@ -52,12 +56,16 @@ export class IpcHandler {
private async createExperienceSample(
promptedAt: number,
question: string,
responseOptions: string,
scale: number,
response: number,
skipped: boolean = false
) {
await this.experienceSamplingService.createExperienceSample(
promptedAt,
question,
responseOptions,
scale,
response,
skipped
);
Expand All @@ -67,6 +75,10 @@ export class IpcHandler {
this.windowService.closeExperienceSamplingWindow();
}

private closeOnboardingWindow(): void {
this.windowService.closeOnboardingWindow();
}

private async getStudyInfo(): Promise<StudyInfoDto> {
const settings: Settings = await Settings.findOne({ where: { onlyOneEntityShouldExist: 1 } });
return {
Expand All @@ -81,4 +93,21 @@ export class IpcHandler {
currentlyActiveTrackers: this.trackerService.getRunningTrackerNames()
};
}

private triggerPermissionCheckAccessibility(prompt: boolean): boolean {
return systemPreferences.isTrustedAccessibilityClient(prompt);
}

private triggerPermissionCheckScreenRecording(): boolean {
const status = systemPreferences.getMediaAccessStatus('screen');
return status === 'granted';
}

private async startAllTrackers(): Promise<void> {
try {
await this.trackerService.startAllTrackers();
} catch (e) {
LOG.error('Error starting trackers', e);
}
}
}
63 changes: 40 additions & 23 deletions src/electron/electron/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'reflect-metadata';
import { app, dialog, powerMonitor } from 'electron';
import { app, dialog, powerMonitor, systemPreferences } from 'electron';
import { release } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -66,6 +66,10 @@ 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 @@ -88,29 +92,35 @@ app.whenReady().then(async () => {
);
}

await trackers.startAllTrackers();
LOG.info(`Trackers started: ${trackers.getRunningTrackerNames().join(', ')}`);

powerMonitor.on('suspend', async (): Promise<void> => {
LOG.debug('The system is going to sleep');
await trackers.stopAllTrackers();
});
powerMonitor.on('resume', async (): Promise<void> => {
LOG.debug('The system is resuming');
await trackers.startAllTrackers();
});
powerMonitor.on('shutdown', async (): Promise<void> => {
LOG.debug('The system is going to shutdown');
await trackers.stopAllTrackers();
});
powerMonitor.on('lock-screen', async (): Promise<void> => {
LOG.debug('The system is going to lock-screen');
await trackers.stopAllTrackers();
});
powerMonitor.on('unlock-screen', async (): Promise<void> => {
LOG.debug('The system is going to unlock-screen');
if (process.platform === 'darwin' && hasAccessibilityAndScreenRecordingPermission() === false) {
LOG.info('Screen recording permission not granted, opening onboarding window...');
await windowService.createOnboardingWindow();
} else {
LOG.info('Screen recording permission granted, starting all trackers...');
await trackers.startAllTrackers();
});
LOG.info(`Trackers started: ${trackers.getRunningTrackerNames().join(', ')}`);

powerMonitor.on('suspend', async (): Promise<void> => {
LOG.debug('The system is going to sleep');
await trackers.stopAllTrackers();
});
powerMonitor.on('resume', async (): Promise<void> => {
LOG.debug('The system is resuming');
await trackers.startAllTrackers();
});
powerMonitor.on('shutdown', async (): Promise<void> => {
LOG.debug('The system is going to shutdown');
await trackers.stopAllTrackers();
});
powerMonitor.on('lock-screen', async (): Promise<void> => {
LOG.debug('The system is going to lock-screen');
await trackers.stopAllTrackers();
});
powerMonitor.on('unlock-screen', async (): Promise<void> => {
LOG.debug('The system is going to unlock-screen');
await trackers.startAllTrackers();
});
}
} catch (error) {
LOG.error('Error during app initialization', error);
dialog.showErrorBox(
Expand Down Expand Up @@ -139,3 +149,10 @@ app.on('window-all-closed', () => {
// app.quit();
// }
});

function hasAccessibilityAndScreenRecordingPermission(): boolean {
return (
systemPreferences.isTrustedAccessibilityClient(false) &&
systemPreferences.getMediaAccessStatus('screen') === 'granted'
);
}
6 changes: 4 additions & 2 deletions src/electron/electron/main/services/WindowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class WindowService {
});
}

private closeOnboardingWindow() {
public closeOnboardingWindow() {
if (this.onboardingWindow) {
this.onboardingWindow?.close();
this.onboardingWindow = null;
Expand Down Expand Up @@ -174,7 +174,9 @@ export class WindowService {
});

if (process.env.VITE_DEV_SERVER_URL) {
await this.onboardingWindow.loadURL(process.env.VITE_DEV_SERVER_URL + '#onboarding');
await this.onboardingWindow.loadURL(
process.env.VITE_DEV_SERVER_URL + `#onboarding?isMacOS=${process.platform === 'darwin'}`
);
} else {
await this.onboardingWindow.loadFile(path.join(process.env.DIST, 'index.html'), {
hash: 'onboarding'
Expand Down
3 changes: 2 additions & 1 deletion src/electron/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const router: Router = createRouter({
{
path: '/onboarding',
name: 'Onboarding',
component: () => import('../views/OnboardingView.vue')
component: () => import('../views/OnboardingView.vue'),
props: (route) => ({ query: route.query.isMacOS })
}
]
});
Expand Down
4 changes: 4 additions & 0 deletions src/electron/src/utils/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ type Commands = {
createExperienceSample: (
promptedAt: number,
question: string,
responseOptions: string,
scale: number,
response?: number,
skipped?: boolean
) => Promise<void>;
closeExperienceSamplingWindow: () => void;
closeOnboardingWindow: () => void;
getStudyInfo: () => Promise<StudyInfoDto>;
startAllTrackers: () => void;
triggerPermissionCheckAccessibility: (prompt: boolean) => boolean;
triggerPermissionCheckScreenRecording: () => boolean;
};
export default Commands;
Loading

0 comments on commit 4c9f9ee

Please sign in to comment.