Class: TuiTui::ModalHost

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_tui/modal_host.rb

Overview

Host-side helper for the app that owns the current modal widget.

Centralizes the open + dispatch loop every app with modals otherwise hand writes: it routes MouseEvents to #handle_mouse and other events to #handle, honors the “resolved value, or nil to stay open” widget contract, and runs the caller’s on_result callback when the modal resolves.

host = TuiTui::ModalHost.new
host.open(TuiTui::Confirm.new("Quit?")) { |r| :quit if r == :ok }
# in update(event):
if host.open?
  outcome = host.handle(event)   # nil while open; on_result value once resolved
  return outcome == :quit ? :quit : self
end
# in view(size): host.draw(canvas, size) if host.open?

Instance Method Summary collapse

Instance Method Details

#closeObject



30
31
32
33
# File 'lib/tui_tui/modal_host.rb', line 30

def close
  @widget = nil
  @on_result = nil
end

#draw(canvas, size) ⇒ Object



35
# File 'lib/tui_tui/modal_host.rb', line 35

def draw(canvas, size) = @widget&.draw(canvas, size)

#handle(event) ⇒ Object

Route one event to the modal. Returns nil while the modal stays open (the event was consumed), or the on_result callback’s value once the widget resolves (e.g. :quit, or a new app model).



40
41
42
43
44
45
46
47
48
49
# File 'lib/tui_tui/modal_host.rb', line 40

def handle(event)
  return nil unless open?

  result = dispatch(event)
  return nil if result.nil?

  callback = @on_result
  close
  callback.call(result)
end

#open(widget, &on_result) ⇒ Object



22
23
24
25
26
# File 'lib/tui_tui/modal_host.rb', line 22

def open(widget, &on_result)
  @widget = widget
  @on_result = on_result || ->(result) { result }
  self
end

#open?Boolean

Returns:

  • (Boolean)


28
# File 'lib/tui_tui/modal_host.rb', line 28

def open? = !@widget.nil?