From 90bea1b7427984608fddcbd197a6334653511fd3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Feb 2023 11:58:50 -0800 Subject: [PATCH] gtk: get proper content scaling --- build.zig | 8 ++++++++ src/apprt/gtk.zig | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 80aebd22a..1ac50bbf4 100644 --- a/build.zig +++ b/build.zig @@ -569,6 +569,14 @@ fn addDeps( }, .gtk => { + // We need glfw for GTK because we use GLFW to get DPI. + step.addModule("glfw", glfw.module(b)); + const glfw_opts: glfw.Options = .{ + .metal = step.target.isDarwin(), + .opengl = false, + }; + try glfw.link(b, step, glfw_opts); + step.linkSystemLibrary("gtk4"); }, } diff --git a/src/apprt/gtk.zig b/src/apprt/gtk.zig index 4c4cb9429..503368d16 100644 --- a/src/apprt/gtk.zig +++ b/src/apprt/gtk.zig @@ -4,6 +4,7 @@ const std = @import("std"); const builtin = @import("builtin"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; +const glfw = @import("glfw"); const apprt = @import("../apprt.zig"); const CoreApp = @import("../App.zig"); const CoreSurface = @import("../Surface.zig"); @@ -32,6 +33,12 @@ pub const App = struct { ctx: *c.GMainContext, pub fn init(core_app: *CoreApp, opts: Options) !App { + // 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; + // Create our GTK Application which encapsulates our process. const app = @ptrCast(?*c.GtkApplication, c.gtk_application_new( opts.id.ptr, @@ -87,6 +94,7 @@ pub const App = struct { while (c.g_main_context_iteration(self.ctx, 0) != 0) {} c.g_main_context_release(self.ctx); c.g_object_unref(self.app); + glfw.terminate(); } pub fn wakeup(self: App) void { @@ -310,7 +318,9 @@ pub const Surface = struct { pub fn getContentScale(self: *const Surface) !apprt.ContentScale { _ = self; - return .{ .x = 1, .y = 1 }; + const monitor = glfw.Monitor.getPrimary() orelse return error.NoMonitor; + const scale = monitor.getContentScale(); + return apprt.ContentScale{ .x = scale.x_scale, .y = scale.y_scale }; } pub fn getSize(self: *const Surface) !apprt.SurfaceSize {