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 focus steal #982

Merged
merged 4 commits into from
Jan 31, 2024
Merged

Sokol app focus steal #982

merged 4 commits into from
Jan 31, 2024

Conversation

zoo-3d
Copy link
Contributor

@zoo-3d zoo-3d commented Jan 28, 2024

Hi, this pr solves the issue sometimes, on luanch an executable file could not get window focus.

it has a time interval between app launch and the app fully activated.
the time interval becomes longer when init_cb is slower.

if there are mouse events(e.g. mouse move) during the interval, then app can not be activated and focus will lost even when app launched and order front.
I read some Apple documents and found nothing about this situation.I think maybe it's a bug in Cocoa Appkit.
By the way, GTK also has this problem. And GTK solved it even though they are not 100% sure about the root cause.
see: https://gitlab.gnome.org/GNOME/gtk/-/issues/2342

As a walkaround, we send a focus event in front of event queue.

@floooh
Copy link
Owner

floooh commented Jan 28, 2024

Interesting fix and thanks for the investigation! I'll try to have a closer look and do some testing within the next couple of days.

@floooh floooh self-assigned this Jan 30, 2024
@floooh
Copy link
Owner

floooh commented Jan 31, 2024

Btw, I think I can reproduce the issue on my M1 Mac with Sonoma, but not sure if it's such a big issue. I simulated a long init callback by putting a sleep(5) into the init function of the clear-sapp sample.

During those 5 seconds, the window is unfocused:

Screenshot 2024-01-31 at 14 55 14

...but as soon as the first frame is rendered, the window becomes focused automatically (also if I moved the moused etc...)

Screenshot 2024-01-31 at 14 55 22

With your fix the window seems to be focused immediately (but can't be interacted with anyway).

In any case, I guess the fix doesn't do any harm either...

@floooh
Copy link
Owner

floooh commented Jan 31, 2024

...ah wait the original issue was about cmdline apps, not app bundles... I'll test that too...

PS: works the same as cmdline app without the fix (e.g. the app start unfocused, but then focuses automatically at the first rendered frame).

I remember that this had issues in the past though... I guess Apple keeps fixing (and breaking) such things randomly in OS updates...

Anyway... I'll merge your fix since it doesn't seem to have any undesired side effects either :)

@floooh
Copy link
Owner

floooh commented Jan 31, 2024

PS: are you actually on the latest Sonoma macOS or an older version?

@floooh floooh merged commit ab668e6 into floooh:master Jan 31, 2024
23 checks passed
@floooh
Copy link
Owner

floooh commented Jan 31, 2024

And merged! I shortened your comment a bit (basically replaced it with a link to this PR), and updated the changelog.

Many thanks for the PR!

@floooh
Copy link
Owner

floooh commented Jan 31, 2024

PPS: whether the window starts focused or unfocused when there's a long delay between window creation and the first random frame seems to be fairly random, but with and without the focusEvent fix. E.g. not sure if the fix is actually redundant in Sonoma, but if it helps with older macOS versions then it's fine.

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.

2 participants