Slightly reorganize code of Fl_MacOS_Sys_Menu_Bar_Driver::play_menu()

This commit is contained in:
ManoloFLTK 2024-05-18 12:09:49 +02:00
parent d0922792ae
commit 642ec5b74b
2 changed files with 23 additions and 24 deletions

View File

@ -729,38 +729,36 @@ void fl_mac_set_about(Fl_Callback *cb, void *user_data, int shortcut) {
void Fl_MacOS_Sys_Menu_Bar_Driver::play_menu(const Fl_Menu_Item *item) { void Fl_MacOS_Sys_Menu_Bar_Driver::play_menu(const Fl_Menu_Item *item) {
// Use the accessibility interface to programmatically open a menu of the system menubar // Use the accessibility interface to programmatically open a menu of the system menubar
NSArray *children = nil; CFArrayRef children = NULL;
CFIndex count = -1; CFIndex count = 0;
AXUIElementRef element; AXUIElementRef element;
NSEnumerator *enumerator = nil;
char *label = remove_ampersand(item->label()); char *label = remove_ampersand(item->label());
NSString *mac_name = NSLocalizedString([NSString stringWithUTF8String:label], nil); NSString *mac_name = NSLocalizedString([NSString stringWithUTF8String:label], nil);
free(label); free(label);
AXUIElementRef appElement = AXUIElementCreateApplication(getpid()); AXUIElementRef appElement = AXUIElementCreateApplication(getpid());
AXUIElementRef menu_bar = NULL; AXUIElementRef menu_bar = NULL;
AXError error = AXUIElementCopyAttributeValue(appElement, kAXMenuBarAttribute, (CFTypeRef *)&menu_bar); AXError error = AXUIElementCopyAttributeValue(appElement, kAXMenuBarAttribute,
if (error) goto way_out; (CFTypeRef *)&menu_bar);
error = AXUIElementGetAttributeValueCount(menu_bar, kAXChildrenAttribute, &count); if (!error) error = AXUIElementGetAttributeValueCount(menu_bar, kAXChildrenAttribute, &count);
if (error) goto way_out; if (!error) error = AXUIElementCopyAttributeValues(menu_bar, kAXChildrenAttribute, 0, count,
error = AXUIElementCopyAttributeValues(menu_bar, kAXChildrenAttribute, 0, count, (CFArrayRef *)&children); &children);
if (error) goto way_out; if (!error) {
enumerator = [children objectEnumerator]; NSEnumerator *enumerator = [(NSArray*)children objectEnumerator];
[enumerator nextObject]; // skip Apple menu [enumerator nextObject]; // skip Apple menu
[enumerator nextObject]; // skip application menu [enumerator nextObject]; // skip application menu
while ((element = (AXUIElementRef)[enumerator nextObject]) != nil) { bool need_more = true;
id title; while (need_more && (element = (AXUIElementRef)[enumerator nextObject]) != nil) {
AXError error = AXUIElementCopyAttributeValue(element, kAXTitleAttribute, (CFTypeRef *)&title); CFTypeRef title = NULL;
if (error) goto way_out; need_more = ( AXUIElementCopyAttributeValue(element, kAXTitleAttribute, &title) == 0 );
if ([title isEqualToString:mac_name]) { if (need_more && [(NSString*)title isEqualToString:mac_name]) {
AXUIElementPerformAction(element, kAXPressAction); AXUIElementPerformAction(element, kAXPressAction);
CFRelease(title); need_more = false;
break; }
if (title) CFRelease(title);
} }
CFRelease(title);
} }
way_out:
if (menu_bar) CFRelease(menu_bar); if (menu_bar) CFRelease(menu_bar);
if (children) [children release]; if (children) CFRelease(children);
CFRelease(appElement); CFRelease(appElement);
} }

View File

@ -243,7 +243,8 @@ void Fl_Sys_Menu_Bar::create_window_menu() {
} }
void Fl_Sys_Menu_Bar::play_menu(const Fl_Menu_Item *item) { void Fl_Sys_Menu_Bar::play_menu(const Fl_Menu_Item *item) {
if (driver()) fl_sys_menu_bar->driver()->play_menu(item); Fl_Sys_Menu_Bar_Driver *dr = driver();
if (dr) dr->play_menu(item);
else Fl_Menu_Bar::play_menu(item); else Fl_Menu_Bar::play_menu(item);
} }