From 31697bbb493adc7343bff416270ebde6a79a7e5b Mon Sep 17 00:00:00 2001 From: zoldalma <46655437+zoldalma999@users.noreply.github.com> Date: Sat, 22 Jun 2024 19:16:22 +0200 Subject: [PATCH] Make window subclassable --- buildconfig/stubs/pygame/window.pyi | 2 +- docs/reST/ref/window.rst | 1 + src_c/window.c | 1 + test/window_test.py | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/buildconfig/stubs/pygame/window.pyi b/buildconfig/stubs/pygame/window.pyi index 9b09e76d2d..02d9fed015 100644 --- a/buildconfig/stubs/pygame/window.pyi +++ b/buildconfig/stubs/pygame/window.pyi @@ -6,7 +6,7 @@ from pygame.rect import Rect from pygame.surface import Surface def get_grabbed_window() -> Optional[Window]: ... -@final + class Window: def __init__( self, diff --git a/docs/reST/ref/window.rst b/docs/reST/ref/window.rst index 1365b636aa..0fe8b52b2f 100644 --- a/docs/reST/ref/window.rst +++ b/docs/reST/ref/window.rst @@ -46,6 +46,7 @@ .. versionadded:: 2.4.0 .. versionchanged:: 2.5.0 when ``opengl`` is ``True``, the ``Window`` has an OpenGL context created by pygame + .. versionchanged:: 2.5.1 Window is now a base class, allowing subclassing .. attribute:: grab_mouse diff --git a/src_c/window.c b/src_c/window.c index c3f3718386..183d85c2d8 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -1141,6 +1141,7 @@ static PyTypeObject pgWindow_Type = { PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.window.Window", .tp_basicsize = sizeof(pgWindowObject), .tp_dealloc = (destructor)window_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_doc = DOC_WINDOW, .tp_methods = window_methods, .tp_init = (initproc)window_init, diff --git a/test/window_test.py b/test/window_test.py index 5ac7f6aa5b..4ec4d440b1 100644 --- a/test/window_test.py +++ b/test/window_test.py @@ -397,6 +397,24 @@ def test_window_opengl(self): pygame.display.quit() pygame.init() + def test_window_subclassable(self): + class WindowSubclass(Window): + def __init__(self, title="Different title", size=(640, 480), **flags): + super().__init__(title, size, pygame.WINDOWPOS_CENTERED, **flags) + self.attribute = 10 + + window = WindowSubclass() + self.assertTrue(issubclass(WindowSubclass, Window)) + self.assertIsInstance(window, WindowSubclass) + self.assertEqual(window.title, "Different title") + self.assertEqual(window.attribute, 10) + window.destroy() + + pygame.display.set_mode((200, 200)) + window = WindowSubclass.from_display_module() + self.assertIsInstance(window, WindowSubclass) + self.assertEqual(window.size, (200, 200)) + def tearDown(self): self.win.destroy()