Handle window DPI changes

This commit is contained in:
Kovid Goyal 2018-05-25 21:59:31 +05:30
parent df9eab279a
commit fe8cf46d56
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 20 additions and 19 deletions

View File

@ -100,20 +100,17 @@ class Boss:
self.add_os_window(startup_session, os_window_id=os_window_id)
def add_os_window(self, startup_session, os_window_id=None, wclass=None, wname=None, size=None, startup_id=None):
dpi_changed = False
if os_window_id is None:
w, h = initial_window_size(self.opts, self.cached_values) if size is None else size
cls = wclass or self.args.cls or appname
os_window_id = create_os_window(w, h, appname, wname or self.args.name or cls, cls)
if startup_id:
ctx = init_startup_notification(os_window_id, startup_id)
dpi_changed = show_window(os_window_id)
show_window(os_window_id)
if startup_id:
end_startup_notification(ctx)
tm = TabManager(os_window_id, self.opts, self.args, startup_session)
self.os_window_map[os_window_id] = tm
if dpi_changed:
self.on_dpi_change(os_window_id)
return os_window_id
def list_os_windows(self):
@ -305,9 +302,12 @@ class Boss:
tm.activate_tab_at(x)
def on_window_resize(self, os_window_id, w, h, dpi_changed):
tm = self.os_window_map.get(os_window_id)
if tm is not None:
tm.resize()
if dpi_changed:
self.on_dpi_change(os_window_id)
else:
tm = self.os_window_map.get(os_window_id)
if tm is not None:
tm.resize()
def increase_font_size(self): # legacy
self.set_font_size(min(self.opts.font_size * 5, self.current_font_size + 2.0))

View File

@ -103,6 +103,17 @@ framebuffer_size_callback(GLFWwindow *w, int width, int height) {
global_state.callback_os_window = NULL;
}
static void
dpi_change_callback(GLFWwindow *w, float x_scale UNUSED, float y_scale UNUSED) {
if (!set_callback_window(w)) return;
// Ensure update_os_window_viewport() is called in the near future, it will
// take care of DPI changes.
OSWindow *window = global_state.callback_os_window;
window->has_pending_resizes = true; global_state.has_pending_resizes = true;
window->last_resize_event_at = monotonic();
global_state.callback_os_window = NULL;
}
static void
refresh_callback(GLFWwindow *w) {
if (!set_callback_window(w)) return;
@ -408,6 +419,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args) {
glfwSetCursor(glfw_window, standard_cursor);
update_os_window_viewport(w, false);
glfwSetFramebufferSizeCallback(glfw_window, framebuffer_size_callback);
glfwSetWindowContentScaleCallback(glfw_window, dpi_change_callback);
glfwSetWindowRefreshCallback(glfw_window, refresh_callback);
glfwSetMouseButtonCallback(glfw_window, mouse_button_callback);
glfwSetScrollCallback(glfw_window, scroll_callback);
@ -440,30 +452,19 @@ static PyObject*
show_window(PyObject UNUSED *self, PyObject *args) {
id_type os_window_id;
int yes = 1;
bool dpi_changed = false;
if (!PyArg_ParseTuple(args, "K|p", &os_window_id, &yes)) return NULL;
for (size_t i = 0; i < global_state.num_os_windows; i++) {
OSWindow *w = global_state.os_windows + i;
if (w->id == os_window_id) {
if (yes) {
bool first_show = !w->shown_once;
glfwShowWindow(w->handle);
w->shown_once = true;
push_focus_history(w);
if (first_show) {
double before_x = w->logical_dpi_x, before_y = w->logical_dpi_y;
set_os_window_dpi(w);
dpi_changed = before_x != w->logical_dpi_x || before_y != w->logical_dpi_y;
if (dpi_changed) load_fonts_for_window(w);
w->has_pending_resizes = true;
global_state.has_pending_resizes = true;
}
} else glfwHideWindow(w->handle);
break;
}
}
if (dpi_changed) Py_RETURN_TRUE;
Py_RETURN_FALSE;
Py_RETURN_NONE;
}
void