button.rs 1.7 KB

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