Module: Rooibos::Welcome

Defined in:
lib/rooibos/welcome.rb

Overview

Built-in welcome screen used by scaffolded applications.

Defined Under Namespace

Modules: UI Classes: Model

Constant Summary collapse

View =
-> (model, _tui) { model.tree }
Update =
-> (message, model) {
  case message
  in _ if message.ctrl_c?
    Rooibos::Command.exit
  in _ if message.resize?
    model.with(button_areas: model.button_areas.for_viewport(message.width, message.height))
  in _ if message.tab?
    cycle_focus(model, :forward)
  in _ if message.shift_back_tab?
    cycle_focus(model, :backward)
  in _ if message.enter? && model.active_button
    activate_button(model.active_button, model)
  in _ if message.mouse? && message.down? && message.button == "left"
    handle_click(message, model)
  in { type: :mouse }
    handle_hover(message, model)
  else
    model
  end
}
Init =
-> {
  viewport = RatatuiRuby.terminal_size
  areas = UI::ButtonAreas.new(website: nil, exit: nil).for_viewport(viewport.width, viewport.height)
  Ractor.make_shareable Model.new(button_areas: areas, focused: nil, hovered: nil)
}

Class Method Summary collapse

Class Method Details

.activate_button(button, model) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/rooibos/welcome.rb', line 246

def self.activate_button(button, model)
  case button
  when :website then [model, Rooibos::Command.system("open 'https://www.rooibos.run'", :open_url)]
  when :exit then Rooibos::Command.exit
  else model
  end
end

.cycle_focus(model, direction) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rooibos/welcome.rb', line 231

def self.cycle_focus(model, direction)
  order = UI::FOCUS_ORDER
  return model.with(focused: order.first) unless model.focused

  current_index = order.index(model.focused)
  return model.with(focused: order.first) unless current_index

  next_index = case direction
              when :forward then (current_index + 1) % order.length
              when :backward then (current_index - 1) % order.length
              else 0
  end
  model.with(focused: order[next_index])
end

.handle_click(message, model) ⇒ Object



219
220
221
222
# File 'lib/rooibos/welcome.rb', line 219

def self.handle_click(message, model)
  button = model.button_areas.button_at(message.x, message.y)
  activate_button(button, model)
end

.handle_hover(message, model) ⇒ Object



224
225
226
227
228
229
# File 'lib/rooibos/welcome.rb', line 224

def self.handle_hover(message, model)
  new_hovered = model.button_areas.button_at(message.x, message.y)
  return model if new_hovered == model.hovered

  model.with(hovered: new_hovered)
end