feat: make FreeDesktop icon thumbnail size as configurable

This commit is contained in:
2026-09-10 09:12:03 +08:00
parent 11d76eca16
commit 0073298d45
4 changed files with 25 additions and 11 deletions
+18 -3
View File
@@ -7,13 +7,19 @@ from pathlib import Path
from typing import TypeVar, Generic, Callable
from ..lowlevel.environment import Prologue as LowPrologue, Environment as LowEnv
from ..lowlevel.artrdr import SvgRenderKind
from .utils import FdContext, WinCategory, MacCategory, FD_THUMBNAIL_HWS
from .utils import FdContext, WinCategory, MacCategory, VART_HW
@dataclass(frozen=True)
class Prologue(LowPrologue):
svg_render: SvgRenderKind
"""The render kind for SVG"""
"""The render kind for SVG."""
fd_thumbnail_hws: tuple[int, ...]
"""
The collection of sizes of generated FreeDesktop icons.
Each size must be greater than zero and lower or equal to ``VART_HW``.
Due to icon is a square rectangle, the height and width are same.
"""
temp_dir_initializer: Callable[[Path], None] | None
"""
The extra initializer for user temporary directory.
@@ -21,6 +27,15 @@ class Prologue(LowPrologue):
``None`` for no extra initializer.
"""
def __post_init__(self) -> None:
super().__post_init__()
for hw in self.fd_thumbnail_hws:
if hw <= 0 or hw > VART_HW:
raise ValueError(
f"Given FreeDesktop icon thumbnail size mismatch the size requirements. "
f"Expect >= 0 and <= {VART_HW}, got {hw}."
)
_TEMP_USER_DIR: str = "user"
_TEMP_LIB_DIR: str = "assemblicon"
@@ -52,7 +67,7 @@ class Environment(Generic[P]):
parents=True, exist_ok=True
)
# and output directory
for hw in FD_THUMBNAIL_HWS:
for hw in self.__prologue.fd_thumbnail_hws:
for kind in FdContext:
self.__output_artifact(f"{hw}x{hw}", str(kind)).mkdir(
parents=True, exist_ok=True
+3 -2
View File
@@ -2,7 +2,7 @@ from dataclasses import dataclass
from .common import Sink
from ..address import Address, AddressDomain
from ..environment import Environment
from ..utils import Artifact, FdContext, FD_THUMBNAIL_HWS, check_vart
from ..utils import Artifact, FdContext, check_vart
from ...lowlevel import artio, artrdr
from ...logger import HIGHLEVEL_LOGGER as LOGGER
@@ -33,7 +33,8 @@ class FdArtSink(Sink):
)
check_vart(art)
for hw, thumbnail in zip(
FD_THUMBNAIL_HWS, artrdr.render_thumbnail(art, FD_THUMBNAIL_HWS)
env.prologue.fd_thumbnail_hws,
artrdr.render_thumbnail(art, env.prologue.fd_thumbnail_hws),
):
dst = env.fd_output_artifact(hw, self.__kind, self.__name)
artio.save_artifact(thumbnail, dst)
@@ -2,7 +2,7 @@ from .common import Source
from ..address import Address, AddressDomain
from ..environment import Environment
from ..sink.fdart_sink import FdArtSink
from ..utils import FdContext, FD_THUMBNAIL_HWS
from ..utils import FdContext
from ...lowlevel import artio
from ...logger import HIGHLEVEL_LOGGER as LOGGER
@@ -28,7 +28,7 @@ class FdArtSource(Source):
sink_hint.kind,
sink_hint.name,
)
for hw in FD_THUMBNAIL_HWS:
for hw in env.prologue.fd_thumbnail_hws:
artio.link_artifact(
env.fd_output_artifact(hw, self.__kind, self.__name),
env.fd_output_artifact(hw, sink_hint.kind, sink_hint.name),
+2 -4
View File
@@ -40,7 +40,5 @@ class MacCategory(enum.StrEnum):
"""Icons for file extensions"""
# TODO: For easy testing, we only build 32x32 and 48x48.
# Enable full generation after testing.
FD_THUMBNAIL_HWS: tuple[int, ...] = (32, 48)
# FD_THUMBNAIL_HWS: tuple[int, ...] = (16, 32, 48, 64, 128, 256)
DEFAULT_FD_THUMBNAIL_HWS: tuple[int, ...] = (16, 32, 48, 64, 128, 256)
"""The default collection of sizes for generating FreeDesktop icons."""