Skip to content

Commit d4a6c40

Browse files
authored
Merge pull request #2307 from yunline/window-always-on-top
Add `always_on_top` attribute for video.Window
2 parents 0b39764 + 6325937 commit d4a6c40

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

buildconfig/stubs/pygame/_sdl2/video.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Window:
6666
def minimize(self) -> None: ...
6767
resizable: bool
6868
borderless: bool
69+
always_on_top: bool
6970
def set_icon(self, surface: Surface) -> None: ...
7071
id: int
7172
size: Iterable[int]

buildconfig/stubs/pygame/_window.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Window:
2828
title: str
2929
resizable: bool
3030
borderless: bool
31+
always_on_top: bool
3132
relative_mouse: bool
3233
id: int
3334
size: Iterable[int]

docs/reST/ref/sdl2_video.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@
146146
Gets or sets whether the window is borderless.
147147

148148
.. note:: You can't change the border state of a fullscreen window.
149+
150+
.. attribute:: always_on_top
151+
152+
| :sl:`Get or set whether the window is always on top`
153+
| :sg:`always_on_top -> bool`
154+
155+
Get or set whether the window is always on top.
156+
157+
Setting the always-on-top mode requires SDL 2.0.16+.
158+
159+
.. versionadded:: 2.3.1
160+
149161
.. attribute:: id
150162

151163
| :sl:`Get the unique window ID (**read-only**)`

src_c/doc/sdl2_video_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define DOC_SDL2_VIDEO_WINDOW_TITLE "title -> str\nGet or set the window title"
1111
#define DOC_SDL2_VIDEO_WINDOW_RESIZABLE "resizable -> bool\nGet or set whether the window is resizable"
1212
#define DOC_SDL2_VIDEO_WINDOW_BORDERLESS "borderless -> bool\nGet or set whether the window is borderless"
13+
#define DOC_SDL2_VIDEO_WINDOW_ALWAYSONTOP "always_on_top -> bool\nGet or set whether the window is always on top"
1314
#define DOC_SDL2_VIDEO_WINDOW_ID "id -> int\nGet the unique window ID (**read-only**)"
1415
#define DOC_SDL2_VIDEO_WINDOW_SIZE "size -> (int, int)\nGet or set the window size in pixels"
1516
#define DOC_SDL2_VIDEO_WINDOW_POSITION "position -> (int, int) or WINDOWPOS_CENTERED or WINDOWPOS_UNDEFINED\nGet or set the window position in screen coordinates"

src_c/window.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,32 @@ window_get_borderless(pgWindowObject *self, void *v)
308308
SDL_WINDOW_BORDERLESS);
309309
}
310310

311+
static int
312+
window_set_always_on_top(pgWindowObject *self, PyObject *arg, void *v)
313+
{
314+
#if SDL_VERSION_ATLEAST(2, 0, 16)
315+
int enable = PyObject_IsTrue(arg);
316+
if (enable == -1)
317+
return -1;
318+
319+
SDL_SetWindowAlwaysOnTop(self->_win, enable);
320+
#else
321+
if (PyErr_WarnEx(PyExc_Warning,
322+
"Setting 'always_on_top' requires SDL 2.0.16+",
323+
1) == -1) {
324+
return -1;
325+
}
326+
#endif // SDL_VERSION_ATLEAST(2, 0, 16)
327+
return 0;
328+
}
329+
330+
static PyObject *
331+
window_get_always_on_top(pgWindowObject *self, void *v)
332+
{
333+
return PyBool_FromLong(SDL_GetWindowFlags(self->_win) &
334+
SDL_WINDOW_ALWAYS_ON_TOP);
335+
}
336+
311337
static PyObject *
312338
window_get_window_id(pgWindowObject *self, PyObject *_null)
313339
{
@@ -711,6 +737,9 @@ static PyGetSetDef _window_getset[] = {
711737
DOC_SDL2_VIDEO_WINDOW_RESIZABLE, NULL},
712738
{"borderless", (getter)window_get_borderless,
713739
(setter)window_set_borderless, DOC_SDL2_VIDEO_WINDOW_BORDERLESS, NULL},
740+
{"always_on_top", (getter)window_get_always_on_top,
741+
(setter)window_set_always_on_top, DOC_SDL2_VIDEO_WINDOW_ALWAYSONTOP,
742+
NULL},
714743
{"relative_mouse", (getter)mouse_get_relative_mode,
715744
(setter)mouse_set_relative_mode, DOC_SDL2_VIDEO_WINDOW_RELATIVEMOUSE,
716745
NULL},

0 commit comments

Comments
 (0)