Make argument handling of create_os_window() to be more robust and match its python signature

This commit is contained in:
Kovid Goyal 2024-01-18 21:58:39 +05:30
parent 1593baa9f9
commit bea8fd25a7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 11 additions and 9 deletions

View File

@ -514,9 +514,9 @@ def create_os_window(
wm_class_class: str,
window_state: Optional[int] = WINDOW_NORMAL,
load_programs: Optional[Callable[[bool], None]] = None,
x: Optional[int] = -1,
y: Optional[int] = -1,
disallow_override_title: Optional[bool] = False,
x: Optional[int] = None,
y: Optional[int] = None,
disallow_override_title: bool = False,
) -> int:
pass

View File

@ -1040,13 +1040,15 @@ native_window_handle(GLFWwindow *w) {
static PyObject*
create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) {
int x = -1, y = -1, window_state = WINDOW_NORMAL, disallow_override_title = 0;
int x = INT_MIN, y = INT_MIN, window_state = WINDOW_NORMAL, disallow_override_title = 0;
char *title, *wm_class_class, *wm_class_name;
PyObject *optional_window_state = NULL, *load_programs = NULL, *get_window_size, *pre_show_callback;
PyObject *optional_window_state = NULL, *load_programs = NULL, *get_window_size, *pre_show_callback, *optional_x = NULL, *optional_y = NULL;
static const char* kwlist[] = {"get_window_size", "pre_show_callback", "title", "wm_class_name", "wm_class_class", "window_state", "load_programs", "x", "y", "disallow_override_title", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kw, "OOsss|OOiip", (char**)kwlist,
&get_window_size, &pre_show_callback, &title, &wm_class_name, &wm_class_class, &optional_window_state, &load_programs, &x, &y, &disallow_override_title)) return NULL;
if (optional_window_state && optional_window_state != Py_None) window_state = (int) PyLong_AsLong(optional_window_state);
if (!PyArg_ParseTupleAndKeywords(args, kw, "OOsss|OOOOp", (char**)kwlist,
&get_window_size, &pre_show_callback, &title, &wm_class_name, &wm_class_class, &optional_window_state, &load_programs, &optional_x, &optional_y, &disallow_override_title)) return NULL;
if (optional_window_state && optional_window_state != Py_None) { if (!PyLong_Check(optional_window_state)) { PyErr_SetString(PyExc_TypeError, "window_state must be an int"); return NULL; } window_state = (int) PyLong_AsLong(optional_window_state); }
if (optional_x && optional_x != Py_None) { if (!PyLong_Check(optional_x)) { PyErr_SetString(PyExc_TypeError, "x must be an int"); return NULL;} x = (int)PyLong_AsLong(optional_x); }
if (optional_y && optional_y != Py_None) { if (!PyLong_Check(optional_y)) { PyErr_SetString(PyExc_TypeError, "y must be an int"); return NULL;} y = (int)PyLong_AsLong(optional_y); }
if (window_state < WINDOW_NORMAL || window_state > WINDOW_MINIMIZED) window_state = WINDOW_NORMAL;
static bool is_first_window = true;
@ -1144,7 +1146,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) {
PyObject *pret = PyObject_CallFunction(pre_show_callback, "N", native_window_handle(glfw_window));
if (pret == NULL) return NULL;
Py_DECREF(pret);
if (x != -1 && y != -1) glfwSetWindowPos(glfw_window, x, y);
if (x != INT_MIN && y != INT_MIN) glfwSetWindowPos(glfw_window, x, y);
#ifndef __APPLE__
glfwShowWindow(glfw_window);
#endif