Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sokol_app.h macos: quit menu #817

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

medvednikov
Copy link
Contributor

Right now there's no way to quit a Sokol application on macOS.

This adds a quit menu and a cmd+q shortcut.

@floooh
Copy link
Owner

floooh commented Apr 7, 2023

Hi, there's a couple of similar PRs and issues already, the main reason why I haven't merged any of them yet is that they all suffer from one or another problem (for instance the 'Quit' being hardcoded, so it won't be properly localized.

I would prefer a solution which allows the menu to be configured from the outside.

Until then it's probably better to do the menu setup outside of sokol_app.h

@medvednikov
Copy link
Contributor Author

Right now there's no way to quit a Sokol app on macos, so I think it's a good temporary fix, that takes care of this pretty big limitation.

Later, when the menu situation is figured out, it can be replaced/updated.

@floooh
Copy link
Owner

floooh commented Apr 8, 2023

Right now there's no way to quit a Sokol app on macos

You can quit the application programmatically, either "hard" or "soft" (where the application has a chance to show a "Really Quit?" dialog). See here:

sokol/sokol_app.h

Lines 733 to 807 in 7373dda

APPLICATION QUIT
================
Without special quit handling, a sokol_app.h application will quit
'gracefully' when the user clicks the window close-button unless a
platform's application model prevents this (e.g. on web or mobile).
'Graceful exit' means that the application-provided cleanup callback will
be called before the application quits.
On native desktop platforms sokol_app.h provides more control over the
application-quit-process. It's possible to initiate a 'programmatic quit'
from the application code, and a quit initiated by the application user can
be intercepted (for instance to show a custom dialog box).
This 'programmatic quit protocol' is implemented through 3 functions
and 1 event:
- sapp_quit(): This function simply quits the application without
giving the user a chance to intervene. Usually this might
be called when the user clicks the 'Ok' button in a 'Really Quit?'
dialog box
- sapp_request_quit(): Calling sapp_request_quit() will send the
event SAPP_EVENTTYPE_QUIT_REQUESTED to the applications event handler
callback, giving the user code a chance to intervene and cancel the
pending quit process (for instance to show a 'Really Quit?' dialog
box). If the event handler callback does nothing, the application
will be quit as usual. To prevent this, call the function
sapp_cancel_quit() from inside the event handler.
- sapp_cancel_quit(): Cancels a pending quit request, either initiated
by the user clicking the window close button, or programmatically
by calling sapp_request_quit(). The only place where calling this
function makes sense is from inside the event handler callback when
the SAPP_EVENTTYPE_QUIT_REQUESTED event has been received.
- SAPP_EVENTTYPE_QUIT_REQUESTED: this event is sent when the user
clicks the window's close button or application code calls the
sapp_request_quit() function. The event handler callback code can handle
this event by calling sapp_cancel_quit() to cancel the quit.
If the event is ignored, the application will quit as usual.
On the web platform, the quit behaviour differs from native platforms,
because of web-specific restrictions:
A `programmatic quit` initiated by calling sapp_quit() or
sapp_request_quit() will work as described above: the cleanup callback is
called, platform-specific cleanup is performed (on the web
this means that JS event handlers are unregisters), and then
the request-animation-loop will be exited. However that's all. The
web page itself will continue to exist (e.g. it's not possible to
programmatically close the browser tab).
On the web it's also not possible to run custom code when the user
closes a browser tab, so it's not possible to prevent this with a
fancy custom dialog box.
Instead the standard "Leave Site?" dialog box can be activated (or
deactivated) with the following function:
sapp_html5_ask_leave_site(bool ask);
The initial state of the associated internal flag can be provided
at startup via sapp_desc.html5_ask_leave_site.
This feature should only be used sparingly in critical situations - for
instance when the user would loose data - since popping up modal dialog
boxes is considered quite rude in the web world. Note that there's no way
to customize the content of this dialog box or run any code as a result
of the user's decision. Also note that the user must have interacted with
the site before the dialog box will appear. These are all security measures
to prevent fishing.
The Dear ImGui HighDPI sample contains example code of how to
implement a 'Really Quit?' dialog box with Dear ImGui (native desktop
platforms only), and for showing the hardwired "Leave Site?" dialog box
when running on the web platform:
https://floooh.github.io/sokol-html5/wasm/imgui-highdpi-sapp.html

@medvednikov
Copy link
Contributor Author

Yeah, I have a full screen app, so not an option there. There's no window frame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants