form_demo.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use kahlo::math::PixelSideOffsets;
  2. use patina::{
  3. layout::SizePolicy,
  4. prelude::*,
  5. ui::{UIControlMsg, UIHandle},
  6. widget,
  7. };
  8. struct FormWindow {
  9. root: widget::Frame<Self>,
  10. }
  11. impl FormWindow {
  12. fn new(uih: &mut UIHandle) -> Self {
  13. let mut group = widget::FormGroup::new(uih);
  14. group
  15. .layout_node_mut()
  16. .set_height_policy(SizePolicy::fixed(0));
  17. group.set_label_margin(PixelSideOffsets::new(0, 10, 0, 10));
  18. group.add_widget(
  19. "button",
  20. widget::Button::new(uih).with_label("engage").boxed(),
  21. );
  22. group.add_widget(
  23. "button 2",
  24. widget::Button::new(uih).with_label("engage").boxed(),
  25. );
  26. group.add_widget(
  27. "button 3",
  28. widget::Button::new(uih).with_label("engage").boxed(),
  29. );
  30. group.add_widget(
  31. "button 4 with longer description",
  32. widget::Button::new(uih).with_label("engage").boxed(),
  33. );
  34. Self {
  35. root: widget::Frame::wrap_widget(group),
  36. }
  37. }
  38. }
  39. impl Component for FormWindow {
  40. type ParentMsg = UIControlMsg;
  41. type Msg = ();
  42. fn process(&mut self, _msg: Self::Msg) -> Vec<Self::ParentMsg> {
  43. let mut out = String::new();
  44. patina::layout::dump_node_tree(self.root.layout_node(), &mut out);
  45. std::fs::write("patina-form-example.layout_tree", out).expect("couldn't dump node tree");
  46. vec![UIControlMsg::Terminate]
  47. }
  48. }
  49. impl WindowComponent for FormWindow {
  50. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  51. match we {
  52. patina::window::WindowEvent::CloseRequested => Some(()),
  53. _ => None,
  54. }
  55. }
  56. type RootWidget = widget::Frame<Self>;
  57. fn root_widget(&self) -> &Self::RootWidget {
  58. &self.root
  59. }
  60. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  61. &mut self.root
  62. }
  63. }
  64. fn main() {
  65. patina::ui::make_opinionated_ui(FormWindow::new).run();
  66. }