Class: Charming::Components::HelpOverlay

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/help_overlay.rb

Overview

HelpOverlay renders a controller’s key bindings as a two-column cheat-sheet inside a Modal — the classic ‘?` help screen. Build it straight from a controller class:

HelpOverlay.for_controller(self.class, theme: theme)

or with explicit entries:

HelpOverlay.new(bindings: {"q" => "Quit", "ctrl+p" => "Command palette"})

Any key dismisses it (‘handle_key` returns :cancelled).

Constant Summary collapse

DEFAULT_TITLE =
"Keyboard Shortcuts"
DEFAULT_WIDTH =
44

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(bindings:, title: DEFAULT_TITLE, width: DEFAULT_WIDTH, theme: nil) ⇒ HelpOverlay

bindings maps key names (symbols or strings) to description strings.



29
30
31
32
33
34
# File 'lib/charming/presentation/components/help_overlay.rb', line 29

def initialize(bindings:, title: DEFAULT_TITLE, width: DEFAULT_WIDTH, theme: nil)
  super(theme: theme)
  @bindings = bindings
  @title = title
  @width = width
end

Class Method Details

.for_controller(controller_class, title: DEFAULT_TITLE, theme: nil) ⇒ Object

Builds an overlay from a controller class’s ‘key_bindings` (key → action name). Action names are humanized into descriptions (“open_command_palette” → “Open command palette”).



21
22
23
24
25
26
# File 'lib/charming/presentation/components/help_overlay.rb', line 21

def self.for_controller(controller_class, title: DEFAULT_TITLE, theme: nil)
  bindings = controller_class.key_bindings.transform_values do |action|
    action.to_s.tr("_", " ").capitalize
  end
  new(bindings: bindings, title: title, theme: theme)
end

Instance Method Details

#captures_text?Boolean

Free-typed characters belong to this component while it is focused.

Returns:

  • (Boolean)


37
38
39
# File 'lib/charming/presentation/components/help_overlay.rb', line 37

def captures_text?
  true
end

#handle_key(_event) ⇒ Object

Any key dismisses the overlay.



42
43
44
# File 'lib/charming/presentation/components/help_overlay.rb', line 42

def handle_key(_event)
  :cancelled
end

#renderObject

Renders the bindings table inside a titled modal.



47
48
49
# File 'lib/charming/presentation/components/help_overlay.rb', line 47

def render
  render_component(Modal.new(content: table, title: @title, width: @width, theme: theme))
end