Class: Tuile::Theme::Resolver

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(theme, background) ⇒ Resolver

@param theme — the unresolved theme.

@param background

Parameters:



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.

Parameters:

  • token (Symbol)

Returns:



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

Parameters:

  • key ([Symbol, Symbol])
  • value (Color, Proc)

Returns:



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