diff --git a/package.json b/package.json index b1f029ca..d9f20166 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "fs-ext": "^2.0.0", "mobx": "^6.6.0", "mobx-react": "^7.5.0", + "node-fetch": "^3.2.10", "react": "^18.1.0", "react-dom": "^18.1.0", "sprintf-js": "^1.1.2", diff --git a/src/emain.ts b/src/emain.ts index 05579dd6..fe80c36a 100644 --- a/src/emain.ts +++ b/src/emain.ts @@ -1,7 +1,10 @@ import * as electron from "electron"; -import {acquireSCElectronLock} from "./base"; import * as path from "path"; import * as fs from "fs"; +import fetch from "node-fetch"; +import {debounce} from "throttle-debounce"; +import {acquireSCElectronLock} from "./base"; +import {handleJsonFetchResponse} from "./util"; let app = electron.app; app.setName("ScriptHaus"); @@ -75,10 +78,13 @@ function createRemotesWindow() { win.webContents.on("will-navigate", shNavHandler); } -function createMainWindow(size : {width : number, height : number}) { +function createMainWindow(clientData) { + let bounds = calcBounds(clientData); let win = new electron.BrowserWindow({ - width: size.width, - height: size.height, + x: bounds.x, + y: bounds.y, + width: bounds.width, + height: bounds.height, webPreferences: { preload: path.join(__dirname, "../src/preload.js"), }, @@ -144,34 +150,64 @@ function createMainWindow(size : {width : number, height : number}) { } }); win.webContents.on("will-navigate", shNavHandler); + win.on("resized", debounce(400, mainResizeHandler)); + win.on("moved", debounce(400, mainResizeHandler)); return win; } -function calcSize() { - let primaryDisplay = electron.screen.getPrimaryDisplay(); - let size = {width: 1800, height: 1200}; - if (primaryDisplay.size.width - 100 < size.width) { - size.width = primaryDisplay.size.width - 100; +function mainResizeHandler(e) { + let win = e.sender; + if (win == null || win.isDestroyed() || win.fullScreen) { + return; } - if (primaryDisplay.size.height - 100 < size.height) { - size.height = primaryDisplay.size.height - 100; + let bounds = win.getBounds(); + console.log("resize/move", win.getBounds()); + let winSize = {width: bounds.width, height: bounds.height, top: bounds.y, left: bounds.x}; + let url = "http://localhost:8080/api/set-winsize"; + fetch(url, {method: "post", body: JSON.stringify(winSize)}).then((resp) => handleJsonFetchResponse(url, resp)).catch((err) => { + console.log("error setting winsize", err) + }); +} + +function calcBounds(clientData) { + let primaryDisplay = electron.screen.getPrimaryDisplay(); + let size = {x: 50, y: 50, width: primaryDisplay.width-150, height: primaryDisplay.height-150}; + if (clientData != null && clientData.winsize != null && clientData.winsize.width > 0) { + let cwinSize = clientData.winsize; + if (cwinSize.width > 0) { + size.width = cwinSize.width; + } + if (cwinSize.height > 0) { + size.height = cwinSize.height; + } + if (cwinSize.top >= 0) { + size.y = cwinSize.top; + } + if (cwinSize.left >= 0) { + size.x = cwinSize.left; + } + } + if (size.width < 300) { + size.width = 300; + } + if (size.height < 300) { + size.height = 300; + } + if (primaryDisplay.size.width < size.width) { + size.width = primaryDisplay.size.width; + } + if (primaryDisplay.size.height < size.height) { + size.height = primaryDisplay.size.height; + } + if (primaryDisplay.size.width < size.x + size.width) { + size.x = primaryDisplay.size.width - size.width; + } + if (primaryDisplay.size.hgiehg < size.y + size.height) { + size.y = primaryDisplay.size.height - size.height; } return size; } -// primary display { width: 1680, height: 1050 } -app.whenReady().then(() => { - let size = calcSize(); - MainWindow = createMainWindow(size); - - app.on('activate', () => { - if (electron.BrowserWindow.getAllWindows().length === 0) { - let size = calcSize(); - createMainWindow(size); - } - }) -}); - app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() }); @@ -188,11 +224,43 @@ function getContextMenu() : any { return menu; } +function getClientData() { + let url = "http://localhost:8080/api/get-client-data"; + return fetch(url).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { + if (data == null) { + return null; + } + return data.data; + }).catch((err) => { + console.log("error getting client-data", err); + return null; + }); +} + electron.ipcMain.on("context-screen", (event, {screenId}, {x, y}) => { console.log("context-screen", screenId); let menu = getContextMenu(); menu.popup({x, y}); }); +async function createMainWindowWrap() { + let clientData = await getClientData(); + MainWindow = createMainWindow(clientData); + if (clientData && clientData.winsize.fullscreen) { + MainWindow.setFullScreen(true); + } +} +// ====== MAIN ====== // + +(async () => { + await app.whenReady(); + await createMainWindowWrap(); + app.on('activate', () => { + if (electron.BrowserWindow.getAllWindows().length === 0) { + createMainWindowWrap().then(); + } + }) +})(); + diff --git a/yarn.lock b/yarn.lock index 8da1ad25..f185c7a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2633,6 +2633,11 @@ csstype@^3.0.2: resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.0.tgz#4ddcac3718d787cf9df0d1b7d15033925c8f29f2" integrity sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA== +data-uri-to-buffer@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz#b5db46aea50f6176428ac05b73be39a57701a64b" + integrity sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA== + dayjs@^1.11.3: version "1.11.5" resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.5.tgz#00e8cc627f231f9499c19b38af49f56dc0ac5e93" @@ -3162,6 +3167,14 @@ fd-slicer@~1.1.0: dependencies: pend "~1.2.0" +fetch-blob@^3.1.2, fetch-blob@^3.1.4: + version "3.2.0" + resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9" + integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ== + dependencies: + node-domexception "^1.0.0" + web-streams-polyfill "^3.0.3" + figures@^3.0.0: version "3.2.0" resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af" @@ -3248,6 +3261,13 @@ follow-redirects@^1.0.0: resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5" integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA== +formdata-polyfill@^4.0.10: + version "4.0.10" + resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423" + integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g== + dependencies: + fetch-blob "^3.1.2" + forwarded@0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811" @@ -4594,6 +4614,11 @@ node-api-version@^0.1.4: dependencies: semver "^7.3.5" +node-domexception@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5" + integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ== + node-fetch@^2.6.7: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" @@ -4601,6 +4626,15 @@ node-fetch@^2.6.7: dependencies: whatwg-url "^5.0.0" +node-fetch@^3.2.10: + version "3.2.10" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.2.10.tgz#e8347f94b54ae18b57c9c049ef641cef398a85c8" + integrity sha512-MhuzNwdURnZ1Cp4XTazr69K0BTizsBroX7Zx3UgDSVcZYKF/6p0CBe4EUb/hLqmzVhl0UpYfgRljQ4yxE+iCxA== + dependencies: + data-uri-to-buffer "^4.0.0" + fetch-blob "^3.1.4" + formdata-polyfill "^4.0.10" + node-forge@^1: version "1.3.1" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.1.tgz#be8da2af243b2417d5f646a770663a92b7e9ded3" @@ -6159,6 +6193,11 @@ wcwidth@^1.0.1: dependencies: defaults "^1.0.3" +web-streams-polyfill@^3.0.3: + version "3.2.1" + resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6" + integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q== + webidl-conversions@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"