Class: Clogs::Button

Inherits:
Control show all
Defined in:
lib/clogs/drawables/controls.rb

Constant Summary collapse

FACE =
[246, 246, 246, 255].freeze
FACE_HOVER =
[252, 252, 252, 255].freeze
FACE_PRESSED =
[225, 225, 225, 255].freeze
EDGE =
[160, 160, 160, 255].freeze

Constants inherited from Control

Control::PADDING_X, Control::PADDING_Y

Instance Attribute Summary

Attributes inherited from Control

#focused, #hovered, #pressed

Attributes inherited from Drawable

#abs_x, #abs_y, #children, #height, #parent, #shoes_linkable_id, #styles, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Control

#clickable?, #label, #on_mouse_enter, #on_mouse_leave, #text_style

Methods inherited from Drawable

#add_child, #app, #clickable?, #contains?, #destroy_self, #each_peer, #focus_gained, #focus_lost, #focusable?, for_shoes_class, #hidden?, #initialize, #margin, #needs_layout!, #notify, #on_key, #on_mouse_move, #paint, #positioned?, #properties_changed, #redraw!, #remove_child, #requested_height, #requested_width, #set_parent, #style

Constructor Details

This class inherits a constructor from Clogs::Drawable

Instance Method Details

#draw(painter, x, y) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clogs/drawables/controls.rb', line 66

def draw(painter, x, y)
  face = Style.color(style(:color), nil) ||
    (pressed ? FACE_PRESSED : (hovered ? FACE_HOVER : FACE))
  painter.draw(fill: face, stroke: EDGE, thickness: 1) do |p|
    Clogs.rounded_rect(p, x + 0.5, y + 0.5, @width - 1, @height - 1, 4)
  end
  st = text_style(color: Style.color(style(:text_color), [0, 0, 0, 255]))
  block = TextBlock.new(
    [Run.new(text: label, size: st.size, family: st.family, color: st.color)],
    -1, default_family: st.family, default_size: st.size
  )
  block.draw(painter, x + (@width - @text_width) / 2.0, y + (@height - @text_height) / 2.0)
  block.free
end

#measure(available_width) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/clogs/drawables/controls.rb', line 51

def measure(available_width)
  st = text_style
  tw, th = Paragraph.measure(label.empty? ? " " : label, st)
  @text_width = tw
  @text_height = th
  @width = requested_width(available_width) || (tw + PADDING_X * 2).ceil
  @height = requested_height(available_width) ||
    (th + PADDING_Y * 2 + style(:padding_top).to_i + style(:padding_bottom).to_i).ceil
end

#on_click(_x, _y, _button) ⇒ Object



81
82
83
84
# File 'lib/clogs/drawables/controls.rb', line 81

def on_click(_x, _y, _button)
  self.pressed = true
  redraw!
end

#on_release(x, y, _button) ⇒ Object



86
87
88
89
90
91
# File 'lib/clogs/drawables/controls.rb', line 86

def on_release(x, y, _button)
  was_pressed = pressed
  self.pressed = false
  redraw!
  notify("click") if was_pressed && contains?(x, y)
end