List is not shown on gpui_component v0.5.0 #1870
-
|
I have tried to show the list component based on the API Docs, but it has not been shown. Do I misundestand? OS: Windows11 Home 25H2
The entire source code is the following. struct MyListDelegate {
items: Vec<String>,
selected_index: Option<gpui_component::IndexPath>,
}
struct MyList {
state: gpui::Entity<gpui_component::list::ListState<MyListDelegate>>,
}
impl gpui_component::list::ListDelegate for MyListDelegate {
type Item = gpui_component::list::ListItem;
fn items_count(&self, _section: usize, _cx: &gpui::App) -> usize {
self.items.len()
}
fn render_item(
&mut self,
ix: gpui_component::IndexPath,
_window: &mut gpui::Window,
_cx: &mut gpui::Context<gpui_component::list::ListState<Self>>,
) -> Option<Self::Item> {
use gpui::*;
self.items.get(ix.row).map(|item| {
gpui_component::list::ListItem::new(ix)
.child(gpui_component::label::Label::new(item.clone()))
.selected(Some(ix) == self.selected_index)
})
}
fn set_selected_index(
&mut self,
ix: Option<gpui_component::IndexPath>,
_window: &mut gpui::Window,
cx: &mut gpui::Context<gpui_component::list::ListState<Self>>,
) {
self.selected_index = ix;
cx.notify();
}
}
impl gpui::Render for MyList {
fn render(&mut self, _window: &mut gpui::Window, _cx: &mut gpui::Context<Self>) -> impl gpui::IntoElement {
use gpui::*;
div().child(gpui_component::list::List::new(&self.state))
}
}
fn main() {
use gpui::AppContext;
let app = gpui::Application::new();
app.run(move |cx: &mut gpui::App| {
// This must be called before using any GPUI Component features.
gpui_component::init(cx);
let window_options = gpui::WindowOptions {
window_bounds: Some(gpui::WindowBounds::centered(gpui::size(gpui::px(400.), gpui::px(200.)), cx)),
..Default::default()
};
cx.open_window(window_options, |window, cx| {
let list_delegate = MyListDelegate {
items: vec!["foo".into(), "bar".into(), "fuga".into()],
selected_index: None,
};
let list_state = cx.new(|cx| gpui_component::list::ListState::new(list_delegate, window, cx));
let view = cx.new(|_| MyList { state: list_state });
// This first level on the window, should be a Root.
cx.new(|cx| gpui_component::Root::new(view, window, cx))
}).unwrap();
});
}And the Cargo.toml is the following. It is appreciated if you give me any helps. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
You are not misunderstanding the API. Your Root CauseIn GPUI, widgets do not auto-size. A component only renders if its parent provides a concrete size. In your case: div().child(gpui_component::list::List::new(&self.state))
This happens silently (no warnings or panics). Fix (Recommended)Give the container and the list a size using flex layout. Corrected
|
Beta Was this translation helpful? Give feedback.
-
|
@aditya-cherukuru The list was rendered correctly by specifying height explicitly. impl gpui::Render for MyList {
fn render(&mut self, _window: &mut gpui::Window, _cx: &mut gpui::Context<Self>) -> impl gpui::IntoElement {
use gpui::*;
div()
.h(px(200.0)) //Specify height explicitly
.child(gpui_component::list::List::new(&self.state))
}
}
|
Beta Was this translation helpful? Give feedback.


You are not misunderstanding the API. Your
ListDelegateand state wiring are correct. The list is not visible due to a layout sizing issue, which is expected behavior in GPUI.Root Cause
In GPUI, widgets do not auto-size. A component only renders if its parent provides a concrete size.
In your case:
div()has no height or flex rulesListdoes not define its own heightThis happens silently (no warnings or panics).
Fix (Recommended)
Give the container and the list a size using flex layout.
Corrected
RenderImplementation