From 3ce554462a65739c635f9f9bd23dbb19f2ec1382 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 15 Nov 2022 19:54:05 -0800 Subject: [PATCH] window cell size event for changing cell size --- src/Window.zig | 25 +++++++++++++++++++++++++ src/renderer/OpenGL.zig | 9 +++++++++ src/window/message.zig | 3 +++ 3 files changed, 37 insertions(+) diff --git a/src/Window.zig b/src/Window.zig index 23f2a563c..640270956 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -527,9 +527,34 @@ pub fn handleMessage(self: *Window, msg: Message) !void { log.debug("changing title \"{s}\"", .{slice}); try self.window.setTitle(slice.ptr); }, + + .cell_size => |size| try self.setCellSize(size), } } +/// Change the cell size for the terminal grid. This can happen as +/// a result of changing the font size at runtime. +fn setCellSize(self: *Window, size: renderer.CellSize) !void { + // Update our new cell size for future calcs + self.cell_size = size; + + // Update our grid_size + self.grid_size = renderer.GridSize.init( + self.screen_size.subPadding(self.padding), + self.cell_size, + ); + + // Notify the terminal + _ = self.io_thread.mailbox.push(.{ + .resize = .{ + .grid_size = self.grid_size, + .screen_size = self.screen_size, + .padding = self.padding, + }, + }, .{ .forever = {} }); + self.io_thread.wakeup.send() catch {}; +} + /// This queues a render operation with the renderer thread. The render /// isn't guaranteed to happen immediately but it will happen as soon as /// practical. diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index b56f9825a..a33c911b7 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -18,6 +18,7 @@ const trace = @import("tracy").trace; const math = @import("../math.zig"); const lru = @import("../lru.zig"); const DevMode = @import("../DevMode.zig"); +const Window = @import("../Window.zig"); const log = std.log.scoped(.grid); @@ -80,6 +81,9 @@ focused: bool, /// Padding options padding: renderer.Options.Padding, +/// The mailbox for communicating with the window. +window_mailbox: Window.Mailbox, + /// The raw structure that maps directly to the buffer sent to the vertex shader. /// This must be "extern" so that the field order is not reordered by the /// Zig compiler. @@ -293,6 +297,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !OpenGL { .foreground = .{ .r = 255, .g = 255, .b = 255 }, .focused = true, .padding = options.padding, + .window_mailbox = options.window_mailbox, }; } @@ -476,8 +481,12 @@ pub fn setFontSize(self: *OpenGL, size: font.face.DesiredSize) !void { // nothing since the grid size couldn't have possibly changed. const new_cell_size = .{ .width = metrics.cell_width, .height = metrics.cell_height }; if (std.meta.eql(self.cell_size, new_cell_size)) return; + self.cell_size = new_cell_size; // Notify the window that the cell size changed. + _ = self.window_mailbox.push(.{ + .cell_size = new_cell_size, + }, .{ .forever = {} }); } /// Reload the font metrics, recalculate cell size, and send that all diff --git a/src/window/message.zig b/src/window/message.zig index 9f74f4e60..ae14ffcdf 100644 --- a/src/window/message.zig +++ b/src/window/message.zig @@ -9,6 +9,9 @@ pub const Message = union(enum) { /// the termio message so that we can more efficiently send strings /// of any length set_title: [256]u8, + + /// Change the cell size. + cell_size: renderer.CellSize, }; /// A window mailbox.