tui_hover.rs 5.3 KB

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