Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- impl Default for MyApp {
- fn default() -> Self {
- let rows = vec![];
- let mut grid: Grid<Message, MyTheme> = Grid::new(
- rows,
- container::Style {
- background: Some(Background::Color(Color::WHITE)),
- ..Default::default()
- },
- |_offset: iced::widget::scrollable::AbsoluteOffset| UiMessage::Sync.into(),
- 400.0,
- 400.0,
- Size::new(100.0, 100.0),
- MyTheme::Main
- );
- let mut row = RowData::default();
- row.push_text("Row 1, Cell 1".into());
- row.push_button("Add Row".into(), CellMessage::Clicked);
- row.push_button("Add Cell".into(), CellMessage::Clicked);
- row.push_container(container("New Cell").center(100));
- grid.add_row(row);
- let mut row2 = RowData::default();
- grid.add_row(row2);
- grid.add_cells_to_all_rows(5);
- grid.style(
- container::Style {
- background: Some(Background::Color(Color::BLACK)),
- ..Default::default()
- }
- );
- let resulting_rows: Vec<RowData> = grid
- .rows_mut_iter()
- .map(|row| RowData::new(std::mem::take(&mut row.cells)))
- .collect();
- MyApp { resulting_rows }
- }
- }
- impl MyApp {
- fn view(&mut self) -> iced::Element<'_, Message, MyTheme> {
- let rows = std::mem::take(&mut self.resulting_rows);
- let grid: Grid<Message, MyTheme> = Grid::new(
- rows, // Consume the rows for the grid
- container::Style {
- background: Some(Background::Color(Color::WHITE)),
- ..Default::default()
- },
- |_offset: iced::widget::scrollable::AbsoluteOffset| Message::Grid(GridMessage::Sync),
- 400.0,
- 400.0,
- Size::new(100.0, 100.0),
- MyTheme::Main,
- );
- iced::Element::from(grid)
- }
- fn update(&mut self, message: Message) {
- match message {
- Message::Ui(ui_message) => match ui_message {
- UiMessage::AddRow => {
- let mut new_row = RowData::default();
- let row_index = self.grid.row_count();
- new_row.push_text(format!("Row {}, Cell 1", row_index + 1).into());
- new_row.push_button("Add Row".into(), CellMessage::Clicked);
- new_row.push_button("Add Cell".into(), CellMessage::Clicked);
- self.grid.add_row(new_row);
- }
- UiMessage::AddCell(row_index) => {
- if let Some(row) = self.grid.get_row_mut(row_index) {
- let cell_count = row.cells.len() - 2;
- row.push_text(format!("Row {}, Cell {}", row_index + 1, cell_count + 1).into());
- }
- }
- UiMessage::ButtonClicked(row, col) => {
- println!("Button clicked in row {}, column {}", row, col);
- }
- UiMessage::Sync => {
- println!("Syncing...");
- }
- },
- Message::Grid(grid_message) => match grid_message {
- iced_grid::GridMessage::Cell(row, col, CellMessage::Clicked) => {
- if col == 1 {
- self.update(Message::Ui(UiMessage::AddRow));
- } else if col == 2 {
- self.update(Message::Ui(UiMessage::AddCell(row)));
- }
- }
- _ => {
- println!("Grid message received: {:?}", grid_message);
- }
- },
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment