Class: Clacky::RichUIController::FormDialog

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/rich_ui_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, fields:, width: 92) ⇒ FormDialog

Returns a new instance of FormDialog.



1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
# File 'lib/clacky/rich_ui_controller.rb', line 1429

def initialize(title:, fields:, width: 92)
  @title = title
  @fields = fields
  @field_index = 0
  @editors = fields.map do |field|
    RubyRich::LineEditor.new.tap { |editor| editor.value = field[:default].to_s }
  end
  @width = width
  @height = [fields.length * 3 + 8, 16].max
  @event_listeners = {}
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @finished = false
  @result = nil
  @panel = RubyRich::Panel.new("", title: title, border_style: :cyan, title_align: :center)
  @layout = RubyRich::Layout.new(name: :form_dialog, width: @width, height: @height)
  @layout.update_content(@panel)
  @layout.calculate_dimensions(@width, @height)
  wire_default_keys
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



1427
1428
1429
# File 'lib/clacky/rich_ui_controller.rb', line 1427

def height
  @height
end

#widthObject

Returns the value of attribute width.



1427
1428
1429
# File 'lib/clacky/rich_ui_controller.rb', line 1427

def width
  @width
end

Instance Method Details

#finish(value) ⇒ Object



1450
1451
1452
1453
1454
1455
1456
1457
# File 'lib/clacky/rich_ui_controller.rb', line 1450

def finish(value)
  @mutex.synchronize do
    @result = value
    @finished = true
    @condition.signal
  end
  true
end

#key(event_name, priority = 0, &block) ⇒ Object



1464
1465
1466
1467
1468
# File 'lib/clacky/rich_ui_controller.rb', line 1464

def key(event_name, priority = 0, &block)
  @event_listeners[event_name] ||= []
  @event_listeners[event_name] << { priority: priority, block: block }
  @event_listeners[event_name].sort_by! { |listener| -listener[:priority] }
end

#notify_listeners(event_data) ⇒ Object



1470
1471
1472
1473
# File 'lib/clacky/rich_ui_controller.rb', line 1470

def notify_listeners(event_data)
  listeners = Array(@event_listeners[event_data[:name]])
  listeners.each { |listener| listener[:block].call(event_data, nil) }
end

#render_to_bufferObject



1475
1476
1477
1478
1479
# File 'lib/clacky/rich_ui_controller.rb', line 1475

def render_to_buffer
  @panel.content = render_content
  @layout.calculate_dimensions(@width, @height)
  @layout.render_to_buffer
end

#waitObject



1459
1460
1461
1462
# File 'lib/clacky/rich_ui_controller.rb', line 1459

def wait
  @mutex.synchronize { @condition.wait(@mutex) until @finished }
  @result
end