Class: Cura::Component::Listbox

Inherits:
Pack show all
Defined in:
lib/cura/component/listbox.rb

Overview

A component containing a selectable list of components.

Instance Attribute Summary collapse

Attributes inherited from Pack

#spacing

Attributes included from Attributes::HasOrientation

#orientation

Attributes included from Attributes::HasAncestry

#parent

Attributes included from Attributes::HasOffsets

#offsets

Attributes included from Attributes::HasEvents

#event_handler

Instance Method Summary collapse

Methods inherited from Pack

#draw, #fill=, #fill?, #height=, #width=

Methods included from Attributes::HasOrientation

#horizontal?, #vertical?

Methods included from Attributes::HasAttributes

included, #update_attributes

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

#visible=, #visible?

Methods included from Attributes::HasRelativeCoordinates

#absolute_x, #absolute_y

Methods included from Attributes::HasCoordinates

#x, #x=, #y, #y=

Methods included from Attributes::HasAncestry

#ancestors, #parent?

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

#focusable=, #focusable?

Methods included from Attributes::HasEvents

included, #on_event

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

#objectsArray (readonly)

Get the objects stored.

Returns:

  • (Array)


132
133
134
# File 'lib/cura/component/listbox.rb', line 132

def objects
  @objects
end

#selected_indexnil, Integer

Get the currently selected item’s index. Returns ‘nil` is nothing is selected.

Returns:

  • (nil, Integer)


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.

Parameters:

  • component_or_type (#to_sym, Component)

    A Symbol representing the child component type or a Base instance. When a Symbol is given, a new child component will be initialized of that type. See Base.type.

  • attributes (#to_h) (defaults to: {})

    When component_or_type is a Symbol, then these attributes will be used to initialize the child component. When component_or_type is a Base, then these attributes will be used to update the child component.

Options Hash (attributes):

  • :object (Object)

    An arbitrary object to associate with the added child in this listbox.

Returns:



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.

Parameters:

  • index (#to_i)

Returns:



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.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • index (#to_i)

Returns:

  • (Object)


138
139
140
# File 'lib/cura/component/listbox.rb', line 138

def object_at(index)
  @objects[index]
end

#selected_childComponent

Get the child at the selected index.

Returns:



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.

Parameters:

Returns:



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_objectComponent

Get the object associated with the child at the selected index.

Returns:



145
146
147
# File 'lib/cura/component/listbox.rb', line 145

def selected_object
  @objects[@selected_index]
end