From 9d0e7ab138fad23422f98b73e1e90a611f9c3763 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 29 Sep 2023 20:18:41 -0700 Subject: [PATCH] input: binding set can track unconsumed triggers --- src/input/Binding.zig | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 81d22327b..ccaee06b6 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -372,6 +372,13 @@ pub const Set = struct { std.hash_map.default_max_load_percentage, ); + const UnconsumedMap = std.HashMapUnmanaged( + Trigger, + void, + Context(Trigger), + std.hash_map.default_max_load_percentage, + ); + /// The set of bindings. bindings: HashMap = .{}, @@ -380,9 +387,23 @@ pub const Set = struct { /// the most recently added binding for an action. reverse: ReverseMap = .{}, + /// The map of triggers that explicitly do not want to be consumed + /// when matched. A trigger is "consumed" when it is not further + /// processed and potentially sent to the terminal. An "unconsumed" + /// trigger will perform both its action and also continue normal + /// encoding processing (if any). + /// + /// This is stored as a separate map since unconsumed triggers are + /// rare and we don't want to bloat our map with a byte per entry + /// (for boolean state) when most entries will be consumed. + /// + /// Assert: trigger in this map is also in bindings. + unconsumed: UnconsumedMap = .{}, + pub fn deinit(self: *Set, alloc: Allocator) void { self.bindings.deinit(alloc); self.reverse.deinit(alloc); + self.unconsumed.deinit(alloc); self.* = undefined; } @@ -393,11 +414,36 @@ pub const Set = struct { alloc: Allocator, t: Trigger, action: Action, + ) Allocator.Error!void { + try self.put_(alloc, t, action, true); + } + + /// Same as put but marks the trigger as unconsumed. An unconsumed + /// trigger will evaluate the action and continue to encode for the + /// terminal. + /// + /// This is a separate function because this case is rare. + pub fn putUnconsumed( + self: *Set, + alloc: Allocator, + t: Trigger, + action: Action, + ) Allocator.Error!void { + try self.put_(alloc, t, action, false); + } + + fn put_( + self: *Set, + alloc: Allocator, + t: Trigger, + action: Action, + consumed: bool, ) Allocator.Error!void { // unbind should never go into the set, it should be handled prior assert(action != .unbind); const gop = try self.bindings.getOrPut(alloc, t); + if (!consumed) try self.unconsumed.put(alloc, t, {}); // If we have an existing binding for this trigger, we have to // update the reverse mapping to remove the old action. @@ -410,6 +456,9 @@ pub const Set = struct { break :it; } } + + // We also have to remove the unconsumed state if it exists. + if (consumed) _ = self.unconsumed.remove(t); } gop.value_ptr.* = action; @@ -429,10 +478,18 @@ pub const Set = struct { return self.reverse.get(a); } + /// Returns true if the given trigger should be consumed. Requires + /// that trigger is in the set to be valid so this should only follow + /// a non-null get. + pub fn getConsumed(self: Set, t: Trigger) bool { + return self.unconsumed.get(t) == null; + } + /// Remove a binding for a given trigger. pub fn remove(self: *Set, t: Trigger) void { const action = self.bindings.get(t) orelse return; _ = self.bindings.remove(t); + _ = self.unconsumed.remove(t); // Look for a matching action in bindings and use that. // Note: we'd LIKE to replace this with the most recent binding but @@ -654,3 +711,20 @@ test "set: overriding a mapping updates reverse" { try testing.expect(trigger == null); } } + +test "set: consumed state" { + const testing = std.testing; + const alloc = testing.allocator; + + var s: Set = .{}; + defer s.deinit(alloc); + + try s.put(alloc, .{ .key = .a }, .{ .new_window = {} }); + try testing.expect(s.getConsumed(.{ .key = .a })); + + try s.putUnconsumed(alloc, .{ .key = .a }, .{ .new_window = {} }); + try testing.expect(!s.getConsumed(.{ .key = .a })); + + try s.put(alloc, .{ .key = .a }, .{ .new_window = {} }); + try testing.expect(s.getConsumed(.{ .key = .a })); +}