Class: MotionKit::MenuLayout

Inherits:
TreeLayout show all
Defined in:
lib/motion-kit-osx/helpers/nsmenu_helpers.rb

Direct Known Subclasses

NSMenuHelpers

Instance Attribute Summary

Attributes inherited from BaseLayout

#parent

Instance Method Summary collapse

Methods inherited from TreeLayout

#all, #all_views, #always, #build, #built?, #child_layouts, #create_default_root_context, #first, #forget, #forget_tree, #forget_view, #get, #get_view, #initial, #initial?, #initialize, #last, #last_view, #name_element, #nearest, #next, #nth, #nth_view, #prev, #reapply, #reapply!, #reapply?, #reapply_blocks, #remove, #remove_view, #run_reapply_blocks, view, #view

Methods inherited from BaseLayout

#add_deferred_block, #apply, #apply_with_context, #apply_with_target, #context, #deferred, #deferred_blocks, delegate_method_fix, #has_context?, #initialize, #ipad?, #iphone35?, #iphone47?, #iphone4?, #iphone55?, #iphone?, #is_parent_layout?, #method_missing, #objc_version, #orientation?, #orientation_block, #parent_layout, #retina?, #ruby_version, #run_deferred, #set_parent_layout, #target, #tv?, #v

Methods included from BaseLayoutClassMethods

#layout_for, #memoize, #target_klasses, #targets

Constructor Details

This class inherits a constructor from MotionKit::TreeLayout

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MotionKit::BaseLayout

Instance Method Details

#add(title_or_item, element_id = nil, options = {}, &block) ⇒ Object

override ‘add’; menus are just a horse of a different color.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 36

def add(title_or_item, element_id=nil, options={}, &block)
  if element_id.is_a?(NSDictionary)
    options = element_id
    element_id = nil
  end

  if title_or_item.is_a?(NSMenuItem)
    item = title_or_item
    menu = nil
    retval = item
  elsif title_or_item.is_a?(NSMenu)
    menu = title_or_item
    item = self.item(menu.title, options)
    item.submenu = menu
    retval = menu
  else
    title = title_or_item
    item = self.item(title, options)

    if block
      menu = create(title)
      item.submenu = menu
      retval = menu
    else
      retval = item
    end
  end

  self.apply(:add_child, item)

  if menu && block
    menuitem_was = @menu_item
    @menu_item = item
    context(menu, &block)
    @menu_item = menuitem_was
  end

  if element_id
    create(retval, element_id)
  end

  return retval
end

#add_child(submenu, options = {}) ⇒ Object



19
20
21
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 19

def add_child(submenu, options={})
  target.addItem(submenu)
end

#create(title_or_item, element_id = nil, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 80

def create(title_or_item, element_id=nil, &block)
  if title_or_item.is_a?(NSString)
    item = NSMenu.alloc.initWithTitle(title_or_item)
  else
    item = title_or_item
  end

  return super(item, element_id, &block)
end

#default_rootObject

platform specific default root view



13
14
15
16
17
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 13

def default_root
  # child WindowLayout classes can return *their* NSView subclass from self.nsview_class
  menu_class = self.class.targets || NSMenu
  menu_class.alloc.init
end

#item(title, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 90

def item(title, options={})
  action = options.fetch(:action, nil)
  key = options.fetch(:keyEquivalent, options.fetch(:key, ''))
  key ||= ''  # key must never be nil
  item = NSMenuItem.alloc.initWithTitle(title, action: action, keyEquivalent: key)

  mask = options.fetch(:keyEquivalentModifierMask, options.fetch(:mask, nil))
  unless mask.nil?
    item.keyEquivalentModifierMask = mask
  end

  return item
end

A more sensible name for the menu that is created.



8
9
10
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 8

def menu
  self.view
end

#remove_child(submenu) ⇒ Object



23
24
25
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 23

def remove_child(submenu)
  target.removeItem(submenu)
end

#root(element, element_id = nil, &block) ⇒ Object

override root to allow a menu title for the top level menu



28
29
30
31
32
33
# File 'lib/motion-kit-osx/helpers/nsmenu_helpers.rb', line 28

def root(element, element_id=nil, &block)
  if element && element.is_a?(NSString)
    element = NSMenu.alloc.initWithTitle(element)
  end
  super(element, element_id, &block)
end