Skip to content

Commit

Permalink
Persist all settings
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
1j01 committed May 8, 2024
1 parent 09bd433 commit 7746a98
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
56 changes: 47 additions & 9 deletions core/tracky-mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,27 @@ TrackyMouse.init = function (div) {
function deserializeSettings(settings) {
// TODO: DRY with deserializeSettings in electron-main.js
if ("globalSettings" in settings) {
if ("swapMouseButtons" in settings.globalSettings) {
// Don't use `in` here. Must ignore `undefined` values for the settings to default to the HTML template's defaults in the Electron app.
if (settings.globalSettings.swapMouseButtons !== undefined) {
swapMouseButtons = settings.globalSettings.swapMouseButtons;
swapMouseButtonsCheckbox.checked = swapMouseButtons;
}
if (settings.globalSettings.mirrorCameraView !== undefined) {
mirror = settings.globalSettings.mirrorCameraView;
mirrorCheckbox.checked = mirror;
}
if (settings.globalSettings.headTrackingSensitivityX !== undefined) {
sensitivityX = settings.globalSettings.headTrackingSensitivityX;
sensitivityXSlider.value = sensitivityX * 1000;
}
if (settings.globalSettings.headTrackingSensitivityY !== undefined) {
sensitivityY = settings.globalSettings.headTrackingSensitivityY;
sensitivityYSlider.value = sensitivityY * 1000;
}
if (settings.globalSettings.headTrackingAcceleration !== undefined) {
acceleration = settings.globalSettings.headTrackingAcceleration;
accelerationSlider.value = acceleration * 100;
}
}
}
const formatVersion = 1;
Expand All @@ -814,11 +831,11 @@ TrackyMouse.init = function (div) {
formatName,
globalSettings: {
swapMouseButtons,
mirrorCameraView: mirror,
headTrackingSensitivityX: sensitivityX,
headTrackingSensitivityY: sensitivityY,
headTrackingAcceleration: acceleration,
// TODO:
// mirrorCameraView,
// headTrackingSensitivityX,
// headTrackingSensitivityY,
// headTrackingAcceleration,
// eyeTrackingSensitivityX,
// eyeTrackingSensitivityY,
// eyeTrackingAcceleration,
Expand Down Expand Up @@ -853,17 +870,37 @@ TrackyMouse.init = function (div) {
}
};

sensitivityXSlider.onchange = () => {
sensitivityXSlider.onchange = (event) => {
sensitivityX = sensitivityXSlider.value / 1000;
// HACK: using event argument as a flag to indicate when it's not the initial setup,
// to avoid saving the default settings before the actual preferences are loaded.
if (event) {
setOptions({ globalSettings: { headTrackingSensitivityX: sensitivityX } });
}
};
sensitivityYSlider.onchange = () => {
sensitivityYSlider.onchange = (event) => {
sensitivityY = sensitivityYSlider.value / 1000;
// HACK: using event argument as a flag to indicate when it's not the initial setup,
// to avoid saving the default settings before the actual preferences are loaded.
if (event) {
setOptions({ globalSettings: { headTrackingSensitivityY: sensitivityY } });
}
};
accelerationSlider.onchange = () => {
accelerationSlider.onchange = (event) => {
acceleration = accelerationSlider.value / 100;
// HACK: using event argument as a flag to indicate when it's not the initial setup,
// to avoid saving the default settings before the actual preferences are loaded.
if (event) {
setOptions({ globalSettings: { headTrackingAcceleration: acceleration } });
}
};
mirrorCheckbox.onchange = () => {
mirrorCheckbox.onchange = (event) => {
mirror = mirrorCheckbox.checked;
// HACK: using event argument as a flag to indicate when it's not the initial setup,
// to avoid saving the default settings before the actual preferences are loaded.
if (event) {
setOptions({ globalSettings: { mirrorCameraView: mirror } });
}
};
swapMouseButtonsCheckbox.onchange = (event) => {
swapMouseButtons = swapMouseButtonsCheckbox.checked;
Expand All @@ -873,6 +910,7 @@ TrackyMouse.init = function (div) {
setOptions({ globalSettings: { swapMouseButtons } });
}
};
// Load defaults from HTML
mirrorCheckbox.onchange();
swapMouseButtonsCheckbox.onchange();
sensitivityXSlider.onchange();
Expand Down
32 changes: 26 additions & 6 deletions desktop-app/src/electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const { setMouseLocation: setMouseLocationWithoutTracking, getMouseLocation, cli
app.commandLine.appendSwitch("--disable-gpu-process-crash-limit");

// Settings
let swapMouseButtons = false; // for left-handed users on Windows, where serenade-driver is affected by the system setting
// (actual defaults come from the HTML template)
let swapMouseButtons = undefined; // for left-handed users on Windows, where serenade-driver is affected by the system setting
let mirror = undefined;
let sensitivityX = undefined;
let sensitivityY = undefined;
let acceleration = undefined;

const settingsFile = path.join(app.getPath('userData'), 'tracky-mouse-settings.json');
const formatName = "tracky-mouse-settings";
Expand Down Expand Up @@ -58,16 +63,17 @@ async function saveSettings() {
await fs.writeFile(settingsFile, JSON.stringify(serializeSettings(), null, '\t'));
}
function serializeSettings() {
// TODO: DRY with serializeSettings in tracky-mouse.js
return {
formatVersion,
formatName,
globalSettings: {
swapMouseButtons,
mirrorCameraView: mirror,
headTrackingSensitivityX: sensitivityX,
headTrackingSensitivityY: sensitivityY,
headTrackingAcceleration: acceleration,
// TODO:
// mirrorCameraView,
// headTrackingSensitivityX,
// headTrackingSensitivityY,
// headTrackingAcceleration,
// eyeTrackingSensitivityX,
// eyeTrackingSensitivityY,
// eyeTrackingAcceleration,
Expand All @@ -78,13 +84,27 @@ function serializeSettings() {
};
};
function deserializeSettings(settings) {
// TODO: DRY with deserializeSettings in tracky-mouse.js
// Handles partial settings objects,
// to allow manually editing the settings file, removing settings to reset them to their defaults,
// as well as accepting settings updates over IPC from the UI.
if ("globalSettings" in settings) {
if ("swapMouseButtons" in settings.globalSettings) {
// Don't use `in` here. Must ignore `undefined` values for the settings to default to the HTML template's defaults in the Electron app.
if (settings.globalSettings.swapMouseButtons !== undefined) {
swapMouseButtons = settings.globalSettings.swapMouseButtons;
}
if (settings.globalSettings.mirrorCameraView !== undefined) {
mirror = settings.globalSettings.mirrorCameraView;
}
if (settings.globalSettings.headTrackingSensitivityX !== undefined) {
sensitivityX = settings.globalSettings.headTrackingSensitivityX;
}
if (settings.globalSettings.headTrackingSensitivityY !== undefined) {
sensitivityY = settings.globalSettings.headTrackingSensitivityY;
}
if (settings.globalSettings.headTrackingAcceleration !== undefined) {
acceleration = settings.globalSettings.headTrackingAcceleration;
}
}
}

Expand Down

0 comments on commit 7746a98

Please sign in to comment.