Skip to content

Commit

Permalink
fixed menu option states between metric and imperial when switching b…
Browse files Browse the repository at this point in the history
…etween different aircraft
  • Loading branch information
N129BZ committed Apr 19, 2024
1 parent 3694545 commit b636a39
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 68 deletions.
87 changes: 63 additions & 24 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ const isLinux = process.platform === "linux" ? true : false;
app.commandLine.appendSwitch ("disable-http-cache");

var mainWindow;

var appData = loadAppData();

const isDebug = appData.settings.debug;
var usemetric = appData.settings.units === "metric";
var isDebug = appData.settings.debug;


function loadAppData() {
let adf = "";
// make sure the file is stored in userData folder
if (!fs.existsSync(jsonPath)) {
fs.copyFileSync(path.join(__dirname, "aircraftwb.json"), jsonPath);
fs.copyFileSync(path.join(__dirname, "zodiacwb.json"), jsonPath);
}
adf = fs.readFileSync(jsonPath, "utf8");
return JSON.parse(adf);
Expand Down Expand Up @@ -74,10 +73,16 @@ const template = [
label: 'Units of Measure',
submenu: [
{ label: "Pounds/Inches",
click: () => app.emit('toggleimperial')
id: "pi",
type: "radio",
checked: getUOMChecked("imperial"),
click: () => app.emit('toggleuom')
},
{ label: "Kilograms/Millimeters",
click: () => app.emit('togglemetric')
id: "km",
type: "radio",
checked: getUOMChecked("metric"),
click: () => app.emit('toggleuom')
}
]
},
Expand All @@ -86,27 +91,27 @@ const template = [
submenu: [
{label: "Zenith ch601xl/ch650",
type: "radio",
checked: getChecked("ch650"),
checked: getAircraftChecked("ch650"),
click: () => mainWindow.webContents.send("acselect", "ch650")
},
{label: "Zenith ch701",
type: "radio",
checked: getChecked("ch701"),
checked: getAircraftChecked("ch701"),
click: () => mainWindow.webContents.send("acselect", "ch701")
},
{label: "Zenith ch750",
type: "radio",
checked: getChecked("ch750"),
checked: getAircraftChecked("ch750"),
click: () => mainWindow.webContents.send("acselect", "ch750")
},
{label: "Vans RV9a",
type: "radio",
checked:getChecked("rv9a"),
checked:getAircraftChecked("rv9a"),
click: () => mainWindow.webContents.send("acselect", "rv9a")
},
{label: "Vans RV9",
type: "radio",
checked: getChecked("rv9"),
checked: getAircraftChecked("rv9"),
click: () => mainWindow.webContents.send("acselect", "rv9")
}
]
Expand All @@ -129,11 +134,15 @@ nativeTheme.themeSource = appData.settings.theme;

function saveAppData() {
fs.writeFileSync(jsonPath, JSON.stringify(appData, null, 4));
loadAppData();
//loadAppData();
}

const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
if (appData.settings.currentview != "ch650") {
let mi = menu.getMenuItemById("km");
mi.visible = false;
}

if (require('electron-squirrel-startup')) app.quit();

Expand Down Expand Up @@ -171,16 +180,6 @@ function createWindow () {
saveAppData();
mainWindow.webContents.send('toggletheme');
});
app.on('toggleimperial', () => {
appData.settings.units = "imperial";
saveAppData();
mainWindow.reload();
});
app.on('togglemetric', () => {
appData.settings.units = "metric";
saveAppData();
mainWindow.reload();
});
app.on('filesave', () => {
mainWindow.webContents.capturePage()
});
Expand Down Expand Up @@ -209,6 +208,23 @@ function createWindow () {
inConvertMode = !inConvertMode;
mainWindow.webContents.send("convert");
});
app.on('toggleuom', function() {
var uom = appData.settings.units;
switch (uom) {
case "metric":
appData.settings.units = "imperial";
break;
case "imperial":
if (appData.settings.currentview === "ch650") {
appData.settings.units = "metric";
setMetricOptionProperties(true);
} else {
setMetricOptionProperties(false)
}
}
saveAppData();
mainWindow.reload();
});
}

function toggleTheme() {
Expand Down Expand Up @@ -246,7 +262,13 @@ ipcMain.on('appdata:save', (e, newappdata) => {
mainWindow.reload();
});

ipcMain.on('function:selectaircraft', () => {
ipcMain.on('function:selectaircraft',(e, aircraft) => {
console.log(aircraft);
if (aircraft === "ch650") {
setMetricOptionProperties(true);
} else {
setMetricOptionProperties(false);
}
mainWindow.reload();
});

Expand All @@ -258,7 +280,13 @@ ipcMain.on('function:print', (e, printpdf) => {
handlePrinting(printpdf);
});

function getChecked(oneAirplane) {
function setMetricOptionProperties(isvisible) {
let mi = menu.getMenuItemById("km");
mi.visible = isvisible;
mi.enabled = isvisible;
}

function getAircraftChecked(oneAirplane) {
if (oneAirplane === appData.settings.currentview) {
return true;
}
Expand All @@ -267,6 +295,17 @@ function getChecked(oneAirplane) {
}
}

function getUOMChecked(uom) {
let state = false;
let valid = true;
if (appData.settings.currentview === "ch650") {
state = uom === appData.settings.units;
} else {
state = uom === "imperial";
}
return state;
}

function handlePrinting() {
if (appData.settings.printaspdf) {
printToPdf();
Expand Down
8 changes: 4 additions & 4 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ const { contextBridge, ipcRenderer } = require("electron");

contextBridge.exposeInMainWorld('electronAPI', {
onAircraftSelect: (callback) => ipcRenderer.on("acselect", (_event, selection) => callback(selection)),
onToggleUOM: (callback) => ipcRenderer.on("uomselect", (_event, selection) => callback(selection)),
saveappdata: (newappdata) => ipcRenderer.send('appdata:save', newappdata),
logentry: (entrytype, entry) => ipcRenderer.send('function:logentry', entrytype, entry),
getappdata: () => ipcRenderer.invoke('appdata:get'),
showdev: (devstate) => ipcRenderer.send('menu:showdev', devstate),
printscreen: (printpdf) => ipcRenderer.send('function:print', printpdf),
selectaircraft: () => ipcRenderer.send('function:selectaircraft'),
selectaircraft: (aircraft) => ipcRenderer.send('function:selectaircraft', aircraft),
reload: () => ipcRenderer.send('function:reload'),
exitapp: () => ipcRenderer.send('function:exit'),
receive: (channel, func) => {
let validChannels = [
"toggletheme",
"toggleimperial",
"togglemetric",
"printpage"
"printpage",
"toggleuom"
];

if (validChannels.includes(channel)) {
Expand Down
9 changes: 5 additions & 4 deletions renderer/css/zodiacwb.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ body {
}
.ch650view {
position: absolute;
top: 521px;
top: 527px;
left: 70px;
width: 700px;
height: 447px;
Expand All @@ -25,14 +25,15 @@ body {
.ch650rectangle {
position: absolute;
top: 117px;
left: 428px;
left: 220px;
width: 56px;
height: 164px;
background: rgba(222, 236, 228, 0.5);
}
.rv9view {
position: absolute;
top: 587px;
left: 81px;
left: 64px;
height: 332px;
width: 706px;
background-color: white;
Expand All @@ -51,7 +52,7 @@ body {
.rv9aview {
position: absolute;
top: 579px;
left: 62px;
left: 29px;
height: 332px;
width: 754px;
background-color: white;
Expand Down
Binary file modified renderer/img/ch650_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified renderer/img/ch650_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 23 additions & 36 deletions renderer/js/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,13 @@ function addArray(theArray) {

function placeDots(weight, moment) {
let rgba = chartcanvas.getContext('2d', { willReadFrequently: true }).getImageData(20,20,1,1).data
let color = appData.settings.overbgcolor;
let tcolor = appData.settings.overbgcolor;
let bgcolor = appData.settings.overbgcolor;
let fgcolor = appData.settings.overfgcolor;
let color;
let tcolor;
let bgcolor;
let fgcolor;
let maxweight = valData.maxgross;
let minweight = valData.mingross;
let isoverwt = true;
let plotleft = false;
let plotdown = true;
let mincg = 0;
let maxcg = 0;
let mom = 0;
Expand All @@ -481,8 +479,6 @@ function placeDots(weight, moment) {
mincg = usemetric ? valData.mincg : Math.round(valData.mincg * 25.4);
maxcg = usemetric ? valData.maxcg : Math.round(valData.maxcg * 25.4);
mom = usemetric ? moment : Math.round(moment * 25.4);
// ch650 on-aircraft cg dot is plotted from the right
plotleft = false;
break;
case "rv9":
case "rv9a":
Expand All @@ -492,8 +488,8 @@ function placeDots(weight, moment) {
break;
}

x = plotX(chartcanvas, maxcg, mincg, mom, plotleft);
y = plotY(chartcanvas, maxweight, minweight, weight, plotdown);
x = plotX(chartcanvas, maxcg, mincg, mom);
y = plotY(chartcanvas, maxweight, minweight, weight);

if ((weight <= maxweight && weight >= minweight) && (mom >= mincg && mom <= maxcg)) {
if ((rgba[0] === 221 && rgba[1] === 238 && rgba[2] === 235) ||
Expand All @@ -504,7 +500,13 @@ function placeDots(weight, moment) {
fgcolor = appData.settings.underfgcolor;
isoverwt = false;
}
} else {
color = appData.settings.overbgcolor;
tcolor = appData.settings.overbgcolor;
bgcolor = appData.settings.overbgcolor;
fgcolor = appData.settings.overfgcolor;
}

}
catch (error){
electronAPI.logentry(logEntryType.error, error.toString());
Expand All @@ -519,10 +521,10 @@ function placeDots(weight, moment) {
let crosshair = document.getElementById("chartcrosshair");
crosshair.setAttribute("style", "font-size:25px;position:relative;top:-6px;left:0px;color:white;")

placeAcDot(plotleft, plotdown, maxcg, mincg, maxweight, minweight, weight, mom, bgcolor, fgcolor);
placeAcDot(maxcg, mincg, maxweight, minweight, weight, mom, bgcolor, fgcolor);
}

function placeAcDot(plotleft, plotdown, maxcg, mincg, maxweight, minweight, weight, moment, bgcolor, fgcolor) {
function placeAcDot(maxcg, mincg, maxweight, minweight, weight, moment, bgcolor, fgcolor) {
let elements = getCogElements();
let rect = elements.rectangle;
let dot = elements.dot;
Expand All @@ -532,10 +534,8 @@ function placeAcDot(plotleft, plotdown, maxcg, mincg, maxweight, minweight, weig

try {

if (currentview === "ch650") plotleft = true;

x = plotX(rect, maxcg, mincg, moment, plotleft);
y = plotY(rect, maxweight, minweight, weight, plotdown);
x = plotX(rect, maxcg, mincg, moment);
y = plotY(rect, maxweight, minweight, weight);

dot.setAttribute(
"style", `height:7px;width:7px;border-radius:50%;position:relative;top:${y-9}px;` +
Expand Down Expand Up @@ -632,8 +632,9 @@ window.electronAPI.onAircraftSelect((aircraft) => {
console.log(aircraft);
currentview = aircraft;
appData.settings.currentview = aircraft;
if (aircraft != "ch650") appData.settings.usemetric = false;
saveAppData();
window.electronAPI.selectaircraft();
window.electronAPI.selectaircraft(aircraft);
});

function togglePrintPDF(chkbox) {
Expand All @@ -650,21 +651,14 @@ function togglePrintPDF(chkbox) {
* @param {number} maxGross
* @param {number} minGross
* @param {number} acWeight
* @param {boolean} plotDown
* @returns {number}
* @returns {number}
*/
function plotY(chart, maxGross, minGross, acWeight, plotDown = true) {
let output = 0;
function plotY(chart, maxGross, minGross, acWeight) {
let range = maxGross - minGross;
let pxfactor = chart.height / range;
let diff = maxGross - acWeight;
let offset = diff * pxfactor;
if (plotDown) {
output = offset;
} else {
output = chart.height - offset;
}
return output;
return offset;
}

/**
Expand All @@ -673,20 +667,13 @@ function plotY(chart, maxGross, minGross, acWeight, plotDown = true) {
* @param {number} maxCg
* @param {number} minCg
* @param {number} acMoment
* @param {boolean} plotLeft
* @returns {number}
*/
function plotX(chart, maxCg, minCg, acMoment, plotLeft = true) {
let output = 0;
function plotX(chart, maxCg, minCg, acMoment) {
let range = maxCg - minCg;
let pxfactor = chart.width / range;
let diff = maxCg - acMoment;
let offset = diff * pxfactor;
if (plotLeft) {
output = offset;
} else {
output = chart.width - offset;
}
return output;
return offset;
}

0 comments on commit b636a39

Please sign in to comment.