Class: Hokusai::Blocks::Toggle

Inherits:
Hokusai::Block show all
Defined in:
ui/src/hokusai/blocks/toggle.rb

Instance Attribute Summary collapse

Attributes inherited from Hokusai::Block

#node, #provides, #publisher

Instance Method Summary collapse

Methods inherited from Hokusai::Block

#children, #children?, compile, computed, computed!, #draw, #draw_with, #dump, #emit, inject, inject!, #method_missing, mount, #on_resize, provide, provides, style, styles_get, template, template_from_file, template_get, #update, use, uses

Constructor Details

#initialize(**args) ⇒ Toggle

Returns a new instance of Toggle.



26
27
28
29
30
# File 'ui/src/hokusai/blocks/toggle.rb', line 26

def initialize(**args)
  @toggled = false

  super(**args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hokusai::Block

Instance Attribute Details

#toggledObject

Returns the value of attribute toggled.



14
15
16
# File 'ui/src/hokusai/blocks/toggle.rb', line 14

def toggled
  @toggled
end

Instance Method Details

#computed_colorObject



22
23
24
# File 'ui/src/hokusai/blocks/toggle.rb', line 22

def computed_color
  toggled ? active_color : inactive_color
end

#render(canvas) {|canvas| ... } ⇒ Object

Yields:

  • (canvas)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ui/src/hokusai/blocks/toggle.rb', line 32

def render(canvas)
  width = size * 2
  radius = size / 2

  start = toggled ? (canvas.x + width - radius) : canvas.x + radius

  draw do
    rect(canvas.x, canvas.y, width.to_f, size) do |command|
      command.color = computed_color
      command.round = size
      command.padding = Hokusai::Padding.convert(20)
    end

    circle(start, canvas.y + radius, radius) do |command|
      command.color = color
    end
  end

  canvas.width = size * 2
  canvas.height = size

  yield(canvas)
end

#toggle(_) ⇒ Object



16
17
18
19
20
# File 'ui/src/hokusai/blocks/toggle.rb', line 16

def toggle(_)
  self.toggled = !toggled

  emit("toggle", value: toggled)
end