core: font size changes work

This commit is contained in:
Mitchell Hashimoto
2024-04-06 19:33:49 -07:00
parent f51dad445f
commit 506ba854fa
6 changed files with 24 additions and 42 deletions

View File

@ -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 => {

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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.

View File

@ -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.

View File

@ -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| {