1+ import { app } from 'electron' ;
12import { MAIN_EVENT , MAIN_EVENT_TYPE } from '../../constant' ;
23import PullActivities from '../workers/pull-activities' ;
34import PushActivities from '../workers/push-activities' ;
@@ -6,7 +7,21 @@ import MainEvent from './events';
67import * as path from 'node:path' ;
78import { TrayNotify } from './tray-notify' ;
89import { TEventArgs } from './event-types' ;
9- import { getAuthConfig , delaySync , TAuthConfig , updateAgentSetting , getAppSetting } from '../util' ;
10+ import {
11+ getAuthConfig ,
12+ delaySync ,
13+ TAuthConfig ,
14+ updateAgentSetting ,
15+ getAppSetting ,
16+ getInitialConfig
17+ } from '../util' ;
18+ import {
19+ DesktopUpdater ,
20+ AppError ,
21+ TranslateService ,
22+ DesktopDialog
23+ } from '@gauzy/desktop-lib' ;
24+ import { logger as log } from '@gauzy/desktop-core' ;
1025
1126const appRootPath : string = path . join ( __dirname , '../..' ) ;
1227
@@ -17,11 +32,17 @@ export default class EventHandler {
1732 private trayNotify : TrayNotify ;
1833 private pullActivities : PullActivities ;
1934 private pushActivities : PushActivities ;
35+ private updater : DesktopUpdater ;
2036
2137 constructor ( ) {
2238 this . mainEvent = MainEvent . getInstance ( ) ;
2339 this . appWindow = AppWindow . getInstance ( appRootPath ) ;
2440 this . trayNotify = TrayNotify . getInstance ( ) ;
41+ this . updater = new DesktopUpdater ( {
42+ repository : process . env . REPO_NAME ,
43+ owner : process . env . REPO_OWNER ,
44+ typeRelease : 'releases'
45+ } ) ;
2546 }
2647
2748 static getInstance ( ) {
@@ -132,6 +153,75 @@ export default class EventHandler {
132153 }
133154 }
134155
156+ private async resumeActivity ( ) {
157+ const authConfig = getAuthConfig ( ) ;
158+ this . getPullActivities ( authConfig ) ;
159+ await this . pullActivities . recordIdleTime ( ) ;
160+ this . pullActivities . startedPausedDate = null ;
161+ await this . pullActivities . startTracking ( this . pullActivities . isPaused ) ;
162+ this . pullActivities . isPaused = false ;
163+ }
164+
165+ private async pauseActivity ( ) {
166+ const authConfig = getAuthConfig ( ) ;
167+ this . getPullActivities ( authConfig ) ;
168+ const isTimeRunning = this . pullActivities . running ;
169+ await this . pullActivities . stopTracking ( true ) ;
170+ if ( isTimeRunning ) {
171+ this . pullActivities . startedPausedDate = new Date ( ) ;
172+ this . pullActivities . isPaused = true ;
173+ }
174+ }
175+
176+ async stopAppActivity ( ) {
177+ const authConfig = getAuthConfig ( ) ;
178+ this . getPullActivities ( authConfig ) ;
179+ this . getPushActivities ( ) ;
180+ if ( ! authConfig ?. token ) {
181+ await this . pushActivities . stopPooling ( ) ;
182+ return ;
183+ }
184+
185+ if ( this . pullActivities . running ) {
186+ await this . pullActivities . stopTracking ( ) ;
187+ }
188+ await this . pushActivities . stopPooling ( ) ;
189+
190+ }
191+
192+ async exitDialog ( ) {
193+ const DIALOG_TITLE = TranslateService . instant ( 'TIMER_TRACKER.DIALOG.WARNING' ) ;
194+ const DIALOG_MESSAGE = TranslateService . instant ( 'TIMER_TRACKER.DIALOG.EXIT_AGENT_PREVENT_CONFIRM' ) ;
195+ const dialog = new DesktopDialog ( DIALOG_TITLE , DIALOG_MESSAGE ) ;
196+ dialog . options . buttons = [
197+ TranslateService . instant ( 'OK' ) ,
198+ ] ;
199+ await dialog . show ( ) ;
200+ }
201+
202+ private async exitApp ( ) {
203+ try {
204+ const appSetting = getAppSetting ( ) ;
205+ const appConfig = getInitialConfig ( ) ;
206+ const authConfig = getAuthConfig ( ) ;
207+ if ( appSetting ?. allowAgentAppExit || ! appConfig ?. isSetup || ! authConfig ?. token ) {
208+ await this . stopAppActivity ( ) ;
209+ this . updater . cancel ( ) ;
210+ app . quit ( ) ;
211+ } else {
212+ await this . exitDialog ( ) ;
213+ }
214+ } catch ( e ) {
215+ log . error ( `ERROR: Failed to exit app: ${ e } ` ) ;
216+ throw new AppError ( 'MAINUPDTABORT' , e ) ;
217+ }
218+ }
219+
220+ private async stopAndExit ( ) {
221+ await this . stopAppTracking ( ) ;
222+ app . quit ( ) ;
223+ }
224+
135225 async handleEvent ( args : TEventArgs ) {
136226 switch ( args . type ) {
137227 case MAIN_EVENT_TYPE . LOGOUT_EVENT :
@@ -157,6 +247,14 @@ export default class EventHandler {
157247 return this . checkStatusTimer ( ) ;
158248 case MAIN_EVENT_TYPE . TRAY_TIMER_STATUS :
159249 return this . trayTimerStatus ( ) ;
250+ case MAIN_EVENT_TYPE . ACTIVITY_RESUME :
251+ return this . resumeActivity ( ) ;
252+ case MAIN_EVENT_TYPE . ACTIVITY_PAUSED :
253+ return this . pauseActivity ( ) ;
254+ case MAIN_EVENT_TYPE . EXIT_APP :
255+ return this . exitApp ( ) ;
256+ case MAIN_EVENT_TYPE . STOP_N_EXIT :
257+ return this . stopAndExit ( ) ;
160258 default : break ;
161259 }
162260 }
0 commit comments