1
0

hover.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #[dxfmt::skip]
  45. div {
  46. width: "100%",
  47. height: "100%",
  48. flex_direction: "column",
  49. div {
  50. width: "100%",
  51. height: "50%",
  52. flex_direction: "row",
  53. div {
  54. border_width: "1px",
  55. width: "50%",
  56. height: "100%",
  57. justify_content: "center",
  58. align_items: "center",
  59. background_color: "{q1_color_str}",
  60. onmouseenter: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]),
  61. onmousedown: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]),
  62. onmouseup: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]),
  63. onwheel: move |w| q1_color.set([q1_color[0] + (10.0 * w.delta().strip_units().y) as i32, 0, 0]),
  64. onmouseleave: move |_| q1_color.set([200; 3]),
  65. onmousemove: update_data,
  66. "click me"
  67. }
  68. div {
  69. width: "50%",
  70. height: "100%",
  71. justify_content: "center",
  72. align_items: "center",
  73. background_color: "{q2_color_str}",
  74. onmouseenter: move |m| q2_color.set([get_brightness(m.inner()); 3]),
  75. onmousedown: move |m| q2_color.set([get_brightness(m.inner()); 3]),
  76. onmouseup: move |m| q2_color.set([get_brightness(m.inner()); 3]),
  77. onwheel: move |w| q2_color.set([q2_color[0] + (10.0 * w.delta().strip_units().y) as i32; 3]),
  78. onmouseleave: move |_| q2_color.set([200; 3]),
  79. onmousemove: update_data,
  80. "click me"
  81. }
  82. }
  83. div {
  84. width: "100%",
  85. height: "50%",
  86. flex_direction: "row",
  87. div {
  88. width: "50%",
  89. height: "100%",
  90. justify_content: "center",
  91. align_items: "center",
  92. background_color: "{q3_color_str}",
  93. onmouseenter: move |m| q3_color.set([0, get_brightness(m.inner()), 0]),
  94. onmousedown: move |m| q3_color.set([0, get_brightness(m.inner()), 0]),
  95. onmouseup: move |m| q3_color.set([0, get_brightness(m.inner()), 0]),
  96. onwheel: move |w| q3_color.set([0, q3_color[1] + (10.0 * w.delta().strip_units().y) as i32, 0]),
  97. onmouseleave: move |_| q3_color.set([200; 3]),
  98. onmousemove: update_data,
  99. "click me"
  100. }
  101. div {
  102. width: "50%",
  103. height: "100%",
  104. justify_content: "center",
  105. align_items: "center",
  106. background_color: "{q4_color_str}",
  107. onmouseenter: move |m| q4_color.set([0, 0, get_brightness(m.inner())]),
  108. onmousedown: move |m| q4_color.set([0, 0, get_brightness(m.inner())]),
  109. onmouseup: move |m| q4_color.set([0, 0, get_brightness(m.inner())]),
  110. onwheel: move |w| q4_color.set([0, 0, q4_color[2] + (10.0 * w.delta().strip_units().y) as i32]),
  111. onmouseleave: move |_| q4_color.set([200; 3]),
  112. onmousemove: update_data,
  113. "click me"
  114. }
  115. }
  116. div { "Page coordinates: {page_coordinates}" }
  117. div { "Element coordinates: {element_coordinates}" }
  118. div { "Buttons: {buttons}" }
  119. div { "Modifiers: {modifiers}" }
  120. }
  121. })
  122. }