app.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /* jshint esversion: 6 */
  2. const electron = require('electron');
  3. const updater = require('electron-updater');
  4. const fs = require('fs');
  5. const os = require('os');
  6. const path = require('path');
  7. const process = require('process');
  8. const url = require('url');
  9. class Application {
  10. constructor() {
  11. this._views = new ViewCollection();
  12. this._configuration = new ConfigurationService();
  13. this._menu = new MenuService();
  14. this._openFileQueue = [];
  15. electron.app.setAppUserModelId('com.lutzroeder.netron');
  16. electron.app.allowRendererProcessReuse = true;
  17. if (!electron.app.requestSingleInstanceLock()) {
  18. electron.app.quit();
  19. }
  20. electron.app.on('second-instance', (event, commandLine, workingDirectory) => {
  21. const currentDirectory = process.cwd();
  22. process.chdir(workingDirectory);
  23. const open = this._parseCommandLine(commandLine);
  24. process.chdir(currentDirectory);
  25. if (!open) {
  26. if (this._views.count > 0) {
  27. const view = this._views.item(0);
  28. if (view) {
  29. view.restore();
  30. }
  31. }
  32. }
  33. });
  34. electron.ipcMain.on('open-file-dialog', () => {
  35. this._openFileDialog();
  36. });
  37. electron.ipcMain.on('drop-files', (e, data) => {
  38. const files = data.files.filter((file) => fs.statSync(file).isFile());
  39. this._dropFiles(e.sender, files);
  40. });
  41. electron.app.on('will-finish-launching', () => {
  42. electron.app.on('open-file', (e, path) => {
  43. this._openFile(path);
  44. });
  45. });
  46. electron.app.on('ready', () => {
  47. this._ready();
  48. });
  49. electron.app.on('window-all-closed', () => {
  50. if (process.platform !== 'darwin') {
  51. electron.app.quit();
  52. }
  53. });
  54. electron.app.on('will-quit', () => {
  55. this._configuration.save();
  56. });
  57. this._parseCommandLine(process.argv);
  58. this._checkForUpdates();
  59. }
  60. _parseCommandLine(argv) {
  61. let open = false;
  62. if (argv.length > 1) {
  63. for (const arg of argv.slice(1)) {
  64. if (!arg.startsWith('-')) {
  65. const extension = arg.split('.').pop().toLowerCase();
  66. if (extension != '' && extension != 'js' && fs.existsSync(arg) && fs.statSync(arg).isFile()) {
  67. this._openFile(arg);
  68. open = true;
  69. }
  70. }
  71. }
  72. }
  73. return open;
  74. }
  75. _ready() {
  76. this._configuration.load();
  77. if (!this._configuration.has('userId')) {
  78. this._configuration.set('userId', require('uuid').v4());
  79. }
  80. if (this._openFileQueue) {
  81. const openFileQueue = this._openFileQueue;
  82. this._openFileQueue = null;
  83. while (openFileQueue.length > 0) {
  84. const file = openFileQueue.shift();
  85. this._openFile(file);
  86. }
  87. }
  88. if (this._views.count == 0) {
  89. this._views.openView();
  90. }
  91. this._resetMenu();
  92. this._views.on('active-view-changed', () => {
  93. this._updateMenu();
  94. });
  95. this._views.on('active-view-updated', () => {
  96. this._updateMenu();
  97. });
  98. }
  99. _openFileDialog() {
  100. const showOpenDialogOptions = {
  101. properties: [ 'openFile' ],
  102. filters: [
  103. { name: 'All Model Files', extensions: [
  104. 'onnx', 'pb',
  105. 'h5', 'hd5', 'hdf5', 'json', 'keras',
  106. 'mlmodel',
  107. 'caffemodel',
  108. 'model', 'dnn', 'cmf', 'mar', 'params',
  109. 'meta',
  110. 'tflite', 'lite', 'tfl',
  111. 'armnn', 'mnn', 'nn', 'uff', 'uff.txt',
  112. 'ncnn', 'param', 'tnnproto',
  113. 'xml',
  114. 'tmfile',
  115. 'pt', 'pth', 't7',
  116. 'pkl', 'joblib',
  117. 'pbtxt', 'prototxt',
  118. 'cfg',
  119. 'xml' ] }
  120. ]
  121. };
  122. const selectedFiles = electron.dialog.showOpenDialogSync(showOpenDialogOptions);
  123. if (selectedFiles) {
  124. for (const file of selectedFiles) {
  125. this._openFile(file);
  126. }
  127. }
  128. }
  129. _openFile(file) {
  130. if (this._openFileQueue) {
  131. this._openFileQueue.push(file);
  132. return;
  133. }
  134. if (file && file.length > 0 && fs.existsSync(file) && fs.statSync(file).isFile()) {
  135. // find existing view for this file
  136. let view = this._views.find(file);
  137. // find empty welcome window
  138. if (view == null) {
  139. view = this._views.find(null);
  140. }
  141. // create new window
  142. if (view == null) {
  143. view = this._views.openView();
  144. }
  145. this._loadFile(file, view);
  146. }
  147. }
  148. _loadFile(file, view) {
  149. const recents = this._configuration.get('recents').filter(recent => file != recent.path);
  150. view.open(file);
  151. recents.unshift({ path: file });
  152. if (recents.length > 9) {
  153. recents.splice(9);
  154. }
  155. this._configuration.set('recents', recents);
  156. this._resetMenu();
  157. }
  158. _dropFiles(sender, files) {
  159. let view = this._views.from(sender);
  160. for (const file of files) {
  161. if (view) {
  162. this._loadFile(file, view);
  163. view = null;
  164. }
  165. else {
  166. this._openFile(file);
  167. }
  168. }
  169. }
  170. _export() {
  171. const view = this._views.activeView;
  172. if (view && view.path) {
  173. let defaultPath = 'Untitled';
  174. const file = view.path;
  175. const lastIndex = file.lastIndexOf('.');
  176. if (lastIndex != -1) {
  177. defaultPath = file.substring(0, lastIndex);
  178. }
  179. const owner = electron.BrowserWindow.getFocusedWindow();
  180. const showSaveDialogOptions = {
  181. title: 'Export',
  182. defaultPath: defaultPath,
  183. buttonLabel: 'Export',
  184. filters: [
  185. { name: 'PNG', extensions: [ 'png' ] },
  186. { name: 'SVG', extensions: [ 'svg' ] }
  187. ]
  188. };
  189. const selectedFile = electron.dialog.showSaveDialogSync(owner, showSaveDialogOptions);
  190. if (selectedFile) {
  191. view.execute('export', { 'file': selectedFile });
  192. }
  193. }
  194. }
  195. service(name) {
  196. if (name == 'configuration') {
  197. return this._configuration;
  198. }
  199. return undefined;
  200. }
  201. execute(command, data) {
  202. const view = this._views.activeView;
  203. if (view) {
  204. view.execute(command, data || {});
  205. }
  206. this._updateMenu();
  207. }
  208. _reload() {
  209. const view = this._views.activeView;
  210. if (view && view.path) {
  211. this._loadFile(view.path, view);
  212. }
  213. }
  214. _checkForUpdates() {
  215. if (!electron.app.isPackaged) {
  216. return;
  217. }
  218. const autoUpdater = updater.autoUpdater;
  219. if (autoUpdater.app && autoUpdater.app.appUpdateConfigPath && !fs.existsSync(autoUpdater.app.appUpdateConfigPath)) {
  220. return;
  221. }
  222. const promise = autoUpdater.checkForUpdates();
  223. if (promise) {
  224. promise.catch((error) => {
  225. console.log(error.message);
  226. });
  227. }
  228. }
  229. get package() {
  230. if (!this._package) {
  231. const file = path.join(path.dirname(__dirname), 'package.json');
  232. const data = fs.readFileSync(file);
  233. this._package = JSON.parse(data);
  234. this._package.date = new Date(fs.statSync(file).mtime);
  235. }
  236. return this._package;
  237. }
  238. _about() {
  239. let dialog = null;
  240. const options = {
  241. show: false,
  242. backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#2d2d2d' : '#e6e6e6',
  243. width: 400,
  244. height: 250,
  245. center: true,
  246. minimizable: false,
  247. maximizable: false,
  248. useContentSize: true,
  249. resizable: true,
  250. fullscreenable: false,
  251. webPreferences: { nodeIntegration: true }
  252. };
  253. if (process.platform === 'darwin') {
  254. options.title = '';
  255. dialog = Application._aboutDialog;
  256. }
  257. else {
  258. options.title = 'About ' + electron.app.name;
  259. options.parent = electron.BrowserWindow.getFocusedWindow();
  260. options.modal = true;
  261. options.showInTaskbar = false;
  262. }
  263. if (process.platform === 'win32') {
  264. options.type = 'toolbar';
  265. }
  266. if (!dialog) {
  267. dialog = new electron.BrowserWindow(options);
  268. if (process.platform === 'darwin') {
  269. Application._aboutDialog = dialog;
  270. }
  271. dialog.removeMenu();
  272. dialog.excludedFromShownWindowsMenu = true;
  273. dialog.webContents.on('new-window', (event, url) => {
  274. if (url.startsWith('http://') || url.startsWith('https://')) {
  275. event.preventDefault();
  276. electron.shell.openExternal(url);
  277. }
  278. });
  279. let content = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8');
  280. content = content.replace('{version}', this.package.version);
  281. content = content.replace('<title>Netron</title>', '');
  282. content = content.replace('<body class="welcome spinner">', '<body class="about desktop">');
  283. content = content.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
  284. content = content.replace(/<link.*>/gi, '');
  285. dialog.once('ready-to-show', () => {
  286. dialog.resizable = false;
  287. dialog.show();
  288. });
  289. dialog.on('close', function() {
  290. electron.globalShortcut.unregister('Escape');
  291. Application._aboutDialog = null;
  292. });
  293. dialog.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(content));
  294. electron.globalShortcut.register('Escape', function() {
  295. dialog.close();
  296. });
  297. }
  298. else {
  299. dialog.show();
  300. }
  301. }
  302. _updateMenu() {
  303. const window = electron.BrowserWindow.getFocusedWindow();
  304. this._menu.update({
  305. window: window,
  306. webContents: window ? window.webContents : null,
  307. view: this._views.activeView
  308. }, this._views.views.map((view) => view.window));
  309. }
  310. _resetMenu() {
  311. const menuRecentsTemplate = [];
  312. if (this._configuration.has('recents')) {
  313. let recents = this._configuration.get('recents');
  314. recents = recents.filter(recent => fs.existsSync(recent.path) && fs.statSync(recent.path).isFile());
  315. if (recents.length > 9) {
  316. recents.splice(9);
  317. }
  318. this._configuration.set('recents', recents);
  319. for (let i = 0; i < recents.length; i++) {
  320. const recent = recents[i];
  321. menuRecentsTemplate.push({
  322. file: recent.path,
  323. label: Application.minimizePath(recent.path),
  324. accelerator: ((process.platform === 'darwin') ? 'Cmd+' : 'Ctrl+') + (i + 1).toString(),
  325. click: (item) => { this._openFile(item.file); }
  326. });
  327. }
  328. }
  329. const menuTemplate = [];
  330. if (process.platform === 'darwin') {
  331. menuTemplate.unshift({
  332. label: electron.app.name,
  333. submenu: [
  334. {
  335. label: 'About ' + electron.app.name,
  336. click: () => this._about()
  337. },
  338. { type: 'separator' },
  339. { role: 'hide' },
  340. { role: 'hideothers' },
  341. { role: 'unhide' },
  342. { type: 'separator' },
  343. { role: 'quit' }
  344. ]
  345. });
  346. }
  347. menuTemplate.push({
  348. label: '&File',
  349. submenu: [
  350. {
  351. label: '&Open...',
  352. accelerator: 'CmdOrCtrl+O',
  353. click: () => { this._openFileDialog(); }
  354. },
  355. {
  356. label: 'Open &Recent',
  357. submenu: menuRecentsTemplate
  358. },
  359. { type: 'separator' },
  360. {
  361. id: 'file.export',
  362. label: '&Export...',
  363. accelerator: 'CmdOrCtrl+Shift+E',
  364. click: () => this._export(),
  365. },
  366. { type: 'separator' },
  367. { role: 'close' },
  368. ]
  369. });
  370. if (process.platform !== 'darwin') {
  371. menuTemplate.slice(-1)[0].submenu.push(
  372. { type: 'separator' },
  373. { role: 'quit' }
  374. );
  375. }
  376. if (process.platform == 'darwin') {
  377. electron.systemPreferences.setUserDefault('NSDisabledDictationMenuItem', 'boolean', true);
  378. electron.systemPreferences.setUserDefault('NSDisabledCharacterPaletteMenuItem', 'boolean', true);
  379. }
  380. menuTemplate.push({
  381. label: '&Edit',
  382. submenu: [
  383. {
  384. id: 'edit.cut',
  385. label: 'Cu&t',
  386. accelerator: 'CmdOrCtrl+X',
  387. click: () => this.execute('cut', null),
  388. },
  389. {
  390. id: 'edit.copy',
  391. label: '&Copy',
  392. accelerator: 'CmdOrCtrl+C',
  393. click: () => this.execute('copy', null),
  394. },
  395. {
  396. id: 'edit.paste',
  397. label: '&Paste',
  398. accelerator: 'CmdOrCtrl+V',
  399. click: () => this.execute('paste', null),
  400. },
  401. {
  402. id: 'edit.select-all',
  403. label: 'Select &All',
  404. accelerator: 'CmdOrCtrl+A',
  405. click: () => this.execute('selectall', null),
  406. },
  407. { type: 'separator' },
  408. {
  409. id: 'edit.find',
  410. label: '&Find...',
  411. accelerator: 'CmdOrCtrl+F',
  412. click: () => this.execute('find', null),
  413. }
  414. ]
  415. });
  416. const viewTemplate = {
  417. label: '&View',
  418. submenu: [
  419. {
  420. id: 'view.show-attributes',
  421. accelerator: 'CmdOrCtrl+D',
  422. click: () => this.execute('toggle-attributes', null),
  423. },
  424. {
  425. id: 'view.show-initializers',
  426. accelerator: 'CmdOrCtrl+I',
  427. click: () => this.execute('toggle-initializers', null),
  428. },
  429. {
  430. id: 'view.show-names',
  431. accelerator: 'CmdOrCtrl+U',
  432. click: () => this.execute('toggle-names', null),
  433. },
  434. {
  435. id: 'view.show-horizontal',
  436. accelerator: 'CmdOrCtrl+K',
  437. click: () => this.execute('toggle-direction', null),
  438. },
  439. { type: 'separator' },
  440. {
  441. id: 'view.reload',
  442. label: '&Reload',
  443. accelerator: (process.platform === 'darwin') ? 'Cmd+R' : 'F5',
  444. click: () => this._reload(),
  445. },
  446. { type: 'separator' },
  447. {
  448. id: 'view.reset-zoom',
  449. label: 'Actual &Size',
  450. accelerator: 'Shift+Backspace',
  451. click: () => this.execute('reset-zoom', null),
  452. },
  453. {
  454. id: 'view.zoom-in',
  455. label: 'Zoom &In',
  456. accelerator: 'Shift+Up',
  457. click: () => this.execute('zoom-in', null),
  458. },
  459. {
  460. id: 'view.zoom-out',
  461. label: 'Zoom &Out',
  462. accelerator: 'Shift+Down',
  463. click: () => this.execute('zoom-out', null),
  464. },
  465. { type: 'separator' },
  466. {
  467. id: 'view.show-properties',
  468. label: '&Properties...',
  469. accelerator: 'CmdOrCtrl+Enter',
  470. click: () => this.execute('show-properties', null),
  471. }
  472. ]
  473. };
  474. if (!electron.app.isPackaged) {
  475. viewTemplate.submenu.push({ type: 'separator' });
  476. viewTemplate.submenu.push({ role: 'toggledevtools' });
  477. }
  478. menuTemplate.push(viewTemplate);
  479. if (process.platform === 'darwin') {
  480. menuTemplate.push({
  481. role: 'window',
  482. submenu: [
  483. { role: 'minimize' },
  484. { role: 'zoom' },
  485. { type: 'separator' },
  486. { role: 'front'}
  487. ]
  488. });
  489. }
  490. const helpSubmenu = [
  491. {
  492. label: '&Search Feature Requests',
  493. click: () => { electron.shell.openExternal('https://www.github.com/' + this.package.repository + '/issues'); }
  494. },
  495. {
  496. label: 'Report &Issues',
  497. click: () => { electron.shell.openExternal('https://www.github.com/' + this.package.repository + '/issues/new'); }
  498. }
  499. ];
  500. if (process.platform != 'darwin') {
  501. helpSubmenu.push({ type: 'separator' });
  502. helpSubmenu.push({
  503. label: 'About ' + electron.app.name,
  504. click: () => this._about()
  505. });
  506. }
  507. menuTemplate.push({
  508. role: 'help',
  509. submenu: helpSubmenu
  510. });
  511. const commandTable = new Map();
  512. commandTable.set('file.export', {
  513. enabled: (context) => { return context.view && context.view.path ? true : false; }
  514. });
  515. commandTable.set('edit.cut', {
  516. enabled: (context) => { return context.view && context.view.path ? true : false; }
  517. });
  518. commandTable.set('edit.copy', {
  519. enabled: (context) => { return context.view && context.view.path ? true : false; }
  520. });
  521. commandTable.set('edit.paste', {
  522. enabled: (context) => { return context.view && context.view.path ? true : false; }
  523. });
  524. commandTable.set('edit.select-all', {
  525. enabled: (context) => { return context.view && context.view.path ? true : false; }
  526. });
  527. commandTable.set('edit.find', {
  528. enabled: (context) => { return context.view && context.view.path ? true : false; }
  529. });
  530. commandTable.set('view.show-attributes', {
  531. enabled: (context) => { return context.view && context.view.path ? true : false; },
  532. label: (context) => { return !context.view || !context.view.get('show-attributes') ? 'Show &Attributes' : 'Hide &Attributes'; }
  533. });
  534. commandTable.set('view.show-initializers', {
  535. enabled: (context) => { return context.view && context.view.path ? true : false; },
  536. label: (context) => { return !context.view || !context.view.get('show-initializers') ? 'Show &Initializers' : 'Hide &Initializers'; }
  537. });
  538. commandTable.set('view.show-names', {
  539. enabled: (context) => { return context.view && context.view.path ? true : false; },
  540. label: (context) => { return !context.view || !context.view.get('show-names') ? 'Show &Names' : 'Hide &Names'; }
  541. });
  542. commandTable.set('view.show-horizontal', {
  543. enabled: (context) => { return context.view && context.view.path ? true : false; },
  544. label: (context) => { return !context.view || !context.view.get('show-horizontal') ? 'Show &Horizontal' : 'Show &Vertical'; }
  545. });
  546. commandTable.set('view.reload', {
  547. enabled: (context) => { return context.view && context.view.path ? true : false; }
  548. });
  549. commandTable.set('view.reset-zoom', {
  550. enabled: (context) => { return context.view && context.view.path ? true : false; }
  551. });
  552. commandTable.set('view.zoom-in', {
  553. enabled: (context) => { return context.view && context.view.path ? true : false; }
  554. });
  555. commandTable.set('view.zoom-out', {
  556. enabled: (context) => { return context.view && context.view.path ? true : false; }
  557. });
  558. commandTable.set('view.show-properties', {
  559. enabled: (context) => { return context.view && context.view.path ? true : false; }
  560. });
  561. this._menu.build(menuTemplate, commandTable, this._views.views.map((view) => view.window));
  562. this._updateMenu();
  563. }
  564. static minimizePath(file) {
  565. if (process.platform != 'win32') {
  566. const homeDir = os.homedir();
  567. if (file.startsWith(homeDir)) {
  568. return '~' + file.substring(homeDir.length);
  569. }
  570. }
  571. return file;
  572. }
  573. }
  574. class View {
  575. constructor(owner) {
  576. this._owner = owner;
  577. this._ready = false;
  578. this._path = null;
  579. this._properties = new Map();
  580. const size = electron.screen.getPrimaryDisplay().workAreaSize;
  581. const options = {
  582. show: false,
  583. title: electron.app.name,
  584. backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#1d1d1d' : '#e6e6e6',
  585. icon: electron.nativeImage.createFromPath(path.join(__dirname, 'icon.png')),
  586. minWidth: 600,
  587. minHeight: 400,
  588. width: size.width > 1024 ? 1024 : size.width,
  589. height: size.height > 768 ? 768 : size.height,
  590. webPreferences: { nodeIntegration: true, enableRemoteModule: true }
  591. };
  592. if (this._owner.count > 0 && View._position && View._position.length == 2) {
  593. options.x = View._position[0] + 30;
  594. options.y = View._position[1] + 30;
  595. if (options.x + options.width > size.width) {
  596. options.x = 0;
  597. }
  598. if (options.y + options.height > size.height) {
  599. options.y = 0;
  600. }
  601. }
  602. this._window = new electron.BrowserWindow(options);
  603. View._position = this._window.getPosition();
  604. this._updateCallback = (e, data) => {
  605. if (e.sender == this._window.webContents) {
  606. this.update(data.name, data.value);
  607. this._raise('updated');
  608. }
  609. };
  610. electron.ipcMain.on('update', this._updateCallback);
  611. this._window.on('closed', () => {
  612. electron.ipcMain.removeListener('update', this._updateCallback);
  613. this._owner.closeView(this);
  614. });
  615. this._window.on('focus', () => {
  616. this._raise('activated');
  617. });
  618. this._window.on('blur', () => {
  619. this._raise('deactivated');
  620. });
  621. this._window.webContents.on('dom-ready', () => {
  622. this._ready = true;
  623. });
  624. this._window.webContents.on('new-window', (event, url) => {
  625. if (url.startsWith('http://') || url.startsWith('https://')) {
  626. event.preventDefault();
  627. electron.shell.openExternal(url);
  628. }
  629. });
  630. this._window.once('ready-to-show', () => {
  631. this._window.show();
  632. });
  633. const location = url.format({ protocol: 'file:', slashes: true, pathname: path.join(__dirname, 'electron.html') });
  634. this._window.loadURL(location);
  635. }
  636. get window() {
  637. return this._window;
  638. }
  639. get path() {
  640. return this._path;
  641. }
  642. open(file) {
  643. this._openPath = file;
  644. if (this._ready) {
  645. this._window.webContents.send('open', { file: file });
  646. }
  647. else {
  648. this._window.webContents.on('dom-ready', () => {
  649. this._window.webContents.send('open', { file: file });
  650. });
  651. const location = url.format({ protocol: 'file:', slashes: true, pathname: path.join(__dirname, 'electron.html') });
  652. this._window.loadURL(location);
  653. }
  654. }
  655. restore() {
  656. if (this._window) {
  657. if (this._window.isMinimized()) {
  658. this._window.restore();
  659. }
  660. this._window.show();
  661. }
  662. }
  663. match(path) {
  664. if (this._openPath) {
  665. if (path === null) {
  666. return false;
  667. }
  668. if (path === this._openPath) {
  669. return true;
  670. }
  671. }
  672. return this._path == path;
  673. }
  674. execute(command, data) {
  675. if (this._window && this._window.webContents) {
  676. this._window.webContents.send(command, data);
  677. }
  678. }
  679. update(name, value) {
  680. if (name === 'path') {
  681. if (value) {
  682. this._path = value;
  683. const title = Application.minimizePath(this._path);
  684. this._window.setTitle(process.platform !== 'darwin' ? title + ' - ' + electron.app.name : title);
  685. this._window.focus();
  686. }
  687. this._openPath = null;
  688. return;
  689. }
  690. this._properties.set(name, value);
  691. }
  692. get(name) {
  693. return this._properties.get(name);
  694. }
  695. on(event, callback) {
  696. this._events = this._events || {};
  697. this._events[event] = this._events[event] || [];
  698. this._events[event].push(callback);
  699. }
  700. _raise(event, data) {
  701. if (this._events && this._events[event]) {
  702. for (const callback of this._events[event]) {
  703. callback(this, data);
  704. }
  705. }
  706. }
  707. }
  708. class ViewCollection {
  709. constructor() {
  710. this._views = [];
  711. }
  712. get views() {
  713. return this._views;
  714. }
  715. get count() {
  716. return this._views.length;
  717. }
  718. item(index) {
  719. return this._views[index];
  720. }
  721. openView() {
  722. const view = new View(this);
  723. view.on('activated', (sender) => {
  724. this._activeView = sender;
  725. this._raise('active-view-changed', { activeView: this._activeView });
  726. });
  727. view.on('updated', () => {
  728. this._raise('active-view-updated', { activeView: this._activeView });
  729. });
  730. view.on('deactivated', () => {
  731. this._activeView = null;
  732. this._raise('active-view-changed', { activeView: this._activeView });
  733. });
  734. this._views.push(view);
  735. this._updateActiveView();
  736. return view;
  737. }
  738. closeView(view) {
  739. for (let i = this._views.length - 1; i >= 0; i--) {
  740. if (this._views[i] == view) {
  741. this._views.splice(i, 1);
  742. }
  743. }
  744. this._updateActiveView();
  745. }
  746. find(path) {
  747. return this._views.find(view => view.match(path));
  748. }
  749. from(contents) {
  750. return this._views.find(view => view && view.window && view.window.webContents && view.window.webContents == contents);
  751. }
  752. get activeView() {
  753. return this._activeView;
  754. }
  755. on(event, callback) {
  756. this._events = this._events || {};
  757. this._events[event] = this._events[event] || [];
  758. this._events[event].push(callback);
  759. }
  760. _raise(event, data) {
  761. if (this._events && this._events[event]) {
  762. for (const callback of this._events[event]) {
  763. callback(this, data);
  764. }
  765. }
  766. }
  767. _updateActiveView() {
  768. const window = electron.BrowserWindow.getFocusedWindow();
  769. const view = this._views.find(view => view.window == window) || null;
  770. if (view != this._activeView) {
  771. this._activeView = view;
  772. this._raise('active-view-changed', { activeView: this._activeView });
  773. }
  774. }
  775. }
  776. class ConfigurationService {
  777. load() {
  778. this._data = { 'recents': [] };
  779. const dir = electron.app.getPath('userData');
  780. if (dir && dir.length > 0) {
  781. const file = path.join(dir, 'configuration.json');
  782. if (fs.existsSync(file)) {
  783. const data = fs.readFileSync(file);
  784. if (data) {
  785. try {
  786. this._data = JSON.parse(data);
  787. }
  788. catch (error) {
  789. // continue regardless of error
  790. }
  791. }
  792. }
  793. }
  794. }
  795. save() {
  796. if (this._data) {
  797. const data = JSON.stringify(this._data, null, 2);
  798. if (data) {
  799. const dir = electron.app.getPath('userData');
  800. if (dir && dir.length > 0) {
  801. const file = path.join(dir, 'configuration.json');
  802. fs.writeFileSync(file, data);
  803. }
  804. }
  805. }
  806. }
  807. has(name) {
  808. return this._data && Object.prototype.hasOwnProperty.call(this._data, name);
  809. }
  810. set(name, value) {
  811. this._data[name] = value;
  812. }
  813. get(name) {
  814. return this._data[name];
  815. }
  816. }
  817. class MenuService {
  818. build(menuTemplate, commandTable, windows) {
  819. this._menuTemplate = menuTemplate;
  820. this._commandTable = commandTable;
  821. this._itemTable = new Map();
  822. for (const menu of menuTemplate) {
  823. for (const item of menu.submenu) {
  824. if (item.id) {
  825. if (!item.label) {
  826. item.label = '';
  827. }
  828. this._itemTable.set(item.id, item);
  829. }
  830. }
  831. }
  832. this._rebuild(windows);
  833. }
  834. update(context, windows) {
  835. if (!this._menu && !this._commandTable) {
  836. return;
  837. }
  838. if (this._updateLabel(context)) {
  839. this._rebuild(windows);
  840. }
  841. this._updateEnabled(context);
  842. }
  843. _rebuild(windows) {
  844. this._menu = electron.Menu.buildFromTemplate(this._menuTemplate);
  845. if (process.platform === 'darwin') {
  846. electron.Menu.setApplicationMenu(this._menu);
  847. }
  848. else {
  849. for (const window of windows) {
  850. window.setMenu(this._menu);
  851. }
  852. }
  853. }
  854. _updateLabel(context) {
  855. let rebuild = false;
  856. for (const entry of this._commandTable.entries()) {
  857. const menuItem = this._menu.getMenuItemById(entry[0]);
  858. const command = entry[1];
  859. if (command && command.label) {
  860. const label = command.label(context);
  861. if (label != menuItem.label) {
  862. if (this._itemTable.has(entry[0])) {
  863. this._itemTable.get(entry[0]).label = label;
  864. rebuild = true;
  865. }
  866. }
  867. }
  868. }
  869. return rebuild;
  870. }
  871. _updateEnabled(context) {
  872. for (const entry of this._commandTable.entries()) {
  873. const menuItem = this._menu.getMenuItemById(entry[0]);
  874. if (menuItem) {
  875. const command = entry[1];
  876. if (command.enabled) {
  877. menuItem.enabled = command.enabled(context);
  878. }
  879. }
  880. }
  881. }
  882. }
  883. global.application = new Application();