From 9944f5d34dfb842a9ba6b33ce1ff040c1e738ae2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Aug 2024 20:00:12 -0700 Subject: [PATCH] input: Binding.Set.clone handles leaders --- src/config/Config.zig | 8 +------- src/input/Binding.zig | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index ca53468fe..e0ed6fcc8 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -3317,13 +3317,7 @@ pub const Keybinds = struct { /// Deep copy of the struct. Required by Config. pub fn clone(self: *const Keybinds, alloc: Allocator) !Keybinds { - // TODO: leaders - return .{ - .set = .{ - .bindings = try self.set.bindings.clone(alloc), - .reverse = try self.set.reverse.clone(alloc), - }, - }; + return .{ .set = try self.set.clone(alloc) }; } /// Compare if two of our value are requal. Required by Config. diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 7bf74292a..41b862eeb 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -1089,6 +1089,32 @@ pub const Set = struct { } } + /// Deep clone the set. + pub fn clone(self: *const Set, alloc: Allocator) !Set { + var result: Set = .{ + .bindings = try self.bindings.clone(alloc), + .reverse = try self.reverse.clone(alloc), + }; + + // If we have any leaders we need to clone them. + var it = result.bindings.iterator(); + while (it.next()) |entry| switch (entry.value_ptr.*) { + // No data to clone + .action, .action_unconsumed => {}, + + // Must be deep cloned. + .leader => |*s| { + const ptr = try alloc.create(Set); + errdefer alloc.destroy(ptr); + ptr.* = try s.*.clone(alloc); + errdefer ptr.deinit(alloc); + s.* = ptr; + }, + }; + + return result; + } + /// The hash map context for the set. This defines how the hash map /// gets the hash key and checks for equality. fn Context(comptime KeyType: type) type {