Skip to content

Race condition with Window.hasFocus() #206

Open
@danieljpetersen

Description

imgui-sfml relies on the false assumption that Window.hasFocus() will report true immediately after creation. Unfortunately, there is a short period of time after window creation where Window.hasFocus() is not yet true. This is the case for both macOS, Windows, and likely Linux though not confirmed there.

This results in an intermittent bug where input such as mouse clicks will be skipped / not recorded.

This was an issue reported by l04 on the SFML Discord. You can find a (spread out making it somewhat hard to follow) conversation about the issue starting here,
https://discord.com/channels/175298431294636032/175298431294636032/972715469310148658

Quoting l04 here

In the constructor of WindowContext, at line 217 of imgui-SFML.cpp, the statement windowHasFocus = window->hasFocus() is called.

However, the window might not have the focus yet, so it returns false.

Then in the function ImGui::SFML::ProcessEvent, the events LostFocus and GainedFocus are checked.

However, the window might have got the focus before calling the function, so it will not register the GainedFocus event.
In this case WindowContext is stuck with windowHasFocus = false until you manually generate the GainedFocus event.
If I replace line 217 of imgui-SFML.cpp with windowHasFocus = true, I can't reproduce the error anymore. Maybe it needs a better way to handle the initial focus?

A simple fix would be to set the windowHasFocus variable to true immediately on init rather than checking with the function call, though this is admittedly an ugly fix and I'm not sure that this is applicable in all cases. For example in the case of multiple windows.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions