@fto-consult/expo-ui 7.5.34 → 7.5.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,22 +3,27 @@ const session = require("../utils/session");
3
3
  const path = require("path");
4
4
  const fs = require("fs");
5
5
  const isValidUrl = require("../utils/isValidUrl");
6
-
7
- const log = (message)=>{
8
- return win != null && win && win.webContents.send("console.log",message);
9
- }
6
+ const debounce = require("../utils/debounce");
10
7
 
11
8
  const isObj = x => x && typeof x =='object';
12
9
 
13
10
  const mainProcessRef = {current:{}};
14
11
 
12
+ // fermee automatiquement quand l'objet JavaScript sera garbage collected.
13
+ let win = undefined;
14
+
15
+ let projectRoot = undefined;
16
+ let packageJSON = undefined;
17
+ let appName = undefined;
18
+
19
+ Menu.setApplicationMenu(null);
20
+
15
21
  module.exports = function(mainOptions){
16
22
  mainOptions = Object.assign({},mainOptions);
17
- let {packageJSON,projectRoot,appName,url:pUrl} = mainOptions;
18
- packageJSON = Object.assign({},packageJSON);
19
- // fermee automatiquement quand l'objet JavaScript sera garbage collected.
20
- let win = undefined;
21
- Menu.setApplicationMenu(null);
23
+ let {url:pUrl} = mainOptions;
24
+ projectRoot = mainOptions.projectRoot;
25
+ appName = mainOptions.appName;
26
+ packageJSON = Object.assign({},mainOptions.packageJSON);
22
27
 
23
28
  appName = typeof appName == "string" && appName || typeof packageJSON.realAppName =="string" && packageJSON.realAppName || typeof packageJSON.name =="string" && packageJSON.name || "";
24
29
  const indexFilePath = path.resolve(path.join(projectRoot,"dist",'index.html'));
@@ -31,218 +36,30 @@ module.exports = function(mainOptions){
31
36
  // Gardez une reference globale de l'objet window, si vous ne le faites pas, la fenetre sera
32
37
 
33
38
  if(!isValidUrl(pUrl) && !fs.existsSync(indexFilePath)){
34
- throw `Unable to start the application: index file located at [${indexFilePath}] does not exists : projectRoot = [${projectRoot}], isAsar:[${require.main}]`
39
+ throw {message:`Unable to start the application: index file located at [${indexFilePath}] does not exists : projectRoot = [${projectRoot}], isAsar:[${require.main}]`}
35
40
  }
36
-
37
- const clipboadContextMenu = (_, props) => {
38
- if (props.isEditable || props.selectionText) {
39
- const menu = new Menu();
40
- if(props.selectionText){
41
- menu.append(new MenuItem({ label: 'Copier', role: 'copy' }));
42
- if(props.isEditable){
43
- menu.append(new MenuItem({ label: 'Couper', role: 'cut' }));
44
- }
45
- }
46
- if(props.isEditable){
47
- menu.append(new MenuItem({ label: 'Coller', role: 'paste' }));
48
- }
49
- menu.popup();
50
- }
51
- };
52
-
53
- const quit = ()=>{
54
- try {
55
- app.quit();
56
- } catch(e){
57
- console.log(e," triing kit app")
58
- }
59
- }
60
- // Quitte l'application quand toutes les fenêtres sont fermées.
61
- app.on('window-all-closed', () => {
62
- // Sur macOS, il est commun pour une application et leur barre de menu
63
- // de rester active tant que l'utilisateur ne quitte pas explicitement avec Cmd + Q
64
- if (process.platform !== 'darwin') {
65
- quit();
66
- }
67
- })
68
41
 
69
- const toggleDevTools = (value,window)=>{
70
- window = window instanceof BrowserWindow ? window : win;
71
- if(window !==null && window.webContents){
72
- const isOpen= window.webContents.isDevToolsOpened();
73
- value = value === undefined ? !isOpen : value;
74
- if(value && !isOpen){
75
- window.webContents.openDevTools();
76
- return window.webContents.isDevToolsOpened();
77
- } else {
78
- if(isOpen) window.webContents.closeDevTools();
79
- }
80
- return window.webContents.isDevToolsOpened();
81
- }
82
- return false;
83
- }
84
- ipcMain.on("toggle-dev-tools",function(event,value) {
85
- return toggleDevTools(value);
86
- });
87
-
88
-
42
+ //app.disableHardwareAcceleration();
43
+
89
44
  app.whenReady().then(() => {
90
45
  const readOpts = {toggleDevTools,browserWindow:win,mainWindow:win};
91
46
  if(typeof mainProcess.whenAppReady =='function'){
92
47
  mainProcess.whenAppReady(readOpts);
93
48
  }
94
49
  globalShortcut.register('CommandOrControl+F12', () => {
95
- toggleDevTools();
50
+ return toggleDevTools();
96
51
  });
97
- }).then(()=>{
98
- createWindow();
99
52
  app.on('activate', function () {
100
53
  if (win == null || (BrowserWindow.getAllWindows().length === 0)) createWindow()
101
54
  });
102
- })
103
-
104
- ipcMain.on("restart-app",x =>{app.relaunch();})
105
- let tray = null;
106
- let isJSON = function (json_string){
107
- if(!json_string || typeof json_string != 'string') return false;
108
- var text = json_string;
109
- return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, '')));
110
- }
111
- ipcMain.on("update-system-tray",(event,opts)=>{
112
- opts = opts && typeof opts == 'object'? opts : {};
113
- let {contextMenu,tooltip} = opts;
114
- if(tray){
115
- } else {
116
- tray = new Tray();
117
- }
118
- if(!tooltip || typeof tooltip !=="string"){
119
- tooltip = ""
120
- }
121
- tray.setToolTip(tooltip);
122
- if(isJSON(contextMenu)){
123
- contextMenu = JSON.parse(contextMenu);
124
- }
125
- if(Array.isArray(contextMenu) && contextMenu.length) {
126
- let tpl = []
127
- contextMenu.map((m,index)=>{
128
- if(!m || typeof m !=='object') return;
129
- m.click = (e)=>{
130
- if(win && win.webContents) win.webContents.send("click-on-system-tray-menu-item",{
131
- action : m.action && typeof m.action =='string'? m.action : undefined,
132
- index,
133
- menuItem : JSON.stringify(m),
134
- })
135
- }
136
- tpl.push(m);
137
- })
138
- contextMenu = Menu.buildFromTemplate(tpl);
139
- } else contextMenu = null;
140
- tray.setContextMenu(contextMenu)
141
- })
142
- ipcMain.on("get-path",(event,pathName)=>{
143
- const p = app.getPath(pathName);
144
- event.returnValue = p;
145
- return p;
146
- });
147
-
148
- ipcMain.on("get-project-root",(event)=>{
149
- event.returnValue = projectRoot;
150
- return event.returnValue;
151
- });
152
- ipcMain.on("get-electron-project-root",(event)=>{
153
- event.returnValue = projectRoot;
154
- return event.returnValue ;
155
- });
156
-
157
- ipcMain.on("get-package.json",(event)=>{
158
- event.returnValue = JSON.stringify(packageJSON);
159
- return event.returnValue ;
160
- });
161
-
162
- ipcMain.on("get-app-name",(event)=>{
163
- event.returnValue = appName;
164
- return event.returnValue ;
165
- });
166
-
167
- ipcMain.on("get-media-access-status",(event,mediaType)=>{
168
- let p = systemPreferences.getMediaAccessStatus(mediaType);
169
- event.returnValue = p;
170
- return p;
171
- });
172
-
173
- ipcMain.on("ask-for-media-access",(event,mediaType)=>{
174
- systemPreferences.askForMediaAccess(mediaType);
175
- });
176
-
177
- ipcMain.on("get-app-icon",(event)=>{
178
- event.returnValue = win != win && win.getIcon && win.getIcon();
179
55
  });
180
- ipcMain.on("set-app-icon",(event,iconPath)=>{
181
- if(iconPath && win != null){
182
- win.setIcon(iconPath);
183
- event.returnValue = iconPath;
184
- } else {
185
- event.returnValue = null;
186
- }
187
- });
188
-
189
- ipcMain.on('minimize-main-window', () => {
190
- if(win !== null && win){
191
- win.blur();
192
- win.minimize();
193
- }
194
- })
195
- ipcMain.on('restore-main-window', () => {
196
- if(win && win !== null){
197
- win.restore()
198
- win.blur();
199
- setTimeout(() => {
200
- win.focus();
201
- win.moveTop();
202
- win.webContents.focus();
203
- }, 200);
204
- }
205
- })
206
- ipcMain.on('close-main-render-process', _ => {
207
- if(win){
208
- win.destroy();
209
- }
210
- win = null;
211
- if(typeof gc =="function"){
212
- gc();
213
- }
214
- quit();
215
- });
216
-
217
-
218
- function debounce(func, wait, immediate) {
219
- var timeout;
220
-
221
- return function executedFunction(...args) {
222
- var context = this;
223
-
224
- var later = function() {
225
- timeout = null;
226
- if (!immediate) func.apply(context, args);
227
- };
228
-
229
- var callNow = immediate && !timeout;
230
-
231
- clearTimeout(timeout);
232
-
233
- timeout = setTimeout(later, wait);
234
-
235
- if (callNow) func.apply(context, args);
236
- };
237
- };
238
- //app.disableHardwareAcceleration();
239
56
 
240
57
 
241
58
  const gotTheLock = app.requestSingleInstanceLock()
242
-
243
- if (!gotTheLock) {
244
- quit()
245
- } else {
59
+ /*if (!gotTheLock) {
60
+ //throw {message : "application want to quit heeein "+gotTheLock};
61
+ //quit()
62
+ } else*/ {
246
63
  app.on('second-instance', (event, commandLine, workingDirectory) => {
247
64
  // Someone tried to run a second instance, we should focus our window.
248
65
  //pour plus tard il sera possible d'afficher la gestion multi fenêtre en environnement electron
@@ -252,249 +69,6 @@ module.exports = function(mainOptions){
252
69
  }
253
70
  })
254
71
  }
255
-
256
- const powerMonitorCallbackEvent = (action)=>{
257
- if(!win || !win.webContents) return;
258
- if(action =="suspend" || action =="lock-screen"){
259
- win.webContents.send("main-app-suspended",action);
260
- return;
261
- }
262
- win.webContents.send("main-app-restaured",action);
263
- win.webContents.focus();
264
- return null;
265
- }
266
- if(powerMonitor){
267
- ["suspend","resume","lock-screen","unlock-screen"].map((action)=>{
268
- powerMonitor.on(action,(event)=>{
269
- powerMonitorCallbackEvent(action,event);
270
- })
271
- })
272
- }
273
- ipcMain.on("set-main-window-title",(event,title)=>{
274
- if(win !== null){
275
- win.setTitle(title);
276
- }
277
- })
278
-
279
-
280
- ipcMain.handle("create-browser-windows",function(event,options){
281
- if(typeof options =='string'){
282
- try {
283
- const t = JSON.parse(options);
284
- options = t;
285
- } catch{}
286
- }
287
- options = Object.assign({},options);
288
- createBrowserWindow(options);
289
- })
290
-
291
- ipcMain.handle("show-open-dialog",function(event,options){
292
- if(!isObj(options)){
293
- options = {};
294
- }
295
- return dialog.showOpenDialog(win,options)
296
- })
297
-
298
- ipcMain.handle("show-save-dialog",function(event,options){
299
- if(!isObj(options)){
300
- options = {};
301
- }
302
- return dialog.showSaveDialog(win,options)
303
- })
304
- ipcMain.on("is-dev-tools-open",function(event,value) {
305
- if(win !==null && win.webContents){
306
- return win.webContents.isDevToolsOpened();
307
- }
308
- return false;
309
- });
310
-
311
-
312
- ipcMain.on("window-set-progressbar",(event,interval)=>{
313
- if(typeof interval !="number" || interval <0) interval = 0;
314
- interval = Math.floor(interval);
315
- if(win){
316
- win.setProgressBar(interval);
317
- }
318
- })
319
-
320
- const setOSTheme = (theme) => {
321
- theme = theme && typeof theme == "string"? theme : "light";
322
- theme = theme.toLowerCase().trim();
323
- if(theme !== 'system' && theme !=='dark'){
324
- theme = "light";
325
- }
326
- nativeTheme.themeSource = theme;
327
- session.set("os-theme",theme);
328
- return nativeTheme.shouldUseDarkColors
329
- }
330
-
331
- /**** customisation des thèmes de l'application */
332
- ipcMain.handle('set-system-theme:toggle', (event,theme) => {
333
- return setOSTheme(theme);
334
- });
335
-
336
- ipcMain.handle('set-system-theme:dark-mode', (event) => {
337
- nativeTheme.themeSource = 'dark';
338
- return nativeTheme.shouldUseDarkColors;
339
- });
340
- ipcMain.handle('set-system-theme:light-mode', (event) => {
341
- nativeTheme.themeSource = 'light';
342
- return nativeTheme.shouldUseDarkColors;
343
- });
344
-
345
-
346
- ipcMain.handle('dark-mode:toggle', () => {
347
- if (nativeTheme.shouldUseDarkColors) {
348
- nativeTheme.themeSource = 'light'
349
- } else {
350
- nativeTheme.themeSource = 'dark'
351
- }
352
- return nativeTheme.shouldUseDarkColors
353
- })
354
-
355
- ipcMain.handle('dark-mode:system', () => {
356
- nativeTheme.themeSource = 'system'
357
- });
358
- function createWindow () {
359
- // Créer le browser window
360
- win = createBrowserWindow({
361
- showOnLoad : false,
362
- loadURL : undefined,
363
- isMainWindow : true,
364
- registerDevToolsCommand : false,
365
- preload : path.resolve(__dirname,'preload.js'),
366
- webPreferences : {
367
- devTools : true,
368
- }
369
- });
370
-
371
- const sOptions = {width: 500, height: 400, transparent: true, frame: false, alwaysOnTop: true};
372
- const splash = typeof mainProcess.splashScreen ==='function'&& mainProcess.splashScreen(sOptions)
373
- || typeof mainProcess.splash ==='function' && mainProcess.splash(sOptions)
374
- || (mainProcess.splash instanceof BrowserWindow) && mainProcess.splash
375
- || (mainProcess.splashScreen instanceof BrowserWindow) && mainProcess.splashScreen;
376
- null;
377
- let hasInitWindows = false;
378
- win.on('show', () => {
379
- //win.blur();
380
- setTimeout(() => {
381
- win.focus();
382
- win.moveTop();
383
- win.webContents.focus();
384
- if(!hasInitWindows){
385
- hasInitWindows = true;
386
- win.webContents.send('appReady');
387
- }
388
- }, 200);
389
- });
390
-
391
- win.on("focus",()=>{
392
- if(win && hasInitWindows){
393
- win.webContents.send("main-window-focus");
394
- }
395
- });
396
- win.on("blur",()=>{
397
- if(win && hasInitWindows){
398
- win.webContents.send("main-window-blur");
399
- }
400
- });
401
-
402
- win.once("ready-to-show",function(){
403
- if(typeof mainProcess.onMainWindowReadyToShow ==='function'){
404
- mainProcess.onMainWindowReadyToShow(win);
405
- }
406
- win.minimize()
407
- try {
408
- if(splash && splash instanceof BrowserWindow){
409
- splash.destroy();
410
- }
411
- } catch{ }
412
- win.restore();
413
- win.show();
414
- //log(icon," is consooleeeee")
415
- })
416
-
417
- win.on('close', (e) => {
418
- if (win) {
419
- if(typeof mainProcess.onMainWindowClose == "function"){
420
- mainProcess.onMainWindowClose(win);
421
- }
422
- e.preventDefault();
423
- win.webContents.send('before-app-exit');
424
- }
425
- });
426
- if(isValidUrl(pUrl)){
427
- win.loadURL(pUrl);
428
- } else {
429
- win.loadFile(indexFilePath)
430
- }
431
-
432
- win.on('unresponsive', async () => {
433
- const { response } = await dialog.showMessageBox({
434
- title: "L'application a cessé de répondre",
435
- message : 'Voulez vous relancer l\'application?',
436
- buttons: ['Relancer', 'Arrêter'],
437
- cancelId: 1
438
- })
439
- if (response === 0) {
440
- win.forcefullyCrashRenderer()
441
- win.reload()
442
- } else {
443
- win.forcefullyCrashRenderer()
444
- app.exit();
445
- }
446
- })
447
-
448
- // Ouvre les DevTools.
449
- //win.webContents.openDevTools()
450
-
451
- // Émit lorsque la fenêtre est fermée.
452
- win.on('closed', () => {
453
- win = null
454
- })
455
- win.webContents.on('context-menu',clipboadContextMenu);
456
- win.setMenu(null);
457
-
458
- /*** les dimenssions de la fenêtre principale */
459
- let mWindowSessinName = "mainWindowSizes";
460
- let mWindowPositionSName = mWindowSessinName+"-positions";
461
- let sizeW = session.get(mWindowSessinName);
462
- if(!sizeW || typeof sizeW !== 'object'){
463
- sizeW = {};
464
- }
465
- let sPositions = session.get(mWindowPositionSName);
466
- if(!sPositions || typeof sPositions !=='object'){
467
- sPositions = {};
468
- }
469
- let isNumber = x => typeof x =="number";
470
- if(isNumber(sizeW.width) && isNumber(sizeW.height)){
471
- win.setSize(sizeW.width,sizeW.height);
472
- if(isNumber(sPositions.x) && isNumber(sPositions.y)){
473
- win.setPosition(sPositions.x,sPositions.y);
474
- }
475
- }
476
- const onWinResizeEv = debounce(function () {
477
- if(win){
478
- let wSize = win.getSize();
479
- if(Array.isArray(wSize) && wSize.length == 2){
480
- let [width,height] = wSize;
481
- if(width && height){
482
- session.set(mWindowSessinName,{width,height});
483
- }
484
- let [x,y] = win.getPosition();
485
- session.set(mWindowPositionSName,{x,y});
486
- }
487
- }
488
- }, 100);
489
- win.off('resize',onWinResizeEv);
490
- win.on('resize',onWinResizeEv);
491
- win.off('move',onWinResizeEv);
492
- win.on('move',onWinResizeEv);
493
- if(typeof mainProcess.onCreateMainWindow =='function'){
494
- mainProcess.onCreateMainWindow(win);
495
- }
496
- return win;
497
- }
498
72
  }
499
73
 
500
74
  function createBrowserWindow (options){
@@ -561,7 +135,423 @@ function createBrowserWindow (options){
561
135
  }
562
136
  _win = null;
563
137
  });
138
+ _win.webContents.on('context-menu',clipboadContextMenu);
564
139
  return _win;
565
140
  }
566
141
 
142
+
143
+ function createWindow () {
144
+ const mainProcess = mainProcessRef.current;
145
+ // Créer le browser window
146
+ win = createBrowserWindow({
147
+ showOnLoad : false,
148
+ loadURL : undefined,
149
+ isMainWindow : true,
150
+ registerDevToolsCommand : false,
151
+ preload : path.resolve(__dirname,'preload.js'),
152
+ webPreferences : {
153
+ devTools : true,
154
+ }
155
+ });
156
+ const sOptions = {width: 500, height: 400, transparent: true, frame: false, alwaysOnTop: true};
157
+ const splash = typeof mainProcess.splashScreen ==='function'&& mainProcess.splashScreen(sOptions)
158
+ || typeof mainProcess.splash ==='function' && mainProcess.splash(sOptions)
159
+ || (mainProcess.splash instanceof BrowserWindow) && mainProcess.splash
160
+ || (mainProcess.splashScreen instanceof BrowserWindow) && mainProcess.splashScreen;
161
+ null;
162
+ let hasInitWindows = false;
163
+ win.on('show', () => {
164
+ //win.blur();
165
+ setTimeout(() => {
166
+ win.focus();
167
+ win.moveTop();
168
+ win.webContents.focus();
169
+ if(!hasInitWindows){
170
+ hasInitWindows = true;
171
+ win.webContents.send('appReady');
172
+ }
173
+ }, 200);
174
+ });
175
+
176
+ win.on("focus",()=>{
177
+ if(win && hasInitWindows){
178
+ win.webContents.send("main-window-focus");
179
+ }
180
+ });
181
+ win.on("blur",()=>{
182
+ if(win && hasInitWindows){
183
+ win.webContents.send("main-window-blur");
184
+ }
185
+ });
186
+
187
+ win.once("ready-to-show",function(){
188
+ if(typeof mainProcess.onMainWindowReadyToShow ==='function'){
189
+ mainProcess.onMainWindowReadyToShow(win);
190
+ }
191
+ win.minimize()
192
+ try {
193
+ if(splash && splash instanceof BrowserWindow){
194
+ splash.destroy();
195
+ }
196
+ } catch{ }
197
+ win.restore();
198
+ win.show();
199
+ //log(icon," is consooleeeee")
200
+ })
201
+
202
+ win.on('close', (e) => {
203
+ if (win) {
204
+ if(typeof mainProcess.onMainWindowClose == "function"){
205
+ mainProcess.onMainWindowClose(win);
206
+ }
207
+ e.preventDefault();
208
+ win.webContents.send('before-app-exit');
209
+ }
210
+ });
211
+ if(isValidUrl(pUrl)){
212
+ win.loadURL(pUrl);
213
+ } else {
214
+ win.loadFile(indexFilePath)
215
+ }
216
+
217
+ win.on('unresponsive', async () => {
218
+ const { response } = await dialog.showMessageBox({
219
+ title: "L'application a cessé de répondre",
220
+ message : 'Voulez vous relancer l\'application?',
221
+ buttons: ['Relancer', 'Arrêter'],
222
+ cancelId: 1
223
+ });
224
+ if (response === 0) {
225
+ win.forcefullyCrashRenderer()
226
+ win.reload()
227
+ } else {
228
+ win.forcefullyCrashRenderer()
229
+ app.exit();
230
+ }
231
+ });
232
+
233
+ // Émit lorsque la fenêtre est fermée.
234
+ win.on('closed', () => {
235
+ win = null
236
+ })
237
+ win.setMenu(null);
238
+
239
+ /*** les dimenssions de la fenêtre principale */
240
+ let mWindowSessinName = "mainWindowSizes";
241
+ let mWindowPositionSName = mWindowSessinName+"-positions";
242
+ let sizeW = session.get(mWindowSessinName);
243
+ if(!sizeW || typeof sizeW !== 'object'){
244
+ sizeW = {};
245
+ }
246
+ let sPositions = session.get(mWindowPositionSName);
247
+ if(!sPositions || typeof sPositions !=='object'){
248
+ sPositions = {};
249
+ }
250
+ let isNumber = x => typeof x =="number";
251
+ if(isNumber(sizeW.width) && isNumber(sizeW.height)){
252
+ win.setSize(sizeW.width,sizeW.height);
253
+ if(isNumber(sPositions.x) && isNumber(sPositions.y)){
254
+ win.setPosition(sPositions.x,sPositions.y);
255
+ }
256
+ }
257
+ const onWinResizeEv = debounce(function () {
258
+ if(win){
259
+ let wSize = win.getSize();
260
+ if(Array.isArray(wSize) && wSize.length == 2){
261
+ let [width,height] = wSize;
262
+ if(width && height){
263
+ session.set(mWindowSessinName,{width,height});
264
+ }
265
+ let [x,y] = win.getPosition();
266
+ session.set(mWindowPositionSName,{x,y});
267
+ }
268
+ }
269
+ }, 100);
270
+ win.off('resize',onWinResizeEv);
271
+ win.on('resize',onWinResizeEv);
272
+ win.off('move',onWinResizeEv);
273
+ win.on('move',onWinResizeEv);
274
+ if(typeof mainProcess.onCreateMainWindow =='function'){
275
+ mainProcess.onCreateMainWindow(win);
276
+ }
277
+ return win;
278
+ }
279
+
280
+
281
+
282
+ const toggleDevTools = (value)=>{
283
+ if(win !==null && win.webContents){
284
+ const isOpen= win.webContents.isDevToolsOpened();
285
+ value = value === undefined ? !isOpen : value;
286
+ if(value && !isOpen){
287
+ win.webContents.openDevTools();
288
+ return win.webContents.isDevToolsOpened();
289
+ } else {
290
+ if(isOpen) win.webContents.closeDevTools();
291
+ }
292
+ return win.webContents.isDevToolsOpened();
293
+ }
294
+ return false;
295
+ }
296
+ ipcMain.on("toggle-dev-tools",function(event,value) {
297
+ return toggleDevTools(value);
298
+ });
299
+
300
+ ipcMain.handle("create-browser-windows",function(event,options){
301
+ if(typeof options =='string'){
302
+ try {
303
+ const t = JSON.parse(options);
304
+ options = t;
305
+ } catch{}
306
+ }
307
+ options = Object.assign({},options);
308
+ createBrowserWindow(options);
309
+ });
310
+
311
+ ipcMain.on("restart-app",x =>{app.relaunch();})
312
+ let tray = null;
313
+ let isJSON = function (json_string){
314
+ if(!json_string || typeof json_string != 'string') return false;
315
+ var text = json_string;
316
+ return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, '')));
317
+ }
318
+ ipcMain.on("update-system-tray",(event,opts)=>{
319
+ opts = opts && typeof opts == 'object'? opts : {};
320
+ let {contextMenu,tooltip} = opts;
321
+ if(tray){
322
+ } else {
323
+ tray = new Tray();
324
+ }
325
+ if(!tooltip || typeof tooltip !=="string"){
326
+ tooltip = ""
327
+ }
328
+ tray.setToolTip(tooltip);
329
+ if(isJSON(contextMenu)){
330
+ contextMenu = JSON.parse(contextMenu);
331
+ }
332
+ if(Array.isArray(contextMenu) && contextMenu.length) {
333
+ let tpl = []
334
+ contextMenu.map((m,index)=>{
335
+ if(!m || typeof m !=='object') return;
336
+ m.click = (e)=>{
337
+ if(win && win.webContents) win.webContents.send("click-on-system-tray-menu-item",{
338
+ action : m.action && typeof m.action =='string'? m.action : undefined,
339
+ index,
340
+ menuItem : JSON.stringify(m),
341
+ })
342
+ }
343
+ tpl.push(m);
344
+ })
345
+ contextMenu = Menu.buildFromTemplate(tpl);
346
+ } else contextMenu = null;
347
+ tray.setContextMenu(contextMenu)
348
+ })
349
+ ipcMain.on("get-path",(event,pathName)=>{
350
+ const p = app.getPath(pathName);
351
+ event.returnValue = p;
352
+ return p;
353
+ });
354
+
355
+ ipcMain.on("get-project-root",(event)=>{
356
+ event.returnValue = projectRoot;
357
+ return event.returnValue;
358
+ });
359
+ ipcMain.on("get-electron-project-root",(event)=>{
360
+ event.returnValue = projectRoot;
361
+ return event.returnValue ;
362
+ });
363
+
364
+ ipcMain.on("get-package.json",(event)=>{
365
+ event.returnValue = JSON.stringify(packageJSON);
366
+ return event.returnValue ;
367
+ });
368
+
369
+ ipcMain.on("get-app-name",(event)=>{
370
+ event.returnValue = appName;
371
+ return event.returnValue ;
372
+ });
373
+
374
+ ipcMain.on("get-media-access-status",(event,mediaType)=>{
375
+ let p = systemPreferences.getMediaAccessStatus(mediaType);
376
+ event.returnValue = p;
377
+ return p;
378
+ });
379
+
380
+ ipcMain.on("ask-for-media-access",(event,mediaType)=>{
381
+ systemPreferences.askForMediaAccess(mediaType);
382
+ });
383
+
384
+ ipcMain.on("get-app-icon",(event)=>{
385
+ event.returnValue = win != win && win.getIcon && win.getIcon();
386
+ });
387
+ ipcMain.on("set-app-icon",(event,iconPath)=>{
388
+ if(iconPath && win != null){
389
+ win.setIcon(iconPath);
390
+ event.returnValue = iconPath;
391
+ } else {
392
+ event.returnValue = null;
393
+ }
394
+ });
395
+
396
+ ipcMain.on('minimize-main-window', () => {
397
+ if(win !== null && win){
398
+ win.blur();
399
+ win.minimize();
400
+ }
401
+ })
402
+ ipcMain.on('restore-main-window', () => {
403
+ if(win && win !== null){
404
+ win.restore()
405
+ win.blur();
406
+ setTimeout(() => {
407
+ win.focus();
408
+ win.moveTop();
409
+ win.webContents.focus();
410
+ }, 200);
411
+ }
412
+ })
413
+ ipcMain.on('close-main-render-process', _ => {
414
+ if(win){
415
+ win.destroy();
416
+ }
417
+ win = null;
418
+ if(typeof gc =="function"){
419
+ gc();
420
+ }
421
+ quit();
422
+ });
423
+
424
+ const quit = ()=>{
425
+ try {
426
+ app.quit();
427
+ } catch(e){
428
+ console.log(e," triing kit app")
429
+ }
430
+ }
431
+
432
+
433
+ const powerMonitorCallbackEvent = (action)=>{
434
+ if(!win || !win.webContents) return;
435
+ if(action =="suspend" || action =="lock-screen"){
436
+ win.webContents.send("main-app-suspended",action);
437
+ return;
438
+ }
439
+ win.webContents.send("main-app-restaured",action);
440
+ win.webContents.focus();
441
+ return null;
442
+ }
443
+ if(powerMonitor){
444
+ ["suspend","resume","lock-screen","unlock-screen"].map((action)=>{
445
+ powerMonitor.on(action,(event)=>{
446
+ powerMonitorCallbackEvent(action,event);
447
+ })
448
+ })
449
+ }
450
+ ipcMain.on("set-main-window-title",(event,title)=>{
451
+ if(win !== null){
452
+ win.setTitle(title);
453
+ }
454
+ });
455
+
456
+ ipcMain.handle("show-open-dialog",function(event,options){
457
+ if(typeof options =="string"){
458
+ try {
459
+ const t = JSON.parse(options);
460
+ options = t;
461
+ } catch{}
462
+ }
463
+ if(!isObj(options)){
464
+ options = {};
465
+ }
466
+ return dialog.showOpenDialog(win,options)
467
+ })
468
+
469
+ ipcMain.handle("show-save-dialog",function(event,options){
470
+ if(!isObj(options)){
471
+ options = {};
472
+ }
473
+ return dialog.showSaveDialog(win,options)
474
+ });
475
+
476
+ ipcMain.on("is-dev-tools-open",function(event,value) {
477
+ if(win !==null && win.webContents){
478
+ return win.webContents.isDevToolsOpened();
479
+ }
480
+ return false;
481
+ });
482
+
483
+ ipcMain.on("window-set-progressbar",(event,interval)=>{
484
+ if(typeof interval !="number" || interval <0) interval = 0;
485
+ interval = Math.floor(interval);
486
+ if(win){
487
+ win.setProgressBar(interval);
488
+ }
489
+ })
490
+
491
+ const setOSTheme = (theme) => {
492
+ theme = theme && typeof theme == "string"? theme : "light";
493
+ theme = theme.toLowerCase().trim();
494
+ if(theme !== 'system' && theme !=='dark'){
495
+ theme = "light";
496
+ }
497
+ nativeTheme.themeSource = theme;
498
+ session.set("os-theme",theme);
499
+ return nativeTheme.shouldUseDarkColors
500
+ }
501
+
502
+ /**** customisation des thèmes de l'application */
503
+ ipcMain.handle('set-system-theme:toggle', (event,theme) => {
504
+ return setOSTheme(theme);
505
+ });
506
+
507
+ ipcMain.handle('set-system-theme:dark-mode', (event) => {
508
+ nativeTheme.themeSource = 'dark';
509
+ return nativeTheme.shouldUseDarkColors;
510
+ });
511
+ ipcMain.handle('set-system-theme:light-mode', (event) => {
512
+ nativeTheme.themeSource = 'light';
513
+ return nativeTheme.shouldUseDarkColors;
514
+ });
515
+
516
+
517
+ ipcMain.handle('dark-mode:toggle', () => {
518
+ if (nativeTheme.shouldUseDarkColors) {
519
+ nativeTheme.themeSource = 'light'
520
+ } else {
521
+ nativeTheme.themeSource = 'dark'
522
+ }
523
+ return nativeTheme.shouldUseDarkColors
524
+ })
525
+
526
+ ipcMain.handle('dark-mode:system', () => {
527
+ nativeTheme.themeSource = 'system'
528
+ });
529
+
530
+ const clipboadContextMenu = (_, props) => {
531
+ if (props.isEditable || props.selectionText) {
532
+ const menu = new Menu();
533
+ if(props.selectionText){
534
+ menu.append(new MenuItem({ label: 'Copier', role: 'copy' }));
535
+ if(props.isEditable){
536
+ menu.append(new MenuItem({ label: 'Couper', role: 'cut' }));
537
+ }
538
+ }
539
+ if(props.isEditable){
540
+ menu.append(new MenuItem({ label: 'Coller', role: 'paste' }));
541
+ }
542
+ menu.popup();
543
+ }
544
+ };
545
+
546
+ // Quitte l'application quand toutes les fenêtres sont fermées.
547
+ app.on('window-all-closed', () => {
548
+ // Sur macOS, il est commun pour une application et leur barre de menu
549
+ // de rester active tant que l'utilisateur ne quitte pas explicitement avec Cmd + Q
550
+ if (process.platform !== 'darwin') {
551
+ quit();
552
+ }
553
+ });
554
+
555
+
556
+
567
557
  module.exports.createBrowserWindow = createBrowserWindow;