Class: Thaum::StatusBar

Inherits:
Object
  • Object
show all
Includes:
Sigil
Defined in:
lib/thaum/sigils/status_bar.rb

Overview

Horizontal bar of labeled segments separated by a configurable delimiter. Segments are either plain Strings (decorative) or Hashes ‘{ label:, on_click: }` whose click handler fires when the user left-presses inside the segment’s column range. Non-focusable —status bars sit at the bottom of the focus order. Mouse events the bar does not act on are eaten (not propagated) so the bar acts as the floor of the click target.

Constant Summary collapse

DEFAULT_SEPARATOR =
""

Instance Attribute Summary collapse

Attributes included from Sigil

#handler_parent, #rect, #thaum_app

Instance Method Summary collapse

Methods included from Sigil

#emit, #focused?, #on_blur, #on_focus, #on_key, #on_mount, #on_paste, #on_tick, #on_unmount, #on_update, #request_render

Constructor Details

#initialize(segments:, separator: DEFAULT_SEPARATOR) ⇒ StatusBar

Returns a new instance of StatusBar.



18
19
20
21
22
# File 'lib/thaum/sigils/status_bar.rb', line 18

def initialize(segments:, separator: DEFAULT_SEPARATOR)
  @segments  = segments
  @separator = separator
  @ranges    = [] # parallel array: [start_col, end_col_exclusive] per segment
end

Instance Attribute Details

#segmentsObject

Returns the value of attribute segments.



16
17
18
# File 'lib/thaum/sigils/status_bar.rb', line 16

def segments
  @segments
end

#separatorObject (readonly)

Returns the value of attribute separator.



16
17
18
# File 'lib/thaum/sigils/status_bar.rb', line 16

def separator
  @separator
end

Instance Method Details

#focusable?Boolean

Returns:

  • (Boolean)


24
# File 'lib/thaum/sigils/status_bar.rb', line 24

def focusable? = false

#on_mouse(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/thaum/sigils/status_bar.rb', line 31

def on_mouse(event)
  return unless event.action == :press && event.button == :left

  seg = segment_at(event.x) or return
  handler = on_click(seg) or return
  if handler.arity.zero?
    handler.call
  else
    handler.call(event)
  end
end

#render(canvas:, theme:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/thaum/sigils/status_bar.rb', line 43

def render(canvas:, theme:)
  canvas.fill(bg: theme.bar_bg)
  @ranges = []
  x = 0
  width = canvas.width
  @segments.each_with_index do |seg, idx|
    x += draw_separator(canvas: canvas, theme: theme, x: x, width: width) if idx.positive?
    break if x >= width

    label = label(seg)
    label_w = label.display_width
    start = x
    canvas.text(content: label, x: x, fg: theme.fg, bg: theme.bar_bg)
    x += label_w
    finish = [x, width].min
    @ranges << [start, finish]
  end
end