feat: add drag_pos and drag_anchor to Button

This commit is contained in:
CJ van den Berg 2025-10-24 16:18:57 +02:00
parent 752a356d38
commit 0493d3899a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -77,6 +77,8 @@ fn State(ctx_type: type) type {
plane: Plane, plane: Plane,
active: bool = false, active: bool = false,
hover: bool = false, hover: bool = false,
drag_anchor: ?Widget.Pos = null,
drag_pos: ?Widget.Pos = null,
opts: Options(ctx_type), opts: Options(ctx_type),
const Self = @This(); const Self = @This();
@ -120,6 +122,7 @@ fn State(ctx_type: type) type {
switch (btn_enum) { switch (btn_enum) {
input.mouse.BUTTON1 => { input.mouse.BUTTON1 => {
self.active = true; self.active = true;
self.drag_anchor = self.to_rel_cursor(x, y);
tui.need_render(); tui.need_render();
}, },
input.mouse.BUTTON4, input.mouse.BUTTON5 => { input.mouse.BUTTON4, input.mouse.BUTTON5 => {
@ -131,9 +134,12 @@ fn State(ctx_type: type) type {
return true; return true;
} else if (try m.match(.{ "B", input.event.release, tp.extract(&btn), tp.any, tp.extract(&x), tp.extract(&y), tp.any, tp.any })) { } else if (try m.match(.{ "B", input.event.release, tp.extract(&btn), tp.any, tp.extract(&x), tp.extract(&y), tp.any, tp.any })) {
self.call_click_handler(@enumFromInt(btn), self.to_rel_cursor(x, y)); self.call_click_handler(@enumFromInt(btn), self.to_rel_cursor(x, y));
self.drag_anchor = null;
self.drag_pos = null;
tui.need_render(); tui.need_render();
return true; return true;
} else if (try m.match(.{ "D", input.event.press, tp.extract(&btn), tp.more })) { } else if (try m.match(.{ "D", input.event.press, tp.extract(&btn), tp.any, tp.extract(&x), tp.extract(&y), tp.any, tp.any })) {
self.drag_pos = .{ .x = x, .y = y };
if (self.opts.on_event) |h| { if (self.opts.on_event) |h| {
self.active = false; self.active = false;
h.send(from, m) catch {}; h.send(from, m) catch {};
@ -145,6 +151,8 @@ fn State(ctx_type: type) type {
h.send(from, m) catch {}; h.send(from, m) catch {};
} }
self.call_click_handler(@enumFromInt(btn), self.to_rel_cursor(x, y)); self.call_click_handler(@enumFromInt(btn), self.to_rel_cursor(x, y));
self.drag_anchor = null;
self.drag_pos = null;
tui.need_render(); tui.need_render();
return true; return true;
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) { } else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
@ -152,6 +160,8 @@ fn State(ctx_type: type) type {
tui.need_render(); tui.need_render();
return true; return true;
} }
self.drag_anchor = null;
self.drag_pos = null;
return self.opts.on_receive(&self.opts.ctx, self, from, m); return self.opts.on_receive(&self.opts.ctx, self, from, m);
} }