Class: TuiTui::Modal::Confirm

Inherits:
Modal
  • Object
show all
Defined in:
lib/tui_tui/modal/confirm.rb

Overview

OK / Cancel confirmation modal.

Constant Summary collapse

GAP =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, ok: "OK", cancel: "Cancel", default: :cancel, theme: Theme::DEFAULT) ⇒ Confirm

Returns a new instance of Confirm.



14
15
16
17
18
19
20
# File 'lib/tui_tui/modal/confirm.rb', line 14

def initialize(message, ok: "OK", cancel: "Cancel", default: :cancel, theme: Theme::DEFAULT)
  @message = DisplayText.new(message)
  @ok = button_text(ok)
  @cancel = button_text(cancel)
  @focus = default
  @theme = theme
end

Instance Attribute Details

#focusObject (readonly)

Returns the value of attribute focus.



12
13
14
# File 'lib/tui_tui/modal/confirm.rb', line 12

def focus
  @focus
end

Instance Method Details

#draw(canvas, size) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/tui_tui/modal/confirm.rb', line 44

def draw(canvas, size)
  inner = [@message.width, buttons_width].max
  rect, col = panel(canvas, inner: inner, body_rows: 3)
  canvas.text(rect.row + 1, col, @message.center(inner), theme.text)
  draw_buttons(canvas, rect.row + 3, col, inner)
  canvas
end

#handle(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tui_tui/modal/confirm.rb', line 22

def handle(key)
  case key
  when :left, :right, "\t", "h", "l"
    toggle
  when "\r", " "
    @focus
  when "y", "Y"
    :ok
  when "n", "N", :escape, KeyCode::CTRL_C
    :cancel
  end
end

#handle_mouse(event) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/tui_tui/modal/confirm.rb', line 35

def handle_mouse(event)
  return nil unless event.action == :press && @buttons_row == event.row

  return :ok if hit?(event.col, @ok_at, @ok.width)
  return :cancel if hit?(event.col, @cancel_at, @cancel.width)

  nil
end