Module: LCDProc::Widget

Overview

NOTE: You may only have one (1) type of the following per screen: hbar, vbar or num. They currently conflict with each other and will not draw properly. You MAY have as many of each type per screen as you like. For instance, you MAY have twenty (20) vertical bars per screen or four (4) horizontal bars on a screen, but you cannot mix and match!

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



35
36
37
# File 'lib/lcdproc/widget.rb', line 35

def id
  @id
end

#screenObject (readonly)

Returns the value of attribute screen.



35
36
37
# File 'lib/lcdproc/widget.rb', line 35

def screen
  @screen
end

#typeObject (readonly)

Returns the value of attribute type.



35
36
37
# File 'lib/lcdproc/widget.rb', line 35

def type
  @type
end

#visibleObject (readonly)

Returns the value of attribute visible.



35
36
37
# File 'lib/lcdproc/widget.rb', line 35

def visible
  @visible
end

Class Method Details

.add_support(type, widget_class) ⇒ Object

Allows a particular type of widget to inform the Widget module (which is a Widget factory) that it is able to support a particular type of widget.



62
63
64
# File 'lib/lcdproc/widget.rb', line 62

def Widget.add_support( type, widget_class )
  @supported_types[type] = widget_class
end

.new(type = :string, id = nil) ⇒ Object

Finds the first widget that is of type type and returns a new instance of it.

  • type - A symbol representing the type of widget you wish to create.

  • id - A unique string by which you can indentify the widget. Defaults to the widget type +

“Widget_” + a sequence number

Example:

w = Widget.new( :string, 'Test' )


47
48
49
50
51
52
53
# File 'lib/lcdproc/widget.rb', line 47

def Widget.new( type = :string, id = nil  )
  if @supported_types.include? type
    @supported_types[ type ].new( id )
  else
    return nil
  end
end

.supported_typesObject

Returns an array of symbols listing the supported types of widgets.



56
57
58
# File 'lib/lcdproc/widget.rb', line 56

def Widget.supported_types
  @supported_types.keys
end

Instance Method Details

#attach_to(screen) ⇒ Object

Allows a widget to be attached to a screen.



67
68
69
70
71
# File 'lib/lcdproc/widget.rb', line 67

def attach_to( screen )
  @screen = screen
  
  return true
end

#detachObject

Allows a widget to be dettached from a screen.



74
75
76
# File 'lib/lcdproc/widget.rb', line 74

def detach
  attach_to nil
end

#hide(send_command = true) ⇒ Object

Sends the command to the LCDd server to remove the widget from the screen unless you pass false, in which case it does everything but send the command



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lcdproc/widget.rb', line 81

def hide( send_command = true )
  
  if not send_command
    @visible = false
    @screen.client.add_message( "Widget '#{@id}' is now hidden" )
    return true
  end
  
  # Only if we are visible, attempt to hide ourselves
  if @visible
    response = @screen.client.send_command( Command.new( "widget_del #{@screen.id} #{self.id}" ) )
    
    if response.successful?
      @visible = false
      @screen.client.add_message( "Widget '#{@id}' is now hidden" )
      return true
    else
      @visible = true
      @screen.client.add_message( "Error: Widget '#{@id}' could NOT be hidden (#{response.message})" )
      return false
    end
  else
    return true
  end
  
end

#show(send_command = true) ⇒ Object

Sends the command to the LCDd server to show the widget on the screen unless you pass false, in which case it does everything but send the command



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
# File 'lib/lcdproc/widget.rb', line 110

def show( send_command = true )
  
  if not send_command
    @visible = true
    @screen.client.add_message( "Widget '#{@id}' is now visible" )
    return true
  end
  
  # If we aren't already visible, attempt to make oursevles visible
  if not @visible
    response = @screen.client.send_command( Command.new( "widget_add #{@screen.id} #{self.id} #{@type.to_s}" ) )
    
    if response.successful?
      @visible = true
      @screen.client.add_message( "Widget '#{@id}' is now visible" )
      
      self.update
      
      return true
    else
      @visible = false
      @screen.client.add_message( "Error: Widget '#{@id}' could NOT be displayed (#{response.message})" )
      return false
    end
  else
    return true
  end
end