1
0
Fork 0

fix: WidgetIter will also return first widget

Also includes some miscelaneous cleanups in WidgetIter.
This commit is contained in:
LordMZTE 2024-01-09 19:26:31 +01:00
parent a899c90a14
commit 4fbfdb11f3
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 21 additions and 9 deletions

View file

@ -46,7 +46,7 @@ If you'd like to suggest a new feature or design change, please open an issue fi
- [ ] Tabbed Pane
- [ ] Split Pane
- [ ] Scrolled Pane
- [ ] Focus Stack
- [ ] Focus System
- [x] Theming
- [ ] Built-in themes
- [ ] Layout overflow handling

View file

@ -1,27 +1,39 @@
//! This is a iterator to iterate over the widget tree
//! Depth-first
//! This is a iterator to iterate over the widget tree using a depth-first search algorithm.
const std = @import("std");
const Widget = @import("widget.zig").Widget;
current: *Widget,
/// The widget the iterator is currently on.
/// This is usually the widget just returned by the next function,
/// although this field should also be used for initializing the iterator,
/// in which case it is returned from next once.
w: *Widget,
/// Only true when the iterator is first initialized to return self.w instead of the next widget.
first: bool = true,
const WidgetIter = @This();
/// Returns the next widget in the tree.
pub fn next(self: *WidgetIter) ?*Widget {
if (self.first) {
self.first = false;
return self.w;
}
var last: ?*Widget = null;
while (true) {
const children = self.current.children();
const children = self.w.children();
const slice = if (last) |l|
children[std.mem.indexOfScalar(*Widget, children, l).? + 1 ..]
else
children;
if (slice.len == 0) {
last = self.current;
self.current = self.current.data.parent orelse return null;
last = self.w;
self.w = self.w.data.parent orelse return null;
} else {
self.current = slice[0];
return self.current;
self.w = slice[0];
return slice[0];
}
}
}