main.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. const electron = require("electron");
  2. const { app, BrowserWindow, Menu, session, remote, dialog, nativeTheme } = electron;
  3. const path = require("path");
  4. const fs = require("fs");
  5. let _mainWin = null;
  6. let _opUrl = "http://127.0.0.1:8999"; ///application/op/00.main/main.html";
  7. let _isDev = false;
  8. let _sessionLogin = false;
  9. const _appTitle = "용인시 ITS 통합운영단말";
  10. // const isWindows = process.platform === "win32";
  11. // let needsFocusFix = false;
  12. // let triggeringProgrammaticBlur = false;
  13. app.commandLine.appendSwitch("autoplay-policy", "no-user-gesture-required");
  14. app.allowRendererProcessReuse = true;
  15. process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
  16. // app.commandLine.appendSwitch("--enable-transparent-visuals");
  17. // app.commandLine.appendSwitch("enable-features", "CSSColorSchemeUARendering, ImpulseScrollAnimations, ParallelDownloading");
  18. try {
  19. _isDev = process.env.NODE_ENV.trim() === "dev";
  20. } catch (error) {
  21. console.log(error);
  22. }
  23. try {
  24. let configFile;
  25. if (_isDev == true) {
  26. configFile = path.join(__dirname, "extraResources", "config.json");
  27. } else {
  28. configFile = path.join(path.dirname(__dirname), "extraResources", "config.json");
  29. }
  30. const config = JSON.parse(fs.readFileSync(configFile, "utf8"));
  31. _opUrl = config.op;
  32. } catch (error) {
  33. console.log(error);
  34. }
  35. function minimizeWindow() {
  36. const win = BrowserWindow.getFocusedWindow();
  37. if (win) {
  38. win.setFullScreen(false);
  39. }
  40. }
  41. function toggleWindow() {
  42. const win = BrowserWindow.getFocusedWindow();
  43. if (win) {
  44. if (win.isFullScreen()) {
  45. win.setFullScreen(false);
  46. } else {
  47. win.setFullScreen(true);
  48. }
  49. }
  50. }
  51. function toggleDevTools() {
  52. const win = BrowserWindow.getFocusedWindow();
  53. if (win) {
  54. win.toggleDevTools();
  55. }
  56. }
  57. function clearCache() {
  58. // const win = remote.getFocusedWindow();
  59. // if (win) {
  60. // win.webContents.session.clearCache(function () {});
  61. // console.log("aaaaaaaaaaaaaaaaaaaaaaaaa");
  62. // }
  63. session.defaultSession.clearCache();
  64. //console.log("aaaaaaaaaaaaaaaaaaaaaaaaa");
  65. }
  66. const createWindow = () => {
  67. app.commandLine.appendSwitch("autoplay-policy", "no-user-gesture-required");
  68. let preloadJs;
  69. if (_isDev) {
  70. preloadJs = path.join(__dirname, "preload.js");
  71. } else {
  72. preloadJs = path.join(path.dirname(__dirname), "preload.js");
  73. }
  74. Menu.setApplicationMenu(null);
  75. const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
  76. _mainWin = new BrowserWindow({
  77. // titleBarStyle: "hidden",
  78. // titleBarOverlay: {
  79. // color: "#2f3241",
  80. // symbolColor: "#74b1be",
  81. // },
  82. webPreferences: {
  83. webSecurity: false,
  84. nodeIntegration: false,
  85. nodeIntegrationInWorker: false,
  86. contextIsolation: true,
  87. enableRemoteModule: true,
  88. plugins: true,
  89. experimentalFeatures: true,
  90. defaultEncoding: "UTF-8",
  91. allowRunningInsecureContent: true,
  92. autoplayPolicy: "no-user-gesture-required", //"document-user-activation-required",
  93. //preload: preloadJs,
  94. },
  95. title: "용인시 ITS 통합운영단말",
  96. center: true,
  97. backgroundColor: "#80FFFFFF",
  98. show: false,
  99. width: width,
  100. height: height,
  101. icon: "./assets/icons/png/main-logo.png",
  102. // icon: path.join(_dirPath, "assets/icons/png/main-logo.png"),
  103. });
  104. _mainWin.once("ready-to-show", () => {
  105. _mainWin?.show();
  106. });
  107. _mainWin.on("close", (e) => {
  108. if (_sessionLogin) {
  109. const opt = {
  110. type: "question", //종류
  111. buttons: ["Yes", "No"], //버튼 스타일
  112. defaultId: 2, //고유 아이디
  113. title: _appTitle, //제목
  114. message: "프로그램을 종료하시겠습니까?.",
  115. detail: _appTitle,
  116. };
  117. let res = dialog.showMessageBoxSync(null, opt);
  118. if (res === 0) {
  119. _mainWin.webContents.executeJavaScript("logout();");
  120. } else {
  121. e.preventDefault();
  122. }
  123. }
  124. });
  125. _mainWin.on("closed", function () {
  126. _mainWin = null;
  127. });
  128. _mainWin.on("page-title-updated", (e, title) => {
  129. session.defaultSession.cookies.get({}).then((cookies) => {
  130. cookies.forEach((cookie) => {
  131. if (cookie.name == "UUID") {
  132. _sessionLogin = true;
  133. }
  134. });
  135. });
  136. });
  137. // _mainWin.on("blur", (event) => {
  138. // if (!triggeringProgrammaticBlur) {
  139. // needsFocusFix = true;
  140. // }
  141. // });
  142. // _mainWin.on("focus", (event) => {
  143. // if (isWindows && needsFocusFix) {
  144. // needsFocusFix = false;
  145. // triggeringProgrammaticBlur = true;
  146. // setTimeout(function () {
  147. // _mainWin.blur();
  148. // _mainWin.focus();
  149. // setTimeout(function () {
  150. // triggeringProgrammaticBlur = false;
  151. // }, 100);
  152. // }, 100);
  153. // }
  154. // });
  155. _mainWin.maximize(); // 최대 화면으로 시작
  156. _mainWin.loadURL(_opUrl);
  157. //_mainWin.webContents.focus();
  158. // ipcMain.handle("dark-mode:toggle", () => {
  159. // if (nativeTheme.shouldUseDarkColors) {
  160. // nativeTheme.themeSource = "light";
  161. // } else {
  162. // nativeTheme.themeSource = "dark";
  163. // }
  164. // return nativeTheme.shouldUseDarkColors;
  165. // });
  166. // ipcMain.handle("dark-mode:system", () => {
  167. // nativeTheme.themeSource = "system";
  168. // });
  169. // if (nativeTheme.shouldUseDarkColors) {
  170. // nativeTheme.themeSource = "dark";
  171. // }
  172. };
  173. app.whenReady().then(() => {
  174. createWindow();
  175. app.on("activate", () => {
  176. if (BrowserWindow.getAllWindows().length === 0) {
  177. createWindow();
  178. }
  179. });
  180. let ret;
  181. ret = electron.globalShortcut.register("F11", function () {
  182. toggleWindow();
  183. });
  184. ret = electron.globalShortcut.register("CmdOrCtrl+Shift+I", function () {
  185. toggleDevTools();
  186. });
  187. ret = electron.globalShortcut.register("CmdOrCtrl+Shift+R", function () {
  188. clearCache();
  189. });
  190. ret = electron.globalShortcut.register("F12", function () {
  191. toggleDevTools();
  192. });
  193. });
  194. app.on("window-all-closed", () => {
  195. //electron.globalShortcut.unregisterAll();
  196. if (process.platform !== "darwin") {
  197. app.quit();
  198. }
  199. });