Skip to content

Commit

Permalink
refactor(shader): pass variables to Shader instead of config
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed Apr 9, 2024
1 parent d27dc8c commit d31d501
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/hyprshade/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
from functools import partial
from typing import TYPE_CHECKING, Any, Final, Literal, TypeVar, overload

import click
Expand Down Expand Up @@ -79,7 +80,10 @@ def convert(
):
obj: ContextObject | None = ctx.obj if ctx is not None else None
config = obj.get_config() if obj is not None else None
return Shader(value, config)
lazy_variables = (
partial(config.shader_variables, value) if config is not None else None
)
return Shader(value, lazy_variables)

def shell_complete(
self, ctx: click.Context, param: click.Parameter, incomplete: str
Expand Down
9 changes: 7 additions & 2 deletions src/hyprshade/config/schedule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from functools import partial
from itertools import chain, pairwise
from typing import TYPE_CHECKING, Any, TypeGuard

Expand All @@ -27,7 +28,8 @@ def __init__(self, config: Config):
def scheduled_shader(self, t: time) -> Shader | None:
for entry in self._resolved_entries():
if is_time_between(t, entry.start_time, entry.end_time):
return Shader(entry.name, self.config)
lazy_variables = partial(self.config.shader_variables, entry.name)
return Shader(entry.name, lazy_variables)

return self.default_shader

Expand All @@ -40,7 +42,10 @@ def event_times(self) -> Iterator[time]:
@property
def default_shader(self) -> Shader | None:
default = only(filter(lambda s: s.default, self.config.model.shaders))
return Shader(default.name, self.config) if default else None
if not default:
return None
lazy_variables = partial(self.config.shader_variables, default.name)
return Shader(default.name, lazy_variables)

def _resolved_entries(self) -> Iterator[ResolvedEntry]:
if not (entries := self._entries()):
Expand Down
31 changes: 21 additions & 10 deletions src/hyprshade/shader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import logging
import os
from typing import TYPE_CHECKING, Final
from collections.abc import Callable
from functools import cached_property
from typing import Any, Final, TypeVar, Union

from more_itertools import flatten

Expand All @@ -15,8 +17,10 @@
from . import hyprctl
from .dirs import ShaderDirs

if TYPE_CHECKING:
from hyprshade.config.core import Config
T = TypeVar("T")
PossiblyLazy = Union[T, Callable[[], T]] # noqa: UP007

ShaderVariables = dict[str, Any]


class PureShader:
Expand Down Expand Up @@ -84,11 +88,15 @@ def _resolve_path_from_shader_dirs(self) -> str:

class Shader(PureShader):
dirs: Final = ShaderDirs
_config: Config | None
_variables: PossiblyLazy[ShaderVariables | None]

def __init__(self, shader_name_or_path: str, config: Config | None):
def __init__(
self,
shader_name_or_path: str,
variables: PossiblyLazy[ShaderVariables | None],
):
super().__init__(shader_name_or_path)
self._config = config
self._variables = variables

def on(self) -> None:
source_path = self._resolve_path()
Expand All @@ -109,12 +117,15 @@ def current() -> PureShader | None:
path = hyprctl.get_screen_shader()
return None if path is None else PureShader(path)

@cached_property
def variables(self) -> ShaderVariables | None:
if callable(self._variables):
return self._variables()
return self._variables

def _render_template(self, path: str) -> str:
with open(path) as f:
variables = (
self._config.shader_variables(self._name) if self._config else None
)
content = mustache.render(f, variables)
content = mustache.render(f, self.variables)
base, _ = os.path.splitext(os.path.basename(path))
rendered_path = os.path.join(user_state_dir("hyprshade"), base)
os.makedirs(os.path.dirname(rendered_path), exist_ok=True)
Expand Down

0 comments on commit d31d501

Please sign in to comment.