diff --git a/main.py b/main.py index ccc8cc3..3adf5fa 100644 --- a/main.py +++ b/main.py @@ -159,6 +159,13 @@ async def set_charge_limit(self, enabled): except Exception as e: decky_plugin.logger.error(f'error while setting charge limit {e}') + async def set_als_enabled(self, enabled): + try: + settings.set_setting('alsEnabled', enabled) + except Exception as e: + decky_plugin.logger.error(f'error while setting als {e}') + + async def remap_button(self, button: str, action: str): decky_plugin.logger.info(f"remap_button {button} {action}") controller_code = None diff --git a/src/backend/utils.tsx b/src/backend/utils.tsx index 16cbb70..dc2f079 100644 --- a/src/backend/utils.tsx +++ b/src/backend/utils.tsx @@ -8,14 +8,14 @@ export enum ServerAPIMethods { LOG_INFO = 'log_info', GET_SETTINGS = 'get_settings', SET_POWER_LED = 'set_power_led', - SET_CHARGE_LIMIT = 'set_charge_limit' + SET_CHARGE_LIMIT = 'set_charge_limit', + SET_ALS_ENABLED = 'set_als_enabled' } -const readAls = - (serverApi: ServerAPI) => async () => { - const { result } = await serverApi.callPluginMethod('read_als', {}); - return Number(result); - }; +const readAls = (serverApi: ServerAPI) => async () => { + const { result } = await serverApi.callPluginMethod('read_als', {}); + return Number(result); +}; const createRgbOn = (serverAPI: ServerAPI) => async (controller: ControllerType) => { @@ -60,6 +60,13 @@ const createSetChargeLimit = }); }; +const createSetAlsEnabled = + (serverAPI: ServerAPI) => async (enabled: boolean) => { + await serverAPI.callPluginMethod(ServerAPIMethods.SET_ALS_ENABLED, { + enabled + }); + }; + const createGetSettings = (serverAPI: ServerAPI) => async () => { return await serverAPI.callPluginMethod(ServerAPIMethods.GET_SETTINGS, {}); }; @@ -89,7 +96,8 @@ export const createServerApiHelpers = (serverAPI: ServerAPI) => { getSettings: createGetSettings(serverAPI), setPowerLed: createSetPowerLed(serverAPI), setChargeLimit: createSetChargeLimit(serverAPI), - readAls: readAls(serverAPI) + readAls: readAls(serverAPI), + setAlsEnabled: createSetAlsEnabled(serverAPI) }; }; diff --git a/src/components/als/ALSPanel.tsx b/src/components/als/ALSPanel.tsx index 45e64d0..6b50749 100644 --- a/src/components/als/ALSPanel.tsx +++ b/src/components/als/ALSPanel.tsx @@ -6,17 +6,36 @@ import { } from 'decky-frontend-lib'; import { useState, useEffect } from 'react'; -import { getServerApi, createServerApiHelpers, logInfo } from '../../backend/utils'; +import { + getServerApi, + createServerApiHelpers, + logInfo +} from '../../backend/utils'; +import { useDispatch, useSelector } from 'react-redux'; +import { selectAlsEnabled, uiSlice } from '../../redux-modules/uiSlice'; let currentBrightness = 40; +const useAlsEnabled = () => { + const enabledAls = useSelector(selectAlsEnabled); + const dispatch = useDispatch(); + + const setAlsEnabled = (enabled: boolean) => { + return dispatch(uiSlice.actions.setAlsEnabled(enabled)); + }; + + return { enabledAls, setAlsEnabled }; +}; + export default function () { - const [enabledAls, setAlsEnabled] = useState(false); + const { enabledAls, setAlsEnabled } = useAlsEnabled(); const previousAlsValues = [-1, -1, -1, -1]; const serverApi = getServerApi(); - const { readAls } = serverApi ? createServerApiHelpers(serverApi) : { readAls: async () => null }; + const { readAls } = serverApi + ? createServerApiHelpers(serverApi) + : { readAls: async () => null }; // Brightness steps // [ALS delta, brightness add in %] @@ -31,7 +50,8 @@ export default function () { const stepCount = 10; const ignoreThreshold = 30; - const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + const sleep = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); const brigtnessPromise = new Promise(async (resolve) => { while (true) { @@ -54,8 +74,12 @@ export default function () { //logInfo(`ALS value: ${alsValue}`); // Set the initial values - if (previousAlsValues[0] === -1 || previousAlsValues[1] === -1 - || previousAlsValues[2] === -1 || previousAlsValues[3] === -1) { + if ( + previousAlsValues[0] === -1 || + previousAlsValues[1] === -1 || + previousAlsValues[2] === -1 || + previousAlsValues[3] === -1 + ) { continue; } @@ -68,7 +92,9 @@ export default function () { alsDeltas.push(previousAlsValues[i + 1] - previousAlsValues[i]); } - const delta = Math.round(alsDeltas.reduce((acc, val) => acc + val, 0) / alsDeltas.length); + const delta = Math.round( + alsDeltas.reduce((acc, val) => acc + val, 0) / alsDeltas.length + ); const absDelta = Math.abs(delta); // Ignore small changes @@ -101,13 +127,18 @@ export default function () { // Smoothing let localCurrentBrightness = currentBrightness; - const brightnessPerMs = brightnessAdd / smoothTime * stepCount; + const brightnessPerMs = (brightnessAdd / smoothTime) * stepCount; for (let i = 0; i < smoothTime / stepCount; i++) { localCurrentBrightness += brightnessPerMs; - localCurrentBrightness = Math.min(100, Math.max(0, localCurrentBrightness)); + localCurrentBrightness = Math.min( + 100, + Math.max(0, localCurrentBrightness) + ); - window.SteamClient.System.Display.SetBrightness(localCurrentBrightness / 100); + window.SteamClient.System.Display.SetBrightness( + localCurrentBrightness / 100 + ); await sleep(smoothTime / stepCount); @@ -120,11 +151,12 @@ export default function () { }); useEffect(() => { - const registration = window.SteamClient.System.Display.RegisterForBrightnessChanges( - async (data: { flBrightness: number }) => { - currentBrightness = data.flBrightness * 100; - } - ); + const registration = + window.SteamClient.System.Display.RegisterForBrightnessChanges( + async (data: { flBrightness: number }) => { + currentBrightness = data.flBrightness * 100; + } + ); return () => { registration.unregister(); @@ -136,17 +168,16 @@ export default function () { brigtnessPromise.then(() => { logInfo('Brightness promise resolved'); }); - } + }; }, []); const handleAlsEnabled = (enabled: boolean) => { setAlsEnabled(enabled); - - } + }; return ( <> - + ); -}; +} diff --git a/src/redux-modules/uiSlice.tsx b/src/redux-modules/uiSlice.tsx index e000fac..b81b7e0 100644 --- a/src/redux-modules/uiSlice.tsx +++ b/src/redux-modules/uiSlice.tsx @@ -5,7 +5,7 @@ import { RootState } from './store'; import { createServerApiHelpers, extractDisplayName, - getServerApi, + getServerApi } from '../backend/utils'; // import type { RootState } from './store'; @@ -14,6 +14,7 @@ type UiStateType = { currentGameId: undefined | string; currentDisplayName: undefined | string; chargeLimitEnabled?: boolean; + alsEnabled?: boolean; pluginVersionNum?: string; }; @@ -35,6 +36,9 @@ export const uiSlice = createSlice({ }, setChargeLimit(state, action: PayloadAction) { state.chargeLimitEnabled = action.payload; + }, + setAlsEnabled(state, action: PayloadAction) { + state.alsEnabled = action.payload; } }, extraReducers: (builder) => { @@ -46,6 +50,9 @@ export const uiSlice = createSlice({ if (action.payload?.chargeLimitEnabled) { state.chargeLimitEnabled = Boolean(action.payload?.chargeLimitEnabled); } + if (action.payload?.alsEnabled) { + state.alsEnabled = Boolean(action.payload?.alsEnabled); + } }); builder.addCase(setCurrentGameId, (state, action) => { if (action?.payload) { @@ -70,6 +77,9 @@ export const selectCurrentGameDisplayName = (state: RootState) => export const selectChargeLimitEnabled = (state: RootState) => Boolean(state.ui?.chargeLimitEnabled); +export const selectAlsEnabled = (state: RootState) => + Boolean(state.ui?.alsEnabled); + export const uiSliceMiddleware = (_store: any) => (next: any) => (action: any) => { const { type } = action; @@ -77,10 +87,16 @@ export const uiSliceMiddleware = const result = next(action); - if (type === uiSlice.actions.setChargeLimit.type && serverApi) { - const { setChargeLimit } = createServerApiHelpers(serverApi); + if (serverApi) { + const { setChargeLimit, setAlsEnabled } = + createServerApiHelpers(serverApi); - setChargeLimit(action.payload); + if (type === uiSlice.actions.setChargeLimit.type && serverApi) { + setChargeLimit(action.payload); + } + if (type === uiSlice.actions.setAlsEnabled.type && serverApi) { + setAlsEnabled(action.payload); + } } return result;