From 57c9ae9a4004a881d146dd32a77a4a13ef85e4e1 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 16 Jul 2021 19:44:47 -0700 Subject: [PATCH] glfw: add Monitor.getContentScale Signed-off-by: Stephen Gutekanst --- glfw/src/Monitor.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/glfw/src/Monitor.zig b/glfw/src/Monitor.zig index 423dd218..df6e4d81 100644 --- a/glfw/src/Monitor.zig +++ b/glfw/src/Monitor.zig @@ -94,6 +94,37 @@ pub fn getPhysicalSize(self: Monitor) Error!PhysicalSize { return PhysicalSize{ .width_mm = @intCast(usize, width_mm), .height_mm = @intCast(usize, height_mm) }; } +/// The content scale for a monitor. +/// +/// This is the ratio between the current DPI and the platform's default DPI. This is especially +/// important for text and any UI elements. If the pixel dimensions of your UI scaled by this look +/// appropriate on your machine then it should appear at a reasonable size on other machines +/// regardless of their DPI and scaling settings. This relies on the system DPI and scaling +/// settings being somewhat correct. +/// +/// The content scale may depend on both the monitor resolution and pixel density and on users +/// settings. It may be very different from the raw DPI calculated from the physical size and +/// current resolution. +const ContentScale = struct { + x_scale: f32, + y_scale: f32, +}; + +/// Returns the content scale for the monitor. +/// +/// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError. +/// +/// @thread_safety This function must only be called from the main thread. +/// +/// see also: monitor_scale, glfw.Window.getContentScale +pub fn getContentScale(self: Monitor) Error!ContentScale { + var x_scale: f32 = 0; + var y_scale: f32 = 0; + c.glfwGetMonitorContentScale(self.handle, &x_scale, &y_scale); + try getError(); + return ContentScale{ .x_scale = @floatCast(f32, x_scale), .y_scale = @floatCast(f32, y_scale) }; +} + /// Returns the currently connected monitors. /// /// This function returns a slice of all currently connected monitors. The primary monitor is @@ -166,3 +197,10 @@ test "getPhysicalSize" { _ = try m.getPhysicalSize(); } } + +test "getContentScale" { + const monitor = try getPrimary(); + if (monitor) |m| { + _ = try m.getContentScale(); + } +}