libui icon indicating copy to clipboard operation
libui copied to clipboard

MacOSX menu item Quit not working

Open bztsrc opened this issue 4 years ago • 4 comments

Hi,

First of all, I'd like to say thanks for this amazing library! I've used it in my cross-platform project: https://gitlab.com/bztsrc/usbimager which is an extremely small (ca. 256k) compressed disk image writer with a minimalistic GUI. I use libui for Cocoa and for GTK under Linux.

I had issues with linking the provided examples under Windows, so I gave up on that part and implemented win32 myself. Any reason why you've used C++ there instead of C? Just curious, off topic.

I also had some other issues under MacOSX, mostly with multiple threads and progressbars, but I was able to solve them all. There's one thing left though, which is kept reported over and over again. Under MacOSX the menu contains a grayed out Quit and the Cmd+Q shortcut does not work either.

I took a deep dive in the source, and I've found https://github.com/andlabs/libui/blob/master/darwin/menu.m#L176 which looks okay to me and also has the keybinding set. Yet Quit is not working. I don't use multiple windows, I don't need user defined menu items, nothing extra. I've found a ticket on the Go version with exactly the same problem, and it was closed pointing to issue #321. However there's no explanation in that issue on how to enable Quit.

So may question is, what I'm doing wrong? How should I change the code to have a working Quit menu item? Here's my code: https://gitlab.com/bztsrc/usbimager/-/blob/master/src/main_libui.c#L440

Do I need some special option or something? (However I did not see any "if"s in "makeMenubar" nor in "buildApplicationMenu"). If I need to add a Quit item manually, how can I do it? "uiMenuAppendQuitItem" requires an uiMenu pointer, but I don't want to call "uiNewMenu", as it has to be added to the already existing application menu. What is the correct way to do this?

Thanks for this awesome library, bzt

bztsrc avatar Jun 26 '20 09:06 bztsrc

Hi,

Could this be the solution? SDL actively kicking the Dock on app launch: https://github.com/spurious/SDL-mirror/blob/master/src/video/cocoa/SDL_cocoaevents.m#L246

    /* The menu bar of SDL apps which don't have the typical .app bundle
     * structure fails to work the first time a window is created (until it's
     * de-focused and re-focused), if this call is in Cocoa_RegisterApp instead
     * of here. https://bugzilla.libsdl.org/show_bug.cgi?id=3051
     */
    if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) {
        /* Get more aggressive for Catalina: activate the Dock first so we definitely reset all activation state. */
        for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) {
            [i activateWithOptions:NSApplicationActivateIgnoringOtherApps];
            break;
        }
        SDL_Delay(300);  /* !!! FIXME: this isn't right. */
        [NSApp activateIgnoringOtherApps:YES];
    }

The "activateIgnoringOtherApps" message is interesting (I'm pretty sure libui should send that message too in window.m), but that comment about Catalina even more interesting...

Cheers, bzt

bztsrc avatar Jun 27 '20 23:06 bztsrc

Strange. Also that message was intentionally omitted from libui; here's an explanation as to why. This is also why the first window is created in the background.

macOS is strange about not running GUI apps in an app bundle; I wonder if the bug happens if you have one? (Regardless of the answer, keep the bug open.)

andlabs avatar Jun 28 '20 06:06 andlabs

Thanks for the comment!

I'm actually using this from an app bundle (if that means that the executable is placed under /Applications/(App)/MacOS and it also has an info.plist). Check out my zip. I believe it has the correct directory structure. The info.plist is here. And a screenshot of the result (from one of the issues my users reported): https://gitlab.com/bztsrc/usbimager/-/issues/15

Thanks for the link too, that page links to two other pages, one of which is not working (bitbucket gives 404), and I already knew the other (cocoawithlove). I've tried that latter, but I can't get it to work. I've checked other sources as well for a simple "Hello World" cocoa app, but either they don't even compile, or they fail to open the window with some run-time messaging error... :-( I really don't understand why is this so problematic under Mac (I mean I realize they try to push their bloated XCode solution as much as possible, but seriously, com'on).

Regarding to the "activateIgnoringOtherApps" message itself, I understand that it should not needed, and I also do agree with the arguments on the linked SO page; however SDL does it, and undeniably menus in SDL apps do work...

Cheers,

bztsrc avatar Jun 30 '20 20:06 bztsrc

FYI: it might look like from my comments that I'm not satisfied with libui, I want to assure you that's not the case! It has some quirks, sure, but it really allowed me to create simple C apps for Mac, which would be entirely impossible without. So I'm grateful, thank you very much for this awesome library! Keep up the good work @andlabs !

bztsrc avatar Jun 30 '20 20:06 bztsrc