Class: Clacky::RichUIController::ConfigMenuDialog

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(choices:, selected_index: 0, title: "Model Configuration", width: 86) ⇒ ConfigMenuDialog

Returns a new instance of ConfigMenuDialog.



1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
# File 'lib/clacky/rich_ui_controller.rb', line 1328

def initialize(choices:, selected_index: 0, title: "Model Configuration", width: 86)
  @choices = choices
  @selected_index = selected_index
  @width = width
  @height = [choices.length + 7, 12].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: :config_dialog, width: @width, height: @height)
  @layout.update_content(@panel)
  @layout.calculate_dimensions(@width, @height)
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



1326
1327
1328
# File 'lib/clacky/rich_ui_controller.rb', line 1326

def height
  @height
end

#widthObject

Returns the value of attribute width.



1326
1327
1328
# File 'lib/clacky/rich_ui_controller.rb', line 1326

def width
  @width
end

Instance Method Details

#finish(value) ⇒ Object



1356
1357
1358
1359
1360
1361
1362
1363
# File 'lib/clacky/rich_ui_controller.rb', line 1356

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

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



1370
1371
1372
1373
1374
# File 'lib/clacky/rich_ui_controller.rb', line 1370

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

#move_downObject



1352
1353
1354
# File 'lib/clacky/rich_ui_controller.rb', line 1352

def move_down
  move(1)
end

#move_upObject



1348
1349
1350
# File 'lib/clacky/rich_ui_controller.rb', line 1348

def move_up
  move(-1)
end

#notify_listeners(event_data) ⇒ Object



1376
1377
1378
# File 'lib/clacky/rich_ui_controller.rb', line 1376

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

#render_to_bufferObject



1380
1381
1382
1383
1384
# File 'lib/clacky/rich_ui_controller.rb', line 1380

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

#selected_choiceObject



1344
1345
1346
# File 'lib/clacky/rich_ui_controller.rb', line 1344

def selected_choice
  @choices[@selected_index]
end

#waitObject



1365
1366
1367
1368
# File 'lib/clacky/rich_ui_controller.rb', line 1365

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