Class: Tuile::Theme::Resolver
- Inherits:
-
Object
- Object
- Tuile::Theme::Resolver
- Defined in:
- lib/tuile/theme.rb,
sig/tuile.rbs
Overview
What a derivation Proc gets as its second argument: the theme being
resolved, read the way paint code reads a resolved one — t[:pane_bg],
t.input_bg_color — with a derived sibling resolved on first read, so
declaration order does not matter.
Instance Method Summary collapse
-
#[](token) ⇒ Color
@param
token— a #custom token. -
#initialize(theme, background) ⇒ Resolver
constructor
@param
theme— the unresolved theme. -
#resolve_token(key, value) ⇒ Color
@param
key— the namespace and name, since a custom token may share a chrome token's name.
Constructor Details
#initialize(theme, background) ⇒ Resolver
@param theme — the unresolved theme.
@param background
209 210 211 212 213 214 215 |
# File 'lib/tuile/theme.rb', line 209 def initialize(theme, background) @chrome = theme.to_h.except(:custom) @custom = theme.custom @background = background @done = {} @resolving = [] end |
Instance Method Details
#[](token) ⇒ Color
@param token — a Tuile::Theme#custom token.
220 |
# File 'lib/tuile/theme.rb', line 220 def [](token) = resolve_token([:custom, token], @custom.fetch(token)) |
#resolve_token(key, value) ⇒ Color
@param key — the namespace and name, since a custom token may share a chrome token's name.
@param value
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/tuile/theme.rb', line 232 def resolve_token(key, value) return @done[key] if @done.key?(key) return @done[key] = value unless value.is_a?(Proc) if @resolving.include?(key) path = [*@resolving, key].map { |(kind, name)| kind == :custom ? "[#{name.inspect}]" : name.to_s } raise ArgumentError, "derived theme tokens form a cycle: #{path.join(" -> ")}" end @resolving << key color = value.call(*[@background, self].first(Theme.derivation_arity(value))) @resolving.pop raise TypeError, "#{key.last.inspect} derived #{color.inspect}, not a Tuile::Color" unless color.is_a?(Color) @done[key] = color end |