fn layout(
&self,
tree: &mut iced_core::widget::Tree,
renderer: &iced_widget::Renderer,
limits: &iced_core::layout::Limits,
) -> iced_core::layout::Node {
let max_size = limits.max();
let width = self.width.min(max_size.width);
let height = self.height.min(max_size.height);
let rows = self.rows.len();
let cols = self.rows.get(0).map_or(0, |row| row.cells.len());
if rows == 0 || cols == 0 {
return iced_core::layout::Node::new(iced_core::Size::ZERO);
}
let cell_width: f32 = 20.0;
let cell_height: f32 = 20.0;
let collum_distance: f32 = 20.0;
let row_distance: f32 = 20.0;
let mut children = Vec::new();
for row_index in 0..rows {
for col_index in 0..cols {
let size = iced_core::Size {
width: cell_width,
height: cell_height,
};
let position = iced_core::Point {
x: col_index as f32 * (cell_width + collum_distance),
y: row_index as f32 * (cell_height + row_distance),
};
let mut child = iced_core::layout::Node::new(size);
children.push(child.move_to(position));
}
}
//iced_core::widget::tree::State::Some(Box::new("x"))
let new_container: Container<'_, Message, Theme, iced_widget::Renderer> = container("Test");
tree.children = self
.rows
.iter()
.flat_map(|row| {
row.cells.iter().map(|cell: &Cell<'_>| match cell {
Cell::Container(container) => {
println!("E");
iced_core::widget::Tree { tag: container.tag(), state: container.state(), children: container.children() }
},
_ => {
println!("e");
//let new_container: Container<'_, Message, Theme, iced_widget::Renderer> = container("Test");
iced_core::widget::Tree { tag: new_container.tag(), state: new_container.state(), children: new_container.children() }
}
})
})
.collect();
iced_core::layout::Node::with_children(
iced_core::Size::new(width, height),
children,
)
}
fn draw(
&self,
tree: &iced_core::widget::Tree,
renderer: &mut iced_widget::Renderer,
theme: &Theme,
style: &iced_core::renderer::Style,
layout: iced_core::Layout<'_>,
cursor: iced_core::mouse::Cursor,
viewport: &iced_core::Rectangle,
) {
let rows = self.rows.len();
let cols = self.rows.get(0).map_or(0, |row| row.cells.len());
for (row_index, row) in self.rows.iter().enumerate() {
for (col_index, cell) in row.cells.iter().enumerate() {
let child_index = row_index * cols + col_index;
if let Some(bounds) = layout
.children()
.nth(child_index)
.map(|child| child.bounds())
{
match cell {
Cell::Container(container) => {
container.draw(
tree,
renderer,
&theme.resolve_theme(),
style,
layout,
cursor,
viewport,
);
}
_ => {
print!("E")
}
}
}
}
}
}
^ my source code
Code causing the error:
.flat_map(|row| {
row.cells.iter().map(|cell: &Cell<'_>| match cell {
Cell::Container(container) => {
println!("E");
iced_core::widget::Tree { tag: container.tag(), state: container.state(), children: container.children() }
},
_ => {
println!("e");
//let new_container: Container<'_, Message, Theme, iced_widget::Renderer> = container("Test");
iced_core::widget::Tree { tag: new_container.tag(), state: new_container.state(), children: new_container.children() }
}
})
})
Error:
E
thread 'main' panicked at /home/spiderunderurbed/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_core-0.13.2/src/widget/tree.rs:238:28:
Downcast on stateless state
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
I am not too familiar with states, I tried making up a state with random data, and using a cells state, in both cases the state ends up being null.
Goal:
I am trying to make a grid widget, in layout I define a layout that looks like a grid, and for grid I draw each element (ill get around to implementing the background at some point)
I will share the source code, but note, it does not completely follow the elm architecture, is all over the place, with redundances, I KNOW, the goal is to get a basic implementation, then refine it, and REFINE it a lot. So if you can help fix the current issue, thank you.
Repo:
github/SpiderUnderUrBed/iced_grid/tree/0112100eeab8a1f73f5ad6723c804e5e1d9558f4