Merge pull request #746 from psnszsn/main

apprt/gtk: handle surface scale changes
This commit is contained in:
Mitchell Hashimoto
2023-10-27 15:20:13 -07:00
committed by GitHub
3 changed files with 21 additions and 31 deletions

View File

@ -795,7 +795,10 @@ fn addDeps(
// get access to glib for dbus.
if (flatpak) step.linkSystemLibrary2("gtk4", dynamic_link_opts);
// We may link GLFW below
switch (app_runtime) {
.none => {},
.glfw => {
const glfw_dep = b.dependency("glfw", .{
.target = step.target,
.optimize = step.optimize,
@ -804,21 +807,12 @@ fn addDeps(
.metal = step.target.isDarwin(),
});
switch (app_runtime) {
.none => {},
.glfw => {
step.addModule("glfw", mach_glfw_dep.module("mach-glfw"));
step.linkLibrary(mach_glfw_dep.artifact("mach-glfw"));
step.linkLibrary(glfw_dep.artifact("glfw"));
},
.gtk => {
// We need glfw for GTK because we use GLFW to get DPI.
step.addModule("glfw", mach_glfw_dep.module("mach-glfw"));
step.linkLibrary(mach_glfw_dep.artifact("mach-glfw"));
step.linkLibrary(glfw_dep.artifact("glfw"));
step.linkSystemLibrary2("gtk4", dynamic_link_opts);
},
}

View File

@ -13,7 +13,6 @@ const App = @This();
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const glfw = @import("glfw");
const configpkg = @import("../../config.zig");
const input = @import("../../input.zig");
const internal_os = @import("../../os/main.zig");
@ -53,12 +52,6 @@ running: bool = true,
pub fn init(core_app: *CoreApp, opts: Options) !App {
_ = opts;
// This is super weird, but we still use GLFW with GTK only so that
// we can tap into their folklore logic to get screen DPI. If we can
// figure out a reliable way to determine this ourselves, we can get
// rid of this dep.
if (!glfw.init(.{})) return error.GlfwInitFailed;
// Load our configuration
var config = try Config.load(core_app.alloc);
errdefer config.deinit();
@ -166,8 +159,6 @@ pub fn terminate(self: *App) void {
if (self.menu) |menu| c.g_object_unref(menu);
self.config.deinit();
glfw.terminate();
}
/// Reload the configuration. This should return the new configuration.

View File

@ -4,7 +4,6 @@
const Surface = @This();
const std = @import("std");
const glfw = @import("glfw");
const configpkg = @import("../../config.zig");
const apprt = @import("../../apprt.zig");
const font = @import("../../font/main.zig");
@ -19,9 +18,6 @@ const c = @import("c.zig");
const log = std.log.scoped(.gtk);
// We need native X11 access to access the primary clipboard.
const glfw_native = glfw.Native(.{ .x11 = true });
/// This is detected by the OpenGL renderer to move to a single-threaded
/// draw operation. This basically puts locks around our draw path.
pub const opengl_single_threaded_draw = true;
@ -354,10 +350,10 @@ pub fn shouldClose(self: *const Surface) bool {
}
pub fn getContentScale(self: *const Surface) !apprt.ContentScale {
_ = self;
const monitor = glfw.Monitor.getPrimary() orelse return error.NoMonitor;
const scale = monitor.getContentScale();
return apprt.ContentScale{ .x = scale.x_scale, .y = scale.y_scale };
// Future: detect GTK version 4.12+ and use gdk_surface_get_scale so we
// can support fractional scaling.
const scale = c.gtk_widget_get_scale_factor(@ptrCast(self.gl_area));
return .{ .x = @floatFromInt(scale), .y = @floatFromInt(scale) };
}
pub fn getSize(self: *const Surface) !apprt.SurfaceSize {
@ -625,6 +621,15 @@ fn gtkResize(area: *c.GtkGLArea, width: c.gint, height: c.gint, ud: ?*anyopaque)
.height = @intCast(height),
};
// We also update the content scale because there is no signal for
// content scale change and it seems to trigger a resize event.
if (self.getContentScale()) |scale| {
self.core_surface.contentScaleCallback(scale) catch |err| {
log.err("error in content scale callback err={}", .{err});
return;
};
} else |_| {}
// Call the primary callback.
if (self.realized) {
self.core_surface.sizeCallback(self.size) catch |err| {