main.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. const {app, screen, shell, BrowserWindow, BrowserView, ipcMain, dialog, clipboard } = require('electron')
  2. const Store = require('electron-store');
  3. const windowStateKeeper = require('electron-window-state');
  4. const fs = require('fs')
  5. const path = require("path")
  6. const Pinokiod = require("pinokiod")
  7. const is_mac = process.platform.startsWith("darwin")
  8. var mainWindow;
  9. var root_url;
  10. var wins = {}
  11. var pinned = {}
  12. var launched
  13. const PORT = 4200
  14. const store = new Store();
  15. const pinokiod = new Pinokiod({
  16. port: PORT,
  17. agent: "electron",
  18. store
  19. })
  20. const titleBarOverlay = (theme) => {
  21. if (is_mac) {
  22. return false
  23. } else {
  24. if (theme === "dark") {
  25. return {
  26. color: "#111",
  27. symbolColor: "white"
  28. }
  29. } else if (theme === "default") {
  30. if (launched) {
  31. return {
  32. color: "#F5F4FA",
  33. symbolColor: "black"
  34. }
  35. } else {
  36. return {
  37. color: "royalblue",
  38. symbolColor: "white"
  39. }
  40. }
  41. }
  42. return {
  43. color: "white",
  44. symbolColor: "black"
  45. }
  46. }
  47. }
  48. const attach = (event, webContents) => {
  49. let wc = webContents
  50. webContents.on('will-navigate', (event, url) => {
  51. if (!webContents.opened) {
  52. // The first time this view is being used, set the "opened" to true, and don't do anything
  53. // The next time the view navigates, "the "opened" is already true, so trigger the URL open logic
  54. // - if the new URL has the same host as the app's url, open in app
  55. // - if it's a remote host, open in external browser
  56. webContents.opened = true
  57. } else {
  58. let host = new URL(url).host
  59. let localhost = new URL(root_url).host
  60. if (host !== localhost) {
  61. event.preventDefault()
  62. shell.openExternal(url);
  63. }
  64. }
  65. })
  66. // webContents.on("did-create-window", (parentWindow, details) => {
  67. // const view = new BrowserView();
  68. // parentWindow.setBrowserView(view);
  69. // view.setBounds({ x: 0, y: 30, width: parentWindow.getContentBounds().width, height: parentWindow.getContentBounds().height - 30 });
  70. // view.setAutoResize({ width: true, height: true });
  71. // view.webContents.loadURL(details.url);
  72. // })
  73. webContents.on('did-navigate', (event, url) => {
  74. let win = webContents.getOwnerBrowserWindow()
  75. if (win && win.setTitleBarOverlay && typeof win.setTitleBarOverlay === "function") {
  76. const overlay = titleBarOverlay("default")
  77. win.setTitleBarOverlay(overlay)
  78. }
  79. launched = true
  80. })
  81. webContents.setWindowOpenHandler((config) => {
  82. let url = config.url
  83. let features = config.features
  84. let params = new URLSearchParams(features.split(",").join("&"))
  85. let win = wc.getOwnerBrowserWindow()
  86. let [width, height] = win.getSize()
  87. let [x,y] = win.getPosition()
  88. if (features) {
  89. if (features.startsWith("app") || features.startsWith("self")) {
  90. return {
  91. action: 'allow',
  92. outlivesOpener: true,
  93. overrideBrowserWindowOptions: {
  94. width: (params.get("width") ? parseInt(params.get("width")) : width),
  95. height: (params.get("height") ? parseInt(params.get("height")) : height),
  96. x: x + 30,
  97. y: y + 30,
  98. parent: null,
  99. titleBarStyle : "hidden",
  100. titleBarOverlay : titleBarOverlay("default"),
  101. }
  102. }
  103. } else if (features.startsWith("file")) {
  104. let u = features.replace("file://", "")
  105. shell.showItemInFolder(u)
  106. return { action: 'deny' };
  107. } else {
  108. return { action: 'deny' };
  109. }
  110. } else {
  111. shell.openExternal(url);
  112. return { action: 'deny' };
  113. }
  114. });
  115. }
  116. const getWinState = (url, options) => {
  117. let filename
  118. try {
  119. let pathname = new URL(url).pathname.slice(1)
  120. filename = pathname.slice("/").join("-")
  121. } catch {
  122. filename = "index.json"
  123. }
  124. let state = windowStateKeeper({
  125. file: filename,
  126. ...options
  127. });
  128. return state
  129. }
  130. const createWindow = (port) => {
  131. let mainWindowState = windowStateKeeper({
  132. // file: "index.json",
  133. defaultWidth: 1000,
  134. defaultHeight: 800
  135. });
  136. mainWindow = new BrowserWindow({
  137. titleBarStyle : "hidden",
  138. titleBarOverlay : titleBarOverlay("default"),
  139. x: mainWindowState.x,
  140. y: mainWindowState.y,
  141. width: mainWindowState.width,
  142. height: mainWindowState.height,
  143. minWidth: 190,
  144. webPreferences: {
  145. nativeWindowOpen: true
  146. // preload: path.join(__dirname, 'preload.js')
  147. },
  148. })
  149. root_url = `http://localhost:${port}`
  150. mainWindow.loadURL(root_url)
  151. // mainWindow.maximize();
  152. mainWindowState.manage(mainWindow);
  153. }
  154. if (process.defaultApp) {
  155. if (process.argv.length >= 2) {
  156. app.setAsDefaultProtocolClient('pinokio', process.execPath, [path.resolve(process.argv[1])])
  157. }
  158. } else {
  159. app.setAsDefaultProtocolClient('pinokio')
  160. }
  161. const gotTheLock = app.requestSingleInstanceLock()
  162. if (!gotTheLock) {
  163. app.quit()
  164. } else {
  165. app.on('second-instance', (event, argv) => {
  166. if (mainWindow) {
  167. if (mainWindow.isMinimized()) mainWindow.restore()
  168. mainWindow.focus()
  169. }
  170. let url = argv.pop()
  171. let u = new URL(url).search
  172. mainWindow.loadURL(`${root_url}${u}`)
  173. })
  174. // Create mainWindow, load the rest of the app, etc...
  175. app.whenReady().then(async () => {
  176. const WIDTH = 300;
  177. const HEIGHT = 300;
  178. let bounds = screen.getPrimaryDisplay().bounds;
  179. let x = Math.ceil(bounds.x + ((bounds.width - WIDTH) / 2));
  180. let y = Math.ceil(bounds.y + ((bounds.height - HEIGHT) / 2));
  181. // splash window
  182. const splash = new BrowserWindow({
  183. width: WIDTH,
  184. height: HEIGHT,
  185. // transparent: true,
  186. frame: false,
  187. alwaysOnTop: true,
  188. x,
  189. y,
  190. show: false
  191. });
  192. splash.type = "splash"
  193. splash.loadFile('splash.html');
  194. splash.once('ready-to-show', () => {
  195. splash.show()
  196. })
  197. await pinokiod.start(PORT)
  198. splash.hide();
  199. app.on('web-contents-created', attach)
  200. app.on('activate', function () {
  201. if (BrowserWindow.getAllWindows().length === 0) createWindow(PORT)
  202. })
  203. app.on('window-all-closed', function () {
  204. if (process.platform !== 'darwin') app.quit()
  205. })
  206. app.on('browser-window-created', (event, win) => {
  207. if (win.type !== "splash") {
  208. if (win.setTitleBarOverlay) {
  209. const overlay = titleBarOverlay("default")
  210. try {
  211. win.setTitleBarOverlay(overlay)
  212. } catch (e) {
  213. // console.log("ERROR", e)
  214. }
  215. }
  216. }
  217. })
  218. let all = BrowserWindow.getAllWindows()
  219. for(win of all) {
  220. try {
  221. if (win.setTitleBarOverlay) {
  222. const overlay = titleBarOverlay("default")
  223. win.setTitleBarOverlay(overlay)
  224. }
  225. } catch (e) {
  226. // console.log("E2", e)
  227. }
  228. }
  229. createWindow(PORT)
  230. splash.close();
  231. })
  232. app.on('open-url', (event, url) => {
  233. let u = new URL(url).search
  234. mainWindow.loadURL(`${root_url}${u}`)
  235. })
  236. }