everyday-menu 0.3.0 → 0.4.0
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 +4 -4
- data/README.md +84 -0
- data/examples/basic_main_menu/app_delegate.rb +0 -5
- data/examples/basic_main_menu/main_menu.rb +5 -4
- data/lib/everyday-menu/menu.rb +17 -0
- data/lib/everyday-menu/menu_builder.rb +1 -9
- data/lib/everyday-menu/menu_item.rb +55 -0
- data/lib/everyday-menu/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4519240679e0389183ed2e8dba773a4ed67c970a
|
4
|
+
data.tar.gz: 9ed56ec79c9022bc2bdeff2f6603363567b3f17b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96ac26b404b7f1bcd0dc78fa3118041e77fdb995084961e0c1e877b8f72fe76c1f86e3419438ae83ee930b30445bdec8e74c919ca8d8fbd8f7c755749a9daf8e
|
7
|
+
data.tar.gz: 7a4ce75150edbe04fb1b8cb7ea985459265625a31e2d17ab5317cd81fdd159a79b42ed151a4cd52eaca07356f3affd5515109d7cbc17f606ce772d98f6a5b9c1
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# EverydayMenu
|
2
2
|
|
3
|
+
## Updates
|
4
|
+
Please see the "Introducing Presets!" section below for an awesome new feature!
|
5
|
+
|
3
6
|
## Issue Tracking
|
4
7
|
Please use <https://everydayprogramminggenius.atlassian.net/browse/EM> for issue tracking.
|
5
8
|
|
@@ -94,6 +97,87 @@ class AppDelegate
|
|
94
97
|
end
|
95
98
|
end
|
96
99
|
```
|
100
|
+
|
101
|
+
You can even put multiple actions on a single item by calling subscribe multiple times.
|
102
|
+
|
103
|
+
## Introducing Presets!
|
104
|
+
With version 0.4.0, I have added the capability to use some presets. Here is the above example with presets:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class MainMenu
|
108
|
+
extend EverydayMenu::MenuBuilder
|
109
|
+
|
110
|
+
menuItem :hide_others, 'Hide Others', preset: :hide_others
|
111
|
+
menuItem :show_all, 'Show All', preset: :show_all
|
112
|
+
menuItem :quit, 'Quit', preset: :quit
|
113
|
+
|
114
|
+
menuItem :services_item, 'Services', preset: :services
|
115
|
+
|
116
|
+
menuItem :open, 'Open', key_equivalent: 'o'
|
117
|
+
menuItem :new, 'New'
|
118
|
+
menuItem :close, 'Close', key_equivalent: 'w'
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
with actions defined as:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
class AppDelegate
|
126
|
+
def applicationDidFinishLaunching(notification)
|
127
|
+
@has_open = false
|
128
|
+
MainMenu.build!
|
129
|
+
|
130
|
+
MainMenu[:file].subscribe(:new) { |_, _|
|
131
|
+
@has_open = true
|
132
|
+
puts 'new'
|
133
|
+
}
|
134
|
+
MainMenu[:file].subscribe(:close) { |_, _|
|
135
|
+
@has_open = false
|
136
|
+
puts 'close'
|
137
|
+
}.canExecuteBlock { |_| @has_open }
|
138
|
+
MainMenu[:file].subscribe(:open) { |_, _|
|
139
|
+
@has_open = true
|
140
|
+
puts 'open'
|
141
|
+
}
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
I didn't use a preset for close because there was special handling. Here are the presets and what they do:
|
147
|
+
|
148
|
+
Preset | Settings | Action
|
149
|
+
--------|-----------------------|------------------------------
|
150
|
+
`:hide` | `key_equivalent: 'h'` | <code>{ |_, _| NSApp.hide(self) }</code>
|
151
|
+
`:hide_others` | `key_equivalent: 'H'` <br> and <br> <code>:key\_equivalent\_modifier_mask: NSCommandKeyMask|NSAlternateKeyMask</code> | <code>{ |_, _| NSApp.hideOtherApplications(self) }</code>
|
152
|
+
`:show_all` | none | <code>{ |_, _| NSApp.unhideAllApplications(self) }</code>
|
153
|
+
`:quit` | `key_equivalent: 'q'` | <code>{ |_, _| NSApp.terminate(self) }</code>
|
154
|
+
`:close` | `key_equivalent: 'w'` | <code>{ |_, _| NSApp.keyWindow.performClose(self) }</code>
|
155
|
+
`:services` | `submenu: (menu :services, <item-title>, services_menu: true)` | none
|
156
|
+
|
157
|
+
Let me know if you have any others you think I should add. If you want to add one of your own, I have included the ability to define presets. You will want to do this at the top of the file where you setup your menu items. Here is an example:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
EverydayMenu::MenuItem.definePreset(:hide_others) { |item|
|
161
|
+
item[:key_equivalent] = 'H'
|
162
|
+
item[:key_equivalent_modifier_mask] = NSCommandKeyMask|NSAlternateKeyMask
|
163
|
+
item.subscribe { |_, _| NSApp.hideOtherApplications(item) }
|
164
|
+
}
|
165
|
+
```
|
166
|
+
|
167
|
+
Since the block is being run after the item instance is created, you have to use the other syntax, `item[<key>]=` in order to set the values. If you want to create a submenu in this, you can use `EverydayMenu::Menu.create(label, title, options = {})`, which accepts the same parameters as the `menu` method when building the menu normally.
|
168
|
+
|
169
|
+
If you set some application property (like `NSApp.servicesMenu`) in your method, you should probably have that delayed until the whole menu setup is built. You can do that like this:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
EverydayMenu::MenuItem.definePreset(:services) { |item|
|
173
|
+
item[:submenu] = Menu.create(:services_menu, item[:title], services_menu: true)
|
174
|
+
item.registerOnBuild { NSApp.servicesMenu = item[:submenu] }
|
175
|
+
}
|
176
|
+
```
|
177
|
+
|
178
|
+
Any block you pass to `item.registerOnBuild(&block)` will be added to a list of blocks to be run when the menu setup is built.
|
179
|
+
|
180
|
+
|
97
181
|
## Known Issues
|
98
182
|
|
99
183
|
Here are known issues. If you encounter one, please log a bug ticket in the issue tracker (link above)
|
@@ -3,19 +3,14 @@ class AppDelegate
|
|
3
3
|
@has_open = false
|
4
4
|
MainMenu.build!
|
5
5
|
|
6
|
-
MainMenu[:app].subscribe(:hide_others) { |_, _| NSApp.hideOtherApplications(self) }
|
7
|
-
MainMenu[:app].subscribe(:quit) { |_, _| NSApp.terminate(self) }
|
8
|
-
|
9
6
|
MainMenu[:file].subscribe(:new) { |_, _|
|
10
7
|
@has_open = true
|
11
8
|
puts 'new'
|
12
9
|
}
|
13
|
-
|
14
10
|
MainMenu[:file].subscribe(:close) { |_, _|
|
15
11
|
@has_open = false
|
16
12
|
puts 'close'
|
17
13
|
}.canExecuteBlock { |_| @has_open }
|
18
|
-
|
19
14
|
MainMenu[:file].subscribe(:open) { |_, _|
|
20
15
|
@has_open = true
|
21
16
|
puts 'open'
|
@@ -1,11 +1,11 @@
|
|
1
1
|
class MainMenu
|
2
2
|
extend EverydayMenu::MenuBuilder
|
3
3
|
|
4
|
-
menuItem :hide_others, 'Hide Others',
|
5
|
-
menuItem :
|
4
|
+
menuItem :hide_others, 'Hide Others', preset: :hide_others
|
5
|
+
menuItem :show_all, 'Show All', preset: :show_all
|
6
|
+
menuItem :quit, 'Quit', preset: :quit
|
6
7
|
|
7
|
-
|
8
|
-
menuItem :services_item, 'Services', submenu: :services
|
8
|
+
menuItem :services_item, 'Services', preset: :services
|
9
9
|
|
10
10
|
menuItem :open, 'Open', key_equivalent: 'o'
|
11
11
|
menuItem :new, 'New'
|
@@ -13,6 +13,7 @@ class MainMenu
|
|
13
13
|
|
14
14
|
mainMenu(:app, 'Blah') {
|
15
15
|
hide_others
|
16
|
+
show_all
|
16
17
|
___
|
17
18
|
services_item
|
18
19
|
___
|
data/lib/everyday-menu/menu.rb
CHANGED
@@ -83,6 +83,19 @@ module EverydayMenu
|
|
83
83
|
prefix.nil? ? rval : "#{prefix}#{rval[0].upcase}#{rval[1..-1]}"
|
84
84
|
end
|
85
85
|
|
86
|
+
def runOnBuild
|
87
|
+
if self.is :services_menu
|
88
|
+
NSApp.servicesMenu = self.menu
|
89
|
+
end
|
90
|
+
if self.is :windows_menu
|
91
|
+
NSApp.windowsMenu = self.menu
|
92
|
+
end
|
93
|
+
if self.is :help_menu
|
94
|
+
NSApp.helpMenu = self.menu
|
95
|
+
end
|
96
|
+
@menuItems.each { |item| item.runOnBuild }
|
97
|
+
end
|
98
|
+
|
86
99
|
def label
|
87
100
|
@label ||= nil
|
88
101
|
end
|
@@ -193,5 +206,9 @@ module EverydayMenu
|
|
193
206
|
end
|
194
207
|
|
195
208
|
alias :select_item_by_member :selectItemByMember
|
209
|
+
|
210
|
+
def each(&block)
|
211
|
+
@menuItems.values.each(&block)
|
212
|
+
end
|
196
213
|
end
|
197
214
|
end
|
@@ -57,15 +57,7 @@ module EverydayMenu
|
|
57
57
|
@mainMenu ||= NSMenu.new
|
58
58
|
@mainMenu.addItem menu.menuItemFromMenu!.menuItem
|
59
59
|
end
|
60
|
-
|
61
|
-
NSApp.servicesMenu = menu.menu
|
62
|
-
end
|
63
|
-
if menu.is :windows_menu
|
64
|
-
NSApp.windowsMenu = menu.menu
|
65
|
-
end
|
66
|
-
if menu.is :help_menu
|
67
|
-
NSApp.helpMenu = menu.menu
|
68
|
-
end
|
60
|
+
menu.runOnBuild
|
69
61
|
end
|
70
62
|
setupMainMenu if @mainMenu
|
71
63
|
end
|
@@ -108,6 +108,61 @@ module EverydayMenu
|
|
108
108
|
alias :keyEquivalentModifierMask= :setKeyEquivalentModifierMask
|
109
109
|
alias :key_equivalent_modifier_mask= :setKeyEquivalentModifierMask
|
110
110
|
|
111
|
+
def setPreset(action)
|
112
|
+
@@presets ||= {}
|
113
|
+
if @@presets.has_key?(action)
|
114
|
+
@@presets[action].call(self)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.definePreset(label, &block)
|
119
|
+
@@presets ||= {}
|
120
|
+
@@presets[label] = block
|
121
|
+
end
|
122
|
+
|
123
|
+
definePreset(:hide) { |item|
|
124
|
+
item[:key_equivalent] = 'h'
|
125
|
+
item.subscribe { |_, _| NSApp.hide(item) }
|
126
|
+
}
|
127
|
+
|
128
|
+
definePreset(:hide_others) { |item|
|
129
|
+
item[:key_equivalent] = 'H'
|
130
|
+
item[:key_equivalent_modifier_mask] = NSCommandKeyMask|NSAlternateKeyMask
|
131
|
+
item.subscribe { |_, _| NSApp.hideOtherApplications(item) }
|
132
|
+
}
|
133
|
+
|
134
|
+
definePreset(:show_all) { |item|
|
135
|
+
item.subscribe { |_, _| NSApp.unhideAllApplications(item) }
|
136
|
+
}
|
137
|
+
|
138
|
+
definePreset(:quit) { |item|
|
139
|
+
item[:key_equivalent] = 'q'
|
140
|
+
item.subscribe { |_, _| NSApp.terminate(item) }
|
141
|
+
}
|
142
|
+
|
143
|
+
definePreset(:close) { |item|
|
144
|
+
item[:key_equivalent] = 'w'
|
145
|
+
item.subscribe { |_, _| NSApp.keyWindow.performClose(item) }
|
146
|
+
}
|
147
|
+
|
148
|
+
definePreset(:services) { |item|
|
149
|
+
item[:submenu] = Menu.create(:services_menu, item[:title], services_menu: true)
|
150
|
+
item.registerOnBuild { NSApp.servicesMenu = item[:submenu] }
|
151
|
+
}
|
152
|
+
|
153
|
+
def runOnBuild
|
154
|
+
onBuild.each { |block| block.call }
|
155
|
+
end
|
156
|
+
|
157
|
+
def onBuild
|
158
|
+
@onBuild ||= []
|
159
|
+
end
|
160
|
+
|
161
|
+
def registerOnBuild(&block)
|
162
|
+
@onBuild ||= []
|
163
|
+
@onBuild << block
|
164
|
+
end
|
165
|
+
|
111
166
|
def subscribe(&block)
|
112
167
|
@menuItem.subscribe(self.label, &block)
|
113
168
|
end
|