generated from SteamDeckHomebrew/decky-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5cf272
commit 580bcfe
Showing
2 changed files
with
168 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { ServerAPI } from 'decky-frontend-lib'; | ||
import { useReducer, useEffect } from 'react'; | ||
import { ControllerType } from '../backend/constants'; | ||
|
||
type StateType = { | ||
enabled: boolean; | ||
red: number; | ||
green: number; | ||
blue: number; | ||
brightness: number; | ||
}; | ||
|
||
enum Actions { | ||
SET_COLOR, | ||
SET_BRIGHTNESS, | ||
TOGGLE_ENABLED, | ||
SET_ENABLED | ||
} | ||
|
||
type ActionType = { | ||
type: Actions; | ||
payload?: any; | ||
}; | ||
|
||
export enum Colors { | ||
RED = 'red', | ||
GREEN = 'green', | ||
BLUE = 'blue' | ||
} | ||
|
||
const rgbReducer = (state: any, action: ActionType) => { | ||
if (action.type === Actions.SET_COLOR && action.payload) { | ||
let newState = { ...state, [action.payload.color]: action.payload.value }; | ||
return newState; | ||
} | ||
if (action.type === Actions.SET_BRIGHTNESS) { | ||
let newState = { ...state, brightness: action.payload }; | ||
return newState; | ||
} | ||
if (action.type === Actions.TOGGLE_ENABLED) { | ||
let newState = { ...state, enabled: !state.enabled }; | ||
return newState; | ||
} | ||
if (action.type === Actions.SET_ENABLED) { | ||
let newState = { ...state, enabled: Boolean(action.payload) }; | ||
return newState; | ||
} | ||
return state; | ||
}; | ||
|
||
const initialState = { | ||
enabled: false, | ||
red: 255, | ||
green: 255, | ||
blue: 255, | ||
brightness: 50 | ||
}; | ||
|
||
const useRgb = ( | ||
// initialState: StateType, | ||
controller: ControllerType, | ||
serverAPI: ServerAPI | ||
) => { | ||
const [state, dispatch] = useReducer(rgbReducer, initialState); | ||
|
||
const { enabled, red, green, blue, brightness } = state as StateType; | ||
|
||
useEffect(() => { | ||
if (enabled) { | ||
serverAPI.callPluginMethod('rgb_on', { controller }); | ||
serverAPI.callPluginMethod('rgb_brightness', { | ||
controller, | ||
value_str: brightness, | ||
red, | ||
green, | ||
blue | ||
}); | ||
} else { | ||
// turn off rgb | ||
serverAPI.callPluginMethod('rgb_off', { controller }); | ||
} | ||
}, [enabled, red, green, blue, brightness]); | ||
|
||
const updateColor = (color: Colors, value: number) => { | ||
return dispatch({ | ||
type: Actions.SET_COLOR, | ||
payload: { | ||
color, | ||
value | ||
} | ||
}); | ||
}; | ||
|
||
// const toggleEnabled = () => { | ||
// return dispatch({ | ||
// type: Actions.TOGGLE_ENABLED | ||
// }); | ||
// }; | ||
|
||
const setEnabled = (enabled: boolean) => { | ||
return dispatch({ | ||
type: Actions.SET_ENABLED, | ||
payload: enabled | ||
}); | ||
}; | ||
|
||
const updateBrightness = (brightness: number) => { | ||
return dispatch({ | ||
type: Actions.SET_BRIGHTNESS, | ||
payload: brightness | ||
}); | ||
}; | ||
|
||
return [state, setEnabled, updateColor, updateBrightness]; | ||
}; | ||
|
||
export default useRgb; |