import { ROUTER_NAVIGATION , RouterNavigationAction } from '@ngrx/router-store' ; import { AppTheme , CommandLineArgs , HardwareConfiguration , Notification , NotificationType , runInElectron , UserConfiguration , VersionInformation } from 'uhk-common' ; import * as App from '../actions/app' ; import { ActionTypes as UserConfigActionTypes , SaveUserConfigSuccessAction } from '../actions/user-config' ; import { ActionTypes as DeviceActionTypes , ConnectionStateChangedAction } from '../actions/device' ; import { KeyboardLayout } from '../../keyboard/keyboard-layout.enum' ; import { getVersions } from '../../util' ; export interface State { appTheme : AppTheme ; animationEnabled : boolean ; started : boolean ; commandLineArgs : CommandLineArgs ; undoableNotification ? : Notification ; navigationCountAfterNotification : number ; prevUserConfig ? : UserConfiguration ; runningInElectron : boolean ; configLoading : boolean ; hardwareConfig ? : HardwareConfiguration ; agentVersionInfo ? : VersionInformation ; privilegeWhatWillThisDoClicked : boolean ; permissionError ? : any ; platform ? : string ; osVersion ? : string ; keypressCapturing : boolean ; everAttemptedSavingToKeyboard : boolean ; udevFileContent : string ; } export const initialState = { appTheme : AppTheme . System , animationEnabled : true , started : false , commandLineArgs : { } , navigationCountAfterNotification : 0 , runningInElectron : runInElectron ( ) , configLoading : true , agentVersionInfo : getVersions ( ) , privilegeWhatWillThisDoClicked : false , keypressCapturing : false , everAttemptedSavingToKeyboard : false , udevFileContent : '' } ; export function reducer ( state = initialState , action ) { switch ( action . type ) { case App . ActionTypes . AppStarted : { return { ... state , started : true } ; } case App . ActionTypes . ApplyAppStartInfo : { const payload = ( action as App . ApplyAppStartInfoAction ) . payload ; return { ... state , commandLineArgs : payload . commandLineArgs , platform : payload . platform , osVersion : payload . osVersion , udevFileContent : payload . udevFileContent } ; } case App . ActionTypes . AppShowNotification : { const currentAction = < App . ShowNotificationAction > action ; if ( currentAction . payload . type !== NotificationType . Undoable ) { return state ; } return { ... state , undoableNotification : currentAction . payload , navigationCountAfterNotification : 0 } ; } case ROUTER_NAVIGATION : { const newState = { ... state } ; newState . navigationCountAfterNotification ++ ; if ( newState . navigationCountAfterNotification > 1 ) { newState . undoableNotification = null ; } return newState ; } case App . ActionTypes . UndoLastSuccess : case App . ActionTypes . DismissUndoNotification : { return { ... state , undoableNotification : null } ; } case UserConfigActionTypes . LoadUserConfigSuccess : case UserConfigActionTypes . SaveUserConfigSuccess : { return { ... state , prevUserConfig : ( action as SaveUserConfigSuccessAction ) . payload , configLoading : false } ; } case UserConfigActionTypes . LoadConfigFromDevice : case UserConfigActionTypes . LoadUserConfig : { return { ... state , configLoading : true } ; } case App . ActionTypes . LoadHardwareConfigurationSuccess : return { ... state , hardwareConfig : ( action as App . LoadHardwareConfigurationSuccessAction ) . payload } ; case DeviceActionTypes . ConnectionStateChanged : { const connectionState = ( action as ConnectionStateChangedAction ) . payload ; if ( connectionState && connectionState . connectedDevice ) { return state ; } return { ... state , hardwareConfig : null } ; } case App . ActionTypes . PrivilegeWhatWillThisDo : return { ... state , privilegeWhatWillThisDoClicked : true } ; case App . ActionTypes . SetupPermissionError : return { ... state , permissionError : ( action as App . SetupPermissionErrorAction ) . payload } ; case DeviceActionTypes . SetPrivilegeOnLinux : return { ... state , permissionError : null } ; case App . ActionTypes . StartKeypressCapturing : return { ... state , keypressCapturing : true } ; case App . ActionTypes . StopKeypressCapturing : return { ... state , keypressCapturing : false } ; case App . ActionTypes . LoadApplicationSettingsSuccess : { const settings = ( action as App . LoadApplicationSettingsSuccessAction ) . payload ; return { ... state , everAttemptedSavingToKeyboard : settings . everAttemptedSavingToKeyboard , animationEnabled : settings . animationEnabled , appTheme : settings . appTheme || AppTheme . System } ; } case DeviceActionTypes . SaveConfiguration : return { ... state , everAttemptedSavingToKeyboard : true } ; case App . ActionTypes . ToggleAnimationEnabled : return { ... state , animationEnabled : ( action as App . ToggleAnimationEnabledAction ) . payload } ; case App . ActionTypes . SetAppTheme : return { ... state , appTheme : ( action as App . SetAppThemeAction ) . payload } ; default : return state ; } } export const showAddonMenu = ( state ) => state . commandLineArgs . modules ; export const getUndoableNotification = ( state ) => state . undoableNotification ; export const getPrevUserConfiguration = ( state ) => state . prevUserConfig ; export const runningInElectron = ( state ) => state . runningInElectron ; export const getKeyboardLayout = ( state ) : KeyboardLayout => { if ( state . hardwareConfig && state . hardwareConfig . isIso ) { return KeyboardLayout . ISO ; } return KeyboardLayout . ANSI ; } ; export const deviceConfigurationLoaded = ( state ) => ! state . runningInElectron ? true : ! ! state . hardwareConfig ; export const getAgentVersionInfo = ( state ) => state . agentVersionInfo || { } as VersionInformation ; export const runningOnNotSupportedWindows = ( state ) : boolean => { if ( ! state . osVersion || state . platform !== 'win32' ) { return false ; } const version = state . osVersion . split ( '.' ) ; const osMajor = + version [ 0 ] ; const osMinor = + version [ 1 ] ; return osMajor < 6 || ( osMajor === 6 && osMinor < 2 ) ; } ; export const keypressCapturing = ( state ) : boolean => state . keypressCapturing ; export const getEverAttemptedSavingToKeyboard = ( state ) : boolean => state . everAttemptedSavingToKeyboard ; export const getUdevFileContent = ( state ) : string => state . udevFileContent ; export const getAnimationEnabled = ( state ) : boolean => state . animationEnabled ; export const getAppTheme = ( state ) : AppTheme => state . appTheme ; export const getPlatform = ( state ) : string => state . platform ;