Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Window Loses Focus on Startup - Mouse Clicks Don't Regain Focus  (Read 1144 times)

0 Members and 1 Guest are viewing this topic.

lehoangan02

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Hello, I'm trying to build a SFML app (dictionary app)
I'm encountering an issue where my SFML window loses focus on startup, leading to unexpected behavior with mouse interactions.
Details:
  • Platform: macOS (apple silicon)
  • SFML version: 2.6.1
  • Behavior: The render window doesn't have focus. Some elements in the SFML window require double-clicking to respond, even though they are designed to work with a single-click. However, it is unable to regain focus when being clicked on (some how still able to poll double-click as two separate single-click event.) Also, the loading screen doesn't draw during startup, the drawLoadingScreen function only works after startup.
  • Cause: This seems to happen if I click on the program icon in my dock while it's starting up (there is a heavy function call before the event loop). The SFML window appears to lose focus but still registers a double-click as two separate single-click events. Despite this, clicking the SFML window itself does not restore focus.
  • Conditions: The problem is particularly noticeable when auto-save is enabled and the last save (serialized data) is large.
  • Temporary workaround: Switching to another program and then switching back to the SFML window restores the expected behavior.
Why is this happening? I never touch the sf::Event on LostFocus and GainFocus. I recheck my code and other than that it works fine.
Here is the relevant part of my code.
void instance::operatePage1()
{
        if (!loadedSave)
        {
                deleteWholeTrie(pRoot);
                drawLoadingPage(); /* this is supposed to draw the loading page but it doesn't */
                deserializeBinaryWrapper(pRoot); /* this is very slow and allow me to click on the executable`s icon in the dock during it`s startup */
                loadedSave = true;
        }
        while (windowInstance.pollEvent(event))
        {
                switch (event.type)
                {
                        case sf::Event::Closed:
                        {
                                windowInstance.close();
                        }
                        case sf::Event::MouseButtonPressed:
                        {
This is the loading page function
void instance::drawLoadingPage()
{
        windowInstance.clear();
        windowInstance.draw(loadingSprite);
        windowInstance.display();
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10987
    • View Profile
    • development blog
    • Email
Re: SFML Window Loses Focus on Startup - Mouse Clicks Don't Regain Focus
« Reply #1 on: July 01, 2024, 10:13:30 am »
I feel like you're mixing a few different topics into one, which makes it hard to understand what you're trying to solve exactly.

Use a debugger to check what is going on in detail, e.g. set a break point in drawLoadingPage to ensure it's called.
If you block on the main thread by loading a lot of data, then events won't be processed and the screen won't be continuously updated. There isn't really a magic trick around this. If you have experience in parallel programming, you could consider using std::async for loading the raw data from disk. Note: Don't create SFML graphics objects in that phase (e.g. loading texture), as you could end up doing OpenGL calls in a separate thread that needs more work. And of course you'll need to properly synchronize the shared objects.

I don't understand what the problem is with losing focus and click or double click
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/