Class: RETerm::Component

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher, KeyBindings, LogHelpers
Defined in:
lib/reterm/component.rb

Overview

A Component is a generic widget container associated with a window. Subclasses each implement a specific UI artifact.

Constant Summary

Constants included from LogHelpers

LogHelpers::LOG_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from KeyBindings

#bind_key, #invoke_key_bindings, #key_bindings, #key_bound?

Methods included from LogHelpers

#logger

Methods included from EventDispatcher

#dispatch, #handle

Constructor Details

#initialize(args = {}) ⇒ Component

Returns a new instance of Component.



21
22
23
24
25
26
# File 'lib/reterm/component.rb', line 21

def initialize(args={})
  self.highlight_focus = args[:highlight_focus] if args.key?(:highlight_focus)
  self.activate_focus  = args[:activate_focus]  if args.key?(:activate_focus)
  self.activatable     = args[:activatable]     if args.key?(:activatable)
  init_cdk(args) if cdk?
end

Instance Attribute Details

#activatable=(value) ⇒ Object (writeonly)

Sets the attribute activatable

Parameters:

  • value

    the value to set the attribute activatable to.



57
58
59
# File 'lib/reterm/component.rb', line 57

def activatable=(value)
  @activatable = value
end

#activate_focus=(value) ⇒ Object (writeonly)

Sets the attribute activate_focus

Parameters:

  • value

    the value to set the attribute activate_focus to.



101
102
103
# File 'lib/reterm/component.rb', line 101

def activate_focus=(value)
  @activate_focus = value
end

#highlight_focus=(value) ⇒ Object (writeonly)

Sets the attribute highlight_focus

Parameters:

  • value

    the value to set the attribute highlight_focus to.



93
94
95
# File 'lib/reterm/component.rb', line 93

def highlight_focus=(value)
  @highlight_focus = value
end

#windowObject

Returns the value of attribute window.



9
10
11
# File 'lib/reterm/component.rb', line 9

def window
  @window
end

Instance Method Details

#activatable?Boolean

Returns a boolean indicating if the user should be able to focus on and activate the component. By default this is false, but interactive components should override and return true.

Returns:

  • (Boolean)


63
64
65
# File 'lib/reterm/component.rb', line 63

def activatable?
  defined?(@activatable) && @activatable
end

#activate!(*input) ⇒ Object

Actual activation mechanism, invoked by the navigation logic when the user has selected an activatable? component. Should be overriden by interactive subcomponents to process user inpute specific to that component

Raises:

  • (RuntimeError)


71
72
73
# File 'lib/reterm/component.rb', line 71

def activate!(*input)
  raise RuntimeError, "should not be activated"
end

#activate_focus?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/reterm/component.rb', line 103

def activate_focus?
  defined?(@activate_focus) && @activate_focus
end

#cdk?Boolean

Overridden by CDK components

Returns:

  • (Boolean)


123
124
125
# File 'lib/reterm/component.rb', line 123

def cdk?
  false
end

#colored?Boolean

Return a boolean indicating if a RETerm::ColorPair has been assign to component

Returns:

  • (Boolean)


48
49
50
# File 'lib/reterm/component.rb', line 48

def colored?
  !!@colors
end

#colors=(c) ⇒ Object

Assign the specified RETerm::ColorPair to the component



53
54
55
# File 'lib/reterm/component.rb', line 53

def colors=(c)
  @colors = c
end

#deactivate!Object

This method is invoked when component loses focus (when navigating to another component, window closed, etc). Subclasses may override to hide / cleanup resources



78
79
80
# File 'lib/reterm/component.rb', line 78

def deactivate!
  @deactivate = true
end

#deactivate?Boolean

Flag indicating that this component should be deactivated

Returns:

  • (Boolean)


84
85
86
# File 'lib/reterm/component.rb', line 84

def deactivate?
  !!(@deactivate ||= false)
end

#distance_from(c) ⇒ Object



17
18
19
# File 'lib/reterm/component.rb', line 17

def distance_from(c)
  window.distance_from(c.kind_of?(Window) ? c : c.window)
end

#extra_paddingObject

Return extra padding to be given to component



108
109
110
# File 'lib/reterm/component.rb', line 108

def extra_padding
  (activatable? && highlight_focus?) ? 3 : 0
end

#finalize!Object

This method is invoked to cleanup the component on shutdown. It should be be overriden by subclasses that needs to clean resources before being deallocated



43
44
# File 'lib/reterm/component.rb', line 43

def finalize!
end

#highlight_focus?Boolean

Return boolean indicating if this component should be highlighted on focus (default true)

Returns:

  • (Boolean)


97
98
99
# File 'lib/reterm/component.rb', line 97

def highlight_focus?
  !defined?(@highlight_focus) || @highlight_focus
end

#reactivate!Object

Reset deactivation



89
90
91
# File 'lib/reterm/component.rb', line 89

def reactivate!
  @deactivate = false
end

#requested_colsObject

This method is invoked when adding component to layout to determine cols needed



36
37
38
# File 'lib/reterm/component.rb', line 36

def requested_cols
  1
end

#requested_rowsObject

This method is invoked when adding component to layout to determine rows needed



30
31
32
# File 'lib/reterm/component.rb', line 30

def requested_rows
  1
end

#resize(rows, cols) ⇒ Object

Dispatch to window.resize



117
118
119
120
# File 'lib/reterm/component.rb', line 117

def resize(rows, cols)
  window.resize(rows, cols)
  self
end

#sync!Object

Method to periodically synchronize component if needed



113
114
# File 'lib/reterm/component.rb', line 113

def sync!
end

#sync_getchObject



127
128
129
130
131
132
# File 'lib/reterm/component.rb', line 127

def sync_getch
  c = window.sync_getch
  c = nil if key_bound?(c) &&
             invoke_key_bindings(c)
  c
end