From 506ba854fa160852e0622c89f41f7463abdff1e2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 6 Apr 2024 19:33:49 -0700 Subject: [PATCH] core: font size changes work --- src/Surface.zig | 26 +++++++++++++++----------- src/apprt/glfw.zig | 2 +- src/apprt/surface.zig | 3 --- src/renderer/Metal.zig | 25 +++++-------------------- src/renderer/Options.zig | 2 +- src/renderer/Thread.zig | 8 ++------ 6 files changed, 24 insertions(+), 42 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 3c7b9a815..f142515c5 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -642,8 +642,6 @@ pub fn handleMessage(self: *Surface, msg: Message) !void { try self.rt_surface.setMouseShape(shape); }, - .cell_size => |size| try self.setCellSize(size), - .clipboard_read => |clipboard| { if (self.config.clipboard_read == .deny) { log.info("application attempted to read clipboard, but 'clipboard-read' is set to deny", .{}); @@ -966,18 +964,25 @@ fn setCellSize(self: *Surface, size: renderer.CellSize) !void { /// Change the font size. /// /// This can only be called from the main thread. -pub fn setFontSize(self: *Surface, size: font.face.DesiredSize) void { +pub fn setFontSize(self: *Surface, size: font.face.DesiredSize) !void { // Update our font size so future changes work self.font_size = size; // We need to build up a new font stack for this font size. - const font_grid_key, const font_grid = self.app.font_grid_set.ref( + const font_grid_key, const font_grid = try self.app.font_grid_set.ref( &self.config.font, self.font_size, - ) catch unreachable; + ); errdefer self.app.font_grid_set.deref(font_grid_key); - // Notify our render thread of the new font stack + // Set our cell size + try self.setCellSize(.{ + .width = font_grid.metrics.cell_width, + .height = font_grid.metrics.cell_height, + }); + + // Notify our render thread of the new font stack. The renderer + // MUST accept the new font grid and deref the old. _ = self.renderer_thread.mailbox.push(.{ .font_grid = .{ .grid = font_grid, @@ -988,7 +993,6 @@ pub fn setFontSize(self: *Surface, size: font.face.DesiredSize) void { }, .{ .forever = {} }); // Once we've sent the key we can replace our key - // TODO(fontmem): we should not store this anymore self.font_grid_key = font_grid_key; // Schedule render which also drains our mailbox @@ -1699,7 +1703,7 @@ pub fn contentScaleCallback(self: *Surface, content_scale: apprt.ContentScale) ! return; } - self.setFontSize(size); + try self.setFontSize(size); // Update our padding which is dependent on DPI. self.padding = padding: { @@ -2998,7 +3002,7 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool var size = self.font_size; size.points +|= delta; - self.setFontSize(size); + try self.setFontSize(size); }, .decrease_font_size => |delta| { @@ -3006,7 +3010,7 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool var size = self.font_size; size.points = @max(1, size.points -| delta); - self.setFontSize(size); + try self.setFontSize(size); }, .reset_font_size => { @@ -3014,7 +3018,7 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool var size = self.font_size; size.points = self.config.original_font_size; - self.setFontSize(size); + try self.setFontSize(size); }, .clear_screen => { diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 55e8069e7..932e27de5 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -246,7 +246,7 @@ pub const App = struct { // If we have a parent, inherit some properties if (self.config.@"window-inherit-font-size") { if (parent_) |parent| { - surface.core_surface.setFontSize(parent.font_size); + try surface.core_surface.setFontSize(parent.font_size); } } diff --git a/src/apprt/surface.zig b/src/apprt/surface.zig index 3060b7a5c..ae3ba050a 100644 --- a/src/apprt/surface.zig +++ b/src/apprt/surface.zig @@ -21,9 +21,6 @@ pub const Message = union(enum) { /// Set the mouse shape. set_mouse_shape: terminal.MouseShape, - /// Change the cell size. - cell_size: renderer.CellSize, - /// Read the clipboard and write to the pty. clipboard_read: apprt.Clipboard, diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index 6c75f9ad2..e9465db40 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -566,18 +566,16 @@ pub fn setFocus(self: *Metal, focus: bool) !void { /// Set the new font size. /// /// Must be called on the render thread. -pub fn setFontGrid(self: *Metal, grid: *font.SharedGrid) !void { +pub fn setFontGrid(self: *Metal, grid: *font.SharedGrid) void { // Update our grid self.font_grid = grid; self.texture_greyscale_modified = 0; self.texture_color_modified = 0; - // Recalculate our metrics - const metrics = metrics: { - grid.lock.lockShared(); - defer grid.lock.unlockShared(); - break :metrics grid.metrics; - }; + // Get our metrics from the grid. This doesn't require a lock because + // the metrics are never recalculated. + const metrics = grid.metrics; + self.grid_metrics = metrics; // Update our uniforms self.uniforms = .{ @@ -590,19 +588,6 @@ pub fn setFontGrid(self: *Metal, grid: *font.SharedGrid) !void { .strikethrough_thickness = @floatFromInt(metrics.strikethrough_thickness), .min_contrast = self.uniforms.min_contrast, }; - - // Recalculate our cell size. If it is the same as before, then we do - // nothing since the grid size couldn't have possibly changed. - if (std.meta.eql(self.grid_metrics, metrics)) return; - self.grid_metrics = metrics; - - // Notify the window that the cell size changed. - _ = self.surface_mailbox.push(.{ - .cell_size = .{ - .width = metrics.cell_width, - .height = metrics.cell_height, - }, - }, .{ .forever = {} }); } /// Update the frame data. diff --git a/src/renderer/Options.zig b/src/renderer/Options.zig index fd7b9d714..8c68affe8 100644 --- a/src/renderer/Options.zig +++ b/src/renderer/Options.zig @@ -8,7 +8,7 @@ const Config = @import("../config.zig").Config; /// The derived configuration for this renderer implementation. config: renderer.Renderer.DerivedConfig, -/// The font grid that should be used. +/// The font grid that should be used along with the key for deref-ing. font_grid: *font.SharedGrid, /// Padding options for the viewport. diff --git a/src/renderer/Thread.zig b/src/renderer/Thread.zig index d73705653..91a213132 100644 --- a/src/renderer/Thread.zig +++ b/src/renderer/Thread.zig @@ -321,13 +321,9 @@ fn drainMailbox(self: *Thread) !void { } }, - .font_grid => |grid| if (self.renderer.setFontGrid(grid.grid)) { - // Success, deref our old grid + .font_grid => |grid| { + self.renderer.setFontGrid(grid.grid); grid.set.deref(grid.old_key); - } else |err| { - // Error, deref our new grid since we didn't use it. - grid.set.deref(grid.new_key); - return err; }, .foreground_color => |color| {