//! Mouse input primitives. //! //! Port of ultraviolet's `mouse.go` (which aliases `github.com/charmbracelet/x/ansi` //! `MouseButton`). Mouse coordinates are zero-based (0,0) at the upper-left; //! with `sgr_pixel` encoding they are pixel coordinates. const std = @import("std"); const ir = @import("root.zig"); const Key = ir.Key; const t = std.testing; const Mouse = @This(); x: i32 = 0, y: i32 = 0, button: Button = .none, mod: Key.Mod = .{}, /// String representation (ctrl+/alt+/shift+ prefix + button name). pub fn string(self: Mouse) []const u8 { var s: [64]u8 = undefined; var n: usize = 0; if (self.mod.ctrl) { @memcpy(s[n .. n + 5], "ctrl+"); n += 5; } if (self.mod.alt) { @memcpy(s[n .. n + 4], "alt+"); n += 4; } if (self.mod.shift) { @memcpy(s[n .. n + 6], "shift+"); n += 6; } const str = self.button.string(); if (str.len == 0) { @memcpy(s[n .. n + 7], "unknown"); n += 7; } else if (!std.mem.eql(u8, str, "none")) { @memcpy(s[n .. n + str.len], str); n += str.len; } return s[0..n]; } /// Mouse tracking mode for the terminal. const Mode = enum(u8) { /// Disable mouse tracking. none, /// Press only (DEC mode 9). press, /// Click tracking (DEC mode 1000). click, /// Drag tracking (DEC mode 1002). drag, /// Motion tracking (DEC mode 1003). motion, }; /// Mouse encoding used for events. const Encoding = enum(u8) { /// Legacy X10-compatible encoding (coords limited to 223). legacy, /// SGR encoding (DEC mode 1006). sgr, /// SGR-pixel encoding (DEC mode 1016). sgr_pixel, }; /// Mouse button. Based on X11 button codes. Mirrors `ansi.MouseButton`. pub const Button = enum(u8) { none = 0, button1 = 1, button2 = 2, button3 = 3, button4 = 4, button5 = 5, button6 = 6, button7 = 7, button8 = 8, button9 = 9, button10 = 10, button11 = 11, // Named aliases. pub const left: Button = .button1; pub const middle: Button = .button2; pub const right: Button = .button3; pub const wheel_up: Button = .button4; pub const wheel_down: Button = .button5; pub const wheel_left: Button = .button6; pub const wheel_right: Button = .button7; pub const backward: Button = .button8; pub const forward: Button = .button9; pub const release: Button = .none; pub fn string(self: Button) []const u8 { return switch (self) { .none => "none", .button1 => "left", .button2 => "middle", .button3 => "right", .button4 => "wheelup", .button5 => "wheeldown", .button6 => "wheelleft", .button7 => "wheelright", .button8 => "backward", .button9 => "forward", .button10 => "button10", .button11 => "button11", }; } /// Whether the button is a wheel button. pub fn isWheel(self: Button) bool { const v = @backingInt(self); return v >= @backingInt(Button.wheel_up) and v <= @backingInt(Button.wheel_right); } }; /// Converts a pixel-coordinate mouse event to cell coordinates based on the /// terminal's reported pixel and cell dimensions. pub fn mousePixelToCell(self: Mouse, ws_col: usize, ws_row: usize, ws_xpixel: usize, ws_ypixel: usize) Mouse { var col: i32 = 0; var row: i32 = 0; if (ws_xpixel > 0) col = @divTrunc(self.x * @as(i32, @intCast(ws_col)), @as(i32, @intCast(ws_xpixel))); if (ws_ypixel > 0) row = @divTrunc(self.y * @as(i32, @intCast(ws_row)), @as(i32, @intCast(ws_ypixel))); return .{ .x = col, .y = row, .button = self.button, .mod = self.mod }; } test "mouse button strings" { try t.expectEqualStrings("left", Button.left.string()); try t.expectEqualStrings("wheelup", Button.wheel_up.string()); try t.expect(Button.wheel_right.isWheel()); try t.expect(!Button.left.isWheel()); }