Files
SaraFem/FolderIcons.py
T

68 lines
2.5 KiB
Python

"""Per-directory custom folder icons (Features: "Add custom icons for
directories... should be able to be changed in properties"). Independent
of the global fullness-variant system in Core.py's FolderIconBands/
AppConfig.FolderFullnessThreshold — a directory listed here always shows
its own fixed icon/image regardless of how full it is; see Browser.py's
_ResolveFolderIcon for how the two combine."""
from dataclasses import dataclass
from pathlib import Path
import toml
from Core import ConfigDir
FolderIconsPath = ConfigDir / "FolderIcons.toml"
# Pre-made choices for the icon picker (Features: "Use the pre-made ones"):
# Material's own folder-shape variants first, then a few of Bookmarks'
# thematic set so a folder can be marked by purpose too, not just shape.
FolderIconChoices = [
"FOLDER_OUTLINED", "FOLDER_OPEN", "FOLDER_SPECIAL", "FOLDER_SHARED",
"FOLDER_ZIP", "FOLDER_COPY", "SNIPPET_FOLDER", "RULE_FOLDER",
"STAR_OUTLINE", "FAVORITE_OUTLINE", "WORK_OUTLINE", "HOME_OUTLINED",
"DOWNLOAD_OUTLINED", "IMAGE_OUTLINED", "MUSIC_NOTE_OUTLINED",
"VIDEOCAM_OUTLINED", "CODE_OUTLINED",
]
FolderIconLabelOverrides = {
"FOLDER_OUTLINED": "Default", "FOLDER_OPEN": "Open", "FOLDER_SPECIAL": "Special",
"FOLDER_SHARED": "Shared", "FOLDER_ZIP": "Archive", "FOLDER_COPY": "Copy",
"SNIPPET_FOLDER": "Snippet", "RULE_FOLDER": "Rules",
}
@dataclass
class FolderIconOverride:
Icon: str = ""
ImagePath: str = ""
def LoadFolderIconOverrides() -> dict[Path, FolderIconOverride]:
if not FolderIconsPath.exists():
return {}
try:
Data = toml.loads(FolderIconsPath.read_text())
except toml.TomlDecodeError:
return {}
Overrides: dict[Path, FolderIconOverride] = {}
for PathText, Entry in Data.items():
if isinstance(Entry, dict):
Overrides[Path(PathText)] = FolderIconOverride(
Icon=Entry.get("Icon", ""), ImagePath=Entry.get("ImagePath", "")
)
return Overrides
def SetFolderIconOverride(TargetPath: Path, Icon: str, ImagePath: str) -> None:
"""Icon and ImagePath both blank clears the override, back to whatever
the global fullness-variant system would otherwise show."""
Overrides = LoadFolderIconOverrides()
if Icon or ImagePath:
Overrides[TargetPath] = FolderIconOverride(Icon, ImagePath)
else:
Overrides.pop(TargetPath, None)
ConfigDir.mkdir(parents=True, exist_ok=True)
Data = {str(P): {"Icon": O.Icon, "ImagePath": O.ImagePath} for P, O in Overrides.items()}
FolderIconsPath.write_text(toml.dumps(Data))