Class: Tuile::Component::PickerWindow
- Defined in:
- lib/tuile/component/picker_window.rb,
sig/tuile.rbs
Overview
A Window that lists options identified by single keyboard keys, asks the user to pick one, and fires a callback with the picked key.
Usable tiled (just add to a Layout and read picks via the block) or
as a popup via PickerWindow.open, which wraps it in a Popup that closes itself
after a pick. ESC / q close without firing the callback.
Defined Under Namespace
Classes: Option
Constant Summary collapse
- MAX_ITEMS =
Scrolls the window when more items.
10
Instance Attribute Summary collapse
-
#on_pick ⇒ Proc?
Callback invoked after the user picks an option (after the block fires).
Attributes inherited from Window
#caption, #footer, #footer_text
Attributes included from HasContent
Class Method Summary collapse
-
.open(caption, options, &block) ⇒ void
Opens a picker as a popup.
Instance Method Summary collapse
-
#handle_key(key) ⇒ Boolean
Handles an option-key press.
-
#initialize(caption, options, &block) ⇒ PickerWindow
constructor
@param
caption— the window caption. - #keyboard_hint ⇒ String
-
#select_option(key) ⇒ void
@param
key.
Methods inherited from Window
#bottom_border, #children, #focusable?, #frame_caption, #handle_mouse, #key_shortcut=, #layout, #layout_footer, #on_focus, #rect=, #repaint, #repaint_border, #scrollbar=
Methods included from HasContent
#children, #handle_mouse, #on_focus, #rect=
Constructor Details
#initialize(caption, options, &block) ⇒ PickerWindow
@param caption — the window caption.
@param options — pairs of keyboard key and option caption. No Rainbow formatting must be used.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tuile/component/picker_window.rb', line 32 def initialize(caption, , &block) raise ArgumentError, "block required" unless block raise ArgumentError, "options must not be empty" if .empty? super(caption) @options = .map { Option.new(_1[0], _1[1]) } @block = block list = Component::List.new list.lines = @options.map { "#{_1.key} #{screen.theme.hint(_1.caption)}" } list.cursor = Component::List::Cursor.new list.on_item_chosen = ->(index, _line) { select_option(@options[index].key) } self.content = list # Optional hook for a containing Popup to dismiss itself after a pick. @on_pick = nil end |
Instance Attribute Details
#on_pick ⇒ Proc?
Callback invoked after the user picks an option (after the block
fires). The Tuile::Component::Popup returned by open sets this to its own close.
51 52 53 |
# File 'lib/tuile/component/picker_window.rb', line 51 def on_pick @on_pick end |
Class Method Details
.open(caption, options, &block) ⇒ void
This method returns an undefined value.
Opens a picker as a popup. Picking an option fires block, then
closes the popup; ESC / q close without firing block.
@param caption
@param options
@return — the wrapping popup.
81 82 83 84 85 86 87 |
# File 'lib/tuile/component/picker_window.rb', line 81 def self.open(caption, , &block) picker = PickerWindow.new(caption, , &block) popup = Popup.new(content: picker) picker.on_pick = -> { popup.close } popup.open popup end |
Instance Method Details
#handle_key(key) ⇒ Boolean
Handles an option-key press. Reached by bubbling: the inner List
(the focused component) sees the key first and handles cursor/Enter
picks; anything it declines bubbles up here, where a key matching an
option's key picks that option.
@param key
59 60 61 62 63 64 65 66 |
# File 'lib/tuile/component/picker_window.rb', line 59 def handle_key(key) if @options.any? { _1.key == key } select_option(key) true else false end end |
#keyboard_hint ⇒ String
69 70 71 |
# File 'lib/tuile/component/picker_window.rb', line 69 def keyboard_hint @options.map { "#{_1.key} #{screen.theme.hint(_1.caption)}" }.join(" ") end |
#select_option(key) ⇒ void
This method returns an undefined value.
@param key
93 94 95 96 |
# File 'lib/tuile/component/picker_window.rb', line 93 def select_option(key) @block.call(key) @on_pick&.call end |