button.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(
  16. vec![
  17. Box::new(widget::Spacer::new(uih)) as Box<dyn Widget<Self>>,
  18. Box::new({
  19. let mut button = widget::Button::new(uih);
  20. button.set_label("Button label");
  21. button.set_hook(Box::new(|| Some(ButtonWindowMsg::Shutdown)));
  22. button
  23. }),
  24. Box::new(widget::Spacer::new(uih)),
  25. ]
  26. .into_iter(),
  27. );
  28. Self { group }
  29. }
  30. }
  31. impl Component for ButtonWindow {
  32. type ParentMsg = UIControlMsg;
  33. type Msg = ButtonWindowMsg;
  34. fn process(&mut self, msg: Self::Msg) -> Vec<Self::ParentMsg> {
  35. match msg {
  36. ButtonWindowMsg::Shutdown => vec![UIControlMsg::Terminate],
  37. }
  38. }
  39. }
  40. impl WindowComponent for ButtonWindow {
  41. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  42. match we {
  43. patina::window::WindowEvent::CloseRequested => Some(ButtonWindowMsg::Shutdown),
  44. _ => None,
  45. }
  46. }
  47. type RootWidget = widget::PlainGroup<Self>;
  48. fn root_widget(&self) -> &Self::RootWidget {
  49. &self.group
  50. }
  51. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  52. &mut self.group
  53. }
  54. }
  55. fn main() {
  56. patina::ui::make_opinionated_ui(ButtonWindow::new).run();
  57. }