Skip to content

Commit

Permalink
Implement #46
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed May 22, 2024
1 parent b993741 commit 2dc9396
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { app, BrowserWindow, dialog, ipcMain } from "electron";
import {
app,
BrowserWindow,
dialog,
ipcMain,
Rectangle,
screen,
} from "electron";

import { TrackAudioAfv, AfvEventTypes } from "trackaudio-afv";
import { Configuration } from "./config.d";
Expand All @@ -20,6 +27,8 @@ Sentry.init({
let version: string;
let mainWindow: BrowserWindow;

const defaultWindowSize = { width: 800, height: 660 };

let currentConfiguration: Configuration = {
audioApi: -1,
audioInputDeviceId: "",
Expand All @@ -44,20 +53,44 @@ const setAudioSettings = () => {
currentConfiguration.audioApi || -1,
currentConfiguration.audioInputDeviceId || "",
currentConfiguration.headsetOutputDeviceId || "",
currentConfiguration.speakerOutputDeviceId || "",
currentConfiguration.speakerOutputDeviceId || ""
);
TrackAudioAfv.SetHardwareType(currentConfiguration.hardwareType || 0);
};

const restoreWindowBounds = (win: BrowserWindow) => {
const savedBounds = store.get("bounds");
const boundsRectangle = savedBounds as Rectangle;
if (savedBounds !== undefined && savedBounds !== null) {
const screenArea = screen.getDisplayMatching(boundsRectangle).workArea;
if (
boundsRectangle.x > screenArea.x + screenArea.width ||
boundsRectangle.x < screenArea.x ||
boundsRectangle.y < screenArea.y ||
boundsRectangle.y > screenArea.y + screenArea.height
) {
// Reset window into existing screenarea
win.setBounds({
x: 0,
y: 0,
width: defaultWindowSize.width,
height: defaultWindowSize.height,
});
} else {
win.setBounds(boundsRectangle);
}
}
};

const createWindow = (): void => {
// Set the store CID
TrackAudioAfv.SetCid(currentConfiguration.cid || "");
TrackAudioAfv.SetRadioGain(currentConfiguration.radioGain || 0.5);

// Create the browser window.
mainWindow = new BrowserWindow({
height: 660,
width: 800,
height: defaultWindowSize.height,
width: defaultWindowSize.width,
minWidth: 210,
minHeight: 120,
webPreferences: {
Expand All @@ -70,6 +103,8 @@ const createWindow = (): void => {
if (process.platform !== "darwin") {
mainWindow.setMenu(null);
}

restoreWindowBounds(mainWindow);
// and load the index.html of the app.
void mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);

Expand All @@ -89,8 +124,11 @@ const createWindow = (): void => {

if (response == 1) {
e.preventDefault();
return;
}
}

store.set("bounds", mainWindow.getBounds());
});
};

Expand All @@ -100,7 +138,7 @@ const createWindow = (): void => {
app.on("ready", () => {
// load the configuration
currentConfiguration = JSON.parse(
store.get("configuration", "{}") as string,
store.get("configuration", "{}") as string
) as Configuration;

if (currentConfiguration.consentedToTelemetry === undefined) {
Expand Down Expand Up @@ -258,7 +296,7 @@ ipcMain.handle(
"audio-add-frequency",
(_, frequency: number, callsign: string) => {
return TrackAudioAfv.AddFrequency(frequency, callsign);
},
}
);

ipcMain.handle("audio-remove-frequency", (_, frequency: number) => {
Expand All @@ -274,17 +312,17 @@ ipcMain.handle(
tx: boolean,
xc: boolean,
onSpeaker: boolean,
crossCoupleAcross: boolean,
crossCoupleAcross: boolean
) => {
return TrackAudioAfv.SetFrequencyState(
frequency,
rx,
tx,
xc,
onSpeaker,
crossCoupleAcross,
crossCoupleAcross
);
},
}
);

ipcMain.handle("audio-get-frequency-state", (_, frequency: number) => {
Expand Down Expand Up @@ -357,15 +395,15 @@ ipcMain.handle(
type: "none" | "info" | "error" | "question" | "warning",
title: string,
message: string,
buttons: string[],
buttons: string[]
) => {
return dialog.showMessageBox(mainWindow, {
type,
title,
buttons,
message,
});
},
}
);

ipcMain.handle("get-version", () => {
Expand Down

0 comments on commit 2dc9396

Please sign in to comment.