summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-06-12 21:51:59 +1000
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-06-12 21:51:59 +1000
commita97de9d430b8a0f900928f5cad33385deec3f659 (patch)
tree61f9f1e1c1587d7afefe193362a30ef34137a2e7
parentfeat: impl wallpaper (diff)
downloadcaelestia-cli-a97de9d430b8a0f900928f5cad33385deec3f659.tar.gz
caelestia-cli-a97de9d430b8a0f900928f5cad33385deec3f659.tar.bz2
caelestia-cli-a97de9d430b8a0f900928f5cad33385deec3f659.zip
wallpaper: cache smart mode
-rw-r--r--src/caelestia/utils/paths.py2
-rw-r--r--src/caelestia/utils/wallpaper.py35
2 files changed, 25 insertions, 12 deletions
diff --git a/src/caelestia/utils/paths.py b/src/caelestia/utils/paths.py
index f81b996..37aeeef 100644
--- a/src/caelestia/utils/paths.py
+++ b/src/caelestia/utils/paths.py
@@ -26,7 +26,7 @@ wallpapers_dir = Path.home() / "Pictures/Wallpapers"
wallpaper_path_path = c_state_dir / "wallpaper/path.txt"
wallpaper_link_path = c_state_dir / "wallpaper/current"
wallpaper_thumbnail_path = c_state_dir / "wallpaper/thumbnail.jpg"
-thumbnail_cache_dir = c_cache_dir / "thumbnails"
+wallpapers_cache_dir = c_cache_dir / "wallpapers"
def compute_hash(path: Path | str) -> str:
diff --git a/src/caelestia/utils/wallpaper.py b/src/caelestia/utils/wallpaper.py
index c8b3a72..1146c73 100644
--- a/src/caelestia/utils/wallpaper.py
+++ b/src/caelestia/utils/wallpaper.py
@@ -10,10 +10,10 @@ from caelestia.utils.hypr import message
from caelestia.utils.material import get_colours_for_image
from caelestia.utils.paths import (
compute_hash,
- thumbnail_cache_dir,
wallpaper_link_path,
wallpaper_path_path,
wallpaper_thumbnail_path,
+ wallpapers_cache_dir,
)
from caelestia.utils.scheme import Scheme, get_scheme
from caelestia.utils.theme import apply_colours
@@ -55,8 +55,8 @@ def get_wallpapers(args: Namespace) -> list[Path]:
return [f for f in walls if check_wall(f, filter_size, args.threshold)]
-def get_thumb(wall: Path) -> Path:
- thumb = (thumbnail_cache_dir / compute_hash(wall)).with_suffix(".jpg")
+def get_thumb(wall: Path, cache: Path) -> Path:
+ thumb = cache / "thumbnail.jpg"
if not thumb.exists():
with Image.open(wall) as img:
@@ -68,28 +68,38 @@ def get_thumb(wall: Path) -> Path:
return thumb
-def get_smart_mode(wall: Path) -> str:
- with Image.open(get_thumb(wall)) as img:
+def get_smart_mode(wall: Path, cache: Path) -> str:
+ mode_cache = cache / "mode.txt"
+
+ if mode_cache.exists():
+ return mode_cache.read_text()
+
+ with Image.open(get_thumb(wall, cache)) as img:
img.thumbnail((1, 1), Image.LANCZOS)
- tone = Hct.from_int(argb_from_rgb(*img.getpixel((0, 0)))).tone
- return "light" if tone > 60 else "dark"
+ mode = "light" if Hct.from_int(argb_from_rgb(*img.getpixel((0, 0)))).tone > 60 else "dark"
+
+ mode_cache.parent.mkdir(parents=True, exist_ok=True)
+ mode_cache.write_text(mode)
+
+ return mode
def get_colours_for_wall(wall: Path | str, no_smart: bool) -> None:
scheme = get_scheme()
+ cache = wallpapers_cache_dir / compute_hash(wall)
if not no_smart:
scheme = Scheme(
{
"name": scheme.name,
"flavour": scheme.flavour,
- "mode": get_smart_mode(wall),
+ "mode": get_smart_mode(wall, cache),
"variant": scheme.variant,
"colours": scheme.colours,
}
)
- return get_colours_for_image(get_thumb(wall), scheme)
+ return get_colours_for_image(get_thumb(wall, cache), scheme)
def set_wallpaper(wall: Path | str, no_smart: bool) -> None:
@@ -103,8 +113,10 @@ def set_wallpaper(wall: Path | str, no_smart: bool) -> None:
wallpaper_link_path.unlink(missing_ok=True)
wallpaper_link_path.symlink_to(wall)
+ cache = wallpapers_cache_dir / compute_hash(wall)
+
# Generate thumbnail or get from cache
- thumb = get_thumb(wall)
+ thumb = get_thumb(wall, cache)
wallpaper_thumbnail_path.parent.mkdir(parents=True, exist_ok=True)
wallpaper_thumbnail_path.unlink(missing_ok=True)
wallpaper_thumbnail_path.symlink_to(thumb)
@@ -112,7 +124,8 @@ def set_wallpaper(wall: Path | str, no_smart: bool) -> None:
scheme = get_scheme()
# Change mode based on wallpaper colour
- scheme.mode = get_smart_mode(wall)
+ if not no_smart:
+ scheme.mode = get_smart_mode(wall, cache)
# Update colours
scheme.update_colours()