moe-window.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * This file is part of Moeditor.
  3. *
  4. * Copyright (c) 2016 Menci <huanghaorui301@gmail.com>
  5. * Copyright (c) 2016 lucaschimweg
  6. *
  7. * Moeditor is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Moeditor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Moeditor. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. 'use strict';
  21. const BrowserWindow = require('electron').BrowserWindow,
  22. dialog = require('electron').dialog,
  23. MoeditorAction = require('./moe-action'),
  24. MoeditorFile = require('./moe-file');
  25. class MoeditorWindow {
  26. constructor(path,defName) {
  27. moeApp.newWindow = this;
  28. if (MoeditorFile.isDirectory(path)) {
  29. this.directory = path
  30. this.fileName = '';
  31. this.fileContent = this.content = '';
  32. } else {
  33. this.directory = require('path').dirname(path);
  34. this.fileName = path;
  35. this.fileContent = this.content = MoeditorFile.read(path).toString();
  36. }
  37. this.defName = defName;
  38. this.isSaved = (path !== defName);
  39. this.changed = path === defName;
  40. const debug = (moeApp.flag.debug | moeApp.config.get('debug')) != 0;
  41. var conf = {
  42. icon: Const.path + "/icons/HexoEditor.ico",
  43. autoHideMenuBar: true,
  44. width: 1000 * moeApp.config.get('scale-factor'),
  45. height: 600 * moeApp.config.get('scale-factor'),
  46. webPreferences: {
  47. zoomFactor: moeApp.config.get('scale-factor')
  48. },
  49. show: debug
  50. };
  51. if (process.platform == 'darwin') conf.titleBarStyle = 'hidden-inset';
  52. else conf.frame = false;
  53. this.window = new BrowserWindow(conf);
  54. this.window.moeditorWindow = this;
  55. this.registerEvents();
  56. this.window.loadURL('file://' + Const.path + '/views/main/index.html');
  57. if (debug) {
  58. this.window.webContents.openDevTools();
  59. }
  60. }
  61. registerEvents() {
  62. this.window.on('close', (e) => {
  63. console.log(moeApp.windows.length,this.changed)
  64. if (moeApp.windows.length && this.changed) {
  65. const choice = dialog.showMessageBox(
  66. this.window,
  67. {
  68. type: 'question',
  69. buttons: [__("Yes"), __("No"), __("Cancel")],
  70. title: __("Confirm"),
  71. cancelId: -1,
  72. message: __("Save changes to file?")
  73. }
  74. );
  75. if (choice == 0) {
  76. if (!MoeditorAction.save(this.window))
  77. e.preventDefault();
  78. return;
  79. } else if (choice == 2 || choice == -1) {
  80. e.preventDefault();
  81. return;
  82. } else{
  83. var fs = require('fs');
  84. if (!this.isSaved && fs.existsSync(this.fileName))
  85. fs.unlinkSync(this.fileName);
  86. }
  87. }
  88. console.log(this.window == moeApp.shellServer.lastWindow)
  89. if (this.window == moeApp.shellServer.lastWindow)
  90. process.nextTick(moeApp.shellServer.kill,false);
  91. const index = moeApp.windows.indexOf(this);
  92. if (index !== -1) moeApp.windows.splice(index, 1);
  93. if ( !moeApp.windows.length) {
  94. process.nextTick(moeApp.shellServer.kill,false);
  95. setTimeout(app.quit,200)
  96. }
  97. });
  98. }
  99. }
  100. module.exports = MoeditorWindow;