Debian Codegraph @

ivette/src/dome/renderer/themes.tsx

raw
1 /* ************************************************************************ */
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /* ************************************************************************ */
22
23 // --------------------------------------------------------------------------
24 // --- Global Color Theme Management
25 // --------------------------------------------------------------------------
26
27 /**
28 @packageDocumentation
29 @module dome/themes
30 */
31
32 //import React from 'react';
33 import * as Dome from 'dome';
34 import * as Settings from 'dome/data/settings';
35 import { State } from 'dome/data/states';
36 import { ipcRenderer } from 'electron';
37
38 /* -------------------------------------------------------------------------- */
39 /* --- Global Settings --- */
40 /* -------------------------------------------------------------------------- */
41
42 export type ColorTheme = 'dark' | 'light';
43 export type ColorSettings = 'dark' | 'light' | 'system';
44
45 export const jColorTheme =
46 (th: string | undefined): ColorTheme => (th === 'dark' ? 'dark' : 'light');
47 export const jColorSettings =
48 (th: string | undefined): ColorSettings => {
49 switch (th) {
50 case 'light':
51 case 'dark':
52 case 'system':
53 return th;
54 default:
55 return 'system';
56 }
57 };
58
59 const ColorThemeSettings = new Settings.GString('dome-color-theme', 'system');
60 const NativeThemeUpdated = new Dome.Event('dome.theme.updated');
61 ipcRenderer.on('dome.theme.updated', () => NativeThemeUpdated.emit());
62
63 async function getNativeTheme(): Promise<ColorTheme> {
64 const th = await ipcRenderer.invoke('dome.ipc.theme');
65 return jColorTheme(th);
66 }
67
68 /* -------------------------------------------------------------------------- */
69 /* --- Color Theme Hooks --- */
70 /* -------------------------------------------------------------------------- */
71
72 export function useColorTheme(): [ColorTheme, (upd: ColorSettings) => void] {
73 Dome.useUpdate(NativeThemeUpdated);
74 const { result: current } = Dome.usePromise(getNativeTheme());
75 const [pref, setTheme] = Settings.useGlobalSettings(ColorThemeSettings);
76 return [current ?? jColorTheme(pref), setTheme];
77 }
78
79 export function useColorThemeSettings(): State<ColorSettings> {
80 const [pref, setTheme] = Settings.useGlobalSettings(ColorThemeSettings);
81 return [jColorSettings(pref), setTheme];
82 }
83
84 /* -------------------------------------------------------------------------- */