simplest.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. use patina::{
  2. prelude::*,
  3. window::{Window, WindowComponent},
  4. };
  5. struct SimpleWindow {
  6. widget: <Self as WindowComponent>::RootWidget,
  7. }
  8. impl Component for SimpleWindow {
  9. type ParentMsg = ();
  10. type Msg = ();
  11. fn process(&mut self, msg: Self::Msg) -> Vec<Self::ParentMsg> {
  12. vec![()]
  13. }
  14. }
  15. impl WindowComponent for SimpleWindow {
  16. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  17. Some(())
  18. }
  19. type RootWidget = patina::widget::Spacer<Self>;
  20. fn root_widget(&self) -> &Self::RootWidget {
  21. &self.widget
  22. }
  23. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  24. &mut self.widget
  25. }
  26. }
  27. struct Simplest {
  28. main_window: Option<Window<SimpleWindow>>,
  29. }
  30. impl Component for Simplest {
  31. type ParentMsg = patina::UIControlMsg;
  32. type Msg = ();
  33. fn process(&mut self, msg: Self::Msg) -> Vec<Self::ParentMsg> {
  34. vec![patina::UIControlMsg::Terminate]
  35. }
  36. }
  37. impl patina::UIComponent for Simplest {
  38. fn init<'l>(&mut self, mut ui_handle: patina::UIHandle<'_>) {
  39. let mut widget = patina::widget::Spacer::new(&ui_handle);
  40. widget.layout_node_mut().set_width_policy(patina::layout::SizePolicy { minimum: 0, desired: 0, slack_weight: 1 });
  41. widget.layout_node_mut().set_height_policy(patina::layout::SizePolicy { minimum: 0, desired: 0, slack_weight: 1 });
  42. self.main_window = Some(ui_handle.window_builder().build(|uih| SimpleWindow {
  43. widget
  44. }));
  45. }
  46. fn poll(&mut self) -> Vec<patina::UIControlMsg> {
  47. let Some(mw) = self.main_window.as_mut() else { return vec![] };
  48. let evts = mw.poll();
  49. self.process_all(evts.into_iter())
  50. }
  51. }
  52. fn main() {
  53. let ui = patina::UI::<Simplest>::new(Simplest { main_window: None });
  54. ui.run();
  55. }