hover.rs 5.5 KB

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