Class: DrinkMenu::Menu

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/drink-menu/menu.rb

Constant Summary

Constants included from Forwardable

Forwardable::FORWARDABLE_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Forwardable

def_instance_delegator, def_instance_delegators, instance_delegate

Constructor Details

#initialize(label, &block) ⇒ Menu

Returns a new instance of Menu.



70
71
72
73
74
75
# File 'lib/drink-menu/menu.rb', line 70

def initialize(label, &block)
  @menuItems = {}
  @label = label
  @builder = block
  @menu = NSMenu.alloc.init
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



18
19
20
# File 'lib/drink-menu/menu.rb', line 18

def builder
  @builder
end

#itemAddedSubjectObject

Returns the value of attribute itemAddedSubject.



19
20
21
# File 'lib/drink-menu/menu.rb', line 19

def itemAddedSubject
  @itemAddedSubject
end

#labelObject (readonly)

Returns the value of attribute label.



18
19
20
# File 'lib/drink-menu/menu.rb', line 18

def label
  @label
end

#memberCommandObject

Returns the value of attribute memberCommand.



19
20
21
# File 'lib/drink-menu/menu.rb', line 19

def memberCommand
  @memberCommand
end

#memberTitlePropertyObject

Returns the value of attribute memberTitleProperty.



19
20
21
# File 'lib/drink-menu/menu.rb', line 19

def memberTitleProperty
  @memberTitleProperty
end

Returns the value of attribute menu.



18
19
20
# File 'lib/drink-menu/menu.rb', line 18

def menu
  @menu
end

Returns the value of attribute menuItems.



18
19
20
# File 'lib/drink-menu/menu.rb', line 18

def menuItems
  @menuItems
end

#statusItemObject

Returns the value of attribute statusItem.



19
20
21
# File 'lib/drink-menu/menu.rb', line 19

def statusItem
  @statusItem
end

#statusItemIconObject

Returns the value of attribute statusItemIcon.



19
20
21
# File 'lib/drink-menu/menu.rb', line 19

def statusItemIcon
  @statusItemIcon
end

#statusItemTitleObject

Returns the value of attribute statusItemTitle.



19
20
21
# File 'lib/drink-menu/menu.rb', line 19

def statusItemTitle
  @statusItemTitle
end

Class Method Details



39
40
41
42
43
# File 'lib/drink-menu/menu.rb', line 39

def self.menuWithLabel(label, title: title, &block)
  new(label, &block).tap do |menu|
    menu.title = title
  end
end

.statusMenuWithLabel(label, title: title, &block) ⇒ Object



21
22
23
24
25
# File 'lib/drink-menu/menu.rb', line 21

def self.statusMenuWithLabel(label, icon: image, statusItemViewClass: statusItemViewClass, &block)
  new(label, &block).tap do |menu|
    menu.createStatusItemWithIcon image, viewClass: statusItemViewClass
  end
end

Instance Method Details

#<<(item) ⇒ Object



77
78
79
80
81
82
# File 'lib/drink-menu/menu.rb', line 77

def <<(item)
  previousItemTag = @menuItems.keys.last || 0
  item.tag = previousItemTag + 1
  @menuItems[item.tag] = item
  @menu.addItem item.menuItem
end

#[](labelOrTag) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/drink-menu/menu.rb', line 140

def [](labelOrTag)
  if labelOrTag.is_a? Fixnum
    @menuItems[labelOrTag]
  else
    @menuItems.values.find do |item|
      item.label == labelOrTag
    end
  end
end

#addMenuItemForMember(member) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/drink-menu/menu.rb', line 84

def addMenuItemForMember(member)
  item = MenuItem.itemWithLabel(member.hash.to_s, title: member.send(memberTitleProperty))
  item.representedObject = member
  item.rac_stateSignal = memberCommand.signal.map ->((_,value)){
    value.representedObject == item.representedObject
  }

  self << item
  itemAddedSubject.sendNext(item)

end

#createStatusItem!Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/drink-menu/menu.rb', line 154

def createStatusItem!
  statusBar = NSStatusBar.systemStatusBar
  @statusItem = statusBar.statusItemWithLength(NSSquareStatusItemLength)
  @statusItem.highlightMode = true

  @statusItem.menu = menu

  if @statusItemViewClass
    statusItemView = @statusItemViewClass.viewWithStatusItem(@statusItem)
    @statusItem.menu.delegate = @statusItemView
    @statusItem.view = statusItemView
  end

  @statusItem.title = @statusItemTitle
  @statusItem.image = @statusItemIcon

  @statusItem
end

#createStatusItemWithIcon(image, viewClass: viewClass) ⇒ Object



117
118
119
120
# File 'lib/drink-menu/menu.rb', line 117

def createStatusItemWithIcon(image)
  @needsStatusItem = true
  @statusItemIcon = image
end

#createStatusItemWithTitle(title) ⇒ Object



112
113
114
115
# File 'lib/drink-menu/menu.rb', line 112

def createStatusItemWithTitle(title)
  @needsStatusItem = true
  @statusItemTitle = title
end

#needsStatusItem?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/drink-menu/menu.rb', line 150

def needsStatusItem?
  @needsStatusItem
end

#observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/drink-menu/menu.rb', line 96

def observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
  return unless keyPath == "arrangedObjects"
  if itemArray.length < object.arrangedObjects.length
    member = object.arrangedObjects.lastObject
    addMenuItemForMember(member)
  end
end

#selectItem(label) ⇒ Object



128
129
130
131
# File 'lib/drink-menu/menu.rb', line 128

def selectItem(label)
  item = self[label]
  item.command.execute(item)
end

#selectItemByMember(member) ⇒ Object



133
134
135
136
137
138
# File 'lib/drink-menu/menu.rb', line 133

def selectItemByMember(member)
  item = @menuItems.values.find do |i|
    i.representedObject == member
  end
  item.command.execute(item)
end

#subscribe(itemLabel, &block) ⇒ Object



108
109
110
# File 'lib/drink-menu/menu.rb', line 108

def subscribe(itemLabel, &block)
  self[itemLabel].subscribe(&block)
end

#subscribeToMembers(&block) ⇒ Object



104
105
106
# File 'lib/drink-menu/menu.rb', line 104

def subscribeToMembers(&block)
  memberCommand.signal.subscribeNext(block)
end