Class: RETerm::Layout
- Includes:
- NavInput
- Defined in:
- lib/reterm/layout.rb
Overview
Provides mechanism to orgranize on screen components according to rules defined by subclasses.
Layouts themselves are specialized types of components as they are intended to be associated with windows in which to be rendered
Direct Known Subclasses
RETerm::Layouts::Grid, RETerm::Layouts::Horizontal, RETerm::Layouts::Vertical
Constant Summary
Constants included from NavControls
NavControls::DOWN_CONTROLS, NavControls::ENTER_CONTROLS, NavControls::LEFT_CONTROLS, NavControls::MOVEMENT_CONTROLS, NavControls::QUIT_CONTROLS, NavControls::RIGHT_CONTROLS, NavControls::UP_CONTROLS
Constants included from MouseInput
MouseInput::ALL_EVENTS, MouseInput::MOUSE_MAP
Constants included from LogHelpers
Instance Attribute Summary collapse
-
#expand(h) ⇒ Object
Flag indicating if layout should expand to be able to contain childen (if false error will be throw if trying to add child to layout that cannot contain it).
Attributes included from NavInput
Attributes inherited from Component
#activatable, #activate_focus, #highlight_focus, #window
Instance Method Summary collapse
-
#activatable? ⇒ Boolean
Return boolean indicating if any child component is activatable.
-
#activate!(*input) ⇒ Object
Activates layout by dispatching to navigation input system.
-
#add_child(h = {}) ⇒ Object
Create new child window and add it to layout.
- #child_windows ⇒ Object
- #children ⇒ Object
-
#contains?(child) ⇒ Boolean
Return boolean indicating if layout contains specified child.
-
#current_cols ⇒ Object
Subclasses should override this method returning current cols in layout.
-
#current_rows ⇒ Object
Subclasses should override this method returning current rows in layout.
-
#draw! ⇒ Object
Draw all layout children.
- #empty? ⇒ Boolean
-
#exceeds_bounds? ⇒ Boolean
Returns boolean indicating if current layout exceeds bounds of window.
-
#exceeds_bounds_with?(child) ⇒ Boolean
Subclasses should overrid this method returning boolean indicating if boundries will be exceeded when child is added.
- #expandable? ⇒ Boolean
- #highlight_focus? ⇒ Boolean
-
#layout_containing(component) ⇒ Object
Return layout containing component If coordinates are contained in a child in current layout.
- #parent ⇒ Object
- #parent? ⇒ Boolean
Methods included from NavInput
#focusable, #focusable?, #handle_input, #valid_input?
Methods included from NavControls
Methods included from MouseInput
#mouse_paste?, #on_button, #process_mouse
Methods inherited from Component
#activate_focus?, #cdk?, #colored?, #colors=, #deactivate!, #deactivate?, #distance_from, #extra_padding, #finalize!, #initialize, #reactivate!, #requested_cols, #requested_rows, #resize, #sync!, #sync_getch
Methods included from KeyBindings
#bind_key, #invoke_key_bindings, #key_bindings, #key_bound?
Methods included from LogHelpers
Methods included from EventDispatcher
Constructor Details
This class inherits a constructor from RETerm::Component
Instance Attribute Details
#expand(h) ⇒ Object
Flag indicating if layout should expand to be able to contain childen (if false error will be throw if trying to add child to layout that cannot contain it)
14 15 16 |
# File 'lib/reterm/layout.rb', line 14 def @expand end |
Instance Method Details
#activatable? ⇒ Boolean
Return boolean indicating if any child component is activatable
97 98 99 100 |
# File 'lib/reterm/layout.rb', line 97 def activatable? return false if empty? children.any? { |c| c.activatable? } end |
#activate!(*input) ⇒ Object
Activates layout by dispatching to navigation input system.
190 191 192 193 194 195 196 197 |
# File 'lib/reterm/layout.rb', line 190 def activate!(*input) reactivate! draw! update_reterm handle_input(*input) deactivate! end |
#add_child(h = {}) ⇒ Object
Create new child window and add it to layout
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/reterm/layout.rb', line 107 def add_child(h={}) c = nil if h.key?(:component) c = h[:component] h = {:rows => c.requested_rows + c.extra_padding, :cols => c.requested_cols + c.extra_padding}.merge(h) end raise ArgumentError, "must specify x/y" unless h.key?(:x) && h.key?(:y) raise ArgumentError, "must specify rows/cols" unless h.key?(:rows) && h.key?(:cols) h[:rows], h[:cols] = *Window.adjust_proportional(window, h[:rows], h[:cols]) h[:x], h[:y] = *Window.align(window, h[:x], h[:y], h[:rows], h[:cols]) h[:rows], h[:cols] = *Window.fill_parent(parent? ? parent.window : Terminal, h[:x], h[:y], h[:rows], h[:cols]) if h[:fill] if exceeds_bounds_with?(h) if # ... && can_expand_to?(h) (h) else raise ArgumentError, "child exceeds bounds" end end child = window.create_child(h) # TODO need to reverse expansion if operation fails at any # point on, or verify expandable before create_child but # do not expand until after if child.win.nil? raise ArgumentError, "could not create child window" end if exceeds_bounds? window.del_child(child) unless child.win.nil? raise ArgumentError, "child exceeds bounds" end child.component = c unless c.nil? update_reterm child end |
#child_windows ⇒ Object
30 31 32 |
# File 'lib/reterm/layout.rb', line 30 def child_windows window.children end |
#children ⇒ Object
21 22 23 |
# File 'lib/reterm/layout.rb', line 21 def children window.children.collect { |w| w.component } end |
#contains?(child) ⇒ Boolean
Return boolean indicating if layout contains specified child
89 90 91 92 93 |
# File 'lib/reterm/layout.rb', line 89 def contains?(child) children.any? { |c| (c.kind_of?(Layout) && c.contains?(child)) || c == child } end |
#current_cols ⇒ Object
Subclasses should override this method returning current cols in layout
52 53 54 |
# File 'lib/reterm/layout.rb', line 52 def current_cols raise "NotImplemented" end |
#current_rows ⇒ Object
Subclasses should override this method returning current rows in layout
46 47 48 |
# File 'lib/reterm/layout.rb', line 46 def current_rows raise "NotImplemented" end |
#draw! ⇒ Object
Draw all layout children
181 182 183 184 |
# File 'lib/reterm/layout.rb', line 181 def draw! children.each { |c| c.draw! } draw_focus! end |
#empty? ⇒ Boolean
25 26 27 28 |
# File 'lib/reterm/layout.rb', line 25 def empty? return true if window.nil? window.children.empty? end |
#exceeds_bounds? ⇒ Boolean
Returns boolean indicating if current layout exceeds bounds of window
65 66 67 68 |
# File 'lib/reterm/layout.rb', line 65 def exceeds_bounds? current_cols > window.cols || current_rows > window.rows end |
#exceeds_bounds_with?(child) ⇒ Boolean
Subclasses should overrid this method returning boolean indicating if boundries will be exceeded when child is added
59 60 61 |
# File 'lib/reterm/layout.rb', line 59 def exceeds_bounds_with?(child) raise "NotImplemented" end |
#expandable? ⇒ Boolean
16 17 18 19 |
# File 'lib/reterm/layout.rb', line 16 def (!defined?(@expand) || !!@expand) && (!parent? || parent.) end |
#highlight_focus? ⇒ Boolean
102 103 104 |
# File 'lib/reterm/layout.rb', line 102 def highlight_focus? false end |
#layout_containing(component) ⇒ Object
Return layout containing component If coordinates are contained in a child in current layout
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/reterm/layout.rb', line 73 def layout_containing(component) return self if children.include?(component) found = nil children.each { |c| next if found if c.kind_of?(Layout) found = c unless c.layout_containing(component).nil? end } found end |
#parent ⇒ Object
38 39 40 |
# File 'lib/reterm/layout.rb', line 38 def parent window.parent.component end |
#parent? ⇒ Boolean
34 35 36 |
# File 'lib/reterm/layout.rb', line 34 def parent? window.parent? end |