Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pygame.display.get/set_window_position() #2816

Merged
merged 8 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/display.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def get_caption() -> Tuple[str, str]: ...
def set_palette(palette: Sequence[ColorValue], /) -> None: ...
def get_num_displays() -> int: ...
def get_window_size() -> Tuple[int, int]: ...
def get_window_pos() -> Tuple[int, int]:...
def set_window_pos(pos: Sequence[int]) -> None:...
def get_allow_screensaver() -> bool: ...
def set_allow_screensaver(value: bool = True) -> None: ...
def get_desktop_sizes() -> List[Tuple[int, int]]: ...
Expand Down
25 changes: 25 additions & 0 deletions docs/reST/ref/display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,31 @@ required).

.. ## pygame.display.get_window_size ##

.. function:: get_window_pos

| :sl:`Return the position of the window or screen`
| :sg:`get_window_pos() -> tuple`

Returns the position of the window initialized with :func:`pygame.display.set_mode()`.
The position will change when the user moves the window or when the position is set manually with :func:`pygame.display.set_window_pos()`.
Coordinates could be negative or outside the desktop size bounds.
The position is relative to the topleft of the primary monitor and the y coordinate ignores the window frame.

.. ## pygame.display.get_window_pos ##

.. function:: set_window_pos

| :sl:`Set the current window position`
| :sg:`set_window_pos((x, y)) -> None`

Sets the position of the window initialized with :func:`pygame.display.set_mode()`.
This differs from updating environment variables as this function can be called after the display has been initialised.
The position is expected to be relative to the topleft of the primary monitor.
The y coordinate will ignore the window frame (y = 0 means the frame is hidden).
The user will still be able to move the window after this call. See also :func:`pygame.display.get_window_pos()`.

.. ## pygame.display.set_window_pos ##

.. function:: get_allow_screensaver

| :sl:`Return whether the screensaver is allowed to run.`
Expand Down
36 changes: 36 additions & 0 deletions src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,38 @@ pg_window_size(PyObject *self, PyObject *_null)
return Py_BuildValue("(ii)", w, h);
}

static PyObject *
pg_get_window_pos(PyObject *self, PyObject *_null)
{
SDL_Window *win = pg_GetDefaultWindow();
int x, y = 0;
if (!win)
return RAISE(pgExc_SDLError, "No open window");
SDL_GetWindowPosition(win, &x, &y);
return pg_tuple_couple_from_values_int(x, y);
}

static PyObject *
pg_set_window_pos(PyObject *self, PyObject *arg)
{
SDL_Window *win = pg_GetDefaultWindow();
PyObject *pos = NULL;
int x, y = 0;

if (!PyArg_ParseTuple(arg, "O", &pos))
return NULL;

if (pos != NULL) {
if (!pg_TwoIntsFromObj(pos, &x, &y))
return RAISE(PyExc_TypeError, "pos must be two numbers");
}

if (win)
SDL_SetWindowPosition(win, x, y);

Py_RETURN_NONE;
}

static PyObject *
pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -2905,6 +2937,10 @@ static PyMethodDef _pg_display_methods[] = {
DOC_DISPLAY_GETSURFACE},
{"get_window_size", (PyCFunction)pg_window_size, METH_NOARGS,
DOC_DISPLAY_GETWINDOWSIZE},
{"set_window_pos", pg_set_window_pos, METH_VARARGS,
DOC_DISPLAY_SETWINDOWPOS},
{"get_window_pos", (PyCFunction)pg_get_window_pos, METH_NOARGS,
DOC_DISPLAY_GETWINDOWPOS},

{"set_mode", (PyCFunction)pg_set_mode, METH_VARARGS | METH_KEYWORDS,
DOC_DISPLAY_SETMODE},
Expand Down
2 changes: 2 additions & 0 deletions src_c/doc/display_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define DOC_DISPLAY_SETPALETTE "set_palette(palette=None, /) -> None\nSet the display color palette for indexed displays"
#define DOC_DISPLAY_GETNUMDISPLAYS "get_num_displays() -> int\nReturn the number of displays"
#define DOC_DISPLAY_GETWINDOWSIZE "get_window_size() -> tuple\nReturn the size of the window or screen"
#define DOC_DISPLAY_GETWINDOWPOS "get_window_pos() -> tuple\nReturn the position of the window or screen"
#define DOC_DISPLAY_SETWINDOWPOS "set_window_pos((x, y)) -> None\nSet the current window position"
#define DOC_DISPLAY_GETALLOWSCREENSAVER "get_allow_screensaver() -> bool\nReturn whether the screensaver is allowed to run."
#define DOC_DISPLAY_SETALLOWSCREENSAVER "set_allow_screensaver(bool) -> None\nSet whether the screensaver may run"
#define DOC_DISPLAY_ISFULLSCREEN "is_fullscreen() -> bool\nReturns True if the pygame window created by pygame.display.set_mode() is in full-screen mode"
Expand Down
7 changes: 7 additions & 0 deletions test/display_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,13 @@ def test_toggle_fullscreen(self):
(test_surf.get_width(), test_surf.get_height()), width_height
)

def test_get_set_window_pos(self):
pygame.display.set_mode((500, 500))
pygame.display.set_window_pos((420, 360))
position = pygame.display.get_window_pos()
self.assertEqual(position[0], 420)
self.assertEqual(position[1], 360)


class DisplayUpdateTest(unittest.TestCase):
def question(self, qstr):
Expand Down
Loading