Ignore initial OS Window positions that are not on any monitor

This applies to --position, remember_window_position and positions from
sessions, which could otherwise place the window on a monitor that is no
longer connected.
This commit is contained in:
Kovid Goyal 2026-10-16 11:27:13 +05:30
parent 6a3cfbcb10
commit beca032240
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 18 additions and 1 deletions

View file

@ -231,6 +231,9 @@ Detailed list of changes
0.50.0 [future]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Ignore initial OS Window positions that are not on any connected monitor, for
example, because the monitor was disconnected
- Sessions: Fix the OS Window size specified in a session being ignored when
:opt:`remember_window_size` is enabled

View file

@ -2211,6 +2211,19 @@ set_window_creation_error(bool is_panel) {
}
}
static bool
is_visible_on_a_monitor(int x, int y, int width, int height) {
int count = 0;
GLFWmonitor **monitors = glfwGetMonitors(&count);
if (count <= 0 || !monitors) return true;
for (int i = 0; i < count; i++) {
int mx, my, mw, mh;
glfwGetMonitorWorkarea(monitors[i], &mx, &my, &mw, &mh);
if (x < mx + mw && mx < x + width && y < my + mh && my < y + height) return true;
}
return false;
}
static PyObject *
create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) {
int x = INT_MIN, y = INT_MIN, window_state = WINDOW_NORMAL, disallow_override_title = 0;
@ -2437,7 +2450,8 @@ 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 != INT_MIN && y != INT_MIN) glfwSetWindowPos(glfw_window, x, y);
// Ignore positions that are not on any monitor, for example, because the monitor was disconnected
if (x != INT_MIN && y != INT_MIN && is_visible_on_a_monitor(x, y, width, height)) glfwSetWindowPos(glfw_window, x, y);
if (!global_state.is_apple && !global_state.is_wayland && window_state != WINDOW_HIDDEN) glfwShowWindow(glfw_window, false);
if (global_state.is_wayland || global_state.is_apple) {
float n_xscale, n_yscale;