From 6530a7a51c7b27e5f7e4cc7bc18b71126e727d48 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 14 Jan 2025 18:53:53 +0100 Subject: [PATCH] feat(win32 gui): add reset_fontsize and rest_fontface commands --- src/keybind/builtin/emacs.json | 1 + src/keybind/builtin/flow.json | 1 + src/renderer/win32/renderer.zig | 10 +++++++++ src/tui/mainview.zig | 12 +++++++++++ src/win32/gui.zig | 38 ++++++++++++++++++++++++++++++++- 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/keybind/builtin/emacs.json b/src/keybind/builtin/emacs.json index 724557b..be5f851 100644 --- a/src/keybind/builtin/emacs.json +++ b/src/keybind/builtin/emacs.json @@ -1,6 +1,7 @@ { "project": { "press": [ + ["ctrl+0", "reset_fontsize"], ["ctrl+=", "adjust_fontsize", 1.0], ["ctrl+-", "adjust_fontsize", -1.0] ] diff --git a/src/keybind/builtin/flow.json b/src/keybind/builtin/flow.json index c7d953f..f4aa868 100644 --- a/src/keybind/builtin/flow.json +++ b/src/keybind/builtin/flow.json @@ -1,6 +1,7 @@ { "project": { "press": [ + ["ctrl+0", "reset_fontsize"], ["ctrl+plus", "adjust_fontsize", 1.0], ["ctrl+minus", "adjust_fontsize", -1.0], ["f5", ["create_scratch_buffer", "*test*"], ["shell_execute_insert", "zig", "build", "test"]], diff --git a/src/renderer/win32/renderer.zig b/src/renderer/win32/renderer.zig index 7d0f066..a455218 100644 --- a/src/renderer/win32/renderer.zig +++ b/src/renderer/win32/renderer.zig @@ -381,11 +381,21 @@ pub fn set_fontsize(self: *Self, fontsize: f32) void { gui.set_fontsize(hwnd, fontsize); } +pub fn reset_fontsize(self: *Self) void { + const hwnd = self.hwnd orelse return; + gui.reset_fontsize(hwnd); +} + pub fn set_fontface(self: *Self, fontface: []const u8) void { const hwnd = self.hwnd orelse return; gui.set_fontface(hwnd, fontface); } +pub fn reset_fontface(self: *Self) void { + const hwnd = self.hwnd orelse return; + gui.reset_fontface(hwnd); +} + pub fn set_terminal_cursor_color(self: *Self, color: Color) void { _ = self; _ = color; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 52ac37c..3076ada 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -653,6 +653,12 @@ const cmds = struct { } pub const set_fontsize_meta = .{ .arguments = &.{.float} }; + pub fn reset_fontsize(_: *Self, _: Ctx) Result { + if (build_options.gui) + tui.current().rdr.reset_fontsize(); + } + pub const reset_fontsize_meta = .{ .description = "Reset font to configured size" }; + pub fn set_fontface(_: *Self, ctx: Ctx) Result { var fontface: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&fontface)})) @@ -661,6 +667,12 @@ const cmds = struct { tui.current().rdr.set_fontface(fontface); } pub const set_fontface_meta = .{ .arguments = &.{.float} }; + + pub fn reset_fontface(_: *Self, _: Ctx) Result { + if (build_options.gui) + tui.current().rdr.reset_fontface(); + } + pub const reset_fontface_meta = .{ .description = "Reset font to configured face" }; }; pub fn handle_editor_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result { diff --git a/src/win32/gui.zig b/src/win32/gui.zig index e880972..826b16a 100644 --- a/src/win32/gui.zig +++ b/src/win32/gui.zig @@ -28,13 +28,17 @@ const WM_APP_SET_BACKGROUND = win32.WM_APP + 2; const WM_APP_ADJUST_FONTSIZE = win32.WM_APP + 3; const WM_APP_SET_FONTSIZE = win32.WM_APP + 4; const WM_APP_SET_FONTFACE = win32.WM_APP + 5; -const WM_APP_UPDATE_SCREEN = win32.WM_APP + 6; +const WM_APP_RESET_FONTSIZE = win32.WM_APP + 6; +const WM_APP_RESET_FONTFACE = win32.WM_APP + 7; +const WM_APP_UPDATE_SCREEN = win32.WM_APP + 8; const WM_APP_EXIT_RESULT = 0x45feaa11; const WM_APP_SET_BACKGROUND_RESULT = 0x369a26cd; const WM_APP_ADJUST_FONTSIZE_RESULT = 0x79aba9ef; const WM_APP_SET_FONTSIZE_RESULT = 0x72fa44bc; const WM_APP_SET_FONTFACE_RESULT = 0x1a49ffa8; +const WM_APP_RESET_FONTSIZE_RESULT = 0x082c4c0c; +const WM_APP_RESET_FONTFACE_RESULT = 0x0101f996; const WM_APP_UPDATE_SCREEN_RESULT = 0x3add213b; pub const DropWriter = struct { @@ -453,6 +457,15 @@ pub fn set_fontsize(hwnd: win32.HWND, fontsize: f32) void { )); } +pub fn reset_fontsize(hwnd: win32.HWND) void { + std.debug.assert(WM_APP_RESET_FONTSIZE_RESULT == win32.SendMessageW( + hwnd, + WM_APP_RESET_FONTSIZE, + 0, + 0, + )); +} + pub fn set_fontface(hwnd: win32.HWND, fontface_utf8: []const u8) void { const fontface = FontFace.initUtf8(fontface_utf8) catch |e| { std.log.err("failed to set fontface '{s}' with {s}", .{ fontface_utf8, @errorName(e) }); @@ -466,6 +479,15 @@ pub fn set_fontface(hwnd: win32.HWND, fontface_utf8: []const u8) void { )); } +pub fn reset_fontface(hwnd: win32.HWND) void { + std.debug.assert(WM_APP_RESET_FONTFACE_RESULT == win32.SendMessageW( + hwnd, + WM_APP_RESET_FONTFACE, + 0, + 0, + )); +} + pub fn updateScreen(hwnd: win32.HWND, screen: *const vaxis.Screen) void { std.debug.assert(WM_APP_UPDATE_SCREEN_RESULT == win32.SendMessageW( hwnd, @@ -1085,6 +1107,13 @@ fn WndProc( win32.invalidateHwnd(hwnd); return WM_APP_SET_FONTSIZE_RESULT; }, + WM_APP_RESET_FONTSIZE => { + const state = stateFromHwnd(hwnd); + global.fontsize = null; + updateWindowSize(hwnd, win32.WMSZ_BOTTOMRIGHT, &state.bounds); + win32.invalidateHwnd(hwnd); + return WM_APP_SET_FONTSIZE_RESULT; + }, WM_APP_SET_FONTFACE => { const state = stateFromHwnd(hwnd); const fontface: *FontFace = @ptrFromInt(wparam); @@ -1093,6 +1122,13 @@ fn WndProc( win32.invalidateHwnd(hwnd); return WM_APP_SET_FONTFACE_RESULT; }, + WM_APP_RESET_FONTFACE => { + const state = stateFromHwnd(hwnd); + global.fontface = null; + updateWindowSize(hwnd, win32.WMSZ_BOTTOMRIGHT, &state.bounds); + win32.invalidateHwnd(hwnd); + return WM_APP_SET_FONTFACE_RESULT; + }, WM_APP_UPDATE_SCREEN => { const screen: *const vaxis.Screen = @ptrFromInt(wparam); _ = global.screen_arena.reset(.retain_capacity);