From f55d71a7cba0adabe38e34508bd5431e06e0c485 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Tue, 18 Feb 2025 19:09:03 +0100 Subject: [PATCH] mod(mouse): fix `input.Mouse.in` method Pass through mouse events where they actually match the corresponding `Container`. Adopt mouse event accordingly in `Scrollable` `Element` trait when passing through the mouse event to the scrollable `Container`. --- src/container.zig | 4 ++++ src/element.zig | 11 +++++++++++ src/input.zig | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/container.zig b/src/container.zig index 6ccda5c..2bcc0dc 100644 --- a/src/container.zig +++ b/src/container.zig @@ -387,6 +387,10 @@ pub fn Container(comptime Event: type) type { try element.handle(.{ .resize = element_size }); } }, + .mouse => |mouse| if (mouse.in(this.size)) { + try this.element.handle(event); + for (this.elements.items) |*element| try element.handle(event); + }, else => { try this.element.handle(event); for (this.elements.items) |*element| try element.handle(event); diff --git a/src/element.zig b/src/element.zig index 84f7769..b11daa4 100644 --- a/src/element.zig +++ b/src/element.zig @@ -112,6 +112,17 @@ pub fn Scrollable(Event: type) type { // TODO: not just pass through the given size, but rather the size that is necessary for scrollable content try this.container.handle(.{ .resize = size }); }, + .mouse => |mouse| { + std.log.debug("mouse event detected in scrollable element {any}", .{mouse.in(this.size)}); + try this.container.handle(.{ + .mouse = .{ + .col = mouse.col + this.anchor.col, + .row = mouse.row + this.anchor.row, + .button = mouse.button, + .kind = mouse.kind, + }, + }); + }, else => try this.container.handle(event), } } diff --git a/src/input.zig b/src/input.zig index fb58dcc..b7ba246 100644 --- a/src/input.zig +++ b/src/input.zig @@ -36,8 +36,8 @@ pub const Mouse = packed struct { } pub fn in(this: @This(), size: Size) bool { - return this.col >= size.anchor.col and this.col <= size.cols -| size.anchor.col and - this.row >= size.anchor.row and this.row <= size.rows -| size.anchor.row; + return this.col >= size.anchor.col and this.col <= size.cols + size.anchor.col and + this.row >= size.anchor.row and this.row <= size.rows + size.anchor.row; } };