From dad49239015c3793e409e637016af01774eaa4a9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 15 Nov 2022 20:10:50 -0800 Subject: [PATCH] hook up all the keyboard actions --- src/Window.zig | 52 +++++++++++++++++++++++++++++++++++++++++ src/config.zig | 17 ++++++++++++++ src/input/Binding.zig | 7 ++++++ src/input/key.zig | 14 +++++++++++ src/renderer/OpenGL.zig | 7 +++++- 5 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/Window.zig b/src/Window.zig index 640270956..7e26fad68 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -49,6 +49,7 @@ app: *App, /// The font structures font_lib: font.Library, font_group: *font.GroupCache, +font_size: font.face.DesiredSize, /// The glfw window handle. window: glfw.Window, @@ -361,6 +362,7 @@ pub fn create(alloc: Allocator, app: *App, config: *const Config) !*Window { .app = app, .font_lib = font_lib, .font_group = font_group, + .font_size = font_size, .window = window, .cursor = cursor, .renderer = renderer_impl, @@ -555,6 +557,20 @@ fn setCellSize(self: *Window, size: renderer.CellSize) !void { self.io_thread.wakeup.send() catch {}; } +/// Change the font size. +fn setFontSize(self: *Window, size: font.face.DesiredSize) void { + // Update our font size so future changes work + self.font_size = size; + + // Notify our render thread of the font size. This triggers everything else. + _ = self.renderer_thread.mailbox.push(.{ + .font_size = size, + }, .{ .forever = {} }); + + // Schedule render which also drains our mailbox + self.queueRender() catch unreachable; +} + /// 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. @@ -760,6 +776,16 @@ fn keyCallback( .x => .x, .y => .y, .z => .z, + .zero => .zero, + .one => .one, + .two => .three, + .three => .four, + .four => .four, + .five => .five, + .six => .six, + .seven => .seven, + .eight => .eight, + .nine => .nine, .up => .up, .down => .down, .right => .right, @@ -782,6 +808,8 @@ fn keyCallback( .F11 => .f11, .F12 => .f12, .grave_accent => .grave_accent, + .minus => .minus, + .equal => .equal, else => .invalid, }, }; @@ -858,6 +886,30 @@ fn keyCallback( } }, + .increase_font_size => |delta| { + log.debug("increase font size={}", .{delta}); + + var size = win.font_size; + size.points +|= delta; + win.setFontSize(size); + }, + + .decrease_font_size => |delta| { + log.debug("decrease font size={}", .{delta}); + + var size = win.font_size; + size.points = @max(1, size.points -| delta); + win.setFontSize(size); + }, + + .reset_font_size => { + log.debug("reset font size", .{}); + + var size = win.font_size; + size.points = win.config.@"font-size"; + win.setFontSize(size); + }, + .toggle_dev_mode => if (DevMode.enabled) { DevMode.instance.visible = !DevMode.instance.visible; win.queueRender() catch unreachable; diff --git a/src/config.zig b/src/config.zig index d7a60753e..2d94fc42c 100644 --- a/src/config.zig +++ b/src/config.zig @@ -176,6 +176,23 @@ pub const Config = struct { try result.keybind.set.put(alloc, .{ .key = .f11 }, .{ .csi = "23~" }); try result.keybind.set.put(alloc, .{ .key = .f12 }, .{ .csi = "24~" }); + // Fonts + try result.keybind.set.put( + alloc, + .{ .key = .equal, .mods = .{ .super = true } }, + .{ .increase_font_size = 1 }, + ); + try result.keybind.set.put( + alloc, + .{ .key = .minus, .mods = .{ .super = true } }, + .{ .decrease_font_size = 1 }, + ); + try result.keybind.set.put( + alloc, + .{ .key = .zero, .mods = .{ .super = true } }, + .{ .reset_font_size = {} }, + ); + // Dev Mode try result.keybind.set.put( alloc, diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 81cc18fdc..8b909f7ad 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -131,6 +131,13 @@ pub const Action = union(enum) { copy_to_clipboard: void, paste_from_clipboard: void, + /// Increase/decrease the font size by a certain amount + increase_font_size: u16, + decrease_font_size: u16, + + /// Reset the font size to the original configured size + reset_font_size: void, + /// Dev mode toggle_dev_mode: void, diff --git a/src/input/key.zig b/src/input/key.zig index 8445547b4..da640ccf1 100644 --- a/src/input/key.zig +++ b/src/input/key.zig @@ -49,8 +49,22 @@ pub const Key = enum { y, z, + // numbers + zero, + one, + two, + three, + four, + five, + six, + seven, + eight, + nine, + // other grave_accent, // ` + minus, + equal, // control up, diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index a33c911b7..3fcd7a7b9 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -320,6 +320,9 @@ pub fn deinit(self: *OpenGL) void { } fn resetCellsLRU(self: *OpenGL) void { + // Preserve the old capacity so that we have space in our LRU + const cap = self.cells_lru.capacity; + // Our LRU values are array lists so we need to deallocate those first var it = self.cells_lru.queue.first; while (it) |node| { @@ -329,7 +332,7 @@ fn resetCellsLRU(self: *OpenGL) void { self.cells_lru.deinit(self.alloc); // Initialize our new LRU - self.cells_lru = CellsLRU.init(0); + self.cells_lru = CellsLRU.init(cap); } /// Returns the hints that we want for this @@ -468,6 +471,8 @@ pub fn blinkCursor(self: *OpenGL, reset: bool) void { /// /// Must be called on the render thread. pub fn setFontSize(self: *OpenGL, size: font.face.DesiredSize) !void { + log.info("set font size={}", .{size}); + // Set our new size, this will also reset our font atlas. try self.font_group.setSize(size);