Implement "attach-mode"

This commit is contained in:
Leon Henrik Plickat
2020-08-17 23:13:16 +02:00
committed by Isaac Freund
parent 340bfbd7f1
commit 59d6432332
6 changed files with 84 additions and 2 deletions

View File

@ -17,6 +17,11 @@
const View = @import("View.zig");
pub const AttachMode = enum {
top,
bottom,
};
/// A specialized doubly-linked stack that allows for filtered iteration
/// over the nodes. T must be View or *View.
pub fn ViewStack(comptime T: type) type {
@ -64,6 +69,33 @@ pub fn ViewStack(comptime T: type) type {
self.first = new_node;
}
/// Add a node to the bottom of the stack.
pub fn append(self: *Self, new_node: *Node) void {
// Set the prev/next pointers of the new node
new_node.prev = self.last;
new_node.next = null;
if (self.last) |last| {
// If the list is not empty, set the next pointer of the current
// first node to the new node.
last.next = new_node;
} else {
// If the list is empty set the first pointer to the new node.
self.first = new_node;
}
// Set the last pointer to the new node
self.last = new_node;
}
/// Attach a node into the viewstack based on the attach mode
pub fn attach(self: *Self, new_node: *Node, mode: AttachMode) void {
switch (mode) {
.top => self.push(new_node),
.bottom => self.append(new_node),
}
}
/// Remove a node from the view stack. This removes it from the stack of
/// all views as well as the stack of visible ones.
pub fn remove(self: *Self, target_node: *Node) void {