-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreducer.ts
More file actions
136 lines (128 loc) · 4.29 KB
/
Copy pathreducer.ts
File metadata and controls
136 lines (128 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { AnyAction } from "redux";
import {SET_STICKER_TYPE, SET_THEME, SYSTEM_THEME_CHANGED, TOGGLE_STICKER, TOGGLE_WALLPAPER} from "./settings";
import {DokiSticker, DokiTheme, StringDictonary} from "./themeTools";
import {DEFAULT_THEME_ID, extractConfig, extractHyperConfig, getCorrectSticker, getTheme, saveConfig} from "./config";
import DokiThemeDefinitions from "./DokiThemeDefinitions";
import {CONFIG_RELOAD} from "./decorator";
enum SystemState {
LIGHT, DARK
}
export interface ThemeState {
activeTheme: DokiTheme;
currentSticker: DokiSticker;
showSticker: boolean;
showWallpaper: boolean;
systemState: SystemState;
}
export const THEME_STATE = "dokiThemeState";
const themeNameToDefinition: StringDictonary<DokiTheme> =
Object.values(
DokiThemeDefinitions,
).reduce((accum, def) => {
accum[def.information.name] = def;
return accum;
}, {} as StringDictonary<any>);
function applySystemState(state: any) {
const hyperConfig = extractHyperConfig();
if (hyperConfig.dokiSettings?.systemMatch?.enabled) {
const previousState: ThemeState = state[THEME_STATE] || {};
const themeKey = previousState.systemState === SystemState.DARK ?
hyperConfig.dokiSettings.systemMatch.darkTheme || 'Zero Two Dark' :
hyperConfig.dokiSettings.systemMatch.lightTheme || 'Zero Two Light';
const activeThemeDef = themeNameToDefinition[themeKey] || themeNameToDefinition[
DokiThemeDefinitions[DEFAULT_THEME_ID].information.name
];
const themeState: ThemeState = {
...previousState,
activeTheme: activeThemeDef
};
saveConfig({
...extractConfig(),
themeId: activeThemeDef.information.id,
});
return state.set(THEME_STATE, themeState);
} else {
return state;
}
}
const reducer = (state: any, action: AnyAction) => {
switch (action.type) {
case SET_THEME: {
const previousState: ThemeState = state[THEME_STATE] || {};
const themeState: ThemeState = {
...previousState,
activeTheme: action.payload,
currentSticker: {
type: previousState.currentSticker.type,
sticker: getCorrectSticker(
action.payload,
previousState.currentSticker.type
),
},
};
saveConfig({
...extractConfig(),
themeId: action.payload.information.id,
});
return state.set(THEME_STATE, themeState);
}
case SET_STICKER_TYPE: {
const previousState: ThemeState = state[THEME_STATE] || {};
const themeState: ThemeState = {
...previousState,
currentSticker: {
type: action.payload,
sticker: getCorrectSticker(previousState.activeTheme, action.payload),
},
};
return state.set(THEME_STATE, themeState);
}
case SYSTEM_THEME_CHANGED: {
const isWindowDark = action.payload.isDark;
const previousThemeState: ThemeState = state[THEME_STATE] || {};
const newThemeState: ThemeState = {
...previousThemeState,
systemState: isWindowDark ? SystemState.DARK : SystemState.LIGHT,
}
return applySystemState(
state.set(THEME_STATE, newThemeState)
);
}
// this forces theme updates on config change.
case CONFIG_RELOAD: {
return applySystemState(state);
}
case TOGGLE_STICKER: {
const previousState2: ThemeState = state[THEME_STATE] || {};
const themeState: ThemeState = {
...previousState2,
showSticker: !state[THEME_STATE].showSticker,
};
return state.set(THEME_STATE, themeState);
}
case TOGGLE_WALLPAPER: {
const previousState2: ThemeState = state[THEME_STATE] || {};
const themeState: ThemeState = {
...previousState2,
showWallpaper: !state[THEME_STATE].showWallpaper,
};
return state.set(THEME_STATE, themeState);
}
case "INIT": {
const { theme, sticker } = getTheme();
const dokiThemeConfig = extractConfig();
const themeState: ThemeState = {
activeTheme: theme,
currentSticker: sticker,
showSticker: dokiThemeConfig.showSticker,
showWallpaper: dokiThemeConfig.showWallpaper,
systemState: SystemState.DARK,
};
return state.set(THEME_STATE, themeState);
}
default: {
return state;
}
}
};
export default reducer;