Guest User

Untitled

a guest
Jan 1st, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. impl Default for MyApp {
  2. fn default() -> Self {
  3. let rows = vec![];
  4.  
  5.  
  6. let mut grid: Grid<Message, MyTheme> = Grid::new(
  7. rows,
  8. container::Style {
  9. background: Some(Background::Color(Color::WHITE)),
  10. ..Default::default()
  11. },
  12. |_offset: iced::widget::scrollable::AbsoluteOffset| UiMessage::Sync.into(),
  13. 400.0,
  14. 400.0,
  15. Size::new(100.0, 100.0),
  16. MyTheme::Main
  17.  
  18. );
  19.  
  20.  
  21. let mut row = RowData::default();
  22. row.push_text("Row 1, Cell 1".into());
  23. row.push_button("Add Row".into(), CellMessage::Clicked);
  24. row.push_button("Add Cell".into(), CellMessage::Clicked);
  25. row.push_container(container("New Cell").center(100));
  26. grid.add_row(row);
  27. let mut row2 = RowData::default();
  28. grid.add_row(row2);
  29.  
  30. grid.add_cells_to_all_rows(5);
  31. grid.style(
  32. container::Style {
  33. background: Some(Background::Color(Color::BLACK)),
  34. ..Default::default()
  35. }
  36. );
  37.  
  38. let resulting_rows: Vec<RowData> = grid
  39. .rows_mut_iter()
  40. .map(|row| RowData::new(std::mem::take(&mut row.cells)))
  41. .collect();
  42.  
  43.  
  44.  
  45. MyApp { resulting_rows }
  46. }
  47. }
  48.  
  49.  
  50.  
  51.  
  52. impl MyApp {
  53.  
  54. fn view(&mut self) -> iced::Element<'_, Message, MyTheme> {
  55. let rows = std::mem::take(&mut self.resulting_rows);
  56.  
  57. let grid: Grid<Message, MyTheme> = Grid::new(
  58. rows, // Consume the rows for the grid
  59. container::Style {
  60. background: Some(Background::Color(Color::WHITE)),
  61. ..Default::default()
  62. },
  63. |_offset: iced::widget::scrollable::AbsoluteOffset| Message::Grid(GridMessage::Sync),
  64. 400.0,
  65. 400.0,
  66. Size::new(100.0, 100.0),
  67. MyTheme::Main,
  68. );
  69.  
  70.  
  71. iced::Element::from(grid)
  72. }
  73.  
  74.  
  75.  
  76.  
  77. fn update(&mut self, message: Message) {
  78. match message {
  79. Message::Ui(ui_message) => match ui_message {
  80. UiMessage::AddRow => {
  81. let mut new_row = RowData::default();
  82. let row_index = self.grid.row_count();
  83. new_row.push_text(format!("Row {}, Cell 1", row_index + 1).into());
  84. new_row.push_button("Add Row".into(), CellMessage::Clicked);
  85. new_row.push_button("Add Cell".into(), CellMessage::Clicked);
  86. self.grid.add_row(new_row);
  87. }
  88. UiMessage::AddCell(row_index) => {
  89. if let Some(row) = self.grid.get_row_mut(row_index) {
  90. let cell_count = row.cells.len() - 2;
  91. row.push_text(format!("Row {}, Cell {}", row_index + 1, cell_count + 1).into());
  92. }
  93. }
  94. UiMessage::ButtonClicked(row, col) => {
  95. println!("Button clicked in row {}, column {}", row, col);
  96. }
  97. UiMessage::Sync => {
  98. println!("Syncing...");
  99. }
  100. },
  101. Message::Grid(grid_message) => match grid_message {
  102. iced_grid::GridMessage::Cell(row, col, CellMessage::Clicked) => {
  103.  
  104. if col == 1 {
  105.  
  106. self.update(Message::Ui(UiMessage::AddRow));
  107. } else if col == 2 {
  108.  
  109. self.update(Message::Ui(UiMessage::AddCell(row)));
  110. }
  111. }
  112. _ => {
  113.  
  114. println!("Grid message received: {:?}", grid_message);
  115. }
  116. },
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment