Class: Tuile::Component::PickerWindow
- Inherits:
-
Window
- Object
- Tuile::Component
- Window
- Tuile::Component::PickerWindow
- Defined in:
- lib/tuile/component/picker_window.rb
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
Attributes included from HasContent
Attributes inherited from Tuile::Component
Class Method Summary collapse
-
.open(caption, options) {|key| ... } ⇒ Popup
Opens a picker as a popup.
Instance Method Summary collapse
- #handle_key(key) ⇒ Boolean
-
#initialize(caption, options) {|key| ... } ⇒ PickerWindow
constructor
A new instance of PickerWindow.
- #keyboard_hint ⇒ String
Methods inherited from Window
#children, #content_size, #focusable?, #handle_mouse, #key_shortcut=, #rect=, #repaint, #scrollbar=, #visible?
Methods included from HasContent
#children, #handle_mouse, #on_focus, #rect=
Methods inherited from Tuile::Component
#active=, #active?, #attached?, #children, #content_size, #cursor_position, #depth, #find_shortcut_component, #focus, #focusable?, #handle_mouse, #on_child_removed, #on_focus, #on_tree, #repaint, #root, #screen
Constructor Details
#initialize(caption, options) {|key| ... } ⇒ PickerWindow
Returns a new instance of PickerWindow.
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(it[0], it[1]) } @block = block list = Component::List.new list.lines = @options.map { "#{it.key} #{Rainbow(it.caption).cadetblue}" } 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) {|key| ... } ⇒ Popup
Opens a picker as a popup. Picking an option fires ‘block`, then closes the popup; ESC / `q` close without firing `block`.
79 80 81 82 83 84 85 |
# File 'lib/tuile/component/picker_window.rb', line 79 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
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tuile/component/picker_window.rb', line 55 def handle_key(key) return true if super if @options.any? { it.key == key } select_option(key) true else false end end |
#keyboard_hint ⇒ String
67 68 69 |
# File 'lib/tuile/component/picker_window.rb', line 67 def keyboard_hint @options.map { "#{it.key} #{Rainbow(it.caption).cadetblue}" }.join(" ") end |