Class: Potty::Widgets::Base

Inherits:
Object
  • Object
show all
Includes:
Events
Defined in:
lib/potty/widgets/base.rb

Overview

Base class for all widgets

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Events

#emit, #listeners?, #off, #on

Constructor Details

#initialize(app) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
20
# File 'lib/potty/widgets/base.rb', line 14

def initialize(app)
  @app = app
  @rect = nil
  @parent = nil
  @focused = false
  @visible = true
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



12
13
14
# File 'lib/potty/widgets/base.rb', line 12

def app
  @app
end

#focusedObject

Returns the value of attribute focused.



11
12
13
# File 'lib/potty/widgets/base.rb', line 11

def focused
  @focused
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/potty/widgets/base.rb', line 11

def parent
  @parent
end

#rectObject

Returns the value of attribute rect.



11
12
13
# File 'lib/potty/widgets/base.rb', line 11

def rect
  @rect
end

Instance Method Details

#activateObject

Lifecycle



23
24
25
# File 'lib/potty/widgets/base.rb', line 23

def activate
  # Override in subclasses
end

#blurObject



80
81
82
83
84
# File 'lib/potty/widgets/base.rb', line 80

def blur
  @focused = false
  on_blur
  emit(:blur, self)
end

#can_focus?Boolean

Focus

Returns:

  • (Boolean)


70
71
72
# File 'lib/potty/widgets/base.rb', line 70

def can_focus?
  false  # Override in interactive widgets
end

#deactivateObject



27
28
29
# File 'lib/potty/widgets/base.rb', line 27

def deactivate
  # Override in subclasses
end

#focusObject



74
75
76
77
78
# File 'lib/potty/widgets/base.rb', line 74

def focus
  @focused = true
  on_focus
  emit(:focus, self)
end

#handle_escapeObject



65
66
67
# File 'lib/potty/widgets/base.rb', line 65

def handle_escape
  false  # Return true if handled
end

#handle_key(ch) ⇒ Object

Input



61
62
63
# File 'lib/potty/widgets/base.rb', line 61

def handle_key(ch)
  false  # Return true if handled
end

#hideObject



99
100
101
102
# File 'lib/potty/widgets/base.rb', line 99

def hide
  @visible = false
  self
end

#layout(rect) ⇒ Object



36
37
38
39
# File 'lib/potty/widgets/base.rb', line 36

def layout(rect)
  @rect = rect
  on_layout
end

#on_blurObject



87
# File 'lib/potty/widgets/base.rb', line 87

def on_blur; end

#on_focusObject



86
# File 'lib/potty/widgets/base.rb', line 86

def on_focus; end

#on_layoutObject



41
42
43
# File 'lib/potty/widgets/base.rb', line 41

def on_layout
  # Override for custom layout logic
end

#preferred_height(width) ⇒ Object

Layout



32
33
34
# File 'lib/potty/widgets/base.rb', line 32

def preferred_height(width)
  1  # Default to single line
end

#render(window) ⇒ Object

Rendering



46
47
48
49
# File 'lib/potty/widgets/base.rb', line 46

def render(window)
  return unless @visible && @rect
  # Override in subclasses
end

#showObject



94
95
96
97
# File 'lib/potty/widgets/base.rb', line 94

def show
  @visible = true
  self
end

#themeObject

Helpers



109
110
111
# File 'lib/potty/widgets/base.rb', line 109

def theme
  @app.theme
end

#tick(now) ⇒ Object

Time-based update hook. Called once per event-loop frame when the Application has a tick_interval set. ‘now` is a single Time read shared across all widgets in the frame (so playback stays in sync and is deterministic to unit-test). Override in time-driven widgets such as Animator and Countdown.



56
57
58
# File 'lib/potty/widgets/base.rb', line 56

def tick(now)
  # Override in time-driven subclasses
end

#visible=(flag) ⇒ Object



104
105
106
# File 'lib/potty/widgets/base.rb', line 104

def visible=(flag)
  flag ? show : hide
end

#visible?Boolean

Visibility

Returns:

  • (Boolean)


90
91
92
# File 'lib/potty/widgets/base.rb', line 90

def visible?
  @visible
end