button.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. use std::collections::HashMap;
  2. use dioxus_html::input_data::keyboard_types::Key;
  3. use dioxus_native_core::{
  4. node::OwnedAttributeDiscription,
  5. node_ref::AttributeMask,
  6. prelude::NodeType,
  7. real_dom::{ElementNodeMut, NodeImmutable, NodeTypeMut, RealDom},
  8. utils::widget_watcher::Widget,
  9. NodeId,
  10. };
  11. use shipyard::UniqueViewMut;
  12. use crate::FormData;
  13. use super::{RinkWidget, WidgetContext};
  14. #[derive(Debug, Default)]
  15. pub(crate) struct Button {
  16. div_id: NodeId,
  17. text_id: NodeId,
  18. value: String,
  19. }
  20. impl Button {
  21. fn width(el: &ElementNodeMut) -> String {
  22. if let Some(value) = el
  23. .get_attribute(&OwnedAttributeDiscription {
  24. name: "width".to_string(),
  25. namespace: None,
  26. })
  27. .and_then(|value| value.as_text())
  28. .map(|value| value.to_string())
  29. {
  30. value
  31. } else {
  32. "1px".to_string()
  33. }
  34. }
  35. fn height(el: &ElementNodeMut) -> String {
  36. if let Some(value) = el
  37. .get_attribute(&OwnedAttributeDiscription {
  38. name: "height".to_string(),
  39. namespace: None,
  40. })
  41. .and_then(|value| value.as_text())
  42. .map(|value| value.to_string())
  43. {
  44. value
  45. } else {
  46. "1px".to_string()
  47. }
  48. }
  49. fn update_size_attr(&mut self, el: &mut ElementNodeMut) {
  50. let width = Self::width(el);
  51. let height = Self::height(el);
  52. let single_char = width == "1px" || height == "1px";
  53. let border_style = if single_char { "none" } else { "solid" };
  54. el.set_attribute(
  55. OwnedAttributeDiscription {
  56. name: "border-style".to_string(),
  57. namespace: Some("style".to_string()),
  58. },
  59. border_style.to_string(),
  60. );
  61. }
  62. fn update_value_attr(&mut self, el: &ElementNodeMut) {
  63. if let Some(value) = el
  64. .get_attribute(&OwnedAttributeDiscription {
  65. name: "value".to_string(),
  66. namespace: None,
  67. })
  68. .and_then(|value| value.as_text())
  69. .map(|value| value.to_string())
  70. {
  71. self.value = value;
  72. }
  73. }
  74. fn write_value(&self, rdom: &mut RealDom) {
  75. if let Some(mut text) = rdom.get_mut(self.text_id) {
  76. let node_type = text.node_type_mut();
  77. let NodeTypeMut::Text(mut text) = node_type else { panic!("input must be an element") };
  78. *text.text_mut() = self.value.clone();
  79. }
  80. }
  81. fn switch(&mut self, ctx: &mut WidgetContext) {
  82. let data = FormData {
  83. value: self.value.to_string(),
  84. values: HashMap::new(),
  85. files: None,
  86. };
  87. ctx.send(crate::Event {
  88. id: self.div_id,
  89. name: "input",
  90. data: crate::EventData::Form(data),
  91. bubbles: true,
  92. });
  93. }
  94. }
  95. impl Widget for Button {
  96. const NAME: &'static str = "input";
  97. fn create(root: &mut dioxus_native_core::real_dom::NodeMut<()>) -> Self {
  98. let node_type = root.node_type();
  99. let NodeType::Element(el) = &*node_type else { panic!("input must be an element") };
  100. let value = el
  101. .attributes
  102. .get(&OwnedAttributeDiscription {
  103. name: "value".to_string(),
  104. namespace: None,
  105. })
  106. .and_then(|value| value.as_text())
  107. .map(|value| value.to_string());
  108. drop(node_type);
  109. let rdom = root.real_dom_mut();
  110. let text = rdom.create_node(value.clone().unwrap_or_default());
  111. let text_id = text.id();
  112. root.add_event_listener("keydown");
  113. root.add_event_listener("click");
  114. let div_id = root.id();
  115. root.add_child(text_id);
  116. Self {
  117. div_id,
  118. text_id,
  119. value: value.unwrap_or_default(),
  120. }
  121. }
  122. fn attributes_changed(
  123. &mut self,
  124. mut root: dioxus_native_core::real_dom::NodeMut<()>,
  125. attributes: &dioxus_native_core::node_ref::AttributeMask,
  126. ) {
  127. match attributes {
  128. AttributeMask::All => {
  129. {
  130. let node_type = root.node_type_mut();
  131. let NodeTypeMut::Element(mut el) = node_type else { panic!("input must be an element") };
  132. self.update_value_attr(&el);
  133. self.update_size_attr(&mut el);
  134. }
  135. self.write_value(root.real_dom_mut());
  136. }
  137. AttributeMask::Some(attrs) => {
  138. {
  139. let node_type = root.node_type_mut();
  140. let NodeTypeMut::Element(mut el) = node_type else { panic!("input must be an element") };
  141. if attrs.contains("width") || attrs.contains("height") {
  142. self.update_size_attr(&mut el);
  143. }
  144. if attrs.contains("value") {
  145. self.update_value_attr(&el);
  146. }
  147. }
  148. if attrs.contains("value") {
  149. self.write_value(root.real_dom_mut());
  150. }
  151. }
  152. }
  153. }
  154. }
  155. impl RinkWidget for Button {
  156. fn handle_event(
  157. &mut self,
  158. event: &crate::Event,
  159. node: &mut dioxus_native_core::real_dom::NodeMut,
  160. ) {
  161. let mut ctx: UniqueViewMut<WidgetContext> = node
  162. .real_dom_mut()
  163. .raw_world_mut()
  164. .borrow()
  165. .expect("expected widget context");
  166. match event.name {
  167. "click" => self.switch(&mut ctx),
  168. "keydown" => {
  169. if let crate::EventData::Keyboard(data) = &event.data {
  170. if !data.is_auto_repeating()
  171. && match data.key() {
  172. Key::Character(c) if c == " " => true,
  173. Key::Enter => true,
  174. _ => false,
  175. }
  176. {
  177. self.switch(&mut ctx);
  178. }
  179. }
  180. }
  181. _ => {}
  182. }
  183. }
  184. }