diff --git a/quickshell/Common/SettingsData.qml b/quickshell/Common/SettingsData.qml index 8f99e1f1..1d45e09d 100644 --- a/quickshell/Common/SettingsData.qml +++ b/quickshell/Common/SettingsData.qml @@ -1390,27 +1390,4 @@ rm -rf '${home}'/.cache/icon-cache '${home}'/.cache/thumbnails 2>/dev/null || tr } property bool pluginSettingsFileExists: false - - IpcHandler { - function reveal(): string { - root.setShowDock(true); - return "DOCK_SHOW_SUCCESS"; - } - - function hide(): string { - root.setShowDock(false); - return "DOCK_HIDE_SUCCESS"; - } - - function toggle(): string { - root.toggleShowDock(); - return root.showDock ? "DOCK_SHOW_SUCCESS" : "DOCK_HIDE_SUCCESS"; - } - - function status(): string { - return root.showDock ? "visible" : "hidden"; - } - - target: "dock" - } } diff --git a/quickshell/DMSShellIPC.qml b/quickshell/DMSShellIPC.qml index fd487239..bf436473 100644 --- a/quickshell/DMSShellIPC.qml +++ b/quickshell/DMSShellIPC.qml @@ -480,56 +480,108 @@ Item { target: "dankdash" } + function getBarConfig(selector: string, value: string): var { + const barSelectors = ["id", "name", "index"]; + if (!barSelectors.includes(selector)) return { error: "BAR_INVALID_SELECTOR" }; + const index = selector === "index" ? Number(value) : SettingsData.barConfigs.findIndex(bar => bar[selector] == value); + const barConfig = SettingsData.barConfigs?.[index]; + if (!barConfig) return { error: "BAR_NOT_FOUND" }; + return { barConfig }; + } + IpcHandler { - function reveal(index: int): string { - const idx = index - 1; - if (idx < 0 || idx >= SettingsData.barConfigs.length) { - return `BAR_${index}_NOT_FOUND`; - } - const bar = SettingsData.barConfigs[idx]; - SettingsData.updateBarConfig(bar.id, { - visible: true - }); - return `BAR_${index}_SHOW_SUCCESS`; + function reveal(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + SettingsData.updateBarConfig(barConfig.id, {visible: true}); + return "BAR_SHOW_SUCCESS"; } - function hide(index: int): string { - const idx = index - 1; - if (idx < 0 || idx >= SettingsData.barConfigs.length) { - return `BAR_${index}_NOT_FOUND`; - } - const bar = SettingsData.barConfigs[idx]; - SettingsData.updateBarConfig(bar.id, { - visible: false - }); - return `BAR_${index}_HIDE_SUCCESS`; + function hide(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + SettingsData.updateBarConfig(barConfig.id, {visible: false}); + return "BAR_HIDE_SUCCESS"; } - function toggle(index: int): string { - const idx = index - 1; - if (idx < 0 || idx >= SettingsData.barConfigs.length) { - return `BAR_${index}_NOT_FOUND`; - } - const bar = SettingsData.barConfigs[idx]; - const newVisible = !(bar.visible ?? true); - SettingsData.updateBarConfig(bar.id, { - visible: newVisible - }); - return newVisible ? `BAR_${index}_SHOW_SUCCESS` : `BAR_${index}_HIDE_SUCCESS`; + function toggle(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + SettingsData.updateBarConfig(barConfig.id, {visible: !barConfig.visible}); + return !barConfig.visible ? "BAR_SHOW_SUCCESS" : "BAR_HIDE_SUCCESS"; } - function status(index: int): string { - const idx = index - 1; - if (idx < 0 || idx >= SettingsData.barConfigs.length) { - return `BAR_${index}_NOT_FOUND`; - } - const bar = SettingsData.barConfigs[idx]; - return (bar.visible ?? true) ? "visible" : "hidden"; + function status(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + return barConfig.visible ? "visible" : "hidden"; + } + + function autoHide(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + SettingsData.updateBarConfig(barConfig.id, {autoHide: true}); + return "BAR_AUTO_HIDE_SUCCESS"; + } + + function manualHide(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + SettingsData.updateBarConfig(barConfig.id, {autoHide: false}); + return "BAR_MANUAL_HIDE_SUCCESS"; + } + + function toggleAutoHide(selector: string, value: string): string { + const { barConfig, error } = getBarConfig(selector, value); + if (error) return error; + SettingsData.updateBarConfig(barConfig.id, {autoHide: !barConfig.autoHide}); + return barConfig.autoHide ? "BAR_MANUAL_HIDE_SUCCESS": "BAR_AUTO_HIDE_SUCCESS"; } target: "bar" } + IpcHandler { + function reveal(): string { + SettingsData.setShowDock(true); + return "DOCK_SHOW_SUCCESS"; + } + + function hide(): string { + SettingsData.setShowDock(false); + return "DOCK_HIDE_SUCCESS"; + } + + function toggle(): string { + SettingsData.toggleShowDock(); + return SettingsData.showDock ? "DOCK_SHOW_SUCCESS" : "DOCK_HIDE_SUCCESS"; + } + + function status(): string { + return SettingsData.showDock ? "visible" : "hidden"; + } + + function autoHide(): string { + SettingsData.dockAutoHide = true + SettingsData.saveSettings() + return "BAR_AUTO_HIDE_SUCCESS"; + } + + function manualHide(): string { + SettingsData.dockAutoHide = false + SettingsData.saveSettings() + return "BAR_MANUAL_HIDE_SUCCESS"; + } + + function toggleAutoHide(): string { + SettingsData.dockAutoHide = !SettingsData.dockAutoHide + SettingsData.saveSettings() + return SettingsData.dockAutoHide ? "BAR_AUTO_HIDE_SUCCESS" : "BAR_MANUAL_HIDE_SUCCESS"; + } + + target: "dock" + } + IpcHandler { function open(): string { PopoutService.openSettings(); @@ -546,6 +598,53 @@ Item { return "SETTINGS_TOGGLE_SUCCESS"; } + function get(key: string): string { + return JSON.stringify(SettingsData?.[key]) + } + + function set(key: string, value: string): string { + + if (!(key in SettingsData)) { + console.warn("Cannot set property, not found:", key) + return "SETTINGS_INVALID_KEY" + } + + const typeName = typeof SettingsData?.[key] + + try { + switch (typeName) { + case "boolean": + if (value === "true" || value === "false") value = (value === "true") + else throw `${value} is not a Boolean` + break + case "number": + value = Number(value) + if (isNaN(value)) throw `${value} is not a Number` + break + case "string": + value = String(value) + break + case "object": + // NOTE: Parsing lists is messed up upstream and not sure if we want + // to make sure objects are well structured or just let people set + // whatever they want but risking messed up settings. + // Objects & Arrays are disabled for now + // https://github.com/quickshell-mirror/quickshell/pull/22 + throw "Setting Objects and Arrays not supported" + default: + throw "Unsupported type" + } + + console.warn("Setting:", key, value) + SettingsData[key] = value + SettingsData.saveSettings() + return "SETTINGS_SET_SUCCESS" + } catch (e) { + console.warn("Failed to set property:", key, "error:", e) + return "SETTINGS_SET_FAILURE" + } + } + target: "settings" }