hover.rs 5.4 KB

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