button.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. use patina::{
  2. prelude::*,
  3. ui::{UIControlMsg, UIHandle},
  4. widget,
  5. };
  6. enum ButtonWindowMsg {
  7. Shutdown,
  8. }
  9. struct ButtonWindow {
  10. group: widget::PlainGroup<Self>,
  11. }
  12. impl ButtonWindow {
  13. fn new(uih: &mut UIHandle) -> Self {
  14. let mut group = widget::PlainGroup::new_column(uih);
  15. group.extend(vec![
  16. Box::new(widget::Spacer::new(uih)) as Box<dyn Widget<Self>>,
  17. Box::new({
  18. let mut button = widget::Button::new(uih);
  19. button.set_label("Button label");
  20. button.set_hook(Box::new(|| Some(ButtonWindowMsg::Shutdown)));
  21. button
  22. }),
  23. Box::new(widget::Spacer::new(uih)),
  24. ].into_iter());
  25. Self { group }
  26. }
  27. }
  28. impl Component for ButtonWindow {
  29. type ParentMsg = UIControlMsg;
  30. type Msg = ButtonWindowMsg;
  31. fn process(&mut self, msg: Self::Msg) -> Vec<Self::ParentMsg> {
  32. match msg {
  33. ButtonWindowMsg::Shutdown => vec![UIControlMsg::Terminate],
  34. }
  35. }
  36. }
  37. impl WindowComponent for ButtonWindow {
  38. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  39. match we {
  40. patina::window::WindowEvent::CloseRequested => Some(ButtonWindowMsg::Shutdown),
  41. _ => None,
  42. }
  43. }
  44. type RootWidget = widget::PlainGroup<Self>;
  45. fn root_widget(&self) -> &Self::RootWidget {
  46. &self.group
  47. }
  48. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  49. &mut self.group
  50. }
  51. }
  52. fn main() {
  53. patina::ui::make_opinionated_ui(ButtonWindow::new).run();
  54. }