Skip to content

Commit

Permalink
feat(niw): close window once server created
Browse files Browse the repository at this point in the history
  • Loading branch information
ariscript committed Nov 25, 2021
1 parent 146cca6 commit 8091ef0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { app, BrowserWindow, ipcMain } from "electron";
import updater from "update-electron-app";
import log from "electron-log";

import createInstance from "./instances/createInstance";

Expand Down Expand Up @@ -59,8 +60,8 @@ app.on("activate", () => {

ipcMain.on("newInstanceWindow", () => {
const newInstanceWindow = new BrowserWindow({
height: 600,
width: 800,
height: 500,
width: 400,
webPreferences: {
preload: NEW_INSTANCE_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
Expand All @@ -69,4 +70,10 @@ ipcMain.on("newInstanceWindow", () => {
newInstanceWindow.loadURL(NEW_INSTANCE_WINDOW_WEBPACK_ENTRY);
});

ipcMain.on("closeWindow", (e) => {
const sender = BrowserWindow.fromWebContents(e.sender);
log.debug(sender);
sender?.close();
});

ipcMain.handle("createInstance", createInstance);
5 changes: 2 additions & 3 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import log from "electron-log";
import type { IpcChannels } from "./types";

contextBridge.exposeInMainWorld("ipc", {
newInstanceWindow: () => {
ipcRenderer.send("newInstanceWindow");
},
newInstanceWindow: () => ipcRenderer.send("newInstanceWindow"),
createInstance: (opts) => ipcRenderer.invoke("createInstance", opts),
closeWindow: () => ipcRenderer.send("closeWindow"),
} as IpcChannels);

contextBridge.exposeInMainWorld("log", log.functions);
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface InstanceOptions {
export type IpcChannels = {
newInstanceWindow: () => void;
createInstance: (opts: InstanceOptions) => Promise<boolean>;
closeWindow: () => void;
};
15 changes: 14 additions & 1 deletion src/windows/NewInstanceWIndow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ const NewInstanceWindow = () => {

const [agreed, setAgreed] = useState(false);

const [err, setErr] = useState(false);

const handleFormSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
await ipc.createInstance({
const result = await ipc.createInstance({
name,
type,
version,
javaPath,
jvmArgs,
});

log.info(`Status: ${result ? "good" : "bad"}`);

if (result) {
ipc.closeWindow();
} else {
setErr(true);
}
};

return (
<div>
{err && <div className="rounded-md p-2 m-2 w-max bg-red-400">
Error creating server. Check Logs for more information.
</div>}
<h2>New Instance</h2>
<form onSubmit={handleFormSubmit}>
<LabelInput
Expand Down

0 comments on commit 8091ef0

Please # to comment.