Class: Thaum::Tabs

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

Overview

Horizontal tab strip. ←/→ cycle the active tab (with wrap). Emits Tabs::ActivatedEvent when the active index changes. The content under each tab is the App’s concern — Tabs only owns the strip.

Constant Summary collapse

ActivatedEvent =
Thaum::Event.define(:index, :label)

Instance Attribute Summary collapse

Attributes included from Sigil

#handler_parent, #rect, #thaum_app

Instance Method Summary collapse

Methods included from Sigil

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

Constructor Details

#initialize(labels:, active: 0) ⇒ Tabs

Returns a new instance of Tabs.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/thaum/sigils/tabs.rb', line 14

def initialize(labels:, active: 0)
  raise ArgumentError, "Tabs needs at least one label" if labels.empty?

  @labels = labels
  @active = active.clamp(0, labels.length - 1)
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



12
13
14
# File 'lib/thaum/sigils/tabs.rb', line 12

def active
  @active
end

#labelsObject (readonly)

Returns the value of attribute labels.



12
13
14
# File 'lib/thaum/sigils/tabs.rb', line 12

def labels
  @labels
end

Instance Method Details

#currentObject



21
# File 'lib/thaum/sigils/tabs.rb', line 21

def current = @labels[@active]

#on_key(event) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/thaum/sigils/tabs.rb', line 23

def on_key(event)
  case event.key
  when :left  then move(-1)
  when :right then move(1)
  else             emit event
  end
end

#render(canvas:, theme:) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/thaum/sigils/tabs.rb', line 31

def render(canvas:, theme:)
  canvas.fill(bg: theme.bar_bg)
  x = 0
  @labels.each_with_index do |label, idx|
    x += draw_tab(canvas: canvas, label: label, idx: idx, x: x, theme: theme)
  end
end