gpui-component icon indicating copy to clipboard operation
gpui-component copied to clipboard

tree: Add context menu support

Open zanmato opened this issue 1 month ago • 4 comments

Fixes #1643 and partially #1489

Adds .context_menu for ListItem

zanmato avatar Nov 19 '25 12:11 zanmato

After making this I'm not sure if it's needed, or the docs could be updated to add context_menu on the list item child instead?

tree(&tree_state, |ix, entry, selected, window, cx| {
    ListItem::new(ix)
        .selected(selected)
        .child(
            div()
                .child(entry.item().label.clone())
                .context_menu(|menu, _, _| {
                    menu.menu("Rename", Box::new(Rename))
                        .menu("Delete", Box::new(Delete))
                        .separator()
                        .menu("Copy Path", Box::new(CopyPath))
                })
        )
})

zanmato avatar Nov 19 '25 12:11 zanmato

There is a bug with this approach, you have to right click twice, because it seems like the window.on_mouse_event in ContextMenu fires before the on_mouse_down event of the table (or tree in this case). You can see it in the table_story. Probably because the uniform list is further down in the element tree than the context menu on the parent/root element?

zanmato avatar Nov 19 '25 14:11 zanmato

It also means that it is showing the wrong context menu, since the right clicked index is the previous one :disappointed: Perhaps context menu needs to be something "global" like dialog/sheet? So we can force it open in the correct event listener?

zanmato avatar Nov 20 '25 08:11 zanmato

@huacnlee Updated it to use the same pattern as table with a delegate. It suffers from the same issue as table though, see #1651

Should we keep tree()?

zanmato avatar Nov 20 '25 17:11 zanmato

No, we can not change the tree API.

You're doing this effected this PR more complex now.

Small improve must be a small changes.


I don't know how to do this better right now, but I am sure we can't to change the Tree API. I will consider this when have a rest time.

huacnlee avatar Nov 21 '25 02:11 huacnlee

No, we can not change the tree API.

You're doing this effected this PR more complex now.

Small improve must be a small changes.

I don't know how to do this better right now, but I am sure we can't to change the Tree API. I will consider this when have a rest time.

OK! I will close this and open a PR with the docs fix. We could do tree(...).context_menu({}) without breaking the API, but that would require storing the context menu handler in the TreeState.

zanmato avatar Nov 21 '25 07:11 zanmato