Fix window focus bugs for Windows
This commit is contained in:
@@ -89,4 +89,26 @@ bool check_window_moved(sf::RenderWindow& win, int& winLastX, int& winLastY, std
|
||||
winLastX = winPosition.x;
|
||||
winLastY = winPosition.y;
|
||||
return moved;
|
||||
}
|
||||
}
|
||||
|
||||
void makeFrontWindow(sf::Window& win) {
|
||||
static sf::Event evt;
|
||||
_makeFrontWindow(win);
|
||||
// Discard GainedFocus events generated by our own meddling:
|
||||
while(pollEvent(win, evt));
|
||||
}
|
||||
|
||||
void makeFrontWindow(sf::Window& win, sf::Window& prev) {
|
||||
static sf::Event evt;
|
||||
_makeFrontWindow(win);
|
||||
// Discard GainedFocus and LostFocus events generated by our own meddling:
|
||||
while(pollEvent(win, evt));
|
||||
while(pollEvent(prev, evt));
|
||||
}
|
||||
|
||||
void setWindowFloating(sf::Window& win, bool floating) {
|
||||
static sf::Event evt;
|
||||
_setWindowFloating(win, floating);
|
||||
// Discard GainedFocus and LostFocus events generated by our own meddling:
|
||||
while(pollEvent(win, evt));
|
||||
}
|
||||
|
@@ -23,8 +23,14 @@ sf::RenderWindow& mainPtr();
|
||||
|
||||
char keyToChar(sf::Keyboard::Key key, bool isShift);
|
||||
|
||||
// public-facing overloads which discard events generated by the operation:
|
||||
void makeFrontWindow(sf::Window& win);
|
||||
void makeFrontWindow(sf::Window& win, sf::Window& prev);
|
||||
void setWindowFloating(sf::Window& win, bool floating);
|
||||
// OS-specific implementations:
|
||||
void _makeFrontWindow(sf::Window& win);
|
||||
void _setWindowFloating(sf::Window& win, bool floating);
|
||||
|
||||
|
||||
// Necessary wrapper for sf::Window.pollEvent()
|
||||
bool pollEvent(sf::Window& win, sf::Event& event);
|
||||
|
@@ -91,10 +91,10 @@ std::string get_os_version() {
|
||||
return version.str();
|
||||
}
|
||||
|
||||
void makeFrontWindow(sf::Window& win) {
|
||||
void _makeFrontWindow(sf::Window& win) {
|
||||
}
|
||||
|
||||
void setWindowFloating(sf::Window& win, bool floating) {
|
||||
void _setWindowFloating(sf::Window& win, bool floating) {
|
||||
// Code adapted from <http://stackoverflow.com/a/16235920>
|
||||
auto display = XOpenDisplay(NULL);
|
||||
Atom wmStateAbove = XInternAtom(display, "_NET_WM_STATE_ABOVE", true);
|
||||
|
@@ -83,7 +83,7 @@ std::string get_os_version() {
|
||||
return os.str();
|
||||
}
|
||||
|
||||
void makeFrontWindow(sf::Window& win) {
|
||||
void _makeFrontWindow(sf::Window& win) {
|
||||
sf::WindowHandle handle = win.getSystemHandle();
|
||||
id nsHandle = id(handle);
|
||||
if([nsHandle isKindOfClass: [NSWindow class]]) {
|
||||
@@ -92,7 +92,7 @@ void makeFrontWindow(sf::Window& win) {
|
||||
}
|
||||
}
|
||||
|
||||
void setWindowFloating(sf::Window& win, bool floating) {
|
||||
void _setWindowFloating(sf::Window& win, bool floating) {
|
||||
sf::WindowHandle handle = win.getSystemHandle();
|
||||
id nsHandle = id(handle);
|
||||
if([nsHandle isKindOfClass: [NSWindow class]]) {
|
||||
|
@@ -144,15 +144,15 @@ std::string get_os_version() {
|
||||
return version.str();
|
||||
}
|
||||
|
||||
void makeFrontWindow(sf::Window& win) {
|
||||
void _makeFrontWindow(sf::Window& win) {
|
||||
HWND win_handle = win.getSystemHandle();
|
||||
BringWindowToTop(win_handle);
|
||||
}
|
||||
|
||||
void setWindowFloating(sf::Window& win, bool floating) {
|
||||
void _setWindowFloating(sf::Window& win, bool floating) {
|
||||
HWND win_handle = win.getSystemHandle();
|
||||
UINT flags = SWP_NOMOVE | SWP_NOSIZE;
|
||||
HWND newPos = floating ? HWND_TOPMOST : HWND_TOP;
|
||||
UINT flags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE;
|
||||
HWND newPos = floating ? HWND_TOPMOST : HWND_BOTTOM;
|
||||
// The flags param specifies that these 0's are ignored.
|
||||
SetWindowPos(win_handle, newPos, 0, 0, 0, 0, flags);
|
||||
}
|
||||
|
Reference in New Issue
Block a user