Class: MenuMotion::Menu

Inherits:
NSMenu
  • Object
show all
Defined in:
lib/menu-motion/menu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, root_menu = nil) ⇒ Menu

Returns a new instance of Menu.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/menu-motion/menu.rb', line 52

def initialize(params = {}, root_menu = nil)
  if params[:title]
    initWithTitle(params[:title])
  else
    super()
  end

  self.root_menu = root_menu
  self.build_menu_from_params(self, params)

  self
end

Instance Attribute Details

Returns the value of attribute menu_items.



5
6
7
# File 'lib/menu-motion/menu.rb', line 5

def menu_items
  @menu_items
end

#root_menuObject

Returns the value of attribute root_menu.



5
6
7
# File 'lib/menu-motion/menu.rb', line 5

def root_menu
  @root_menu
end

Instance Method Details

#add_rows_to_menu(menu, rows) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/menu-motion/menu.rb', line 7

def add_rows_to_menu(menu, rows)
  rows.each do |row|
    row[:root_menu] = WeakRef.new(self.root_menu || self)
    menu_item = MenuMotion::MenuItem.new(row)
    menu_item.target = self
    menu_item.action = "perform_action:"

    if tag = row[:tag]
      tag = tag.to_sym
      menu_item.tag = tag

      if self.root_menu
        self.root_menu.menu_items ||= {}
        self.root_menu.menu_items[tag] = WeakRef.new(menu_item)
      else
        self.menu_items ||= {}
        self.menu_items[tag] = WeakRef.new(menu_item)
      end
    end

    menu.addItem(menu_item)
  end
end

#add_sections_to_menu(menu, sections) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/menu-motion/menu.rb', line 31

def add_sections_to_menu(menu, sections)
  sections.each_with_index do |section, index|
    if section[:rows]
      if index > 0
        # Add the separator before the new section,
        # skipping the first section
        menu.addItem(NSMenuItem.separatorItem)
      end
      add_rows_to_menu(menu, section[:rows])
    end
  end
end

#build_menu_from_params(menu, params) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/menu-motion/menu.rb', line 44

def build_menu_from_params(menu, params)
  if params[:sections]
    add_sections_to_menu(menu, params[:sections])
  elsif params[:rows]
    add_rows_to_menu(menu, params[:rows])
  end
end

#item_with_tag(tag) ⇒ Object



65
66
67
68
# File 'lib/menu-motion/menu.rb', line 65

def item_with_tag(tag)
  @menu_items ||= {}
  @menu_items[tag.to_sym]
end

#perform_action(menu_item) ⇒ Object



70
71
72
# File 'lib/menu-motion/menu.rb', line 70

def perform_action(menu_item)
  menu_item.perform_action
end

#update_item_with_tag(tag, params) ⇒ Object



74
75
76
77
# File 'lib/menu-motion/menu.rb', line 74

def update_item_with_tag(tag, params)
  menu_item = self.item_with_tag(tag)
  menu_item.update(params)
end

#validateMenuItem(menu_item) ⇒ Object



79
80
81
# File 'lib/menu-motion/menu.rb', line 79

def validateMenuItem(menu_item)
  menu_item.valid?
end