text_edits.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. use kahlo::math::PixelSideOffsets;
  2. use patina::{
  3. layout::SizePolicy,
  4. prelude::*,
  5. ui::{UIControlMsg, UIHandle},
  6. widget,
  7. };
  8. enum TextEditMsg {
  9. CloseRequest,
  10. }
  11. struct TextEditWindow {
  12. root: widget::Frame<Self>,
  13. }
  14. impl TextEditWindow {
  15. fn new(uih: &mut UIHandle) -> Self {
  16. let mut group = widget::FormGroup::new(uih);
  17. group
  18. .layout_node_mut()
  19. .set_height_policy(SizePolicy::fixed(0));
  20. group.set_label_margin(PixelSideOffsets::new(0, 10, 0, 10));
  21. group.add_widget("simple editor:", {
  22. let mut edit = widget::TextEdit::new(uih);
  23. edit.set_text("default text");
  24. edit.boxed()
  25. });
  26. group.add_widget("simple editor:", {
  27. let mut edit = widget::TextEdit::new(uih);
  28. edit.set_text("......");
  29. edit.boxed()
  30. });
  31. group.add_widget("simple editor:", {
  32. let mut edit = widget::TextEdit::new(uih);
  33. edit.set_text("^^^^^^");
  34. edit.boxed()
  35. });
  36. Self {
  37. root: widget::Frame::wrap_widget(group),
  38. }
  39. }
  40. }
  41. impl Component for TextEditWindow {
  42. type ParentMsg = UIControlMsg;
  43. type Msg = TextEditMsg;
  44. fn process(&mut self, _msg: Self::Msg) -> Vec<Self::ParentMsg> {
  45. let mut out = String::new();
  46. patina::layout::dump_node_tree(self.root.layout_node(), &mut out);
  47. std::fs::write("patina-form-example.layout_tree", out).expect("couldn't dump node tree");
  48. let jsout = std::fs::File::create("patina-text-edit-example.json").unwrap();
  49. patina::layout::dump_tree_json(
  50. self.root.layout_node(),
  51. &mut std::io::BufWriter::new(jsout),
  52. )
  53. .expect("couldn't dump node tree JSON");
  54. vec![UIControlMsg::Terminate]
  55. }
  56. }
  57. impl WindowComponent for TextEditWindow {
  58. fn map_window_event(&self, we: patina::window::WindowEvent) -> Option<Self::Msg> {
  59. match we {
  60. patina::window::WindowEvent::CloseRequested => Some(TextEditMsg::CloseRequest),
  61. _ => None,
  62. }
  63. }
  64. type RootWidget = widget::Frame<Self>;
  65. fn root_widget(&self) -> &Self::RootWidget {
  66. &self.root
  67. }
  68. fn root_widget_mut(&mut self) -> &mut Self::RootWidget {
  69. &mut self.root
  70. }
  71. }
  72. fn main() {
  73. patina::ui::make_opinionated_ui(TextEditWindow::new).run();
  74. }