app.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. import * as base from './base.js';
  2. import * as electron from 'electron';
  3. import * as fs from 'fs';
  4. import * as os from 'os';
  5. import * as path from 'path';
  6. import * as process from 'process';
  7. import * as updater from 'electron-updater';
  8. import * as url from 'url';
  9. const app = {};
  10. app.Application = class {
  11. constructor() {
  12. this._views = new app.ViewCollection(this);
  13. this._configuration = new app.ConfigurationService();
  14. this._menu = new app.MenuService(this._views);
  15. this._openQueue = [];
  16. this._package = {};
  17. }
  18. async start() {
  19. const dirname = path.dirname(url.fileURLToPath(import.meta.url));
  20. const packageFile = path.join(path.dirname(dirname), 'package.json');
  21. const packageContent = fs.readFileSync(packageFile, 'utf-8');
  22. this._package = JSON.parse(packageContent);
  23. electron.app.setAppUserModelId('com.lutzroeder.netron');
  24. electron.app.allowRendererProcessReuse = true;
  25. if (!electron.app.requestSingleInstanceLock()) {
  26. electron.app.quit();
  27. return;
  28. }
  29. electron.app.on('second-instance', (event, commandLine, workingDirectory) => {
  30. const currentDirectory = process.cwd();
  31. process.chdir(workingDirectory);
  32. const open = this._parseCommandLine(commandLine);
  33. process.chdir(currentDirectory);
  34. if (!open && !this._views.empty) {
  35. const view = this._views.first();
  36. if (view) {
  37. view.restore();
  38. }
  39. }
  40. });
  41. electron.ipcMain.on('get-environment', (event) => {
  42. event.returnValue = this.environment;
  43. });
  44. electron.ipcMain.on('get-configuration', (event, obj) => {
  45. event.returnValue = this._configuration.has(obj.name) ? this._configuration.get(obj.name) : undefined;
  46. });
  47. electron.ipcMain.on('set-configuration', (event, obj) => {
  48. this._configuration.set(obj.name, obj.value);
  49. this._configuration.save();
  50. event.returnValue = null;
  51. });
  52. electron.ipcMain.on('delete-configuration', (event, obj) => {
  53. this._configuration.delete(obj.name);
  54. this._configuration.save();
  55. event.returnValue = null;
  56. });
  57. electron.ipcMain.on('drop-paths', (event, data) => {
  58. const paths = data.paths.filter((path) => {
  59. if (fs.existsSync(path)) {
  60. const stat = fs.statSync(path);
  61. return stat.isFile() || stat.isDirectory();
  62. }
  63. return false;
  64. });
  65. this._dropPaths(event.sender, paths);
  66. event.returnValue = null;
  67. });
  68. electron.ipcMain.on('update-recents', (event, data) => {
  69. this._updateRecents(data.path);
  70. event.returnValue = null;
  71. });
  72. electron.ipcMain.on('show-save-dialog', async (event, options) => {
  73. const owner = event.sender.getOwnerBrowserWindow();
  74. const argument = {};
  75. try {
  76. const { filePath, canceled } = await electron.dialog.showSaveDialog(owner, options);
  77. argument.filePath = filePath;
  78. argument.canceled = canceled;
  79. } catch (error) {
  80. argument.error = error.message;
  81. }
  82. event.sender.send('show-save-dialog-complete', argument);
  83. });
  84. electron.ipcMain.on('execute', async (event, data) => {
  85. const owner = event.sender.getOwnerBrowserWindow();
  86. const argument = {};
  87. try {
  88. argument.value = await this.execute(data.name, data.value || null, owner);
  89. } catch (error) {
  90. argument.error = error.message;
  91. }
  92. event.sender.send('execute-complete', argument);
  93. });
  94. electron.app.on('will-finish-launching', () => {
  95. electron.app.on('open-file', (event, path) => {
  96. this._openPath(path);
  97. });
  98. });
  99. electron.app.on('ready', () => {
  100. this._ready();
  101. });
  102. electron.app.on('window-all-closed', () => {
  103. if (process.platform !== 'darwin') {
  104. electron.app.quit();
  105. }
  106. });
  107. electron.app.on('will-quit', () => {
  108. this._configuration.save();
  109. });
  110. this._parseCommandLine(process.argv);
  111. await this._checkForUpdates();
  112. }
  113. get environment() {
  114. this._environment = this._environment || {
  115. packaged: electron.app.isPackaged,
  116. name: this._package.productName,
  117. version: this._package.version,
  118. date: this._package.date,
  119. repository: `https://github.com/${this._package.repository}`,
  120. platform: process.platform,
  121. separator: path.sep,
  122. titlebar: true // process.platform === 'darwin'
  123. };
  124. return this._environment;
  125. }
  126. _parseCommandLine(argv) {
  127. let open = false;
  128. if (argv.length > 1) {
  129. for (const arg of argv.slice(1)) {
  130. const dirname = path.dirname(url.fileURLToPath(import.meta.url));
  131. if (!arg.startsWith('-') && arg !== path.dirname(dirname)) {
  132. const extension = path.extname(arg).toLowerCase();
  133. if (extension !== '' && extension !== '.js' && fs.existsSync(arg)) {
  134. const stat = fs.statSync(arg);
  135. if (stat.isFile() || stat.isDirectory()) {
  136. this._openPath(arg);
  137. open = true;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. return open;
  144. }
  145. _ready() {
  146. this._configuration.open();
  147. if (this._openQueue) {
  148. const queue = this._openQueue;
  149. this._openQueue = null;
  150. while (queue.length > 0) {
  151. const file = queue.shift();
  152. this._openPath(file);
  153. }
  154. }
  155. if (this._views.empty) {
  156. this._views.openView();
  157. }
  158. this._updateMenu();
  159. this._views.on('active-view-changed', () => {
  160. this._menu.update();
  161. });
  162. this._views.on('active-view-updated', () => {
  163. this._menu.update();
  164. });
  165. }
  166. _open(path) {
  167. let paths = path ? [path] : [];
  168. if (paths.length === 0) {
  169. const extensions = new base.Metadata().extensions;
  170. const options = {
  171. properties: ['openFile'],
  172. filters: [{ name: 'All Model Files', extensions }]
  173. };
  174. const owner = electron.BrowserWindow.getFocusedWindow();
  175. paths = electron.dialog.showOpenDialogSync(owner, options);
  176. }
  177. if (Array.isArray(paths) && paths.length > 0) {
  178. for (const path of paths) {
  179. this._openPath(path);
  180. }
  181. }
  182. }
  183. _openPath(path) {
  184. if (this._openQueue) {
  185. this._openQueue.push(path);
  186. return;
  187. }
  188. if (path && path.length > 0) {
  189. const exists = fs.existsSync(path);
  190. if (exists) {
  191. const stat = fs.statSync(path);
  192. if (stat.isFile() || stat.isDirectory()) {
  193. const views = Array.from(this._views.views);
  194. // find existing view for this file
  195. let view = views.find((view) => view.match(path));
  196. // find empty welcome window
  197. if (!view) {
  198. view = views.find((view) => view.match(null));
  199. }
  200. // create new window
  201. if (!view) {
  202. view = this._views.openView();
  203. }
  204. view.open(path);
  205. }
  206. }
  207. this._updateRecents(exists ? path : undefined);
  208. }
  209. }
  210. _dropPaths(sender, paths) {
  211. const window = sender.getOwnerBrowserWindow();
  212. let view = this._views.get(window);
  213. for (const path of paths) {
  214. if (view) {
  215. view.open(path);
  216. view = null;
  217. } else {
  218. this._openPath(path);
  219. }
  220. }
  221. }
  222. async _export() {
  223. const view = this._views.activeView;
  224. if (view && view.path) {
  225. let defaultPath = 'Untitled';
  226. const file = view.path;
  227. const lastIndex = file.lastIndexOf('.');
  228. if (lastIndex !== -1) {
  229. defaultPath = file.substring(0, lastIndex);
  230. }
  231. const owner = electron.BrowserWindow.getFocusedWindow();
  232. const options = {
  233. title: 'Export',
  234. defaultPath,
  235. buttonLabel: 'Export',
  236. filters: [
  237. { name: 'PNG', extensions: ['png'] },
  238. { name: 'SVG', extensions: ['svg'] }
  239. ]
  240. };
  241. const { filePath, canceled } = await electron.dialog.showSaveDialog(owner, options);
  242. if (filePath && !canceled) {
  243. view.execute('export', { 'file': filePath });
  244. }
  245. }
  246. }
  247. async execute(command, value, window) {
  248. switch (command) {
  249. case 'open': this._open(value); break;
  250. case 'export': await this._export(); break;
  251. case 'close': window.close(); break;
  252. case 'quit': electron.app.quit(); break;
  253. case 'reload': this._reload(); break;
  254. case 'report-issue': electron.shell.openExternal(`https://github.com/${this._package.repository}/issues/new`); break;
  255. case 'about': this._about(); break;
  256. default: {
  257. const view = this._views.get(window) || this._views.activeView;
  258. if (view) {
  259. view.execute(command, value || {});
  260. }
  261. this._menu.update();
  262. }
  263. }
  264. }
  265. _reload() {
  266. const view = this._views.activeView;
  267. if (view && view.path) {
  268. view.open(view.path);
  269. }
  270. }
  271. async _checkForUpdates() {
  272. if (!electron.app.isPackaged) {
  273. return;
  274. }
  275. const autoUpdater = updater.default.autoUpdater;
  276. if (autoUpdater.app && autoUpdater.app.appUpdateConfigPath && !fs.existsSync(autoUpdater.app.appUpdateConfigPath)) {
  277. return;
  278. }
  279. const promise = autoUpdater.checkForUpdates();
  280. if (promise) {
  281. promise.catch((error) => {
  282. /* eslint-disable no-console */
  283. console.log(error.message);
  284. /* eslint-enable no-console */
  285. });
  286. }
  287. }
  288. _about() {
  289. let view = this._views.activeView;
  290. if (!view) {
  291. view = this._views.openView();
  292. }
  293. view.execute('about');
  294. }
  295. _updateRecents(path) {
  296. let updated = false;
  297. let recents = this._configuration.has('recents') ? this._configuration.get('recents') : [];
  298. if (path && (recents.length === 0 || recents[0] !== path)) {
  299. recents = recents.filter((recent) => path !== recent);
  300. recents.unshift(path);
  301. updated = true;
  302. }
  303. const value = [];
  304. for (const recent of recents) {
  305. if (value.length >= 9) {
  306. updated = true;
  307. break;
  308. }
  309. if (!fs.existsSync(recent)) {
  310. updated = true;
  311. continue;
  312. }
  313. const stat = fs.statSync(recent);
  314. if (!stat.isFile() && !stat.isDirectory()) {
  315. updated = true;
  316. continue;
  317. }
  318. value.push(recent);
  319. }
  320. if (updated) {
  321. this._configuration.set('recents', value);
  322. this._updateMenu();
  323. }
  324. }
  325. _updateMenu() {
  326. let recents = [];
  327. if (this._configuration.has('recents')) {
  328. const value = this._configuration.get('recents');
  329. recents = value.map((recent) => app.Application.location(recent));
  330. }
  331. if (this.environment.titlebar && recents.length > 0) {
  332. for (const view of this._views.views) {
  333. view.execute('recents', recents);
  334. }
  335. }
  336. const darwin = process.platform === 'darwin';
  337. if (!this.environment.titlebar || darwin) {
  338. const menuRecentsTemplate = [];
  339. for (let i = 0; i < recents.length; i++) {
  340. const recent = recents[i];
  341. menuRecentsTemplate.push({
  342. path: recent.path,
  343. label: recent.label,
  344. accelerator: (darwin ? 'Cmd+' : 'Ctrl+') + (i + 1).toString(),
  345. click: (item) => this._openPath(item.path)
  346. });
  347. }
  348. const menuTemplate = [];
  349. if (darwin) {
  350. menuTemplate.unshift({
  351. label: electron.app.name,
  352. submenu: [
  353. {
  354. label: `About ${electron.app.name}`,
  355. click: () => /* this.execute('about', null) */ this._about()
  356. },
  357. { type: 'separator' },
  358. { role: 'hide' },
  359. { role: 'hideothers' },
  360. { role: 'unhide' },
  361. { type: 'separator' },
  362. { role: 'quit' }
  363. ]
  364. });
  365. }
  366. menuTemplate.push({
  367. label: '&File',
  368. submenu: [
  369. {
  370. label: '&Open...',
  371. accelerator: 'CmdOrCtrl+O',
  372. click: () => this._open(null)
  373. },
  374. {
  375. label: 'Open &Recent',
  376. submenu: menuRecentsTemplate
  377. },
  378. { type: 'separator' },
  379. {
  380. id: 'file.export',
  381. label: '&Export...',
  382. accelerator: 'CmdOrCtrl+Shift+E',
  383. click: async () => await this.execute('export', null)
  384. },
  385. { type: 'separator' },
  386. { role: 'close' },
  387. ]
  388. });
  389. if (!darwin) {
  390. menuTemplate.slice(-1)[0].submenu.push(
  391. { type: 'separator' },
  392. { role: 'quit' }
  393. );
  394. }
  395. if (darwin) {
  396. electron.systemPreferences.setUserDefault('NSDisabledDictationMenuItem', 'boolean', true);
  397. electron.systemPreferences.setUserDefault('NSDisabledCharacterPaletteMenuItem', 'boolean', true);
  398. }
  399. menuTemplate.push({
  400. label: '&Edit',
  401. submenu: [
  402. {
  403. id: 'edit.cut',
  404. label: 'Cu&t',
  405. accelerator: 'CmdOrCtrl+X',
  406. click: async () => await this.execute('cut', null),
  407. },
  408. {
  409. id: 'edit.copy',
  410. label: '&Copy',
  411. accelerator: 'CmdOrCtrl+C',
  412. click: async () => await this.execute('copy', null),
  413. },
  414. {
  415. id: 'edit.paste',
  416. label: '&Paste',
  417. accelerator: 'CmdOrCtrl+V',
  418. click: async () => await this.execute('paste', null),
  419. },
  420. {
  421. id: 'edit.select-all',
  422. label: 'Select &All',
  423. accelerator: 'CmdOrCtrl+A',
  424. click: async () => await this.execute('selectall', null),
  425. },
  426. { type: 'separator' },
  427. {
  428. id: 'edit.find',
  429. label: '&Find...',
  430. accelerator: 'CmdOrCtrl+F',
  431. click: async () => await this.execute('find', null),
  432. }
  433. ]
  434. });
  435. const viewTemplate = {
  436. label: '&View',
  437. submenu: [
  438. {
  439. id: 'view.toggle-attributes',
  440. accelerator: 'CmdOrCtrl+D',
  441. click: async () => await this.execute('toggle', 'attributes'),
  442. },
  443. {
  444. id: 'view.toggle-weights',
  445. accelerator: 'CmdOrCtrl+I',
  446. click: async () => await this.execute('toggle', 'weights'),
  447. },
  448. {
  449. id: 'view.toggle-names',
  450. accelerator: 'CmdOrCtrl+U',
  451. click: async () => await this.execute('toggle', 'names'),
  452. },
  453. {
  454. id: 'view.toggle-direction',
  455. accelerator: 'CmdOrCtrl+K',
  456. click: async () => await this.execute('toggle', 'direction')
  457. },
  458. {
  459. id: 'view.toggle-mousewheel',
  460. accelerator: 'CmdOrCtrl+M',
  461. click: async () => await this.execute('toggle', 'mousewheel'),
  462. },
  463. { type: 'separator' },
  464. {
  465. id: 'view.reload',
  466. label: '&Reload',
  467. accelerator: darwin ? 'Cmd+R' : 'F5',
  468. click: async () => await this._reload(),
  469. },
  470. { type: 'separator' },
  471. {
  472. id: 'view.reset-zoom',
  473. label: 'Actual &Size',
  474. accelerator: 'Shift+Backspace',
  475. click: async () => await this.execute('reset-zoom', null),
  476. },
  477. {
  478. id: 'view.zoom-in',
  479. label: 'Zoom &In',
  480. accelerator: 'Shift+Up',
  481. click: async () => await this.execute('zoom-in', null),
  482. },
  483. {
  484. id: 'view.zoom-out',
  485. label: 'Zoom &Out',
  486. accelerator: 'Shift+Down',
  487. click: async () => await this.execute('zoom-out', null),
  488. },
  489. { type: 'separator' },
  490. {
  491. id: 'view.show-properties',
  492. label: '&Properties...',
  493. accelerator: 'CmdOrCtrl+Enter',
  494. click: async () => await this.execute('show-properties', null),
  495. }
  496. ]
  497. };
  498. if (!electron.app.isPackaged) {
  499. viewTemplate.submenu.push({ type: 'separator' });
  500. viewTemplate.submenu.push({ role: 'toggledevtools' });
  501. }
  502. menuTemplate.push(viewTemplate);
  503. if (darwin) {
  504. menuTemplate.push({
  505. role: 'window',
  506. submenu: [
  507. { role: 'minimize' },
  508. { role: 'zoom' },
  509. { type: 'separator' },
  510. { role: 'front' }
  511. ]
  512. });
  513. }
  514. const helpSubmenu = [
  515. {
  516. label: 'Report &Issue',
  517. click: async () => await this.execute('report-issue', null)
  518. }
  519. ];
  520. if (!darwin) {
  521. helpSubmenu.push({ type: 'separator' });
  522. helpSubmenu.push({
  523. label: `&About ${electron.app.name}`,
  524. click: async () => await this.execute('about', null)
  525. });
  526. }
  527. menuTemplate.push({
  528. role: 'help',
  529. submenu: helpSubmenu
  530. });
  531. const commandTable = new Map();
  532. commandTable.set('file.export', {
  533. enabled: (view) => view && view.path ? true : false
  534. });
  535. commandTable.set('edit.cut', {
  536. enabled: (view) => view && view.path ? true : false
  537. });
  538. commandTable.set('edit.copy', {
  539. enabled: (view) => view && (view.path || view.get('can-copy')) ? true : false
  540. });
  541. commandTable.set('edit.paste', {
  542. enabled: (view) => view && view.path ? true : false
  543. });
  544. commandTable.set('edit.select-all', {
  545. enabled: (view) => view && view.path ? true : false
  546. });
  547. commandTable.set('edit.find', {
  548. enabled: (view) => view && view.path ? true : false
  549. });
  550. commandTable.set('view.toggle-attributes', {
  551. enabled: (view) => view && view.path ? true : false,
  552. label: (view) => !view || view.get('attributes') ? 'Hide &Attributes' : 'Show &Attributes'
  553. });
  554. commandTable.set('view.toggle-weights', {
  555. enabled: (view) => view && view.path ? true : false,
  556. label: (view) => !view || view.get('weights') ? 'Hide &Weights' : 'Show &Weights'
  557. });
  558. commandTable.set('view.toggle-names', {
  559. enabled: (view) => view && view.path ? true : false,
  560. label: (view) => !view || view.get('names') ? 'Hide &Names' : 'Show &Names'
  561. });
  562. commandTable.set('view.toggle-direction', {
  563. enabled: (view) => view && view.path ? true : false,
  564. label: (view) => !view || view.get('direction') === 'vertical' ? 'Show &Horizontal' : 'Show &Vertical'
  565. });
  566. commandTable.set('view.toggle-mousewheel', {
  567. enabled: (view) => view && view.path ? true : false,
  568. label: (view) => !view || view.get('mousewheel') === 'scroll' ? '&Mouse Wheel: Zoom' : '&Mouse Wheel: Scroll'
  569. });
  570. commandTable.set('view.reload', {
  571. enabled: (view) => view && view.path ? true : false
  572. });
  573. commandTable.set('view.reset-zoom', {
  574. enabled: (view) => view && view.path ? true : false
  575. });
  576. commandTable.set('view.zoom-in', {
  577. enabled: (view) => view && view.path ? true : false
  578. });
  579. commandTable.set('view.zoom-out', {
  580. enabled: (view) => view && view.path ? true : false
  581. });
  582. commandTable.set('view.show-properties', {
  583. enabled: (view) => view && view.path ? true : false
  584. });
  585. this._menu.build(menuTemplate, commandTable);
  586. this._menu.update();
  587. }
  588. }
  589. static location(path) {
  590. if (process.platform !== 'win32') {
  591. const homeDir = os.homedir();
  592. if (path.startsWith(homeDir)) {
  593. return { path, label: `~${path.substring(homeDir.length)}` };
  594. }
  595. }
  596. return { path, label: path };
  597. }
  598. };
  599. app.View = class {
  600. constructor(owner) {
  601. this._owner = owner;
  602. this._ready = false;
  603. this._path = null;
  604. this._properties = new Map();
  605. this._dispatch = [];
  606. const dirname = path.dirname(url.fileURLToPath(import.meta.url));
  607. const size = electron.screen.getPrimaryDisplay().workAreaSize;
  608. const options = {
  609. show: false,
  610. title: electron.app.name,
  611. backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#1e1e1e' : '#ececec',
  612. icon: electron.nativeImage.createFromPath(path.join(dirname, 'icon.png')),
  613. minWidth: 600,
  614. minHeight: 600,
  615. width: size.width > 1024 ? 1024 : size.width,
  616. height: size.height > 768 ? 768 : size.height,
  617. webPreferences: {
  618. preload: path.join(dirname, 'desktop.mjs'),
  619. nodeIntegration: true,
  620. enableDeprecatedPaste: true
  621. }
  622. };
  623. if (owner.application.environment.titlebar) {
  624. options.frame = false;
  625. options.thickFrame = true;
  626. options.titleBarStyle = 'hiddenInset';
  627. }
  628. if (!this._owner.empty && app.View._position && app.View._position.length === 2) {
  629. options.x = app.View._position[0] + 30;
  630. options.y = app.View._position[1] + 30;
  631. if (options.x + options.width > size.width) {
  632. options.x = 0;
  633. }
  634. if (options.y + options.height > size.height) {
  635. options.y = 0;
  636. }
  637. }
  638. this._window = new electron.BrowserWindow(options);
  639. app.View._position = this._window.getPosition();
  640. this._window.on('close', () => this._owner.closeView(this));
  641. this._window.on('focus', () => this.emit('activated'));
  642. this._window.on('blur', () => this.emit('deactivated'));
  643. this._window.on('minimize', () => this.state());
  644. this._window.on('restore', () => this.state());
  645. this._window.on('maximize', () => this.state());
  646. this._window.on('unmaximize', () => this.state());
  647. this._window.on('enter-full-screen', () => this.state('enter-full-screen'));
  648. this._window.on('leave-full-screen', () => this.state('leave-full-screen'));
  649. this._window.webContents.on('did-finish-load', () => {
  650. this._didFinishLoad = true;
  651. });
  652. this._window.webContents.setWindowOpenHandler((detail) => {
  653. const url = detail.url;
  654. if (url.startsWith('http://') || url.startsWith('https://')) {
  655. electron.shell.openExternal(url);
  656. }
  657. return { action: 'deny' };
  658. });
  659. this._window.once('ready-to-show', () => {
  660. this._window.show();
  661. });
  662. if (owner.application.environment.titlebar && process.platform !== 'darwin') {
  663. this._window.removeMenu();
  664. }
  665. this._loadURL();
  666. }
  667. get window() {
  668. return this._window;
  669. }
  670. get path() {
  671. return this._path;
  672. }
  673. open(path) {
  674. this._openPath = path;
  675. const location = app.Application.location(path);
  676. if (this._didFinishLoad) {
  677. this._window.webContents.send('open', location);
  678. } else {
  679. this._window.webContents.on('did-finish-load', () => {
  680. this._window.webContents.send('open', location);
  681. });
  682. this._loadURL();
  683. }
  684. }
  685. _loadURL() {
  686. const dirname = path.dirname(url.fileURLToPath(import.meta.url));
  687. const pathname = path.join(dirname, 'index.html');
  688. let content = fs.readFileSync(pathname, 'utf-8');
  689. content = content.replace(/<\s*script[^>]*>[\s\S]*?(<\s*\/script[^>]*>|$)/ig, '');
  690. const data = `data:text/html;charset=utf-8,${encodeURIComponent(content)}`;
  691. const options = {
  692. baseURLForDataURL: url.pathToFileURL(pathname).toString()
  693. };
  694. this._window.loadURL(data, options);
  695. }
  696. restore() {
  697. if (this._window) {
  698. if (this._window.isMinimized()) {
  699. this._window.restore();
  700. }
  701. this._window.show();
  702. }
  703. }
  704. match(path) {
  705. if (this._openPath) {
  706. return this._openPath === path;
  707. }
  708. return this._path === path;
  709. }
  710. execute(command, data) {
  711. if (this._dispatch) {
  712. this._dispatch.push({ command, data });
  713. } else if (this._window && this._window.webContents) {
  714. const window = this._window;
  715. const contents = window.webContents;
  716. switch (command) {
  717. case 'toggle-developer-tools':
  718. if (contents.isDevToolsOpened()) {
  719. contents.closeDevTools();
  720. } else {
  721. contents.openDevTools();
  722. }
  723. break;
  724. case 'fullscreen':
  725. window.setFullScreen(!window.isFullScreen());
  726. break;
  727. default:
  728. contents.send(command, data);
  729. break;
  730. }
  731. }
  732. }
  733. update(data) {
  734. for (const [name, value] of Object.entries(data)) {
  735. switch (name) {
  736. case 'path': {
  737. if (value) {
  738. this._path = value;
  739. const location = app.Application.location(this._path);
  740. const title = process.platform === 'darwin' ? location.label : `${location.label} - ${electron.app.name}`;
  741. this._window.setTitle(title);
  742. this._window.focus();
  743. }
  744. delete this._openPath;
  745. break;
  746. }
  747. default: {
  748. this._properties.set(name, value);
  749. }
  750. }
  751. }
  752. this.emit('updated');
  753. }
  754. get(name) {
  755. return this._properties.get(name);
  756. }
  757. on(event, callback) {
  758. this._events = this._events || {};
  759. this._events[event] = this._events[event] || [];
  760. this._events[event].push(callback);
  761. }
  762. emit(event, data) {
  763. if (this._events && this._events[event]) {
  764. for (const callback of this._events[event]) {
  765. callback(this, data);
  766. }
  767. }
  768. }
  769. state(event) {
  770. let fullscreen = false;
  771. switch (event) {
  772. case 'enter-full-screen': fullscreen = true; break;
  773. case 'leave-full-screen': fullscreen = false; break;
  774. default: fullscreen = this._window.isFullScreen(); break;
  775. }
  776. this.execute('window-state', {
  777. minimized: this._window.isMinimized(),
  778. maximized: this._window.isMaximized(),
  779. fullscreen
  780. });
  781. if (this._dispatch) {
  782. const dispatch = this._dispatch;
  783. delete this._dispatch;
  784. for (const obj of dispatch) {
  785. this.execute(obj.command, obj.data);
  786. }
  787. }
  788. }
  789. };
  790. app.ViewCollection = class {
  791. constructor(application) {
  792. this._application = application;
  793. this._views = new Map();
  794. electron.ipcMain.on('window-close', (event) => {
  795. const window = event.sender.getOwnerBrowserWindow();
  796. window.close();
  797. event.returnValue = null;
  798. });
  799. electron.ipcMain.on('window-toggle', (event) => {
  800. const window = event.sender.getOwnerBrowserWindow();
  801. if (window.isFullScreen()) {
  802. window.setFullScreen(false);
  803. } else if (window.isMaximized()) {
  804. window.unmaximize();
  805. } else {
  806. window.maximize();
  807. }
  808. event.returnValue = null;
  809. });
  810. electron.ipcMain.on('window-minimize', (event) => {
  811. const window = event.sender.getOwnerBrowserWindow();
  812. window.minimize();
  813. event.returnValue = null;
  814. });
  815. electron.ipcMain.on('window-update', (event, data) => {
  816. const window = event.sender.getOwnerBrowserWindow();
  817. if (this._views.has(window)) {
  818. const view = this._views.get(window);
  819. view.update(data);
  820. }
  821. event.returnValue = null;
  822. });
  823. electron.ipcMain.on('update-window-state', (event) => {
  824. const window = event.sender.getOwnerBrowserWindow();
  825. if (this._views.has(window)) {
  826. this._views.get(window).state();
  827. }
  828. event.returnValue = null;
  829. });
  830. }
  831. get application() {
  832. return this._application;
  833. }
  834. get views() {
  835. return this._views.values();
  836. }
  837. get empty() {
  838. return this._views.size === 0;
  839. }
  840. get(window) {
  841. return this._views.get(window);
  842. }
  843. openView() {
  844. const view = new app.View(this);
  845. view.on('activated', (view) => {
  846. this._activeView = view;
  847. this.emit('active-view-changed', { activeView: this._activeView });
  848. });
  849. view.on('updated', () => {
  850. this.emit('active-view-updated', { activeView: this._activeView });
  851. });
  852. view.on('deactivated', () => {
  853. this._activeView = null;
  854. this.emit('active-view-changed', { activeView: this._activeView });
  855. });
  856. this._views.set(view.window, view);
  857. this._updateActiveView();
  858. return view;
  859. }
  860. closeView(view) {
  861. this._views.delete(view.window);
  862. this._updateActiveView();
  863. }
  864. first() {
  865. return this.empty ? null : this._views.values().next().value;
  866. }
  867. get activeView() {
  868. return this._activeView;
  869. }
  870. on(event, callback) {
  871. this._events = this._events || {};
  872. this._events[event] = this._events[event] || [];
  873. this._events[event].push(callback);
  874. }
  875. emit(event, data) {
  876. if (this._events && this._events[event]) {
  877. for (const callback of this._events[event]) {
  878. callback(this, data);
  879. }
  880. }
  881. }
  882. _updateActiveView() {
  883. const window = electron.BrowserWindow.getFocusedWindow();
  884. const view = window && this._views.has(window) ? this._views.get(window) : null;
  885. if (view !== this._activeView) {
  886. this._activeView = view;
  887. this.emit('active-view-changed', { activeView: this._activeView });
  888. }
  889. }
  890. };
  891. app.ConfigurationService = class {
  892. constructor() {
  893. const dir = electron.app.getPath('userData');
  894. if (dir && dir.length > 0) {
  895. this._file = path.join(dir, 'configuration.json');
  896. }
  897. }
  898. open() {
  899. this._content = { 'recents': [] };
  900. if (this._file && fs.existsSync(this._file)) {
  901. const data = fs.readFileSync(this._file, 'utf-8');
  902. if (data) {
  903. try {
  904. this._content = JSON.parse(data);
  905. if (Array.isArray(this._content.recents)) {
  906. this._content.recents = this._content.recents.map((recent) => typeof recent !== 'string' && recent && recent.path ? recent.path : recent);
  907. }
  908. } catch {
  909. // continue regardless of error
  910. }
  911. }
  912. }
  913. }
  914. save() {
  915. if (this._content && this._file) {
  916. const data = JSON.stringify(this._content, null, 2);
  917. fs.writeFileSync(this._file, data);
  918. }
  919. }
  920. has(name) {
  921. return this._content && Object.prototype.hasOwnProperty.call(this._content, name);
  922. }
  923. set(name, value) {
  924. this._content[name] = value;
  925. }
  926. get(name) {
  927. return this._content[name];
  928. }
  929. delete(name) {
  930. delete this._content[name];
  931. }
  932. };
  933. app.MenuService = class {
  934. constructor(views) {
  935. this._views = views;
  936. }
  937. build(menuTemplate, commandTable) {
  938. this._menuTemplate = menuTemplate;
  939. this._commandTable = commandTable;
  940. this._itemTable = new Map();
  941. for (const menu of menuTemplate) {
  942. for (const item of menu.submenu) {
  943. if (item.id) {
  944. if (!item.label) {
  945. item.label = '';
  946. }
  947. this._itemTable.set(item.id, item);
  948. }
  949. }
  950. }
  951. this._rebuild();
  952. }
  953. update() {
  954. if (!this._menu && !this._commandTable) {
  955. return;
  956. }
  957. const view = this._views.activeView;
  958. if (this._updateLabel(view)) {
  959. this._rebuild();
  960. }
  961. this._updateEnabled(view);
  962. }
  963. _rebuild() {
  964. if (process.platform === 'darwin') {
  965. this._menu = electron.Menu.buildFromTemplate(this._menuTemplate);
  966. electron.Menu.setApplicationMenu(this._menu);
  967. } else if (!this._views.application.environment.titlebar) {
  968. this._menu = electron.Menu.buildFromTemplate(this._menuTemplate);
  969. for (const view of this._views.views) {
  970. view.window.setMenu(this._menu);
  971. }
  972. }
  973. }
  974. _updateLabel(view) {
  975. let rebuild = false;
  976. for (const [name, command] of this._commandTable.entries()) {
  977. if (this._menu) {
  978. const item = this._menu.getMenuItemById(name);
  979. if (command && command.label) {
  980. const label = command.label(view);
  981. if (label !== item.label) {
  982. if (this._itemTable.has(name)) {
  983. this._itemTable.get(name).label = label;
  984. rebuild = true;
  985. }
  986. }
  987. }
  988. }
  989. }
  990. return rebuild;
  991. }
  992. _updateEnabled(view) {
  993. for (const [name, command] of this._commandTable.entries()) {
  994. if (this._menu) {
  995. const item = this._menu.getMenuItemById(name);
  996. if (item && command.enabled) {
  997. item.enabled = command.enabled(view);
  998. }
  999. }
  1000. }
  1001. }
  1002. };
  1003. try {
  1004. global.application = new app.Application();
  1005. await global.application.start();
  1006. } catch (error) {
  1007. /* eslint-disable no-console */
  1008. console.error(error.message);
  1009. /* eslint-enable no-console */
  1010. }