Skip to content

Commit

Permalink
Merge pull request #3079 from damusss/Window-get_focus
Browse files Browse the repository at this point in the history
Add `Window.focused`
  • Loading branch information
bilhox authored Aug 27, 2024
2 parents c3d5fdc + fe8f985 commit 4a4814f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/window.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Window:
@property
def keyboard_grabbed(self) -> bool: ...
@property
def focused(self) -> bool: ...
@property
def id(self) -> int: ...
@property
def mouse_rect(self) -> Optional[Rect]: ...
Expand Down
12 changes: 12 additions & 0 deletions docs/reST/ref/window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@

.. versionadded:: 2.4.0

.. attribute:: focused

| :sl:`Get if the window is focused (**read-only**)`
| :sg:`focused -> bool`
Get if the window is currently focused. The same result can be achieved using
the ``WINDOWFOCUSGAINED`` and ``WINDOWFOCUSLOST`` events.

Use :meth:`focus` to focus and raise the window.

.. versionadded:: 2.5.2

.. attribute:: title

| :sl:`Get or set the window title`
Expand Down
1 change: 1 addition & 0 deletions src_c/doc/window_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define DOC_WINDOW_GRABKEYBOARD "grab_keyboard -> bool\nGet or set the window's keyboard grab mode"
#define DOC_WINDOW_MOUSEGRABBED "mouse_grabbed -> bool\nGet if the mouse cursor is confined to the window (**read-only**)"
#define DOC_WINDOW_KEYBOARDGRABBED "keyboard_grabbed -> bool\nGet if the keyboard shortcuts are captured by the window (**read-only**)"
#define DOC_WINDOW_FOCUSED "focused -> bool\nGet if the window is focused (**read-only**)"
#define DOC_WINDOW_TITLE "title -> str\nGet or set the window title"
#define DOC_WINDOW_RESIZABLE "resizable -> bool\nGet or set whether the window is resizable"
#define DOC_WINDOW_BORDERLESS "borderless -> bool\nGet or set whether the window is borderless"
Expand Down
8 changes: 8 additions & 0 deletions src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ window_focus(pgWindowObject *self, PyObject *args, PyObject *kwargs)
Py_RETURN_NONE;
}

static PyObject *
window_get_focused(pgWindowObject *self, void *v)
{
uint32_t flags = SDL_GetWindowFlags(self->_win);
return PyBool_FromLong((flags & SDL_WINDOW_INPUT_FOCUS) != 0);
}

static PyObject *
window_hide(pgWindowObject *self, PyObject *_null)
{
Expand Down Expand Up @@ -1169,6 +1176,7 @@ static PyGetSetDef _window_getset[] = {
DOC_WINDOW_MOUSEGRABBED, NULL},
{"keyboard_grabbed", (getter)window_get_keyboard_grabbed, NULL,
DOC_WINDOW_KEYBOARDGRABBED, NULL},
{"focused", (getter)window_get_focused, NULL, DOC_WINDOW_FOCUSED, NULL},
{"title", (getter)window_get_title, (setter)window_set_title,
DOC_WINDOW_TITLE, NULL},
{"resizable", (getter)window_get_resizable, (setter)window_set_resizable,
Expand Down
4 changes: 4 additions & 0 deletions test/window_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ def test_window_flash(self):
except pygame.error:
pass

def test_window_focused(self):
window = pygame.Window()
self.assertIsInstance(window.focused, bool)

def tearDown(self):
self.win.destroy()

Expand Down

0 comments on commit 4a4814f

Please sign in to comment.