1
0

hover.rs 5.4 KB

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