main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Markdown Viewer Desktop — Neutralino.js integration layer
  2. // Handles system tray, window close confirmation, and file association
  3. /*
  4. Function to set up a system tray menu with options specific to the window mode.
  5. This function checks if the application is running in window mode, and if so,
  6. it defines the tray menu items and sets up the tray accordingly.
  7. */
  8. function setTray() {
  9. // Tray menu is only available in window mode
  10. if (typeof NL_MODE === "undefined" || NL_MODE != "window") {
  11. console.log("INFO: Tray menu is only available in the window mode.");
  12. return;
  13. }
  14. // Define tray menu items
  15. let tray = {
  16. icon: "/resources/assets/icon.jpg",
  17. menuItems: [
  18. { id: "VERSION", text: "Get version" },
  19. { id: "SEP", text: "-" },
  20. { id: "QUIT", text: "Quit" },
  21. ],
  22. };
  23. // Set the tray menu
  24. try {
  25. Neutralino.os.setTray(tray);
  26. } catch (e) {
  27. console.warn("Failed to set system tray:", e);
  28. }
  29. }
  30. /*
  31. Function to handle click events on the tray menu items.
  32. This function performs different actions based on the clicked item's ID,
  33. such as displaying version information or exiting the application.
  34. */
  35. function onTrayMenuItemClicked(event) {
  36. switch (event.detail.id) {
  37. case "VERSION":
  38. // Display version information
  39. Neutralino.os.showMessageBox(
  40. "Version information",
  41. `Neutralinojs server: v${NL_VERSION} | Neutralinojs client: v${NL_CVERSION}`,
  42. );
  43. break;
  44. case "QUIT":
  45. // Exit the application
  46. Neutralino.app.exit();
  47. break;
  48. }
  49. }
  50. async function onWindowClose() {
  51. try {
  52. let response = await Neutralino.os.showMessageBox(
  53. "Exit Markdown Viewer",
  54. "Are you sure you want to close the application? Any unsaved changes in your tabs may be lost.",
  55. "YES_NO",
  56. "QUESTION"
  57. );
  58. if (response === "YES") {
  59. Neutralino.app.exit();
  60. }
  61. } catch (e) {
  62. Neutralino.app.exit();
  63. }
  64. }
  65. function isNeutralinoRuntime() {
  66. if (typeof Neutralino === 'undefined' || typeof NL_PORT === 'undefined') {
  67. return false;
  68. }
  69. try {
  70. return typeof NL_TOKEN !== 'undefined' || Boolean(sessionStorage.getItem('NL_TOKEN'));
  71. } catch (e) {
  72. return typeof NL_TOKEN !== 'undefined';
  73. }
  74. }
  75. // Initialize Neutralino if in native environment
  76. if (isNeutralinoRuntime()) {
  77. Neutralino.init();
  78. // Register event listeners
  79. Neutralino.events.on("trayMenuItemClicked", onTrayMenuItemClicked);
  80. Neutralino.events.on("windowClose", onWindowClose);
  81. // Conditional initialization: Set up system tray if not running on macOS
  82. if (typeof NL_OS !== 'undefined' && NL_OS != "Darwin") {
  83. // TODO: Fix https://github.com/neutralinojs/neutralinojs/issues/615
  84. setTray();
  85. }
  86. }
  87. // Open file passed as command-line argument (e.g. when double-clicking a .md file)
  88. (async function loadInitialFile() {
  89. if (!isNeutralinoRuntime() || typeof NL_ARGS === 'undefined') return;
  90. const args = Array.isArray(NL_ARGS) ? NL_ARGS : (() => { try { return JSON.parse(NL_ARGS); } catch(e) { return []; } })();
  91. const filePath = args.find(a => typeof a === 'string' && /\.(md|markdown)$/i.test(a));
  92. if (!filePath) return;
  93. try {
  94. const content = await Neutralino.filesystem.readFile(filePath);
  95. const fileName = filePath.split(/[/\\]/).pop().replace(/\.(md|markdown)$/i, '');
  96. window.NL_INITIAL_FILE_CONTENT = {
  97. name: fileName,
  98. content: content
  99. };
  100. // Callback hook in case script.js loaded first
  101. if (window.NL_IMPORT_EXTERNAL_FILE) {
  102. window.NL_IMPORT_EXTERNAL_FILE(content, fileName);
  103. }
  104. } catch (e) {
  105. console.warn('Could not open initial file:', e);
  106. }
  107. })();