Skip to content

Commit

Permalink
fix(Kiosk): Makes it possible to start a Kiosk after the server has b…
Browse files Browse the repository at this point in the history
…een started. Closes #2728
  • Loading branch information
Alex Anderson committed Feb 1, 2020
1 parent 0f87c56 commit b8af5f0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
24 changes: 21 additions & 3 deletions public/electron/helpers/loadPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ const triggerWindow = require("./triggerWindow");
const http = require("http");
const https = require("https");
const {clearMenubar, setMenubar} = require("./setMenubar");
const {windows} = require("./multiWindow");
const {windows, addWindow} = require("./multiWindow");
const {setLoadedUrl} = require("./loadedUrl");
const initHotkeys = require("./hotkeys");
const initRemote = require("./remote");

function openPage(url, kiosk) {
setLoadedUrl(`${url}/client`);
for (let i = windows.length - 1; i >= 0; i--) {
if (windows[i] && windows[i].isDestroyed()) {
windows.splice(i, 1);
}
}
if (windows.length === 0) {
addWindow({
main: false,
x: 500,
y: 500,
loadedUrl: `${url}/client`,
server: false,
});
initHotkeys();
initRemote();
return;
}
windows.forEach(mainWindow => {
mainWindow && mainWindow.loadURL(`${url}/client`);
triggerWindow(mainWindow, kiosk);
Expand All @@ -21,7 +40,6 @@ module.exports = function loadPage(uri, kiosk) {
if (!url.includes("http")) {
url += "http://";
}

let httpHandler = http;
let agent = new http.Agent({});
if (url.includes("https")) {
Expand Down Expand Up @@ -62,6 +80,6 @@ module.exports = function loadPage(uri, kiosk) {
}).catch(err => {
// Tried too many times. Give up.
setMenubar();
return Promise.reject();
return Promise.reject(err);
});
};
21 changes: 9 additions & 12 deletions public/electron/helpers/multiWindow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {screen, BrowserWindow, ipcMain} = require("electron");
const {screen, BrowserWindow} = require("electron");
const path = require("path");
const {bonjour} = require("./bonjour");
const uuid = require("uuid");
Expand All @@ -25,6 +25,7 @@ module.exports.windows = windows;
let serverOpen = false;
let browserCount = 0;
let serverWindow = null;

function addWindow({main, x, y, loadedUrl, server}) {
browserCount++;
const config = {
Expand Down Expand Up @@ -87,21 +88,14 @@ function addWindow({main, x, y, loadedUrl, server}) {
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.

windows = windows.filter(w => w.uniqueId !== window.uniqueId);
windows.splice(
windows.findIndex(w => w.uniqueId === window.uniqueId),
1,
);
if (windows.length === 0) {
bonjour.stop();
}
});
console.log(
url.format({
pathname: path.join(
__dirname,
main ? "../index.html" : "../external.html",
),
protocol: "file:",
slashes: true,
}),
);
window.loadURL(
loadedUrl
? loadedUrl
Expand All @@ -114,6 +108,9 @@ function addWindow({main, x, y, loadedUrl, server}) {
slashes: true,
}),
);
window.focus();
window.webContents.focus();

windows.push(window);
}
}
Expand Down
13 changes: 12 additions & 1 deletion public/electron/helpers/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ document.addEventListener("DOMContentLoaded", async function() {
});

window.cancelAutostart = function cancelAutostart() {
console.log("stopping");
ipcRenderer.send("cancelServerAutostart");
};

Expand Down Expand Up @@ -171,6 +170,18 @@ document.addEventListener(
if (openEl) {
openEl.addEventListener("click", openBrowser);
}
const kioskEl = document.getElementById("open-client-window");
if (kioskEl) {
kioskEl.addEventListener("click", function openClient() {
console.log("Opening Client");
ipcRenderer.send("loadPage", {
url: `${printUrl()}/client`,
kiosk: false,
auto: false,
});
});
}

// Auto Update
const autoUpdateEl = document.getElementById("auto-update");
const autoUpdateLabel = document.getElementById("auto-update-label");
Expand Down
5 changes: 2 additions & 3 deletions public/electron/helpers/startApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ module.exports = () => {
let port =
process.env.PORT ||
settings.get("port") ||
semver.parse(os.release()).major >= 18
? 443
: 4444;
(semver.parse(os.release()).major >= 18 ? 443 : 4444);
let httpOnly =
process.env.HTTP_ONLY === "true" ||
settings.get("httpOnly") === "true" ||
Expand Down Expand Up @@ -67,6 +65,7 @@ module.exports = () => {
settings.set("autostart", loadUrl);
}
require("./loadPage")(loadUrl, kiosk).catch(err => {
console.log(err);
settings.set("autostart", null);
bonjour.start();
});
Expand Down
3 changes: 2 additions & 1 deletion public/electron/helpers/triggerWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = function triggerWindow(mainWindow, kiosk) {
initHotkeys();
}
initRemote();

mainWindow.focus();
mainWindow.webContents.focus();
mainWindow.on("closed", function() {
app.quit();
});
Expand Down
3 changes: 3 additions & 0 deletions public/electron/server.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
<button id="open-in-browser" onclick="openBrowser()">
Open in Browser
</button>
<button id="open-client-window">
Open Kiosk Window
</button>
<button id="stop-auto-start" hidden onclick="cancelAutostart()">
Stop Automatically Starting Server
</button>
Expand Down

0 comments on commit b8af5f0

Please sign in to comment.