SFML community forums

Help => Window => Topic started by: Phanoo on December 05, 2017, 10:11:04 am

Title: SFML doesnt catch all window Resized events +focusLost doesn't fire in some case
Post by: Phanoo on December 05, 2017, 10:11:04 am
hello, some problems I have, not sure if they are SFML bugs :

I know my post needs more testing, but it was to see if other people noticed the same problems
Title: Re: SFML doesnt catch all window Resized events +focusLost doesn't fire in some case
Post by: Laurent on December 09, 2017, 06:32:25 pm
If you post a complete and minimal example that reproduces the problem(s), then maybe more people can test it and check if it's a SFML bug.
Title: Re: SFML doesnt catch all window Resized events +focusLost doesn't fire in some case
Post by: Phanoo on December 12, 2017, 02:42:38 am
100 % reproductible, two programs, the first request to get to the front, the second checks if it lost focus :

Requester, brings window to foreground every 2sec, using windows api
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

#include <windows.h>

int main(){
        sf::RenderWindow window(sf::VideoMode(200, 200), "request focus");

        sf::Event evt;

        while(window.isOpen()){
                while (window.pollEvent(evt)){
                }
                sf::sleep(sf::seconds(2));
                SwitchToThisWindow(window.getSystemHandle(),0);
                window.clear();
                window.display();
        }


       
        return 0;
}


Focus checker, displays a green window when it has focus, red if it hasn't

#include <SFML/Graphics.hpp>

int main(){
        sf::RenderWindow window(sf::VideoMode(200, 200), "focus checker");
       
        sf::Event evt;

        sf::Color c(0,255,0);

        while(window.isOpen()){
                while (window.pollEvent(evt)){
                        switch(evt.type){
                                case sf::Event::LostFocus:
                                        c = sf::Color(255,0,0);
                                        break;
                                case sf::Event::GainedFocus:
                                        c = sf::Color(0,255,0);
                                        break;
                        }
                }

                window.clear(c);
                window.display();
        }


        return 0;
}


Run both programs, you'll see the focus checker stays green when the requester gets to the foreground. No problem when the user switch window or click outside, it seems to happen only when another window takes the focus by itself.

See compiled test in attachment. It doesn't check for window close event so you have to kill them using task manager
Title: Re: SFML doesnt catch all window Resized events +focusLost doesn't fire in some case
Post by: Laurent on December 12, 2017, 06:31:48 pm
Works fine for me (SFML 2.4, Windows 10).
Title: Re: SFML doesnt catch all window Resized events +focusLost doesn't fire in some case
Post by: dabbertorres on December 13, 2017, 12:15:47 am
Isn't that how the behavior of focus is defined in Windows? A program can be in the foreground, but it doesn't have focus until the user interacts with it? (ie: I can use my mouse's scroll wheel in one window, but be typing in another window)

Looking at the documentation for SwitchToThisWindow (https://msdn.microsoft.com/en-us/library/windows/desktop/ms633553(v=vs.85).aspx)
Quote
[This function is not intended for general use. It may be altered or unavailable in subsequent versions of Windows.]
and
Quote
This function is typically called to maintain window z-ordering.
That makes me think it might not do quite what the name says it would.

Does using SetFocus (https://msdn.microsoft.com/en-us/library/windows/desktop/ms646312(v=vs.85).aspx) instead change the behavior of your example?
(Also, it's documentation specifically references the events that SFML listens for (https://github.com/SFML/SFML/blob/master/src/SFML/Window/Win32/WindowImplWin32.cpp#L665-L686), WM_KILLFOCUS and WM_SETFOCUS)
Title: Re: SFML doesnt catch all window Resized events +focusLost doesn't fire in some case
Post by: eXpl0it3r on December 13, 2017, 08:37:07 am
What's your version of Windows and what's your version of SFML?

What if you set fAltTab (second parameter) to true?

In the end, if the OS isn't providing us the required events, then there's little we can do without introducing ugly or performance impacting hacks.
We know that focus can potentially be a bit unstable, which is why we introduced an event independent focus checking and focus requesting function.