this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(vxfw): reset event context after ticks

authored by

TimBot and committed by
Tim Culverhouse
cac2f799 f3b69d1d

+63 -5
+63 -5
src/vxfw/App.zig
··· 128 128 try loop.queue.lock(); 129 129 defer loop.queue.unlock(); 130 130 while (loop.queue.drain()) |event| { 131 - defer { 132 - // Reset our context 133 - ctx.consume_event = false; 134 - ctx.phase = .capturing; 135 - } 131 + defer resetEventState(&ctx); 136 132 switch (event) { 137 133 .key_press => { 138 134 try focus_handler.handleEvent(&ctx, event); ··· 296 292 } 297 293 } 298 294 295 + fn resetEventState(ctx: *vxfw.EventContext) void { 296 + ctx.consume_event = false; 297 + ctx.phase = .capturing; 298 + } 299 + 299 300 fn checkTimers(self: *App, ctx: *vxfw.EventContext) anyerror!void { 300 301 const now: std.Io.Timestamp = .now(self.io, .awake); 301 302 ··· 307 308 try self.timers.append(self.allocator, tick); 308 309 break; 309 310 } 311 + resetEventState(ctx); 312 + ctx.phase = .at_target; 310 313 try tick.widget.handleEvent(ctx, .tick); 314 + resetEventState(ctx); 311 315 } 312 316 try self.handleCommand(&ctx.cmds); 313 317 } ··· 604 608 } 605 609 } 606 610 }; 611 + 612 + test "timer consume does not leak to the next event" { 613 + const testing = std.testing; 614 + 615 + const TestWidget = struct { 616 + fn handle(_: *anyopaque, ctx: *vxfw.EventContext, event: vxfw.Event) anyerror!void { 617 + if (event == .tick) ctx.consumeAndRedraw(); 618 + } 619 + 620 + fn draw(_: *anyopaque, _: vxfw.DrawContext) Allocator.Error!vxfw.Surface { 621 + unreachable; 622 + } 623 + }; 624 + 625 + var app: App = .{ 626 + .io = testing.io, 627 + .allocator = testing.allocator, 628 + .tty = undefined, 629 + .vx = undefined, 630 + .timers = .empty, 631 + .wants_focus = null, 632 + }; 633 + defer app.timers.deinit(testing.allocator); 634 + 635 + var userdata: u8 = 0; 636 + const widget: vxfw.Widget = .{ 637 + .userdata = &userdata, 638 + .eventHandler = TestWidget.handle, 639 + .drawFn = TestWidget.draw, 640 + }; 641 + 642 + const now: std.Io.Timestamp = .now(testing.io, .awake); 643 + try app.timers.append(testing.allocator, .{ 644 + .deadline = now.addDuration(.fromMilliseconds(-1)), 645 + .widget = widget, 646 + }); 647 + 648 + var ctx: vxfw.EventContext = .{ 649 + .io = testing.io, 650 + .alloc = testing.allocator, 651 + .phase = .capturing, 652 + .cmds = .empty, 653 + .consume_event = false, 654 + .redraw = false, 655 + .quit = false, 656 + }; 657 + defer ctx.cmds.deinit(testing.allocator); 658 + 659 + try app.checkTimers(&ctx); 660 + 661 + try testing.expect(ctx.redraw); 662 + try testing.expect(!ctx.consume_event); 663 + try testing.expectEqual(vxfw.EventContext.Phase.capturing, ctx.phase); 664 + } 607 665 608 666 test { 609 667 std.testing.refAllDecls(@This());