Files
custum-hyprpanel/src/globals/useTheme.ts
davfsa a949b34632 Feat: Add live reloading of configuration file (#684)
* Add live reloading of configuration file

This also removes the need for a file with all the available
configuration and a shadow configuration file.

Additionally, added several improvements:
1. Reduce I/O on initial configuration loading by only reading file once
2. Remove unnecesary back and forth events when editing configuration

* Add missing return type

* Consistently reset on config changes and error if failed to initialize config

* Fix massive I/O load on startup by numerical options

* Use _findVal when monitoring config file

* Apply PR requested changes

Signed-off-by: davfsa <davfsa@gmail.com>

* Add missing =>

Signed-off-by: davfsa <davfsa@gmail.com>

* Fix reassignment to const, change to let.

---------

Signed-off-by: davfsa <davfsa@gmail.com>
Co-authored-by: Jas Singh <jaskiratpal.singh@outlook.com>
2025-03-16 02:39:25 -07:00

36 lines
1.1 KiB
TypeScript

import options from '../options';
import Gio from 'gi://Gio';
import { bash, errorHandler } from '../lib/utils';
import { filterConfigForThemeOnly, loadJsonFile, saveConfigToFile } from '../components/settings/shared/FileChooser';
const { restartCommand } = options.hyprpanel;
export const hexColorPattern = /^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/;
globalThis.useTheme = (filePath: string): void => {
try {
const importedConfig = loadJsonFile(filePath);
if (!importedConfig) {
return;
}
const optionsConfigFile = Gio.File.new_for_path(CONFIG);
const [optionsSuccess, optionsContent] = optionsConfigFile.load_contents(null);
if (!optionsSuccess) {
throw new Error('Failed to load theme file.');
}
let optionsConfig = JSON.parse(new TextDecoder('utf-8').decode(optionsContent));
const filteredConfig = filterConfigForThemeOnly(importedConfig);
optionsConfig = { ...optionsConfig, ...filteredConfig };
saveConfigToFile(optionsConfig, CONFIG);
bash(restartCommand.get());
} catch (error) {
errorHandler(error);
}
};