hover.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. use dioxus::{events::MouseData, prelude::*};
  2. use dioxus_core::Event;
  3. use dioxus_html::PointInteraction;
  4. use std::convert::TryInto;
  5. use std::fmt::Write;
  6. use std::rc::Rc;
  7. fn main() {
  8. dioxus_tui::launch(app);
  9. }
  10. fn app(cx: Scope) -> Element {
  11. fn to_str(c: &[i32; 3]) -> String {
  12. let mut result = String::new();
  13. result += "#";
  14. for c in c.iter() {
  15. write!(result, "{c:02X?}").unwrap();
  16. }
  17. result
  18. }
  19. fn get_brightness(m: &Rc<MouseData>) -> i32 {
  20. let b: i32 = m.held_buttons().len().try_into().unwrap();
  21. 127 * b
  22. }
  23. let q1_color = use_state(cx, || [200; 3]);
  24. let q2_color = use_state(cx, || [200; 3]);
  25. let q3_color = use_state(cx, || [200; 3]);
  26. let q4_color = use_state(cx, || [200; 3]);
  27. let q1_color_str = to_str(q1_color);
  28. let q2_color_str = to_str(q2_color);
  29. let q3_color_str = to_str(q3_color);
  30. let q4_color_str = to_str(q4_color);
  31. let page_coordinates = use_state(cx, || "".to_string());
  32. let element_coordinates = use_state(cx, || "".to_string());
  33. let buttons = use_state(cx, || "".to_string());
  34. let modifiers = use_state(cx, || "".to_string());
  35. let update_data = move |event: Event<MouseData>| {
  36. let mouse_data = event.inner();
  37. page_coordinates.set(format!("{:?}", mouse_data.page_coordinates()));
  38. element_coordinates.set(format!("{:?}", mouse_data.element_coordinates()));
  39. // Note: client coordinates are also available, but they would be the same as the page coordinates in this example, because there is no scrolling.
  40. // There are also screen coordinates, but they are currently the same as client coordinates due to technical limitations
  41. buttons.set(format!("{:?}", mouse_data.held_buttons()));
  42. modifiers.set(format!("{:?}", mouse_data.modifiers()));
  43. };
  44. cx.render(rsx! {
  45. div { width: "100%", height: "100%", flex_direction: "column",
  46. div { width: "100%", height: "50%", flex_direction: "row",
  47. div {
  48. border_width: "1px",
  49. width: "50%",
  50. height: "100%",
  51. justify_content: "center",
  52. align_items: "center",
  53. background_color: "{q1_color_str}",
  54. onmouseenter: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]),
  55. onmousedown: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]),
  56. onmouseup: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]),
  57. onwheel: move |w| q1_color.set([q1_color[0] + (10.0 * w.delta().strip_units().y) as i32, 0, 0]),
  58. onmouseleave: move |_| q1_color.set([200; 3]),
  59. onmousemove: update_data,
  60. "click me"
  61. }
  62. div {
  63. width: "50%",
  64. height: "100%",
  65. justify_content: "center",
  66. align_items: "center",
  67. background_color: "{q2_color_str}",
  68. onmouseenter: move |m| q2_color.set([get_brightness(m.inner()); 3]),
  69. onmousedown: move |m| q2_color.set([get_brightness(m.inner()); 3]),
  70. onmouseup: move |m| q2_color.set([get_brightness(m.inner()); 3]),
  71. onwheel: move |w| q2_color.set([q2_color[0] + (10.0 * w.delta().strip_units().y) as i32; 3]),
  72. onmouseleave: move |_| q2_color.set([200; 3]),
  73. onmousemove: update_data,
  74. "click me"
  75. }
  76. }
  77. div { width: "100%", height: "50%", flex_direction: "row",
  78. div {
  79. width: "50%",
  80. height: "100%",
  81. justify_content: "center",
  82. align_items: "center",
  83. background_color: "{q3_color_str}",
  84. onmouseenter: move |m| q3_color.set([0, get_brightness(m.inner()), 0]),
  85. onmousedown: move |m| q3_color.set([0, get_brightness(m.inner()), 0]),
  86. onmouseup: move |m| q3_color.set([0, get_brightness(m.inner()), 0]),
  87. onwheel: move |w| q3_color.set([0, q3_color[1] + (10.0 * w.delta().strip_units().y) as i32, 0]),
  88. onmouseleave: move |_| q3_color.set([200; 3]),
  89. onmousemove: update_data,
  90. "click me"
  91. }
  92. div {
  93. width: "50%",
  94. height: "100%",
  95. justify_content: "center",
  96. align_items: "center",
  97. background_color: "{q4_color_str}",
  98. onmouseenter: move |m| q4_color.set([0, 0, get_brightness(m.inner())]),
  99. onmousedown: move |m| q4_color.set([0, 0, get_brightness(m.inner())]),
  100. onmouseup: move |m| q4_color.set([0, 0, get_brightness(m.inner())]),
  101. onwheel: move |w| q4_color.set([0, 0, q4_color[2] + (10.0 * w.delta().strip_units().y) as i32]),
  102. onmouseleave: move |_| q4_color.set([200; 3]),
  103. onmousemove: update_data,
  104. "click me"
  105. }
  106. }
  107. div { "Page coordinates: {page_coordinates}" }
  108. div { "Element coordinates: {element_coordinates}" }
  109. div { "Buttons: {buttons}" }
  110. div { "Modifiers: {modifiers}" }
  111. }
  112. })
  113. }