form_demo.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.layout_node_mut().set_height_policy(SizePolicy::fixed(0));
  15. group.set_label_margin(PixelSideOffsets::new(0, 10, 0, 10));
  16. group.add_widget("button", widget::Button::new_with_label(uih, "engage").boxed());
  17. group.add_widget("button 2", widget::Button::new_with_label(uih, "engage").boxed());
  18. group.add_widget("button 3", widget::Button::new_with_label(uih, "engage").boxed());
  19. group.add_widget("button 4 with longer description", widget::Button::new_with_label(uih, "engage").boxed());
  20. Self { root: widget::Frame::wrap_widget(group) }
  21. }
  22. }
  23. impl Component for FormWindow {
  24. type ParentMsg = UIControlMsg;
  25. type Msg = ();
  26. fn process(&mut self, _msg: Self::Msg) -> Vec<Self::ParentMsg> {
  27. let mut out = String::new();
  28. patina::layout::dump_node_tree(self.root.layout_node(), &mut out);
  29. std::fs::write("patina-form-example.layout_tree", out)
  30. .expect("couldn't dump node tree");
  31. vec![UIControlMsg::Terminate]
  32. }
  33. }
  34. impl WindowComponent for FormWindow {
  35. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  36. match we {
  37. patina::window::WindowEvent::CloseRequested => Some(()),
  38. _ => None,
  39. }
  40. }
  41. type RootWidget = widget::Frame<Self>;
  42. fn root_widget(&self) -> &Self::RootWidget {
  43. &self.root
  44. }
  45. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  46. &mut self.root
  47. }
  48. }
  49. fn main() {
  50. patina::ui::make_opinionated_ui(FormWindow::new).run();
  51. }