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.system.get_theme() (w/ sdl3) #3325

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion buildconfig/stubs/pygame/system.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, TypedDict
from typing import Optional, TypedDict, Literal

from pygame._data_classes import PowerState

Expand Down Expand Up @@ -28,3 +28,4 @@ def get_total_ram() -> int: ...
def get_pref_path(org: str, app: str) -> str: ...
def get_pref_locales() -> list[_Locale]: ...
def get_power_state() -> Optional[PowerState]: ...
def get_theme() -> Literal["light", "dark", "unknown"]: ...
12 changes: 12 additions & 0 deletions docs/reST/ref/system.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,15 @@
but not both.

.. versionadded:: 2.4.0

.. function:: get_theme

| :sl:`get the system theme`
| :sg:`get_theme() -> 'light' or 'dark' or 'unknown'`

Returns the system theme which could be ``"light"``, ``"dark"`` or ``"unknown"``.

This function requires SDL 3.2.0+. A :mod:`pygame.error` exception will be raised
otherwise.

.. versionadded:: ???
1 change: 1 addition & 0 deletions src_c/doc/system_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#define DOC_SYSTEM_GETPREFPATH "get_pref_path(org, app) -> path\nget a writeable folder for your app"
#define DOC_SYSTEM_GETPREFLOCALES "get_pref_locales() -> list[locale]\nget preferred locales set on the system"
#define DOC_SYSTEM_GETPOWERSTATE "get_pref_power_state() -> PowerState\nget the current power supply state"
#define DOC_SYSTEM_GETTHEME "get_theme() -> 'light' or 'dark' or 'unknown'\nget the system theme"
19 changes: 19 additions & 0 deletions src_c/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ pg_system_get_power_state(PyObject *self, PyObject *_null)
return PyObject_Call(PowerState_class, return_args, return_kwargs);
}

static PyObject *
pg_system_get_theme(PyObject *self, PyObject *_null)
{
#if SDL_VERSION_ATLEAST(3, 2, 0)
switch (SDL_GetSystemTheme()) {
case SDL_SYSTEM_THEME_LIGHT:
return PyUnicode_FromString("light");
case SDL_SYSTEM_THEME_DARK:
return PyUnicode_FromString("dark");
default:
return PyUnicode_FromString("unknown");
}
#else
return RAISE(pgExc_SDLError,
"'pygame.system.get_theme' requires SDL 3.2.0+");
#endif
}

static PyMethodDef _system_methods[] = {
{"get_cpu_instruction_sets", pg_system_get_cpu_instruction_sets,
METH_NOARGS, DOC_SYSTEM_GETCPUINSTRUCTIONSETS},
Expand All @@ -258,6 +276,7 @@ static PyMethodDef _system_methods[] = {
DOC_SYSTEM_GETPREFLOCALES},
{"get_power_state", pg_system_get_power_state, METH_NOARGS,
DOC_SYSTEM_GETPOWERSTATE},
{"get_theme", pg_system_get_theme, METH_NOARGS, DOC_SYSTEM_GETTHEME},
{NULL, NULL, 0, NULL}};

MODINIT_DEFINE(system)
Expand Down
10 changes: 10 additions & 0 deletions test/system_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

import pygame
from pygame.version import SDL


class SystemModuleTest(unittest.TestCase):
Expand Down Expand Up @@ -118,6 +119,15 @@ def test_get_power_state(self):
1,
)

def test_get_theme(self):
if SDL >= (3, 2, 0):
theme = pygame.system.get_theme()
self.assertIsInstance(theme, str)
self.assertIn(theme, ["light", "dark", "unknown"])
else:
with self.assertRaises(pygame.error):
pygame.system.get_theme()


if __name__ == "__main__":
unittest.main()
Loading