hover.rs 5.3 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() -> 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 mut q1_color = use_signal(|| [200; 3]);
  23. let mut q2_color = use_signal(|| [200; 3]);
  24. let mut q3_color = use_signal(|| [200; 3]);
  25. let mut q4_color = use_signal(|| [200; 3]);
  26. let mut page_coordinates = use_signal(|| "".to_string());
  27. let mut element_coordinates = use_signal(|| "".to_string());
  28. let mut buttons = use_signal(|| "".to_string());
  29. let mut modifiers = use_signal(|| "".to_string());
  30. let update_data = move |event: Event<MouseData>| {
  31. page_coordinates.set(format!("{:?}", event.page_coordinates()));
  32. element_coordinates.set(format!("{:?}", event.element_coordinates()));
  33. // Note: client coordinates are also available, but they would be the same as the page coordinates in this example, because there is no scrolling.
  34. // There are also screen coordinates, but they are currently the same as client coordinates due to technical limitations
  35. buttons.set(format!("{:?}", event.held_buttons()));
  36. modifiers.set(format!("{:?}", event.modifiers()));
  37. };
  38. rsx! {
  39. div {
  40. width: "100%",
  41. height: "100%",
  42. flex_direction: "column",
  43. div {
  44. width: "100%",
  45. height: "50%",
  46. 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: to_str(&q1_color()),
  54. onmouseenter: move |m| q1_color.set([get_brightness(m.data()), 0, 0]),
  55. onmousedown: move |m| q1_color.set([get_brightness(m.data()), 0, 0]),
  56. onmouseup: move |m| q1_color.set([get_brightness(m.data()), 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: to_str(&q2_color()),
  68. onmouseenter: move |m| q2_color.set([get_brightness(m.data()); 3]),
  69. onmousedown: move |m| q2_color.set([get_brightness(m.data()); 3]),
  70. onmouseup: move |m| q2_color.set([get_brightness(m.data()); 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 {
  78. width: "100%",
  79. height: "50%",
  80. flex_direction: "row",
  81. div {
  82. width: "50%",
  83. height: "100%",
  84. justify_content: "center",
  85. align_items: "center",
  86. background_color: to_str(&q3_color()),
  87. onmouseenter: move |m| q3_color.set([0, get_brightness(m.data()), 0]),
  88. onmousedown: move |m| q3_color.set([0, get_brightness(m.data()), 0]),
  89. onmouseup: move |m| q3_color.set([0, get_brightness(m.data()), 0]),
  90. onwheel: move |w| q3_color.set([0, q3_color()[1] + (10.0 * w.delta().strip_units().y) as i32, 0]),
  91. onmouseleave: move |_| q3_color.set([200; 3]),
  92. onmousemove: update_data,
  93. "click me"
  94. }
  95. div {
  96. width: "50%",
  97. height: "100%",
  98. justify_content: "center",
  99. align_items: "center",
  100. background_color: to_str(&q4_color()),
  101. onmouseenter: move |m| q4_color.set([0, 0, get_brightness(m.data())]),
  102. onmousedown: move |m| q4_color.set([0, 0, get_brightness(m.data())]),
  103. onmouseup: move |m| q4_color.set([0, 0, get_brightness(m.data())]),
  104. onwheel: move |w| q4_color.set([0, 0, q4_color()[2] + (10.0 * w.delta().strip_units().y) as i32]),
  105. onmouseleave: move |_| q4_color.set([200; 3]),
  106. onmousemove: update_data,
  107. "click me"
  108. }
  109. }
  110. div { "Page coordinates: {page_coordinates}" }
  111. div { "Element coordinates: {element_coordinates}" }
  112. div { "Buttons: {buttons}" }
  113. div { "Modifiers: {modifiers}" }
  114. }
  115. }
  116. }