1
0

widgets.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use dioxus_native_core::{
  2. prelude::*,
  3. real_dom::{NodeImmutable, NodeTypeMut},
  4. NodeId,
  5. };
  6. use plasmo::{render, Config, Driver, EventData};
  7. use std::rc::Rc;
  8. use std::sync::{Arc, RwLock};
  9. #[derive(Default)]
  10. struct Counter {
  11. count: f64,
  12. button_id: NodeId,
  13. }
  14. impl Counter {
  15. fn create(mut root: NodeMut) -> Self {
  16. let mut myself = Self::default();
  17. let root_id = root.id();
  18. let rdom = root.real_dom_mut();
  19. // create the counter
  20. let count = myself.count;
  21. let mut button = rdom.create_node(NodeType::Element(ElementNode {
  22. tag: "input".to_string(),
  23. attributes: [
  24. // supported types: button, checkbox, textbox, password, number, range
  25. ("type".to_string().into(), "range".to_string().into()),
  26. ("display".to_string().into(), "flex".to_string().into()),
  27. (("flex-direction", "style").into(), "row".to_string().into()),
  28. (
  29. ("justify-content", "style").into(),
  30. "center".to_string().into(),
  31. ),
  32. (("align-items", "style").into(), "center".to_string().into()),
  33. (
  34. "value".to_string().into(),
  35. format!("click me {count}").into(),
  36. ),
  37. (("width", "style").into(), "50%".to_string().into()),
  38. (("height", "style").into(), "10%".to_string().into()),
  39. ("min".to_string().into(), "20".to_string().into()),
  40. ("max".to_string().into(), "80".to_string().into()),
  41. ]
  42. .into_iter()
  43. .collect(),
  44. ..Default::default()
  45. }));
  46. button.add_event_listener("input");
  47. myself.button_id = button.id();
  48. rdom.get_mut(root_id).unwrap().add_child(myself.button_id);
  49. myself
  50. }
  51. }
  52. impl Driver for Counter {
  53. fn update(&mut self, rdom: &Arc<RwLock<RealDom>>) {
  54. // update the counter
  55. let mut rdom = rdom.write().unwrap();
  56. let mut node = rdom.get_mut(self.button_id).unwrap();
  57. if let NodeTypeMut::Element(mut el) = node.node_type_mut() {
  58. el.set_attribute(
  59. ("background-color", "style"),
  60. format!("rgb({}, {}, {})", 255.0 - self.count * 2.0, 0, 0,),
  61. );
  62. };
  63. }
  64. fn handle_event(
  65. &mut self,
  66. _: &Arc<RwLock<RealDom>>,
  67. _: NodeId,
  68. event_type: &str,
  69. event: Rc<EventData>,
  70. _: bool,
  71. ) {
  72. if event_type == "input" {
  73. // when the button is clicked, increment the counter
  74. if let EventData::Form(input_event) = &*event {
  75. if let Ok(value) = input_event.value.parse::<f64>() {
  76. self.count = value;
  77. }
  78. }
  79. }
  80. }
  81. fn poll_async(&mut self) -> std::pin::Pin<Box<dyn futures::Future<Output = ()> + '_>> {
  82. Box::pin(async move { tokio::time::sleep(std::time::Duration::from_millis(1000)).await })
  83. }
  84. }
  85. fn main() {
  86. render(Config::new(), |rdom, _, _| {
  87. let mut rdom = rdom.write().unwrap();
  88. let root = rdom.root_id();
  89. Counter::create(rdom.get_mut(root).unwrap())
  90. })
  91. .unwrap();
  92. }