drink-menu 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa0caded737aae5b8e19f02d234a62bec5532129
4
- data.tar.gz: 3670d72c16d59197c56304cfe94992ab2218e71f
3
+ metadata.gz: 6a271cb84ff5bcafdc557d9413b0d4a12a7f5860
4
+ data.tar.gz: 341c7658bf3bf5699a169b83217da897f7ede78c
5
5
  SHA512:
6
- metadata.gz: 30a6f653df7169b157e7424c5be84b35c34c3f339a88a3361f057f3e3822092005c2df993a1e91fe6dca366b2b70bf2a83a975f83fc719f849750ed8b301b892
7
- data.tar.gz: f1d041f9171d4c9cd4ffe7671d60a82383c9d3b7d9fcf9868dd3fdf90eab3d7cc183adc37993931be0793b57bc91cdc2f496ec49c4483f455a33ae977d8710e3
6
+ metadata.gz: 579a9cfc385fddb960177447ffe87a968f01c0991c905163d0bc6edba0a4357c917759f223ae00fdb89e5f295c60eea99b80336a21731d5ac114bafce84e22b7
7
+ data.tar.gz: c6c7198e649ae76722ea91ec76825be29a44a0a853545516002718379896527eaafc7232136ff07bbeb8555dec9f726f233239d83ddbfeb40e9ff84490ec24f5
data/Rakefile CHANGED
@@ -17,6 +17,10 @@ Motion::Project::App.setup do |app|
17
17
  app.identifier = 'com.densitypop.drink-menu'
18
18
  app.specs_dir = "spec/"
19
19
 
20
+ if ENV['example']
21
+ app.files << Dir["examples/#{ENV['example']}/**/*.rb"]
22
+ end
23
+
20
24
  app.pods do
21
25
  pod 'ReactiveCocoa'
22
26
  end
@@ -0,0 +1,24 @@
1
+ class AppDelegate
2
+
3
+ def applicationDidFinishLaunching(notification)
4
+ MainMenu.build!
5
+
6
+ MainMenu[:app].subscribe :quit do |_|
7
+ NSApp.terminate(self)
8
+ end
9
+
10
+ MainMenu[:file].subscribe :new do |_|
11
+ puts "new"
12
+ end
13
+
14
+ MainMenu[:file].subscribe :close do |_|
15
+ puts "close"
16
+ end
17
+
18
+ MainMenu[:file].subscribe :open do |_|
19
+ puts "open"
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,32 @@
1
+ class MainMenu
2
+ extend DrinkMenu::MenuBuilder
3
+
4
+ menuItem :quit do |item|
5
+ item.title = 'Quit'
6
+ item.keyEquivalent = 'q'
7
+ end
8
+
9
+ menuItem :open do |item|
10
+ item.title = 'Open'
11
+ item.keyEquivalent = 'o'
12
+ end
13
+
14
+ menuItem :new, title: 'New'
15
+
16
+ menuItem :close do |item|
17
+ item.title = 'Close'
18
+ item.keyEquivalent = 'w'
19
+ end
20
+
21
+ mainMenu :app, title: 'Blah' do
22
+ quit
23
+ end
24
+
25
+ mainMenu :file, title: 'File' do
26
+ new
27
+ open
28
+ ___
29
+ close
30
+ end
31
+
32
+ end
@@ -36,6 +36,14 @@ module DrinkMenu
36
36
  end
37
37
  end
38
38
 
39
+ def self.menuWithLabel(label, title: title, menuItem: needsMenuItem, &block)
40
+ new(label, &block).tap do |menu|
41
+ menu.title = title
42
+ menu.createMenuItemWithTitle title if needsMenuItem
43
+ end
44
+ end
45
+
46
+
39
47
  def self.menuWithLabel(label, title: title, &block)
40
48
  new(label, &block).tap do |menu|
41
49
  menu.title = title
@@ -72,6 +80,8 @@ module DrinkMenu
72
80
  @label = label
73
81
  @builder = block
74
82
  @menu = NSMenu.alloc.init
83
+ @needsStatusItem = false
84
+ @needsMenuItem = false
75
85
  end
76
86
 
77
87
  def <<(item)
@@ -81,6 +91,10 @@ module DrinkMenu
81
91
  @menu.addItem item.menuItem
82
92
  end
83
93
 
94
+ def menuItemFromMenu!
95
+ @mainMenuItem ||= MenuItem.itemWithLabel(label, title: title, submenu: self)
96
+ end
97
+
84
98
  def addMenuItemForMember(member)
85
99
  item = MenuItem.itemWithLabel(member.hash.to_s, title: member.send(memberTitleProperty))
86
100
  item.representedObject = member
@@ -101,6 +115,7 @@ module DrinkMenu
101
115
  end
102
116
  end
103
117
 
118
+ # Only works when built with menuWithLabel:itemsFromCollection:titleProperty
104
119
  def subscribeToMembers(&block)
105
120
  memberCommand.signal.subscribeNext(block)
106
121
  end
@@ -109,6 +124,11 @@ module DrinkMenu
109
124
  self[itemLabel].subscribe(&block)
110
125
  end
111
126
 
127
+ def createMenuItemWithTitle(title)
128
+ @needsMenuItem = true
129
+ @statusItemTitle = title
130
+ end
131
+
112
132
  def createStatusItemWithTitle(title)
113
133
  @needsStatusItem = true
114
134
  @statusItemTitle = title
@@ -151,6 +171,10 @@ module DrinkMenu
151
171
  @needsStatusItem
152
172
  end
153
173
 
174
+ def needsMenuItem?
175
+ @needsMenuItem
176
+ end
177
+
154
178
  def createStatusItem!
155
179
  statusBar = NSStatusBar.systemStatusBar
156
180
  @statusItem = statusBar.statusItemWithLength(NSSquareStatusItemLength)
@@ -1,7 +1,7 @@
1
1
  module DrinkMenu
2
2
  module MenuBuilder
3
3
 
4
- class Context
4
+ class Context < BasicObject
5
5
  def initialize(menu, menuItems={})
6
6
  @menu = menu
7
7
  @menuItems = menuItems
@@ -55,6 +55,11 @@ module DrinkMenu
55
55
  @menus[label] = Menu.statusMenuWithLabel label, icon: image, &block
56
56
  end
57
57
 
58
+ def mainMenu(label, title: title, &block)
59
+ @menus ||= {}
60
+ @menus[label] = Menu.menuWithLabel label, title: title, menuItem: true, &block
61
+ end
62
+
58
63
  def menu(label, title: title, &block)
59
64
  @menus ||= {}
60
65
  @menus[label] = Menu.menuWithLabel label, title: title, &block
@@ -75,8 +80,18 @@ module DrinkMenu
75
80
  context.instance_eval(&menu.builder) if menu.builder
76
81
  if menu.needsStatusItem?
77
82
  menu.createStatusItem!
83
+ elsif menu.needsMenuItem?
84
+ @mainMenu ||= NSMenu.new
85
+ @mainMenu.addItem menu.menuItemFromMenu!.menuItem
78
86
  end
79
87
  end
88
+ setupMainMenu if @mainMenu
89
+ end
90
+
91
+ private
92
+
93
+ def setupMainMenu
94
+ NSApp.mainMenu = @mainMenu
80
95
  end
81
96
 
82
97
  end
@@ -1,3 +1,3 @@
1
1
  module DrinkMenu
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  class TestMenu; extend DrinkMenu::MenuBuilder; end
2
+ class TestMenu2; include DrinkMenu::MenuBuilder; end
2
3
 
3
4
  describe "sugar for creating menus" do
4
5
 
@@ -39,21 +40,52 @@ describe "sugar for creating menus" do
39
40
  end
40
41
 
41
42
  it "evaluates menu's block to add items to menu" do
42
- item1 = TestMenu.menuItem :test_item1, title: "Blah"
43
- item2 = TestMenu.menuItem :test_item2, title: "Blah"
43
+ builder = TestMenu2.new
44
+ item1 = builder.menuItem :test_item1, title: "Blah"
45
+ item2 = builder.menuItem :test_item2, title: "Blah"
44
46
 
45
- TestMenu.menu :main_menu, title: "Main" do
47
+ builder.menu :main_menu, title: "Main" do
46
48
  test_item1
47
49
  ___
48
50
  test_item2
49
51
  end
50
52
 
51
- TestMenu.build!
53
+ builder.build!
52
54
 
53
- TestMenu[:main_menu][:test_item1].should.equal item1
54
- TestMenu[:main_menu][:test_item2].should.equal item2
55
+ builder[:main_menu][:test_item1].should.equal item1
56
+ builder[:main_menu][:test_item2].should.equal item2
55
57
 
56
- TestMenu[:main_menu][2].isSeparatorItem.should.be.true
58
+ builder[:main_menu][2].isSeparatorItem.should.be.true
59
+ end
60
+
61
+ it "supports generating an NSApp's mainMenu items" do
62
+ builder = TestMenu2.new
63
+ testItem1 = builder.menuItem :test_item1, title: "Blah 1"
64
+ testItem2 = builder.menuItem :test_item2, title: "Blah 2"
65
+
66
+ menu1 = builder.mainMenu :menu1, title: "Menu 1" do
67
+ test_item1
68
+ end
69
+
70
+ menu2 = builder.mainMenu :menu2, title: "Menu 2" do
71
+ test_item2
72
+ end
73
+
74
+ builder.build!
75
+
76
+ mainMenu = NSApp.mainMenu
77
+
78
+ menuItem = mainMenu.itemArray[0]
79
+ puts "menu1 title: #{menu1.title.inspect}"
80
+ puts "menuItem title: #{menuItem.title.inspect}"
81
+ menuItem.title.should.equal menu1.title
82
+ menuItem.submenu.title.should.equal menu1.title
83
+ menuItem.submenu.itemArray[0].title.should.equal("Blah 1")
84
+
85
+ menuItem = mainMenu.itemArray[1]
86
+ menuItem.title.should.equal(menu2.title)
87
+ menuItem.submenu.title.should.equal menu2.title
88
+ menuItem.submenu.itemArray[0].title.should.equal("Blah 2")
57
89
  end
58
90
 
59
91
  describe "builder's context class" do
@@ -77,9 +109,8 @@ describe "sugar for creating menus" do
77
109
 
78
110
  menu[1].should.be.nil
79
111
 
80
- Context.new(menu).send :"___"
112
+ Context.new(menu).__send__ :"___"
81
113
 
82
- puts menu.menuItems.inspect
83
114
  menu[1].isSeparatorItem.should.be.true
84
115
  end
85
116
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drink-menu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Fiorini
@@ -82,6 +82,8 @@ files:
82
82
  - README.md
83
83
  - Rakefile
84
84
  - drink-menu.gemspec
85
+ - examples/basic_main_menu/app_delegate.rb
86
+ - examples/basic_main_menu/main_menu.rb
85
87
  - lib/drink-menu.rb
86
88
  - lib/drink-menu/ext/forwardable.rb
87
89
  - lib/drink-menu/ext/ns_menu_item.rb