rename some key sequence state so it is clearer what it is

This commit is contained in:
Mitchell Hashimoto
2025-12-20 13:46:00 -08:00
parent c53b3fffd5
commit 8c59143c1a

View File

@@ -253,18 +253,9 @@ const Mouse = struct {
/// Keyboard state for the surface.
pub const Keyboard = struct {
/// The currently active keybindings for the surface. This is used to
/// implement sequences: as leader keys are pressed, the active bindings
/// set is updated to reflect the current leader key sequence. If this is
/// null then the root bindings are used.
bindings: ?*const input.Binding.Set = null,
/// The last handled binding. This is used to prevent encoding release
/// events for handled bindings. We only need to keep track of one because
/// at least at the time of writing this, its impossible for two keys of
/// a combination to be handled by different bindings before the release
/// of the prior (namely since you can't bind modifier-only).
last_trigger: ?u64 = null,
/// The currently active key sequence for the surface. If this is null
/// then we're not currently in a key sequence.
sequence_set: ?*const input.Binding.Set = null,
/// The queued keys when we're in the middle of a sequenced binding.
/// These are flushed when the sequence is completed and unconsumed or
@@ -272,7 +263,14 @@ pub const Keyboard = struct {
///
/// This is naturally bounded due to the configuration maximum
/// length of a sequence.
queued: std.ArrayListUnmanaged(termio.Message.WriteReq) = .{},
sequence_queued: std.ArrayListUnmanaged(termio.Message.WriteReq) = .empty,
/// The last handled binding. This is used to prevent encoding release
/// events for handled bindings. We only need to keep track of one because
/// at least at the time of writing this, its impossible for two keys of
/// a combination to be handled by different bindings before the release
/// of the prior (namely since you can't bind modifier-only).
last_trigger: ?u64 = null,
};
/// The configuration that a surface has, this is copied from the main
@@ -793,8 +791,8 @@ pub fn deinit(self: *Surface) void {
}
// Clean up our keyboard state
for (self.keyboard.queued.items) |req| req.deinit();
self.keyboard.queued.deinit(self.alloc);
for (self.keyboard.sequence_queued.items) |req| req.deinit();
self.keyboard.sequence_queued.deinit(self.alloc);
// Clean up our font grid
self.app.font_grid_set.deref(self.font_grid_key);
@@ -2565,7 +2563,7 @@ pub fn keyEventIsBinding(
// Our keybinding set is either our current nested set (for
// sequences) or the root set.
const set = self.keyboard.bindings orelse &self.config.keybind.set;
const set = self.keyboard.sequence_set orelse &self.config.keybind.set;
// log.warn("text keyEventIsBinding event={} match={}", .{ event, set.getEvent(event) != null });
@@ -2791,7 +2789,7 @@ fn maybeHandleBinding(
// Find an entry in the keybind set that matches our event.
const entry: input.Binding.Set.Entry = entry: {
const set = self.keyboard.bindings orelse &self.config.keybind.set;
const set = self.keyboard.sequence_set orelse &self.config.keybind.set;
// Get our entry from the set for the given event.
if (set.getEvent(event)) |v| break :entry v;
@@ -2802,7 +2800,7 @@ fn maybeHandleBinding(
//
// We also ignore modifiers so that nested sequences such as
// ctrl+a>ctrl+b>c work.
if (self.keyboard.bindings != null and
if (self.keyboard.sequence_set != null and
!event.key.modifier())
{
// Encode everything up to this point
@@ -2816,13 +2814,13 @@ fn maybeHandleBinding(
const leaf: input.Binding.Set.Leaf = switch (entry.value_ptr.*) {
.leader => |set| {
// Setup the next set we'll look at.
self.keyboard.bindings = set;
self.keyboard.sequence_set = set;
// Store this event so that we can drain and encode on invalid.
// We don't need to cap this because it is naturally capped by
// the config validation.
if (try self.encodeKey(event, insp_ev)) |req| {
try self.keyboard.queued.append(self.alloc, req);
try self.keyboard.sequence_queued.append(self.alloc, req);
}
// Start or continue our key sequence
@@ -2861,8 +2859,8 @@ fn maybeHandleBinding(
// perform an action (below)
self.keyboard.last_trigger = null;
// An action also always resets the binding set.
self.keyboard.bindings = null;
// An action also always resets the sequence set.
self.keyboard.sequence_set = null;
// Attempt to perform the action
log.debug("key event binding flags={} action={f}", .{
@@ -2952,13 +2950,13 @@ fn endKeySequence(
);
};
// No matter what we clear our current binding set. This restores
// No matter what we clear our current sequence set. This restores
// the set we look at to the root set.
self.keyboard.bindings = null;
self.keyboard.sequence_set = null;
if (self.keyboard.queued.items.len > 0) {
if (self.keyboard.sequence_queued.items.len > 0) {
switch (action) {
.flush => for (self.keyboard.queued.items) |write_req| {
.flush => for (self.keyboard.sequence_queued.items) |write_req| {
self.queueIo(switch (write_req) {
.small => |v| .{ .write_small = v },
.stable => |v| .{ .write_stable = v },
@@ -2966,12 +2964,12 @@ fn endKeySequence(
}, .unlocked);
},
.drop => for (self.keyboard.queued.items) |req| req.deinit(),
.drop => for (self.keyboard.sequence_queued.items) |req| req.deinit(),
}
switch (mem) {
.free => self.keyboard.queued.clearAndFree(self.alloc),
.retain => self.keyboard.queued.clearRetainingCapacity(),
.free => self.keyboard.sequence_queued.clearAndFree(self.alloc),
.retain => self.keyboard.sequence_queued.clearRetainingCapacity(),
}
}
}