simplest.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use patina::{prelude::*, ui::UIControlMsg, window::WindowComponent};
  2. /// struct to represent a window and dispatch event processing within it
  3. struct SimpleWindow {
  4. widget: <Self as WindowComponent>::RootWidget,
  5. }
  6. impl SimpleWindow {
  7. fn new(uih: &mut patina::ui::UIHandle) -> Self {
  8. Self {
  9. widget: patina::widget::Frame::new(uih),
  10. }
  11. }
  12. }
  13. /// messages that can be sent to the window component
  14. enum SimpleWindowMsg {
  15. Close,
  16. }
  17. /// internal message handling
  18. impl Component for SimpleWindow {
  19. type ParentMsg = UIControlMsg;
  20. type Msg = SimpleWindowMsg;
  21. fn process(&mut self, msg: Self::Msg) -> Vec<Self::ParentMsg> {
  22. match msg {
  23. SimpleWindowMsg::Close => vec![UIControlMsg::Terminate],
  24. }
  25. }
  26. }
  27. /// window-specific event handling
  28. impl WindowComponent for SimpleWindow {
  29. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  30. match we {
  31. patina::window::WindowEvent::CloseRequested => Some(SimpleWindowMsg::Close),
  32. _ => None,
  33. }
  34. }
  35. type RootWidget = patina::widget::Frame<Self>;
  36. fn root_widget(&self) -> &Self::RootWidget {
  37. &self.widget
  38. }
  39. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  40. &mut self.widget
  41. }
  42. }
  43. fn main() {
  44. // see the docs for make_opinionated_ui
  45. patina::ui::make_opinionated_ui(SimpleWindow::new).run();
  46. }