table_layout.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use kahlo::math::PixelSideOffsets;
  2. use patina::{
  3. layout::{HorizontalAlignment, TableCell, VerticalAlignment},
  4. prelude::*,
  5. ui::{UIControlMsg, UIHandle},
  6. widget,
  7. };
  8. struct TableWindow {
  9. root: widget::PlainGroup<Self>,
  10. }
  11. impl TableWindow {
  12. fn new(uih: &mut UIHandle) -> Self {
  13. let mut group = widget::PlainGroup::new_table(uih);
  14. group.extend(
  15. vec![
  16. widget::Label::new_with_text(uih, "x0y0")
  17. .with_valign(VerticalAlignment::Centre)
  18. // .framed()
  19. .with_table_cell(TableCell::new(0, 0))
  20. .boxed(),
  21. widget::Label::new_with_text(uih, "x0y1 [left aligned]")
  22. .with_halign(HorizontalAlignment::Left)
  23. .with_valign(VerticalAlignment::Centre)
  24. .framed()
  25. .with_margins(PixelSideOffsets::new_all_same(15))
  26. .with_table_cell(TableCell::new(0, 1))
  27. .boxed(),
  28. widget::Label::new_with_text(uih, "x1y1")
  29. .with_valign(VerticalAlignment::Centre)
  30. // .framed()
  31. .with_table_cell(TableCell::new(1, 1))
  32. .boxed(),
  33. widget::Label::new_with_text(uih, "x1y2")
  34. .with_valign(VerticalAlignment::Centre)
  35. .framed()
  36. .with_table_cell(TableCell::new(1, 2))
  37. .boxed(),
  38. widget::Label::new_with_text(uih, "x3y2")
  39. .with_valign(VerticalAlignment::Centre)
  40. // .framed()
  41. .with_table_cell(TableCell::new(3, 2))
  42. .boxed(),
  43. ]
  44. .into_iter(),
  45. );
  46. Self { root: group }
  47. }
  48. }
  49. impl Component for TableWindow {
  50. type ParentMsg = UIControlMsg;
  51. type Msg = ();
  52. fn process(&mut self, _msg: Self::Msg) -> Vec<Self::ParentMsg> {
  53. let mut out = String::new();
  54. patina::layout::dump_node_tree(self.root.layout_node(), &mut out);
  55. std::fs::write("patina-table-layout-example.layout_tree", out)
  56. .expect("couldn't dump node tree");
  57. vec![UIControlMsg::Terminate]
  58. }
  59. }
  60. impl WindowComponent for TableWindow {
  61. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  62. match we {
  63. patina::window::WindowEvent::CloseRequested => Some(()),
  64. _ => None,
  65. }
  66. }
  67. type RootWidget = widget::PlainGroup<Self>;
  68. fn root_widget(&self) -> &Self::RootWidget {
  69. &self.root
  70. }
  71. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  72. &mut self.root
  73. }
  74. }
  75. fn main() {
  76. patina::ui::make_opinionated_ui(TableWindow::new).run();
  77. }