Consolidate theme importing code. (#178)
This commit is contained in:
@@ -1,59 +1,22 @@
|
|||||||
import Gio from "gi://Gio"
|
import Gio from "gi://Gio"
|
||||||
import { bash, Notify } from "lib/utils";
|
import { bash, Notify } from "lib/utils";
|
||||||
import icons from "lib/icons"
|
import icons from "lib/icons"
|
||||||
|
import { filterConfigForThemeOnly, loadJsonFile, saveConfigToFile } from "widget/settings/shared/FileChooser";
|
||||||
|
|
||||||
globalThis.useTheme = (filePath: string): void => {
|
globalThis.useTheme = (filePath: string): void => {
|
||||||
const themeOnly = true;
|
let importedConfig = loadJsonFile(filePath);
|
||||||
let file = Gio.File.new_for_path(filePath as string);
|
|
||||||
let [success, content] = file.load_contents(null);
|
|
||||||
|
|
||||||
if (!success) {
|
if (!importedConfig) {
|
||||||
console.error(`Failed to import: ${filePath}`);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Notify({
|
Notify({
|
||||||
summary: `Importing ${themeOnly ? "Theme" : "Config"}`,
|
summary: `Importing Theme`,
|
||||||
body: `Importing: ${filePath}`,
|
body: `Importing: ${filePath}`,
|
||||||
iconName: icons.ui.info,
|
iconName: icons.ui.info,
|
||||||
timeout: 7000
|
timeout: 7000
|
||||||
});
|
});
|
||||||
|
|
||||||
let jsonString = new TextDecoder("utf-8").decode(content);
|
|
||||||
let importedConfig = JSON.parse(jsonString);
|
|
||||||
|
|
||||||
const hexColorPattern = /^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/;
|
|
||||||
|
|
||||||
const saveConfigToFile = (config: object, filePath: string) => {
|
|
||||||
let file = Gio.File.new_for_path(filePath);
|
|
||||||
let outputStream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);
|
|
||||||
let dataOutputStream = new Gio.DataOutputStream({ base_stream: outputStream });
|
|
||||||
|
|
||||||
let jsonString = JSON.stringify(config, null, 2);
|
|
||||||
dataOutputStream.put_string(jsonString, null);
|
|
||||||
dataOutputStream.close(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const filterConfigForThemeOnly = (config: object) => {
|
|
||||||
let filteredConfig = {};
|
|
||||||
for (let key in config) {
|
|
||||||
if (typeof config[key] === 'string' && hexColorPattern.test(config[key])) {
|
|
||||||
filteredConfig[key] = config[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filteredConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
const filterConfigForNonTheme = (config: object) => {
|
|
||||||
let filteredConfig = {};
|
|
||||||
for (let key in config) {
|
|
||||||
if (!(typeof config[key] === 'string' && hexColorPattern.test(config[key]))) {
|
|
||||||
filteredConfig[key] = config[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filteredConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
let tmpConfigFile = Gio.File.new_for_path(`${TMP}/config.json`);
|
let tmpConfigFile = Gio.File.new_for_path(`${TMP}/config.json`);
|
||||||
let optionsConfigFile = Gio.File.new_for_path(OPTIONS);
|
let optionsConfigFile = Gio.File.new_for_path(OPTIONS);
|
||||||
|
|
||||||
@@ -68,17 +31,12 @@ globalThis.useTheme = (filePath: string): void => {
|
|||||||
let tmpConfig = JSON.parse(new TextDecoder("utf-8").decode(tmpContent));
|
let tmpConfig = JSON.parse(new TextDecoder("utf-8").decode(tmpContent));
|
||||||
let optionsConfig = JSON.parse(new TextDecoder("utf-8").decode(optionsContent));
|
let optionsConfig = JSON.parse(new TextDecoder("utf-8").decode(optionsContent));
|
||||||
|
|
||||||
if (themeOnly) {
|
const filteredConfig = filterConfigForThemeOnly(importedConfig);
|
||||||
const filteredConfig = filterConfigForThemeOnly(importedConfig);
|
tmpConfig = { ...tmpConfig, ...filteredConfig };
|
||||||
tmpConfig = { ...tmpConfig, ...filteredConfig };
|
optionsConfig = { ...optionsConfig, ...filteredConfig };
|
||||||
optionsConfig = { ...optionsConfig, ...filteredConfig };
|
|
||||||
} else {
|
|
||||||
const filteredConfig = filterConfigForNonTheme(importedConfig);
|
|
||||||
tmpConfig = { ...tmpConfig, ...filteredConfig };
|
|
||||||
optionsConfig = { ...optionsConfig, ...filteredConfig };
|
|
||||||
}
|
|
||||||
|
|
||||||
saveConfigToFile(tmpConfig, `${TMP}/config.json`);
|
saveConfigToFile(tmpConfig, `${TMP}/config.json`);
|
||||||
saveConfigToFile(optionsConfig, OPTIONS);
|
saveConfigToFile(optionsConfig, OPTIONS);
|
||||||
bash("pkill ags && ags");
|
bash("pkill ags && ags");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,59 @@ const whiteListedThemeProp = [
|
|||||||
"theme.bar.buttons.style"
|
"theme.bar.buttons.style"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
// Helper functions
|
||||||
|
export const loadJsonFile = (filePath: string): object | null => {
|
||||||
|
let file = Gio.File.new_for_path(filePath as string);
|
||||||
|
let [success, content] = file.load_contents(null);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
console.error(`Failed to import: ${filePath}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let jsonString = new TextDecoder("utf-8").decode(content);
|
||||||
|
return JSON.parse(jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const saveConfigToFile = (config: object, filePath: string): void => {
|
||||||
|
let file = Gio.File.new_for_path(filePath);
|
||||||
|
let outputStream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);
|
||||||
|
let dataOutputStream = new Gio.DataOutputStream({ base_stream: outputStream });
|
||||||
|
|
||||||
|
let jsonString = JSON.stringify(config, null, 2);
|
||||||
|
dataOutputStream.put_string(jsonString, null);
|
||||||
|
dataOutputStream.close(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const hexColorPattern = /^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/;
|
||||||
|
|
||||||
|
export const filterConfigForThemeOnly = (config: object) => {
|
||||||
|
let filteredConfig = {};
|
||||||
|
|
||||||
|
for (let key in config) {
|
||||||
|
if (typeof config[key] === 'string' && hexColorPattern.test(config[key])) {
|
||||||
|
filteredConfig[key] = config[key];
|
||||||
|
} else if (whiteListedThemeProp.includes(key)) {
|
||||||
|
filteredConfig[key] = config[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const filterConfigForNonTheme = (config: object) => {
|
||||||
|
let filteredConfig = {};
|
||||||
|
for (let key in config) {
|
||||||
|
if (whiteListedThemeProp.includes(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!(typeof config[key] === 'string' && hexColorPattern.test(config[key]))) {
|
||||||
|
filteredConfig[key] = config[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredConfig;
|
||||||
|
};
|
||||||
|
|
||||||
export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
|
export const saveFileDialog = (filePath: string, themeOnly: boolean): void => {
|
||||||
const original_file_path = filePath;
|
const original_file_path = filePath;
|
||||||
|
|
||||||
@@ -134,18 +187,15 @@ export const importFiles = (themeOnly: boolean = false): void => {
|
|||||||
|
|
||||||
let response = dialog.run();
|
let response = dialog.run();
|
||||||
|
|
||||||
|
|
||||||
if (response === Gtk.ResponseType.CANCEL) {
|
if (response === Gtk.ResponseType.CANCEL) {
|
||||||
dialog.destroy();
|
dialog.destroy();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (response === Gtk.ResponseType.ACCEPT) {
|
if (response === Gtk.ResponseType.ACCEPT) {
|
||||||
let filePath = dialog.get_filename();
|
let filePath = dialog.get_filename();
|
||||||
let file = Gio.File.new_for_path(filePath as string);
|
let importedConfig = loadJsonFile(filePath as string);
|
||||||
let [success, content] = file.load_contents(null);
|
|
||||||
|
|
||||||
if (!success) {
|
if (!importedConfig) {
|
||||||
console.error(`Failed to import: ${filePath}`);
|
|
||||||
dialog.destroy();
|
dialog.destroy();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -157,48 +207,6 @@ export const importFiles = (themeOnly: boolean = false): void => {
|
|||||||
timeout: 7000
|
timeout: 7000
|
||||||
});
|
});
|
||||||
|
|
||||||
let jsonString = new TextDecoder("utf-8").decode(content);
|
|
||||||
let importedConfig = JSON.parse(jsonString);
|
|
||||||
|
|
||||||
const hexColorPattern = /^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/;
|
|
||||||
|
|
||||||
const saveConfigToFile = (config: object, filePath: string) => {
|
|
||||||
let file = Gio.File.new_for_path(filePath);
|
|
||||||
let outputStream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);
|
|
||||||
let dataOutputStream = new Gio.DataOutputStream({ base_stream: outputStream });
|
|
||||||
|
|
||||||
let jsonString = JSON.stringify(config, null, 2);
|
|
||||||
dataOutputStream.put_string(jsonString, null);
|
|
||||||
dataOutputStream.close(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const filterConfigForThemeOnly = (config: object) => {
|
|
||||||
let filteredConfig = {};
|
|
||||||
|
|
||||||
for (let key in config) {
|
|
||||||
if (typeof config[key] === 'string' && hexColorPattern.test(config[key])) {
|
|
||||||
filteredConfig[key] = config[key];
|
|
||||||
} else if (whiteListedThemeProp.includes(key)) {
|
|
||||||
filteredConfig[key] = config[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filteredConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
const filterConfigForNonTheme = (config: object) => {
|
|
||||||
let filteredConfig = {};
|
|
||||||
for (let key in config) {
|
|
||||||
// do not add key-value pair if its in whiteListedThemeProp
|
|
||||||
if (whiteListedThemeProp.includes(key)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!(typeof config[key] === 'string' && hexColorPattern.test(config[key]))) {
|
|
||||||
filteredConfig[key] = config[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filteredConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
let tmpConfigFile = Gio.File.new_for_path(`${TMP}/config.json`);
|
let tmpConfigFile = Gio.File.new_for_path(`${TMP}/config.json`);
|
||||||
let optionsConfigFile = Gio.File.new_for_path(OPTIONS);
|
let optionsConfigFile = Gio.File.new_for_path(OPTIONS);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user