From 97db055b545cdcf4280e0c94c3abb6fc7f6a3693 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Sun, 11 Aug 2024 17:47:21 -0500 Subject: [PATCH] fix(surface): account for padding in mouse pixel reports Padding was accounted for in cell reports, but not pixel reports. Update inspector to report the pixel coordinates the terminal reports. --- src/Surface.zig | 31 +++++++++++++++++++------------ src/inspector/Inspector.zig | 5 +++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 6aa39142f..2e283f985 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -2086,14 +2086,15 @@ fn mouseReport( .sgr_pixels => { // Final character to send in the CSI const final: u8 = if (action == .release) 'm' else 'M'; + const adjusted = self.posAdjusted(pos.x, pos.y); // Response always is at least 4 chars, so this leaves the // remainder for numbers which are very large... var data: termio.Message.WriteReq.Small.Array = undefined; const resp = try std.fmt.bufPrint(&data, "\x1B[<{d};{d};{d}{c}", .{ button_code, - @as(i32, @intFromFloat(@round(pos.x))), - @as(i32, @intFromFloat(@round(pos.y))), + @as(i32, @intFromFloat(@round(adjusted.x))), + @as(i32, @intFromFloat(@round(adjusted.y))), final, }); @@ -3104,34 +3105,40 @@ pub fn colorSchemeCallback(self: *Surface, scheme: apprt.ColorScheme) !void { if (report) try self.reportColorScheme(); } -pub fn posToViewport(self: Surface, xpos: f64, ypos: f64) terminal.point.Coordinate { - // xpos/ypos need to be adjusted for window padding - // (i.e. "window-padding-*" settings. +pub fn posAdjusted(self: Surface, xpos: f64, ypos: f64) struct { x: f64, y: f64 } { const pad = if (self.config.window_padding_balance) renderer.Padding.balanced(self.screen_size, self.grid_size, self.cell_size) else self.padding; - const xpos_adjusted: f64 = xpos - @as(f64, @floatFromInt(pad.left)); - const ypos_adjusted: f64 = ypos - @as(f64, @floatFromInt(pad.top)); + return .{ + .x = xpos - @as(f64, @floatFromInt(pad.left)), + .y = ypos - @as(f64, @floatFromInt(pad.top)), + }; +} - // xpos and ypos can be negative if while dragging, the user moves the +pub fn posToViewport(self: Surface, xpos: f64, ypos: f64) terminal.point.Coordinate { + // xpos/ypos need to be adjusted for window padding + // (i.e. "window-padding-*" settings. + const adjusted = self.posAdjusted(xpos, ypos); + + // adjusted.x and adjusted.y can be negative if while dragging, the user moves the // mouse off the surface. Likewise, they can be larger than our surface // width if the user drags out of the surface positively. return .{ - .x = if (xpos_adjusted < 0) 0 else x: { + .x = if (adjusted.x < 0) 0 else x: { // Our cell is the mouse divided by cell width const cell_width: f64 = @floatFromInt(self.cell_size.width); - const x: usize = @intFromFloat(xpos_adjusted / cell_width); + const x: usize = @intFromFloat(adjusted.x / cell_width); // Can be off the screen if the user drags it out, so max // it out on our available columns break :x @min(x, self.grid_size.columns - 1); }, - .y = if (ypos_adjusted < 0) 0 else y: { + .y = if (adjusted.y < 0) 0 else y: { const cell_height: f64 = @floatFromInt(self.cell_size.height); - const y: usize = @intFromFloat(ypos_adjusted / cell_height); + const y: usize = @intFromFloat(adjusted.y / cell_height); break :y @min(y, self.grid_size.rows - 1); }, }; diff --git a/src/inspector/Inspector.zig b/src/inspector/Inspector.zig index 755a69618..1de666b09 100644 --- a/src/inspector/Inspector.zig +++ b/src/inspector/Inspector.zig @@ -784,6 +784,7 @@ fn renderSizeWindow(self: *Inspector) void { } { + const adjusted = self.surface.posAdjusted(self.mouse.last_xpos, self.mouse.last_ypos); cimgui.c.igTableNextRow(cimgui.c.ImGuiTableRowFlags_None, 0); { _ = cimgui.c.igTableSetColumnIndex(0); @@ -793,8 +794,8 @@ fn renderSizeWindow(self: *Inspector) void { _ = cimgui.c.igTableSetColumnIndex(1); cimgui.c.igText( "(%dpx, %dpx)", - @as(u32, @intFromFloat(self.mouse.last_xpos)), - @as(u32, @intFromFloat(self.mouse.last_ypos)), + @as(i64, @intFromFloat(adjusted.x)), + @as(i64, @intFromFloat(adjusted.y)), ); } }