2 classvar <topLevelItems;
7 var parent; // nil = top level
10 var <isBranch = false;
14 topLevelItems.copy.do({|item| item.remove });
19 default = SCMenuGroup(nil, "Library", 7);
23 if(default.isNil, {this.initDefaultMenu});
27 *add { |names, action|
28 if(default.isNil) {this.initDefaultMenu };
29 ^this.deepNew(default, nil, names, action)
32 *deepNew { |parent, index, names = #[""], action|
33 var menu, name, insertionIndex;
34 name = names.removeAt(0);
35 insertionIndex = if(names.size <= 1) { index } ? parent.lastIndex;
37 menu = parent.findByName(name);
39 menu = if(names.size > 0, {SCMenuGroup}, {SCMenuItem})
40 .new(parent, name, insertionIndex);
45 "failed to add menu item: item % has submenu".format(name.quote).warn;
51 if(menu.isBranch.not) {
52 "failed to add menu item: item % allows no submenu".format(name.quote).warn;
55 menu = this.deepNew(menu, index, names, action)
62 ^children.asArray.size
66 ^children.detect { |x| x.name == name };
70 children.copy.do({|child| child.remove}); // cleanup my kids
72 (parent.notNil && (parent != 'Help')).if({parent.removeChild(this)});
73 this.prRemoveMenuItem;
74 topLevelItems.remove(this);
78 children = children.add(child);
82 children.remove(child);
87 ^this.primitiveFailed;
92 this.prSetState(bool);
96 setShortCut {|string, alt = false, ctrl = false|
97 _SetMenuItemKeyboardEquivalent
102 ^this.primitiveFailed;
105 prAddMenuItem { |parent, index, name, hasSubmenu|
107 ^this.primitiveFailed;
112 ^this.primitiveFailed;
123 SCMenuItem : CocoaMenuItem {
125 *new { |parent, name = "", index|
126 ^super.new.init(parent, index, name);
129 init { |argparent, argindex, argname|
134 this.prAddMenuItem(argparent, argindex, argname, isBranch);
135 if(parent.notNil && (parent != 'Help')) { parent.addChild(this) }
136 { topLevelItems = topLevelItems.add(this) };
140 SCMenuGroup : CocoaMenuItem {
142 *new { |parent, name = "", index|
143 ^super.new.init(parent, index, name);
146 init { |argparent, argindex, argname|
151 this.prAddMenuItem(argparent, argindex, argname, isBranch);
152 if(parent.notNil && (parent != 'Help')) { parent.addChild(this) }
153 { topLevelItems = topLevelItems.add(this) };
161 SCMenuSeparator : CocoaMenuItem {
163 *new { arg parent, index;
164 ^super.new.init( parent, index );
167 init { |argparent, argindex|
171 this.prAddMenuSeparator(argparent, argindex);
172 if(parent.notNil && (parent != 'Help')) { parent.addChild(this) }
173 { topLevelItems = topLevelItems.add(this) };
176 prAddMenuSeparator { |parent, index|