Class: Cura::Component::Listbox
- Defined in:
- lib/cura/component/listbox.rb
Overview
A component containing a selectable list of components.
Instance Attribute Summary collapse
-
#objects ⇒ Array
readonly
Get the objects stored.
-
#selected_index ⇒ nil, Integer
Get the currently selected item’s index.
Attributes inherited from Pack
Attributes included from Attributes::HasOrientation
Attributes included from Attributes::HasAncestry
Attributes included from Attributes::HasOffsets
Attributes included from Attributes::HasEvents
Instance Method Summary collapse
-
#add_child(component_or_type, attributes = {}) ⇒ Component
Add a child to this group.
-
#delete_child_at(index) ⇒ Component
Remove a child from this listbox’s children at the given index.
-
#initialize(attributes = {}) ⇒ Listbox
constructor
A new instance of Listbox.
-
#loopable=(value) ⇒ Boolean
Set whether this listbox is loopable or not.
-
#loopable? ⇒ Boolean
Get whether this listbox is loopable or not.
-
#object_at(index) ⇒ Object
Get the associated object with the child at the given index.
-
#selected_child ⇒ Component
Get the child at the selected index.
-
#selected_child=(child) ⇒ Component
Set the selected child.
-
#selected_object ⇒ Component
Get the object associated with the child at the selected index.
Methods inherited from Pack
#draw, #fill=, #fill?, #height=, #width=
Methods included from Attributes::HasOrientation
Methods included from Attributes::HasAttributes
Methods inherited from Group
#draw, #height, #update, #width
Methods included from Attributes::HasChildren
#add_children, #children, #children?, #delete_child, #delete_children, #each
Methods inherited from Base
#application, #background, #contains_coordinates?, #cursor, #draw, #draw=, #draw?, #focus, #focused?, #foreground, #get_or_inherit_color, inherited, #inspect, #pencil, type, #update, #window
Methods included from Attributes::HasVisibility
Methods included from Attributes::HasRelativeCoordinates
Methods included from Attributes::HasCoordinates
Methods included from Attributes::HasAncestry
Methods included from Attributes::HasOffsets
#border, #border=, #margin, #margin=, #padding, #padding=
Methods included from Attributes::HasColors
#background, #background=, #foreground, #foreground=
Methods included from Attributes::HasFocusability
Methods included from Attributes::HasEvents
Methods included from Attributes::HasDimensions
#height, #height=, #resize, #width, #width=
Constructor Details
#initialize(attributes = {}) ⇒ Listbox
Returns a new instance of Listbox.
40 41 42 43 44 45 46 47 |
# File 'lib/cura/component/listbox.rb', line 40 def initialize(attributes={}) @focusable = true @loopable = true @selected_index = 0 @objects = [] super end |
Instance Attribute Details
#objects ⇒ Array (readonly)
Get the objects stored.
132 133 134 |
# File 'lib/cura/component/listbox.rb', line 132 def objects @objects end |
#selected_index ⇒ nil, Integer
Get the currently selected item’s index. Returns ‘nil` is nothing is selected.
53 54 55 |
# File 'lib/cura/component/listbox.rb', line 53 def selected_index @selected_index end |
Instance Method Details
#add_child(component_or_type, attributes = {}) ⇒ Component
Add a child to this group.
107 108 109 110 111 112 113 114 |
# File 'lib/cura/component/listbox.rb', line 107 def add_child(component_or_type, attributes={}) object = attributes.delete(:object) child = super @objects << object child end |
#delete_child_at(index) ⇒ Component
Remove a child from this listbox’s children at the given index.
120 121 122 123 124 125 126 127 |
# File 'lib/cura/component/listbox.rb', line 120 def delete_child_at(index) deleted_child = super @objects.delete_at(index) self.selected_index = @children.length - 1 if @selected_index >= @children.length deleted_child end |
#loopable=(value) ⇒ Boolean
Set whether this listbox is loopable or not.
160 161 162 |
# File 'lib/cura/component/listbox.rb', line 160 def loopable=(value) @loopable = !!value end |
#loopable? ⇒ Boolean
Get whether this listbox is loopable or not.
152 153 154 |
# File 'lib/cura/component/listbox.rb', line 152 def loopable? @loopable end |
#object_at(index) ⇒ Object
Get the associated object with the child at the given index.
138 139 140 |
# File 'lib/cura/component/listbox.rb', line 138 def object_at(index) @objects[index] end |
#selected_child ⇒ Component
Get the child at the selected index.
80 81 82 |
# File 'lib/cura/component/listbox.rb', line 80 def selected_child @children[@selected_index] end |
#selected_child=(child) ⇒ Component
Set the selected child.
88 89 90 91 92 93 94 95 |
# File 'lib/cura/component/listbox.rb', line 88 def selected_child=(child) index = @children.index(child) return nil if index.nil? # TODO: Raise error? self.selected_index = index selected_child end |
#selected_object ⇒ Component
Get the object associated with the child at the selected index.
145 146 147 |
# File 'lib/cura/component/listbox.rb', line 145 def selected_object @objects[@selected_index] end |