-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
107 lines (88 loc) · 3.33 KB
/
index.js
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
const electron = require('electron');
const hasCalled = {
getDefinedThemes: false,
isDarkMode: null,
};
function getThemeObj(themePath, config) {
const themeModule = require(themePath);
const theme = themeModule.decorateConfig(config);
return theme;
}
function getConfigFromObject(hyperConfig) {
if ('hyperPreferColorScheme' in hyperConfig) return hyperConfig.hyperPreferColorScheme;
if ('hyperPrefersColorScheme' in hyperConfig) return hyperConfig.hyperPrefersColorScheme;
throw new Error('Unable to find `hyperPrefersColorScheme` within config object.');
}
const getDefinedThemes = (() => {
let themes = null;
return (pluginsWithPaths, config) => {
if (themes) return themes;
const colorSchemeObject = getConfigFromObject(config);
const pathMap = pluginsWithPaths.plugins.reduce(
(map, cur) => {
if (cur.endsWith(colorSchemeObject.light)) {
map.light = cur;
}
if (cur.endsWith(colorSchemeObject.dark)) {
map.dark = cur;
}
return map;
},
{ light: null, dark: null }
);
const lightTheme = getThemeObj(pathMap.light, config);
const darkTheme = getThemeObj(pathMap.dark, config);
themes = { darkTheme, lightTheme };
hasCalled.getDefinedThemes = true;
return themes;
};
})();
module.exports = {
middleware: (store) => (next) => (action) => {
// Once Electron sets the themes and `hasSetTheme` is toggled we need to start listening for color scheme changes
if (window.themes && window.hasSetTheme === false) {
window.rpc.on('hyperPrefersColorScheme:toggle', (isDarkMode) => {
store.dispatch({
type: 'PREFERS_COLOR_SCHEME_CHANGE',
isDarkMode,
});
});
window.hasSetTheme = true;
}
return next(action);
},
onWindow(window) {
const themes = getDefinedThemes(electron.app.plugins.getPaths(), electron.app.config.getConfig());
window.webContents
.executeJavaScript(`window.themes=${JSON.stringify(themes)};window.hasSetTheme=false`)
.then(() => {
window.rpc.emit('hyperPrefersColorScheme:toggle', electron.nativeTheme.shouldUseDarkColors);
});
electron.nativeTheme.on('updated', () => {
window.rpc.emit('hyperPrefersColorScheme:toggle', electron.nativeTheme.shouldUseDarkColors);
});
},
reduceUI(state, action) {
if (action.type === 'PREFERS_COLOR_SCHEME_CHANGE') {
const theme = window.themes[action.isDarkMode ? 'darkTheme' : 'lightTheme'];
return (
state
//colors
.set('foregroundColor', theme?.foregroundColor ?? config.foregroundColor)
.set('backgroundColor', theme?.backgroundColor ?? config.backgroundColor)
.set('borderColor', theme?.borderColor ?? config.borderColor)
.set('colors', theme?.colors ?? config.colors)
.set('cursorColor', theme?.cursorColor ?? config.cursorColor)
.set('selectionColor', theme?.selectionColor ?? config.selectionColor)
//fonts
.set('fontSize', theme?.fontSize ?? config.fontSize)
.set('fontFamily', theme?.fontFamily ?? config.fontFamily)
//misc
.set('css', theme?.css ?? config.css)
.set('termCSS', theme?.termCSS ?? config.termCSS)
.set('cursorShape', theme?.cursorShape ?? config.cursorShape)
);
}
return state;
},
};