Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler Reorderable Lists Api #109

Closed
VisenDev opened this issue Aug 21, 2024 · 3 comments
Closed

Simpler Reorderable Lists Api #109

VisenDev opened this issue Aug 21, 2024 · 3 comments

Comments

@VisenDev
Copy link
Collaborator

VisenDev commented Aug 21, 2024

The simple reorderable list example still seems quite complex. I struggled to understand the simple and advanced usages in Example.zig

For example, it seems like this function could be automatically generated with comptime, and shouldn't need to be manually written by the user

        pub fn reorder(removed_idx: ?usize, insert_before_idx: ?usize) void {
            if (removed_idx) |ri| {
                if (insert_before_idx) |ibi| {
                    // save this index
                    const removed = strings[ri];
                    if (ri < ibi) {
                        // moving down, shift others up
                        for (ri..ibi - 1) |i| {
                            strings[i] = strings[i + 1];
                        }
                        strings[ibi - 1] = removed;
                    } else {
                        // moving up, shift others down
                        for (ibi..ri, 0..) |_, i| {
                            strings[ri - i] = strings[ri - i - 1];
                        }
                        strings[ibi] = removed;
                    }
                }
            }
        }

It would be really nice to have some reorderable list helper functions which would make this widget much more convenient to use.

For example, something along the lines of

pub fn reorderList(src: std.builtin.SourceLocation, comptime T: type, list_items: []T,  list_item_widget: *fn(src: std.builtin.SourceLocation, item: *T, index: usize) !void) !ReorderableList(T){}

might be possible

@VisenDev VisenDev mentioned this issue Aug 21, 2024
5 tasks
@david-vanderson
Copy link
Owner

Yes you are totally right. I wrote the Reorderable stuff to be as generic as possible, which is useful. But a simple function for the normal case given a slice is a great idea. Will do.

@david-vanderson
Copy link
Owner

I added a helper function reorderSlice like you suggested, and that looks good.

I spent a lot of time trying to figure out a useful function that did it all in one go, but as of now I'm not sure it makes sense. Like you wrote, it would have to take a callback to render the entry in the list but that seems as complicated as directly using the widget. I'm also struggling to think of a usecase where the containing code didn't care when things got reordered.

But this did give me the nudge to make ReorderWidget a normal widget (it had been a kind of half-way widget from during development).

I think we'll have to wait for some usecases to show up to understand if a wrapping function is worthwhile. Sound good?

@VisenDev
Copy link
Collaborator Author

I think we'll have to wait for some usecases to show up to understand if a wrapping function is worthwhile. Sound good?

Thanks! I will experiment with the new widget in my structEntry pr and see how it ends up working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants