| 1 |
#include "FGCocoaMenuBar.hxx" |
| 2 |
|
| 3 |
#include <AppKit/NSMenu.h> |
| 4 |
#include <AppKit/NSApplication.h> |
| 5 |
|
| 6 |
#include <simgear/props/props.hxx> |
| 7 |
#include <simgear/props/props_io.hxx> |
| 8 |
#include <simgear/debug/logstream.hxx> |
| 9 |
#include <simgear/structure/SGBinding.hxx> |
| 10 |
#include <simgear/misc/strutils.hxx> |
| 11 |
#include <simgear/sg_inlines.h> |
| 12 |
|
| 13 |
#include <Main/fg_props.hxx> |
| 14 |
#include <GUI/CocoaHelpers_private.h> |
| 15 |
#include <Main/sentryIntegration.hxx> |
| 16 |
|
| 17 |
#include <iostream> |
| 18 |
|
| 19 |
using std::string; |
| 20 |
using std::map; |
| 21 |
using std::cout; |
| 22 |
using namespace simgear; |
| 23 |
|
| 24 |
typedef std::map<NSMenuItem*, SGBindingList> MenuItemBindings; |
| 25 |
|
| 26 |
@class CocoaMenuDelegate; |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
class CocoaItemListener : public SGPropertyChangeListener |
| 31 |
{ |
| 32 |
public: |
| 33 |
CocoaItemListener(SGPropertyNode_ptr prop, NSMenuItem* i) : |
| 34 |
enableProp(prop->getNode("enabled")), |
| 35 |
checkedProp(prop->getNode("checked")), |
| 36 |
item(i) |
| 37 |
{ |
| 38 |
if (enableProp) { |
| 39 |
enableProp->addChangeListener(this); |
| 40 |
BOOL b = enableProp->getBoolValue(); |
| 41 |
[item setEnabled:b]; |
| 42 |
} |
| 43 |
|
| 44 |
if (checkedProp) { |
| 45 |
checkedProp->addChangeListener(this); |
| 46 |
auto b = checkedProp->getBoolValue(); |
| 47 |
item.state = b ? NSControlStateValueOn : NSControlStateValueOff; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
~CocoaItemListener() |
| 52 |
{ |
| 53 |
if (enableProp) { |
| 54 |
enableProp->removeChangeListener(this); |
| 55 |
} |
| 56 |
if (checkedProp) { |
| 57 |
checkedProp->removeChangeListener(this); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
virtual void valueChanged(SGPropertyNode *node) |
| 63 |
{ |
| 64 |
CocoaAutoreleasePool pool; |
| 65 |
if (node == enableProp) { |
| 66 |
BOOL b = node->getBoolValue(); |
| 67 |
[item setEnabled:b]; |
| 68 |
} |
| 69 |
|
| 70 |
if (node == checkedProp) { |
| 71 |
auto b = node->getBoolValue(); |
| 72 |
item.state = b ? NSControlStateValueOn : NSControlStateValueOff; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
private: |
| 77 |
SGPropertyNode_ptr enableProp; |
| 78 |
SGPropertyNode_ptr checkedProp; |
| 79 |
NSMenuItem* item; |
| 80 |
}; |
| 81 |
} // of anonymous namespace |
| 82 |
|
| 83 |
class FGCocoaMenuBar::CocoaMenuBarPrivate |
| 84 |
{ |
| 85 |
public: |
| 86 |
CocoaMenuBarPrivate(); |
| 87 |
~CocoaMenuBarPrivate(); |
| 88 |
|
| 89 |
void menuFromProps(NSMenu* menu, SGPropertyNode* menuNode); |
| 90 |
|
| 91 |
void fireBindingsForItem(NSMenuItem* item); |
| 92 |
|
| 93 |
public: |
| 94 |
CocoaMenuDelegate* delegate; |
| 95 |
|
| 96 |
MenuItemBindings itemBindings; |
| 97 |
std::vector<CocoaItemListener*> listeners; |
| 98 |
}; |
| 99 |
|
| 100 |
@interface CocoaMenuDelegate : NSObject <NSMenuDelegate> { |
| 101 |
@private |
| 102 |
FGCocoaMenuBar::CocoaMenuBarPrivate* peer; |
| 103 |
} |
| 104 |
|
| 105 |
@property (nonatomic, assign) FGCocoaMenuBar::CocoaMenuBarPrivate* peer; |
| 106 |
@end |
| 107 |
|
| 108 |
@implementation CocoaMenuDelegate |
| 109 |
|
| 110 |
@synthesize peer; |
| 111 |
|
| 112 |
- (void) itemAction:(id) sender |
| 113 |
{ |
| 114 |
peer->fireBindingsForItem((NSMenuItem*) sender); |
| 115 |
} |
| 116 |
|
| 117 |
@end |
| 118 |
|
| 119 |
static void setFunctionKeyShortcut(const std::string& shortcut, NSMenuItem* item) |
| 120 |
{ |
| 121 |
unichar shortcutChar = NSF1FunctionKey; |
| 122 |
if (shortcut == "F1") { |
| 123 |
shortcutChar = NSF1FunctionKey; |
| 124 |
} else if (shortcut == "F2") { |
| 125 |
shortcutChar = NSF2FunctionKey; |
| 126 |
} else if (shortcut == "F3") { |
| 127 |
shortcutChar = NSF3FunctionKey; |
| 128 |
} else if (shortcut == "F10") { |
| 129 |
shortcutChar = NSF10FunctionKey; |
| 130 |
} else if (shortcut == "F11") { |
| 131 |
shortcutChar = NSF11FunctionKey; |
| 132 |
} else if (shortcut == "F12") { |
| 133 |
shortcutChar = NSF12FunctionKey; |
| 134 |
} else { |
| 135 |
SG_LOG(SG_GENERAL, SG_WARN, "CocoaMenu:setFunctionKeyShortcut: unsupported:" << shortcut); |
| 136 |
} |
| 137 |
|
| 138 |
unichar ch[1]; |
| 139 |
ch[0] = shortcutChar; |
| 140 |
[item setKeyEquivalentModifierMask:NSEventModifierFlagFunction]; |
| 141 |
[item setKeyEquivalent:[NSString stringWithCharacters:ch length:1]]; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static void setItemShortcutFromString(NSMenuItem* item, const string& s) |
| 148 |
{ |
| 149 |
std::string shortcut; |
| 150 |
|
| 151 |
bool hasCtrl = strutils::starts_with(s, "Ctrl-"); |
| 152 |
bool hasShift = strutils::starts_with(s, "Shift-"); |
| 153 |
bool hasAlt = strutils::starts_with(s, "Alt-"); |
| 154 |
|
| 155 |
int offset = 0; // character offset from start of string |
| 156 |
if (hasShift) offset += 6; |
| 157 |
if (hasCtrl) offset += 5; |
| 158 |
if (hasAlt) offset += 4; |
| 159 |
|
| 160 |
shortcut = s.substr(offset); |
| 161 |
if (shortcut == "Esc") { |
| 162 |
shortcut = "\e"; |
| 163 |
} |
| 164 |
|
| 165 |
if ((shortcut.length() >= 2) && (shortcut[0] == 'F') && isdigit(shortcut[1])) { |
| 166 |
setFunctionKeyShortcut(shortcut, item); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
shortcut = simgear::strutils::lowercase(shortcut); |
| 171 |
[item setKeyEquivalent:[NSString stringWithCString:shortcut.c_str() encoding:NSUTF8StringEncoding]]; |
| 172 |
NSUInteger modifiers = 0; |
| 173 |
if (hasCtrl) modifiers |= NSEventModifierFlagControl; |
| 174 |
if (hasShift) modifiers |= NSEventModifierFlagShift; |
| 175 |
if (hasAlt) modifiers |= NSEventModifierFlagOption; |
| 176 |
|
| 177 |
[item setKeyEquivalentModifierMask:modifiers]; |
| 178 |
} |
| 179 |
|
| 180 |
static bool doesBindingShowDialog(const SGBindingList& bindings) |
| 181 |
{ |
| 182 |
auto it = std::find_if(bindings.begin(), bindings.end(), [](SGAbstractBinding_ptr binding) |
| 183 |
{ |
| 184 |
auto nab = dynamic_cast<SGBinding*>(binding.get()); |
| 185 |
if (!nab) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
return nab->getCommandName() == "dialog-show"; |
| 190 |
}); |
| 191 |
|
| 192 |
return it != bindings.end(); |
| 193 |
} |
| 194 |
|
| 195 |
FGCocoaMenuBar::CocoaMenuBarPrivate::CocoaMenuBarPrivate() |
| 196 |
{ |
| 197 |
delegate = [[CocoaMenuDelegate alloc] init]; |
| 198 |
delegate.peer = this; |
| 199 |
} |
| 200 |
|
| 201 |
FGCocoaMenuBar::CocoaMenuBarPrivate::~CocoaMenuBarPrivate() |
| 202 |
{ |
| 203 |
CocoaAutoreleasePool pool; |
| 204 |
[delegate release]; |
| 205 |
} |
| 206 |
|
| 207 |
static bool labelIsSeparator(NSString* s) |
| 208 |
{ |
| 209 |
return [s hasPrefix:@"---"]; |
| 210 |
} |
| 211 |
|
| 212 |
void FGCocoaMenuBar::CocoaMenuBarPrivate::menuFromProps(NSMenu* menu, SGPropertyNode* menuNode) |
| 213 |
{ |
| 214 |
int index = 0; |
| 215 |
for (SGPropertyNode_ptr n : menuNode->getChildren("item")) { |
| 216 |
if (!n->hasValue("enabled")) { |
| 217 |
n->setBoolValue("enabled", true); |
| 218 |
} |
| 219 |
|
| 220 |
SGBindingList bl = readBindingList(n->getChildren("binding"), globals->get_props()); |
| 221 |
bool showsDialog = n->getBoolValue("shows-dialog") || doesBindingShowDialog(bl); |
| 222 |
|
| 223 |
string l = strutils::simplify(getLocalizedLabel(n)); |
| 224 |
if (showsDialog && !strutils::ends_with(l, "...")) { |
| 225 |
l += u8"…"; |
| 226 |
} |
| 227 |
|
| 228 |
NSString* label = stdStringToCocoa(l); |
| 229 |
string shortcut = n->getStringValue("key"); |
| 230 |
|
| 231 |
NSMenuItem* item; |
| 232 |
if (index >= [menu numberOfItems]) { |
| 233 |
if (labelIsSeparator(label)) { |
| 234 |
item = [NSMenuItem separatorItem]; |
| 235 |
[menu addItem:item]; |
| 236 |
} else { |
| 237 |
item = [menu addItemWithTitle:label action:nil keyEquivalent:@""]; |
| 238 |
if (!shortcut.empty()) { |
| 239 |
setItemShortcutFromString(item, shortcut); |
| 240 |
} |
| 241 |
|
| 242 |
[item setTarget:delegate]; |
| 243 |
[item setAction:@selector(itemAction:)]; |
| 244 |
|
| 245 |
auto cl = new CocoaItemListener(n, item); |
| 246 |
listeners.push_back(cl); |
| 247 |
} |
| 248 |
} else { |
| 249 |
item = [menu itemAtIndex:index]; |
| 250 |
[item setTitle:label]; |
| 251 |
} |
| 252 |
|
| 253 |
auto subMenuNode = n->getChild("menu"); |
| 254 |
if (subMenuNode) { |
| 255 |
NSMenu* subMenu = [[NSMenu alloc] init]; |
| 256 |
menuFromProps(subMenu, subMenuNode); |
| 257 |
[item setSubmenu: subMenu]; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
itemBindings[item] = bl; |
| 262 |
++index; |
| 263 |
} // of item iteration |
| 264 |
} |
| 265 |
|
| 266 |
void FGCocoaMenuBar::CocoaMenuBarPrivate::fireBindingsForItem(NSMenuItem *item) |
| 267 |
{ |
| 268 |
MenuItemBindings::iterator it = itemBindings.find(item); |
| 269 |
if (it == itemBindings.end()) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
NSString* label = [item title]; |
| 274 |
const auto s = stdStringFromCocoa(label); |
| 275 |
flightgear::addSentryBreadcrumb("fire menu item:" + s, "info"); |
| 276 |
fireBindingList(it->second); |
| 277 |
} |
| 278 |
|
| 279 |
FGCocoaMenuBar::FGCocoaMenuBar() : |
| 280 |
p(new CocoaMenuBarPrivate) |
| 281 |
{ |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
FGCocoaMenuBar::~FGCocoaMenuBar() |
| 286 |
{ |
| 287 |
CocoaAutoreleasePool ap; |
| 288 |
NSMenu* mainBar = [[NSApplication sharedApplication] mainMenu]; |
| 289 |
|
| 290 |
int num = [mainBar numberOfItems]; |
| 291 |
for (int index=1; index < num; ++index) { |
| 292 |
NSMenuItem* topLevelItem = [mainBar itemAtIndex:index]; |
| 293 |
[topLevelItem.submenu removeAllItems]; |
| 294 |
} |
| 295 |
|
| 296 |
for (auto it = p->listeners.begin(); it != p->listeners.end(); ++it) { |
| 297 |
delete *it; |
| 298 |
} |
| 299 |
|
| 300 |
// owing to the bizarre destructor behaviour of SGBinding, we need |
| 301 |
// to explicitly clear these bindings. (PUIMenuBar takes a different |
| 302 |
// approach, and copies each binding into /sim/bindings) |
| 303 |
MenuItemBindings::iterator j; |
| 304 |
for (j = p->itemBindings.begin(); j != p->itemBindings.end(); ++j) { |
| 305 |
clearBindingList(j->second); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
void FGCocoaMenuBar::init() |
| 310 |
{ |
| 311 |
CocoaAutoreleasePool pool; |
| 312 |
|
| 313 |
NSMenu* mainBar = [[NSApplication sharedApplication] mainMenu]; |
| 314 |
SGPropertyNode_ptr props = fgGetNode("/sim/menubar/default",true); |
| 315 |
|
| 316 |
int index = 0; |
| 317 |
NSMenuItem* previousMenu = [mainBar itemAtIndex:0]; |
| 318 |
if (![[previousMenu title] isEqualToString:@"FlightGear"]) { |
| 319 |
[previousMenu setTitle:@"FlightGear"]; |
| 320 |
} |
| 321 |
|
| 322 |
// clear other menus in the menu-bar, eg from the launcher |
| 323 |
while ([mainBar numberOfItems] > 1) { |
| 324 |
[mainBar removeItemAtIndex:1]; |
| 325 |
} |
| 326 |
|
| 327 |
for (auto n : props->getChildren("menu")) { |
| 328 |
NSString* label = stdStringToCocoa(getLocalizedLabel(n)); |
| 329 |
NSMenuItem* item = [mainBar itemWithTitle:label]; |
| 330 |
NSMenu* menu; |
| 331 |
|
| 332 |
if (!item) { |
| 333 |
NSInteger insertIndex = [mainBar indexOfItem:previousMenu] + 1; |
| 334 |
item = [mainBar insertItemWithTitle:label action:nil keyEquivalent:@"" atIndex:insertIndex]; |
| 335 |
item.tag = index + 400; |
| 336 |
|
| 337 |
menu = [[NSMenu alloc] init]; |
| 338 |
menu.title = label; |
| 339 |
[menu setAutoenablesItems:NO]; |
| 340 |
[mainBar setSubmenu:menu forItem:item]; |
| 341 |
[menu autorelease]; |
| 342 |
} else { |
| 343 |
menu = item.submenu; |
| 344 |
} |
| 345 |
|
| 346 |
// synchronise menu with properties |
| 347 |
p->menuFromProps(menu, n); |
| 348 |
++index; |
| 349 |
previousMenu = item; |
| 350 |
|
| 351 |
// track menu enable/disable state |
| 352 |
if (!n->hasValue("enabled")) { |
| 353 |
n->setBoolValue("enabled", true); |
| 354 |
} |
| 355 |
|
| 356 |
auto l = new CocoaItemListener( n, item); |
| 357 |
p->listeners.push_back(l); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
bool FGCocoaMenuBar::isVisible() const |
| 362 |
{ |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
void FGCocoaMenuBar::show() |
| 367 |
{ |
| 368 |
// no-op |
| 369 |
} |
| 370 |
|
| 371 |
void FGCocoaMenuBar::hide() |
| 372 |
{ |
| 373 |
// no-op |
| 374 |
} |
| 375 |
|
| 376 |
void FGCocoaMenuBar::setHideIfOverlapsWindow(bool hide) |
| 377 |
{ |
| 378 |
SG_UNUSED(hide); |
| 379 |
// no-op |
| 380 |
} |
| 381 |
|
| 382 |
bool FGCocoaMenuBar::getHideIfOverlapsWindow() const |
| 383 |
{ |
| 384 |
return false; |
| 385 |
} |
| 386 |
|