Skip to content

Commit

Permalink
chore: reformat code using prettier
Browse files Browse the repository at this point in the history
Signed-off-by: Sefa Eyeoglu <[email protected]>
  • Loading branch information
Scrumplex committed Apr 11, 2023
1 parent e2b78ae commit fcc1be5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 60 deletions.
7 changes: 5 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
checks = {
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = { nixfmt.enable = true; };
hooks = {
nixfmt.enable = true;
prettier.enable = true;
};
};
};
devShells.default = pkgs.mkShell {
inherit (self.checks.${system}.pre-commit-check) shellHook;
packages = with pkgs; [ nodePackages.pnpm ];
packages = with pkgs; [ nodePackages.pnpm nodePackages.prettier ];
};
});
}
115 changes: 80 additions & 35 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,46 @@ import {
} from "decky-frontend-lib";
import { VFC, useState, useEffect } from "react";
import { FaEyeDropper } from "react-icons/fa";
import { loadSettingsFromLocalStorage, Settings, saveSettingsToLocalStorage, GammaSetting } from "./settings";
import {
loadSettingsFromLocalStorage,
Settings,
saveSettingsToLocalStorage,
GammaSetting,
} from "./settings";
import { RunningApps, Backend, DEFAULT_APP } from "./util";

// Appease TypeScript
declare var SteamClient: any;

let settings: Settings;

const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, listenModeFn: (enabled: boolean) => void}> = ({ applyFn, resetFn, listenModeFn }) => {
const Content: VFC<{
applyFn: (appId: string) => void;
resetFn: () => void;
listenModeFn: (enabled: boolean) => void;
}> = ({ applyFn, resetFn, listenModeFn }) => {
const [initialized, setInitialized] = useState<boolean>(false);

const [currentEnabled, setCurrentEnabled] = useState<boolean>(true);
const [currentAppOverride, setCurrentAppOverride] = useState<boolean>(false);
const [currentAppOverridable, setCurrentAppOverridable] = useState<boolean>(false);
const [currentTargetSaturation, setCurrentTargetSaturation] = useState<number>(100);
const [currentTargetGammaLinear, setCurrentTargetGammaLinear] = useState<boolean>(true);
const [currentTargetGammaRed, setCurrentTargetGammaRed] = useState<number>(100);
const [currentTargetGammaGreen, setCurrentTargetGammaGreen] = useState<number>(100);
const [currentTargetGammaBlue, setCurrentTargetGammaBlue] = useState<number>(100);
const [currentAppOverridable, setCurrentAppOverridable] =
useState<boolean>(false);
const [currentTargetSaturation, setCurrentTargetSaturation] =
useState<number>(100);
const [currentTargetGammaLinear, setCurrentTargetGammaLinear] =
useState<boolean>(true);
const [currentTargetGammaRed, setCurrentTargetGammaRed] =
useState<number>(100);
const [currentTargetGammaGreen, setCurrentTargetGammaGreen] =
useState<number>(100);
const [currentTargetGammaBlue, setCurrentTargetGammaBlue] =
useState<number>(100);

const refresh = () => {
// prevent updates while we are reloading
setInitialized(false);

setCurrentEnabled(settings.enabled)
setCurrentEnabled(settings.enabled);

const activeApp = RunningApps.active();
// does active app have a saved setting
Expand All @@ -66,15 +81,16 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
setCurrentTargetGammaBlue(settings.appGamma(activeApp).gainB);

setInitialized(true);
}
};

useEffect(() => {
if (!initialized || !currentEnabled)
return;
if (!initialized || !currentEnabled) return;

let activeApp = RunningApps.active();
if (currentAppOverride && currentAppOverridable) {
console.log(`Setting app ${activeApp} to saturation ${currentTargetSaturation}`);
console.log(
`Setting app ${activeApp} to saturation ${currentTargetSaturation}`
);
} else {
console.log(`Setting global to saturation ${currentTargetSaturation}`);
activeApp = DEFAULT_APP;
Expand All @@ -86,32 +102,45 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
}, [currentTargetSaturation, currentEnabled, initialized]);

useEffect(() => {
if (!initialized || !currentEnabled)
return;
if (!initialized || !currentEnabled) return;

let activeApp = RunningApps.active();
if (currentAppOverride && currentAppOverridable) {
console.log(`Setting app ${activeApp} to${currentTargetGammaLinear ? " linear" : ""} gamma ${currentTargetGammaRed} ${currentTargetGammaGreen} ${currentTargetGammaBlue}`);
console.log(
`Setting app ${activeApp} to${
currentTargetGammaLinear ? " linear" : ""
} gamma ${currentTargetGammaRed} ${currentTargetGammaGreen} ${currentTargetGammaBlue}`
);
} else {
console.log(`Setting global to${currentTargetGammaLinear ? " linear" : ""} gamma ${currentTargetGammaRed} ${currentTargetGammaGreen} ${currentTargetGammaBlue}`);
console.log(
`Setting global to${
currentTargetGammaLinear ? " linear" : ""
} gamma ${currentTargetGammaRed} ${currentTargetGammaGreen} ${currentTargetGammaBlue}`
);
activeApp = DEFAULT_APP;
}
settings.ensureApp(activeApp).ensureGamma().linear = currentTargetGammaLinear;
settings.ensureApp(activeApp).ensureGamma().linear =
currentTargetGammaLinear;
settings.ensureApp(activeApp).ensureGamma().gainR = currentTargetGammaRed;
settings.ensureApp(activeApp).ensureGamma().gainG = currentTargetGammaGreen;
settings.ensureApp(activeApp).ensureGamma().gainB = currentTargetGammaBlue;
applyFn(RunningApps.active());

saveSettingsToLocalStorage(settings);
}, [currentTargetGammaLinear, currentTargetGammaRed, currentTargetGammaGreen, currentTargetGammaBlue, currentEnabled, initialized]);
}, [
currentTargetGammaLinear,
currentTargetGammaRed,
currentTargetGammaGreen,
currentTargetGammaBlue,
currentEnabled,
initialized,
]);

useEffect(() => {
if (!initialized || !currentEnabled)
return;
if (!initialized || !currentEnabled) return;

const activeApp = RunningApps.active();
if (activeApp == DEFAULT_APP)
return;
if (activeApp == DEFAULT_APP) return;

console.log(`Setting app ${activeApp} to override ${currentAppOverride}`);

Expand All @@ -128,13 +157,11 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
}, [currentAppOverride, currentEnabled, initialized]);

useEffect(() => {
if (!initialized)
return;
if (!initialized) return;

listenModeFn(currentEnabled);

if (!currentEnabled)
resetFn();
if (!currentEnabled) resetFn();

settings.enabled = currentEnabled;
saveSettingsToLocalStorage(settings);
Expand All @@ -157,12 +184,18 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
/>
</PanelSectionRow>
</PanelSection>
{currentEnabled &&
{currentEnabled && (
<PanelSection title="Profile">
<PanelSectionRow>
<ToggleField
label="Use per-game profile"
description={"Currently using " + (currentAppOverride && currentAppOverridable ? "per-app" : "global") + " profile"}
description={
"Currently using " +
(currentAppOverride && currentAppOverridable
? "per-app"
: "global") +
" profile"
}
checked={currentAppOverride && currentAppOverridable}
disabled={!currentAppOverridable}
onChange={(override) => {
Expand Down Expand Up @@ -197,7 +230,9 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
<PanelSectionRow>
<SliderField
label="Gamma Red"
description={`Control${currentTargetGammaLinear ? " linear" : ""} gamma gain for red`}
description={`Control${
currentTargetGammaLinear ? " linear" : ""
} gamma gain for red`}
value={currentTargetGammaRed}
step={1}
max={900}
Expand All @@ -211,7 +246,9 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
<PanelSectionRow>
<SliderField
label="Gamma Green"
description={`Control${currentTargetGammaLinear ? " linear" : ""} gamma gain for green´`}
description={`Control${
currentTargetGammaLinear ? " linear" : ""
} gamma gain for green´`}
value={currentTargetGammaGreen}
step={1}
max={900}
Expand All @@ -225,7 +262,9 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
<PanelSectionRow>
<SliderField
label="Gamma Blue"
description={`Control${currentTargetGammaLinear ? " linear" : ""} gamma gain for blue`}
description={`Control${
currentTargetGammaLinear ? " linear" : ""
} gamma gain for blue`}
value={currentTargetGammaBlue}
step={1}
max={900}
Expand All @@ -237,7 +276,7 @@ const Content: VFC<{ applyFn: (appId: string) => void, resetFn: () => void, list
/>
</PanelSectionRow>
</PanelSection>
}
)}
</div>
);
};
Expand Down Expand Up @@ -283,12 +322,18 @@ export default definePlugin((serverAPI: ServerAPI) => {

return {
title: <div className={staticClasses.Title}>vibrantDeck</div>,
content: <Content applyFn={applySettings} resetFn={resetSettings} listenModeFn={listenForRunningApps} />,
content: (
<Content
applyFn={applySettings}
resetFn={resetSettings}
listenModeFn={listenForRunningApps}
/>
),
icon: <FaEyeDropper />,
onDismount() {
runningApps.unregister();
// reset color settings to default values
resetSettings();
}
},
};
});
17 changes: 9 additions & 8 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { JsonObject, JsonProperty, JsonSerializer } from 'typescript-json-serializer';
import { DEFAULT_APP } from './util';
import {
JsonObject,
JsonProperty,
JsonSerializer,
} from "typescript-json-serializer";
import { DEFAULT_APP } from "./util";

const SETTINGS_KEY = "vibrantDeck";

Expand All @@ -25,16 +29,13 @@ export class AppSetting {
gamma?: GammaSetting;

ensureGamma(): GammaSetting {
if (this.gamma == undefined)
this.gamma = new GammaSetting();
if (this.gamma == undefined) this.gamma = new GammaSetting();
return this.gamma;
}

hasSettings(): boolean {
if (this.saturation != undefined)
return true;
if (this.gamma != undefined)
return true;
if (this.saturation != undefined) return true;
if (this.gamma != undefined) return true;
return false;
}
}
Expand Down
59 changes: 44 additions & 15 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Router, ServerAPI } from "decky-frontend-lib";
import { GammaSetting } from "./settings";

interface SaturationArgs {
saturation: number
saturation: number;
}
interface GammaGainArgs {
values: number[]
values: number[];
}
interface GammaBlendArgs {
value: number
value: number;
}

type ActiveAppChangedHandler = (newAppId: string, oldAppId: string) => void;
Expand All @@ -35,8 +35,7 @@ export class RunningApps {
}

unregister() {
if (this.intervalId != undefined)
clearInterval(this.intervalId);
if (this.intervalId != undefined) clearInterval(this.intervalId);
this.intervalId = undefined;
}

Expand All @@ -61,22 +60,52 @@ export class Backend {

applySaturation(saturation: number) {
console.log("Applying saturation " + saturation.toString());
this.serverAPI.callPluginMethod<SaturationArgs, boolean>("set_saturation", { "saturation": saturation / 100.0 });
this.serverAPI.callPluginMethod<SaturationArgs, boolean>("set_saturation", {
saturation: saturation / 100.0,
});
}

applyGamma(gamma: GammaSetting) {
const defaults = new GammaSetting();
const default_values = [defaults.gainR / 100.0, defaults.gainG / 100.0, defaults.gainB / 100.0]
const values = [gamma.gainR / 100.0, gamma.gainG / 100.0, gamma.gainB / 100.0];
console.log(`Applying gamma ${gamma.linear ? "linear" : ""} gain ${values.toString()}`);
const default_values = [
defaults.gainR / 100.0,
defaults.gainG / 100.0,
defaults.gainB / 100.0,
];
const values = [
gamma.gainR / 100.0,
gamma.gainG / 100.0,
gamma.gainB / 100.0,
];
console.log(
`Applying gamma ${gamma.linear ? "linear" : ""} gain ${values.toString()}`
);
if (gamma.linear) {
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>("set_gamma_gain", { "values": default_values });
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>("set_gamma_linear_gain", { "values": values });
this.serverAPI.callPluginMethod<GammaBlendArgs, boolean>("set_gamma_linear_gain_blend", { "value": 1.0 });
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>(
"set_gamma_gain",
{ values: default_values }
);
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>(
"set_gamma_linear_gain",
{ values: values }
);
this.serverAPI.callPluginMethod<GammaBlendArgs, boolean>(
"set_gamma_linear_gain_blend",
{ value: 1.0 }
);
} else {
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>("set_gamma_gain", { "values": values });
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>("set_gamma_linear_gain", { "values": default_values });
this.serverAPI.callPluginMethod<GammaBlendArgs, boolean>("set_gamma_linear_gain_blend", { "value": 0.0 });
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>(
"set_gamma_gain",
{ values: values }
);
this.serverAPI.callPluginMethod<GammaGainArgs, boolean>(
"set_gamma_linear_gain",
{ values: default_values }
);
this.serverAPI.callPluginMethod<GammaBlendArgs, boolean>(
"set_gamma_linear_gain_blend",
{ value: 0.0 }
);
}
}
}

0 comments on commit fcc1be5

Please sign in to comment.