refactor: adopt noctalia.pluginDataDir() (#29)

This commit is contained in:
Ian Ribeiro
2026-07-17 09:25:27 -04:00
committed by GitHub
parent c5ee4f4fe1
commit 82524c1bc0
2 changed files with 24 additions and 26 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ id = "oldirtty/color_picker"
name = "Color Picker" name = "Color Picker"
description = "Pick a color from your screen with hyprpicker." description = "Pick a color from your screen with hyprpicker."
license = "MIT" license = "MIT"
version = "1.0.2" version = "1.0.3"
plugin_api = 3 plugin_api = 3
icon = "palette" icon = "palette"
dependencies = ["hyprpicker"] dependencies = ["hyprpicker"]
+20 -22
View File
@@ -9,9 +9,9 @@
-- CONSTANTS -- CONSTANTS
--============================================== --==============================================
local MAX_HISTORY = 6 local MAX_HISTORY = 6
local STATE_DIR = "~/.local/state/noctalia/plugin-cache/community/color_picker" local PERSISTENT_DIR = noctalia.pluginDataDir()
local HISTORY_FILE = STATE_DIR .. "/history.json" local HISTORY_FILE = PERSISTENT_DIR .. "/history.json"
local SELECTED_FILE = STATE_DIR .. "/selected.json" local SELECTED_FILE = PERSISTENT_DIR .. "/selected.json"
local tr_color_picker = noctalia.tr("color_picker") local tr_color_picker = noctalia.tr("color_picker")
--============================================== --==============================================
@@ -101,11 +101,9 @@ end
--============================================== --==============================================
-- STATE MODEL -- STATE MODEL
-- --
-- selectedColor / selectedOpacity: ephemeral, what the panel is currently -- selectedColor / selectedOpacity: persisted color, state is saved once
-- showing. Set here on every successful pick; also updated by the panel -- per panel session when closed.
-- itself for pure-UI interactions (history swatch click, field edit,
-- opacity slider) that don't touch hyprpicker or disk.
--
-- colorHistory: persisted list. -- colorHistory: persisted list.
-- Only mutated by pushCurrentColor(), called on immediate-commit picks -- Only mutated by pushCurrentColor(), called on immediate-commit picks
-- (widget right-click) and on "commit" (panel close). -- (widget right-click) and on "commit" (panel close).
@@ -139,28 +137,28 @@ local function pushCurrentColor(hex: string, opacity: number)
end end
local function loadHistoryFromDisk() local function loadHistoryFromDisk()
local content = noctalia.readFile(HISTORY_FILE) local raw = noctalia.readFile(HISTORY_FILE)
if content == nil or content == "" then return end if raw == nil or raw == "" then return end
local decoded = noctalia.json.decode(content) local decoded = noctalia.json.decode(raw)
if type(decoded) ~= "table" then return end if type(decoded) ~= "table" then return end
noctalia.state.set("colorHistory", decoded) noctalia.state.set("colorHistory", decoded)
end end
local function saveHistoryToDisk() local function saveHistoryToDisk()
local history = noctalia.state.get("colorHistory") or {} local history = noctalia.state.get("colorHistory") or {}
local encoded = noctalia.json.encode(history) local encoded = noctalia.json.encode(history)
if encoded ~= nil then if encoded ~= nil then
noctalia.writeFile(HISTORY_FILE, encoded) noctalia.writeFile(HISTORY_FILE, encoded)
end end
end end
local function loadSelectedFromDisk() local function loadSelectedFromDisk()
local content = noctalia.readFile(SELECTED_FILE) local raw = noctalia.readFile(SELECTED_FILE)
if content == nil or content == "" then return end if raw == nil or raw == "" then return end
local decoded = noctalia.json.decode(content) local decoded = noctalia.json.decode(raw)
if type(decoded) ~= "table" or decoded.hex == nil then return end if type(decoded) ~= "table" or decoded.hex == nil then return end
setSelected(decoded.hex, decoded.opacity or 1) setSelected(decoded.hex, decoded.opacity or 1)
@@ -291,7 +289,7 @@ end
--============================================== --==============================================
function load() function load()
noctalia.mkdirAll(STATE_DIR) noctalia.mkdirAll(PERSISTENT_DIR)
loadHistoryFromDisk() loadHistoryFromDisk()
loadSelectedFromDisk() loadSelectedFromDisk()
end end