Repository navigation
|
Hi. I can't add any item to a DataTable. This is a very thin reproduction I wrote: use gpui::*;
use gpui_component::{
button::Button,
table::{Column, DataTable, TableDelegate, TableState},
*,
};
pub struct AppView {
users_table_state: Entity<TableState<UsersTableDelegate>>,
}
impl AppView {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let users_table_delegate = UsersTableDelegate::new();
let users_table_state = cx.new(|cx| TableState::new(users_table_delegate, window, cx));
Self { users_table_state }
}
}
struct User {
name: String,
address: String,
}
struct UsersTableDelegate {
data: Vec<User>,
columns: Vec<Column>,
}
impl UsersTableDelegate {
fn new() -> Self {
Self {
data: vec![],
columns: vec![Column::new("name", "Name"), Column::new("address", "Address")],
}
}
}
impl TableDelegate for UsersTableDelegate {
fn columns_count(&self, cx: &App) -> usize {
self.columns.len()
}
fn rows_count(&self, cx: &App) -> usize {
self.data.len()
}
fn column(&self, col_ix: usize, cx: &App) -> Column {
self.columns[col_ix].clone()
}
fn render_td(
&mut self,
row_ix: usize,
col_ix: usize,
window: &mut Window,
cx: &mut Context<table::TableState<Self>>,
) -> impl IntoElement {
let row = &self.data[row_ix];
let col = &self.columns[col_ix];
match col.key.as_ref() {
"name" => row.name.clone(),
"address" => row.address.to_owned(),
_ => "".to_string(),
}
}
}
impl Render for AppView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.child(h_flex().child(Button::new("new-user").label("New User").on_click(cx.listener(
move |this, ev, window, cx| {
let users_table_state = this.users_table_state.clone();
users_table_state.update(cx, |this, cx| {
this
.delegate_mut()
.data
.push(User { name: "Name".to_string(), address: "Address".to_string() });
this.refresh(cx);
cx.notify();
});
},
))))
.child(DataTable::new(&self.users_table_state))
}
}
fn main() {
gpui_platform::application().run(move |cx| {
gpui_component::init(cx);
cx.spawn(async move |cx| {
cx.open_window(WindowOptions::default(), |window, cx| {
let view = cx.new(|cx| AppView::new(window, cx));
cx.new(|cx| Root::new(view, window, cx).bg(cx.theme().background))
})
.expect("Failed to open window");
})
.detach();
});
}Dependencies: gpui = { git = "https://github.com/zed-industries/zed" }
gpui_platform = { git = "https://github.com/zed-industries/zed", features = [
"font-kit",
"wayland",
] }
gpui-component = { git = "https://github.com/longbridge/gpui-component" } |
Answered by
mahdi739
Sep 3, 2026
Replies: 1 comment
|
It seems it's just a layouting problem. |
0 replies
Answer selected by
mahdi739
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems it's just a layouting problem.
It solved by make the main
v_flex()full size:v_flex().size_full()