node.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use crate::focus::Focus;
  2. use crate::layout::TaffyLayout;
  3. use crate::style_attributes::StyleModifier;
  4. use dioxus_native_core::{real_dom::RealDom, Dependancy, Pass};
  5. use dioxus_native_core_macro::{sorted_str_slice, AnyMapLike, State};
  6. pub(crate) type TuiDom = RealDom<NodeState>;
  7. pub(crate) type TuiNode = dioxus_native_core::node::Node<NodeState>;
  8. #[derive(Debug, Clone, State, AnyMapLike, Default)]
  9. pub(crate) struct NodeState {
  10. #[skip_clone]
  11. pub layout: TaffyLayout,
  12. pub style: StyleModifier,
  13. pub prevent_default: PreventDefault,
  14. pub focus: Focus,
  15. #[skip]
  16. pub focused: bool,
  17. }
  18. #[derive(PartialEq, Debug, Clone)]
  19. pub(crate) enum PreventDefault {
  20. Focus,
  21. KeyPress,
  22. KeyRelease,
  23. KeyDown,
  24. KeyUp,
  25. MouseDown,
  26. Click,
  27. MouseEnter,
  28. MouseLeave,
  29. MouseOut,
  30. Unknown,
  31. MouseOver,
  32. ContextMenu,
  33. Wheel,
  34. MouseUp,
  35. }
  36. impl Default for PreventDefault {
  37. fn default() -> Self {
  38. PreventDefault::Unknown
  39. }
  40. }
  41. impl Pass for PreventDefault {
  42. type ParentDependencies = ();
  43. type ChildDependencies = ();
  44. type NodeDependencies = ();
  45. type Ctx = ();
  46. const NODE_MASK: dioxus_native_core::node_ref::NodeMask =
  47. dioxus_native_core::node_ref::NodeMask::new_with_attrs(
  48. dioxus_native_core::node_ref::AttributeMask::Static(&sorted_str_slice!([
  49. "dioxus-prevent-default"
  50. ])),
  51. )
  52. .with_listeners();
  53. fn pass<'a>(
  54. &mut self,
  55. node_view: dioxus_native_core::node_ref::NodeView,
  56. node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  57. parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  58. children: Option<
  59. impl Iterator<Item = <Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
  60. >,
  61. context: <Self::Ctx as Dependancy>::ElementBorrowed<'a>,
  62. ) -> bool {
  63. let new = match node_view.attributes().and_then(|mut attrs| {
  64. attrs
  65. .find(|a| a.attribute.name == "dioxus-prevent-default")
  66. .and_then(|a| a.value.as_text())
  67. }) {
  68. Some("onfocus") => PreventDefault::Focus,
  69. Some("onkeypress") => PreventDefault::KeyPress,
  70. Some("onkeyrelease") => PreventDefault::KeyRelease,
  71. Some("onkeydown") => PreventDefault::KeyDown,
  72. Some("onkeyup") => PreventDefault::KeyUp,
  73. Some("onclick") => PreventDefault::Click,
  74. Some("onmousedown") => PreventDefault::MouseDown,
  75. Some("onmouseup") => PreventDefault::MouseUp,
  76. Some("onmouseenter") => PreventDefault::MouseEnter,
  77. Some("onmouseover") => PreventDefault::MouseOver,
  78. Some("onmouseleave") => PreventDefault::MouseLeave,
  79. Some("onmouseout") => PreventDefault::MouseOut,
  80. Some("onwheel") => PreventDefault::Wheel,
  81. Some("oncontextmenu") => PreventDefault::ContextMenu,
  82. _ => return false,
  83. };
  84. if new == *self {
  85. false
  86. } else {
  87. *self = new;
  88. true
  89. }
  90. }
  91. }