Class: LCDProc::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/lcdproc/menu.rb

Direct Known Subclasses

LCDProc::MenuItems::SubMenu

Constant Summary collapse

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ Menu

Create a new Menu object optionally attached to a client. Note that if you would like to create a sub menu which can be attached to a menu, you should use LCDProc::MenuItems::Submenu.

Note that when a client is created, this function is called and and it becomes the default client’s menu

  • client - The client to which you wish this menu to be attached. Defaults to nil



52
53
54
55
56
57
# File 'lib/lcdproc/menu.rb', line 52

def initialize( client = nil )
  @id = "".quotify
  @parent = nil
  @client = client
  @stored_items = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



41
42
43
# File 'lib/lcdproc/menu.rb', line 41

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



41
42
43
# File 'lib/lcdproc/menu.rb', line 41

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



41
42
43
# File 'lib/lcdproc/menu.rb', line 41

def items
  @items
end

#parentObject

Returns the value of attribute parent.



42
43
44
# File 'lib/lcdproc/menu.rb', line 42

def parent
  @parent
end

Instance Method Details

#add_item(menu_item, &block) ⇒ Object

Adds the specified menu item to this menu. Returns the menu item that was added.

  • menu_item - The menu item that you wish to add

  • &block - You may optionally pass a block to this menthod to register a menu event with the client

Example:

c = Client.new
a = c.menu.add_item( MenuItem.new( :action, :id => 'AnAction' ) ) { |response| puts "AnAction was pressed!!!" }

# a now contains the action menu item


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lcdproc/menu.rb', line 71

def add_item( menu_item, &block )
  
  # This slight hack allows for sub menus to correctly know to whom they belong
  if menu_item.respond_to? :parent=
    menu_item.send( :parent=, self )
  end
  
  @stored_items << MenuItemStore.new( menu_item, MenuEvent.new( menu_item, &block ), false )
  
  update
  
  return menu_item
end

#attach_to(client = nil, should_update = true) ⇒ Object

Attaches this menu to a particular client. Note that this will detach this menu from any existing clients.

  • client - The client to which you wish to be attached. Defaults to nil.

  • should_update - Whether or not we should go ahead and display all of our items on the screen. Defaults to true.

Example:

c1 = Client.new
c2 = Client.new
c2.detach

c1.menu.attach_to( c2 )


98
99
100
101
102
103
104
105
106
107
# File 'lib/lcdproc/menu.rb', line 98

def attach_to( client = nil, should_update = true )
  
  self.detach
  
  @client = client
  
  if should_update
    self.update
  end
end

#detachObject

Detaches this menu from the current client. Note that if this fails, it still detaches the client, so you might have extraneous menu items lying around (but you shouldn’t so long as you don’t bypass the add_item method somehow…)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lcdproc/menu.rb', line 113

def detach
  errors = []
  
  @stored_items.select{ |si| si.added_to_screen == true }.each do |visible_stored_item|
    menu_item = visible_stored_item.menu_item
    
    response = client.send_command( Command.new( "menu_del_item #{@id} #{menu_item.id.quotify}" ) )
    
    if not response.successful?
      errors << true
    end
  end
  
  # Make sure that we don't still think that any are visible on the screen
  @stored_items.each { |si| si.added_to_screen = false }
  
  @client = nil
  
  return errors.length == 0
end

#updateObject

Adds any existing, but not currently added, menu items to the screen (including sub menus and their items). This may be called to ensure that all items are displayed in the menu and is called whenever a menu item is added to a menu. This technically allows you to create a menu object that is not attached to a client, later attach it to a client and then call the update method to display the menu and it’s items on screen.

Example

c = Client.new
c.menu.update


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/lcdproc/menu.rb', line 145

def update
  
  if @client
    client = @client
  else
    parent_menu = @parent
    
    while not parent_menu.nil? and parent_menu.client.nil?
      parent_menu = parent_menu.parent
    end
    
    client = parent_menu.client unless parent_menu.nil? or parent_menu.client.nil?
  end
  
  if client
    
    @stored_items.select{ |mis| mis.added_to_screen == false }.each do |stored_item|
      menu_item = stored_item.menu_item
      
      response = client.send_command( Command.new( "menu_add_item #{@id} #{menu_item.id.quotify} #{menu_item.lcdproc_type} #{menu_item.lcdproc_options_as_string}" ) )
      
      if not response.successful?
        return nil
      else
        stored_item.added_to_screen = true
      end
      
      if not stored_item.menu_event.nil?
        client.register_menu_event( stored_item.menu_event )
      end
      
      if menu_item.respond_to? :update
        menu_item.send( :update )
      end
      
    end
    
  end
  
end