Class: MotionKit::NSMenuHelpers

Inherits:
MenuLayout show all
Defined in:
lib/motion-kit-osx/helpers/nsmenu_helpers.rb,
lib/motion-kit-osx/helpers/nsmenu_extensions.rb,
lib/motion-kit-osx/helpers/nsmenuitem_extensions.rb

Direct Known Subclasses

NSMenuLayout

Instance Attribute Summary

Attributes inherited from BaseLayout

#parent

Instance Method Summary collapse

Methods inherited from MenuLayout

#add, #add_child, #create, #default_root, #item, #menu, #remove_child, #root

Methods inherited from TreeLayout

#add, #all, #all_views, #always, #build, #built?, #child_layouts, #create, #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, #root, #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

#_menu_title_and_options(title, options, default_title = nil, default_options = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 12

def _menu_title_and_options(title, options, default_title=nil, default_options={})
  if title.is_a?(NSDictionary)
    options = title
    title = options[:title]
  end
  title ||= default_title
  return title, default_options.merge(options)
end

#about_item(title = nil, options = {}) ⇒ Object



92
93
94
95
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 92

def about_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, "About #{app_name}", { action: 'orderFrontStandardAboutPanel:' })
  return self.item(title, options)
end

#app_menu(title = nil, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 21

def app_menu(title=nil, options={})
  title, options = _menu_title_and_options(title, options, app_name)

  exclude = Array(options.fetch(:exclude, []))
  return create(title) do
    unless exclude.include?(:about)
      add about_item
      add separator_item
    end
    unless exclude.include?(:prefs)
      add preferences_item
      add separator_item
    end
    unless exclude.include?(:services)
      add services_item
      add separator_item
    end
    unless exclude.include?(:hide)
      add hide_item
      add hide_others_item
    end
    add show_all_item unless exclude.include?(:show)
    add quit_item unless exclude.include?(:quit)
  end
end

#app_nameObject

useful when writing menus



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

def app_name
  # this returns an ImmediateRef, whatever that is.  It needs to be
  # converted to a String.  Doesn't work in 10.10.
  "#{NSBundle.mainBundle.infoDictionary['CFBundleName']}"
end

#attributed_title(value) ⇒ Object



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

def attributed_title(value)
  target.title = value.to_s
  @menu_item.attributedTitle = value
end

#attributedTitle(value) ⇒ Object



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

def attributedTitle(value)
  target.title = value.to_s
  @menu_item.attributedTitle = value
end

#bring_all_to_front_item(title = nil, options = {}) ⇒ Object



177
178
179
180
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 177

def bring_all_to_front_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Bring All To Front', { action: 'arrangeInFront:' })
  return self.item(title, options)
end

#close_item(title = nil, options = {}) ⇒ Object



137
138
139
140
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 137

def close_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Close', { key: 'w', action: 'performClose:' })
  return self.item(title, options)
end

#file_menu(title = nil, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 47

def file_menu(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'File')

  exclude = Array(options.fetch(:exclude, []))
  return create(title) do
    add new_item unless exclude.include?(:new)
    add open_item unless exclude.include?(:open)
    add separator_item unless [:new, :open].all? { |menu_name| exclude.include?(menu_name) }

    add close_item unless exclude.include?(:close)
    add save_item unless exclude.include?(:save)
    add revert_to_save_item unless exclude.include?(:revert)
    add separator_item unless [:close, :save, :revert].all? { |menu_name| exclude.include?(menu_name) }

    add page_setup_item unless exclude.include?(:page_setup)
    add print_item unless exclude.include?(:print)
  end
end

#help_item(title = nil, options = {}) ⇒ Object



182
183
184
185
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 182

def help_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, "#{app_name} Help", { key: '?', action: 'showHelp:' })
  return self.item("#{app_name} Help", options)
end

#help_menu(title = nil, options = {}) ⇒ Object



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

def help_menu(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Help')

  exclude = Array(options.fetch(:exclude, []))
  return create(title) do
    add help_item
  end
end

#hide_item(title = nil, options = {}) ⇒ Object



107
108
109
110
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 107

def hide_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, "Hide #{app_name}", { key: 'h', action: 'hide:' })
  return self.item(title, options)
end

#hide_others_item(title = nil, options = {}) ⇒ Object



112
113
114
115
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 112

def hide_others_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Hide Others', { key: 'h', action: 'hideOtherApplications:', mask: NSCommandKeyMask | NSAlternateKeyMask })
  return self.item(title, options)
end

#key_equivalent_modifier_mask(value) ⇒ Object



40
41
42
# File 'lib/motion-kit-osx/helpers/nsmenuitem_extensions.rb', line 40

def key_equivalent_modifier_mask(value)
  @menu_item.keyEquivalentModifierMask = value
end

#keyEquivalentModifierMask(value) ⇒ Object



36
37
38
# File 'lib/motion-kit-osx/helpers/nsmenuitem_extensions.rb', line 36

def keyEquivalentModifierMask(value)
  @menu_item.keyEquivalentModifierMask = value
end

#minimize_item(title = nil, options = {}) ⇒ Object



167
168
169
170
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 167

def minimize_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Minimize', { key: 'm', action: 'performMiniaturize:' })
  return self.item(title, options)
end

#new_item(title = nil, options = {}) ⇒ Object



127
128
129
130
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 127

def new_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'New', { key: 'n', action: 'newDocument:' })
  return self.item(title, options)
end

#open_item(title = nil, options = {}) ⇒ Object



132
133
134
135
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 132

def open_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Open…', { key: 'o', action: 'openDocument:' })
  return self.item(title, options)
end

#page_setup_item(title = nil, options = {}) ⇒ Object



157
158
159
160
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 157

def page_setup_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Page Setup…', { key: 'P', action: 'runPageLayout:' })
  return self.item(title, options)
end

#preferences_item(title = nil, options = {}) ⇒ Object



97
98
99
100
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 97

def preferences_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Preferences', { key: ',', action: 'openPreferences:' })
  return self.item(title, options)
end


162
163
164
165
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 162

def print_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Print…', { key: 'p', action: 'printDocument:' })
  return self.item(title, options)
end

#quit_item(title = nil, options = {}) ⇒ Object



122
123
124
125
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 122

def quit_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, "Quit #{app_name}", { key: 'q', action: 'terminate:' })
  return self.item(title, options)
end

#revert_to_save_item(title = nil, options = {}) ⇒ Object



152
153
154
155
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 152

def revert_to_save_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Revert to Saved', { action: 'revertDocumentToSaved:' })
  return self.item(title, options)
end

#save_as_item(title = nil, options = {}) ⇒ Object



147
148
149
150
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 147

def save_as_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Save as…', { key: 'S', action: 'saveDocumentAs:' })
  return self.item(title, options)
end

#save_item(title = nil, options = {}) ⇒ Object



142
143
144
145
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 142

def save_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Save…', { key: 's', action: 'saveDocument:' })
  return self.item(title, options)
end

#separator_itemObject



88
89
90
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 88

def separator_item
  NSMenuItem.separatorItem
end

#services_item(title = nil, options = {}) ⇒ Object



102
103
104
105
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 102

def services_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Services')
  return self.item(title, options)
end

#show_all_item(title = nil, options = {}) ⇒ Object



117
118
119
120
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 117

def show_all_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Show All', { action: 'unhideAllApplications:' })
  return self.item(title, options)
end

#state(value) ⇒ Object



28
29
30
# File 'lib/motion-kit-osx/helpers/nsmenuitem_extensions.rb', line 28

def state(value)
  @menu_item.state = value
end

#tag(value) ⇒ Object



32
33
34
# File 'lib/motion-kit-osx/helpers/nsmenuitem_extensions.rb', line 32

def tag(value)
  @menu_item.tag = value
end

#title(value) ⇒ Object

sets the title of the current NSMenu AND it’s parent NSMenuItem



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

def title(value)
  target.title = value
  @menu_item.title = value
end

#window_menu(title = nil, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 66

def window_menu(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Window')

  exclude = Array(options.fetch(:exclude, []))
  return create(title) do
    add minimize_item unless exclude.include?(:minimize)
    add zoom_item unless exclude.include?(:zoom)
    add separator_item unless [:minimize, :zoom].all? { |menu_name| exclude.include?(menu_name) }

    add bring_all_to_front_item unless exclude.include?(:bring_all_to_front)
  end
end

#zoom_item(title = nil, options = {}) ⇒ Object



172
173
174
175
# File 'lib/motion-kit-osx/helpers/nsmenu_extensions.rb', line 172

def zoom_item(title=nil, options={})
  title, options = _menu_title_and_options(title, options, 'Zoom', { action: 'performMiniaturize:' })
  return self.item(title, options)
end