Class: Potty::View
- Inherits:
-
Object
- Object
- Potty::View
- Defined in:
- lib/potty/view.rb
Overview
Base class for views
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#widgets ⇒ Object
readonly
Returns the value of attribute widgets.
Instance Method Summary collapse
- #activate(app) ⇒ Object
-
#build_layout ⇒ Object
Override to build widget tree.
- #deactivate ⇒ Object
- #handle_escape ⇒ Object
- #handle_key(ch) ⇒ Object
-
#initialize(app) ⇒ View
constructor
A new instance of View.
- #layout_widgets ⇒ Object
- #on_activate ⇒ Object
- #on_deactivate ⇒ Object
-
#render ⇒ Object
Draw the widget tree onto the application’s surface.
-
#tick(now) ⇒ Object
Fan a time tick out to top-level widgets.
Constructor Details
#initialize(app) ⇒ View
Returns a new instance of View.
11 12 13 14 15 16 |
# File 'lib/potty/view.rb', line 11 def initialize(app) @app = app @widgets = [] @focused_index = 0 build_layout end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/potty/view.rb', line 9 def app @app end |
#widgets ⇒ Object (readonly)
Returns the value of attribute widgets.
9 10 11 |
# File 'lib/potty/view.rb', line 9 def @widgets end |
Instance Method Details
#activate(app) ⇒ Object
18 19 20 21 22 |
# File 'lib/potty/view.rb', line 18 def activate(app) @app = app on_activate end |
#build_layout ⇒ Object
Override to build widget tree
32 33 34 |
# File 'lib/potty/view.rb', line 32 def build_layout # Override in subclasses end |
#deactivate ⇒ Object
24 25 26 |
# File 'lib/potty/view.rb', line 24 def deactivate on_deactivate end |
#handle_escape ⇒ Object
79 80 81 |
# File 'lib/potty/view.rb', line 79 def handle_escape false # Return true if handled end |
#handle_key(ch) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/potty/view.rb', line 62 def handle_key(ch) # Delegate to focused widget first return true if &.handle_key(ch) # Handle view-level keys case ch when Keys::TAB cycle_focus(1) true when Keys::SHIFT_TAB cycle_focus(-1) true else false end end |
#layout_widgets ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/potty/view.rb', line 36 def rows, cols = @app.surface.size container = Layout::Rect.new(0, 0, cols, rows) # Simple stack layout with spacing rects = Layout.stack(container, @widgets, spacing: 1) @widgets.zip(rects).each do |, rect| .layout(rect) end end |
#on_activate ⇒ Object
28 |
# File 'lib/potty/view.rb', line 28 def on_activate; end |
#on_deactivate ⇒ Object
29 |
# File 'lib/potty/view.rb', line 29 def on_deactivate; end |
#render ⇒ Object
Draw the widget tree onto the application’s surface. The surface frame (erase/present) is owned by Application#refresh_all, so this just paints.
49 50 51 52 53 54 |
# File 'lib/potty/view.rb', line 49 def render surface = @app.surface @widgets.each do || .render(surface) end end |
#tick(now) ⇒ Object
Fan a time tick out to top-level widgets. Driven by Application#tick when a tick_interval is set. ‘now` is one Time read per frame.
58 59 60 |
# File 'lib/potty/view.rb', line 58 def tick(now) @widgets.each { || .tick(now) } end |