/** * Launcher folder state: which apps are grouped into which named folder. * Stored as one JSON string (appId -> folder name) in the settings store, so * it persists and its changes broadcast to every isolate. An unset value means * the default grouping; any edit persists the whole map, so clearing out the * defaults sticks. Folders exist only while at least one app is assigned to * them — there is no separate create/delete step. */ import { getStringSetting, setStringSetting } from "../../native/settings-store"; const STORAGE_KEY = "launcher.folders"; /** The out-of-the-box grouping, applied only while the setting is unset. */ const DEFAULT_ASSIGNMENTS: Record = { blocks: "Games", freecell: "Games", minesweeper: "Games", pinball: "Games", }; export const FOLDER_NAME_MAX_LENGTH = 24; /** appId -> folder name. Malformed stored JSON reads as "no folders". */ export function getFolderAssignments(): Record { const raw = getStringSetting(STORAGE_KEY, ""); if (!raw) return { ...DEFAULT_ASSIGNMENTS }; try { const parsed = JSON.parse(raw); if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {}; const assignments: Record = {}; for (const appId of Object.keys(parsed)) { const folder = parsed[appId]; if (typeof folder === "string" && folder.trim()) { assignments[appId] = folder.trim(); } } return assignments; } catch { return {}; } } /** folder name -> appIds (assignment order), folder names sorted. */ export function getFolders(): Map { const assignments = getFolderAssignments(); const unsorted = new Map(); for (const appId of Object.keys(assignments)) { const folder = assignments[appId]!; const members = unsorted.get(folder); if (members) { members.push(appId); } else { unsorted.set(folder, [appId]); } } const folders = new Map(); for (const name of Array.from(unsorted.keys()).sort((a, b) => a.localeCompare(b))) { folders.set(name, unsorted.get(name)!); } return folders; } /** Assign an app to a folder, or (with null) return it to the top level. */ export function setAppFolder(appId: string, folder: string | null): void { const assignments = getFolderAssignments(); if (folder) { assignments[appId] = folder; } else { delete assignments[appId]; } setStringSetting(STORAGE_KEY, JSON.stringify(assignments)); } /** Move every app out of a folder. Returns how many apps were moved. */ export function disbandFolder(folder: string): number { const assignments = getFolderAssignments(); let moved = 0; for (const appId of Object.keys(assignments)) { if (assignments[appId] === folder) { delete assignments[appId]; moved++; } } if (moved > 0) { setStringSetting(STORAGE_KEY, JSON.stringify(assignments)); } return moved; } /** * Match an existing folder name case-insensitively (so "games" reuses * "Games" instead of creating a near-duplicate), else the trimmed input. */ export function resolveFolderName(input: string): string { const trimmed = input.trim(); for (const name of getFolders().keys()) { if (name.toLowerCase() === trimmed.toLowerCase()) return name; } return trimmed; } /** Raw stored value, for cheap change detection by the launcher window. */ export function getFolderStateFingerprint(): string { return getStringSetting(STORAGE_KEY, ""); }