tui_hover.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use std::{convert::TryInto, sync::Arc};
  2. use dioxus::{events::MouseData, prelude::*};
  3. fn main() {
  4. dioxus::tui::launch(app);
  5. }
  6. fn app(cx: Scope) -> Element {
  7. fn to_str(c: &[i32; 3]) -> String {
  8. "#".to_string() + &c.iter().map(|c| format!("{c:02X?}")).collect::<String>()
  9. }
  10. fn get_brightness(m: Arc<MouseData>) -> i32 {
  11. let b: i32 = m.buttons.count_ones().try_into().unwrap();
  12. 127 * b
  13. }
  14. let q1_color = use_state(&cx, || [200; 3]);
  15. let q2_color = use_state(&cx, || [200; 3]);
  16. let q3_color = use_state(&cx, || [200; 3]);
  17. let q4_color = use_state(&cx, || [200; 3]);
  18. let q1_color_str = to_str(q1_color);
  19. let q2_color_str = to_str(q2_color);
  20. let q3_color_str = to_str(q3_color);
  21. let q4_color_str = to_str(q4_color);
  22. cx.render(rsx! {
  23. div {
  24. width: "100%",
  25. height: "100%",
  26. flex_direction: "column",
  27. div {
  28. width: "100%",
  29. height: "50%",
  30. flex_direction: "row",
  31. div {
  32. border_width: "1px",
  33. width: "50%",
  34. height: "100%",
  35. justify_content: "center",
  36. align_items: "center",
  37. background_color: "{q1_color_str}",
  38. onmouseenter: move |m| q1_color.set([get_brightness(m.data), 0, 0]),
  39. onmousedown: move |m| q1_color.set([get_brightness(m.data), 0, 0]),
  40. onmouseup: move |m| q1_color.set([get_brightness(m.data), 0, 0]),
  41. onwheel: move |w| q1_color.set([q1_color[0] + (10.0*w.delta_y) as i32, 0, 0]),
  42. onmouseleave: move |_| q1_color.set([200; 3]),
  43. "click me"
  44. }
  45. div {
  46. width: "50%",
  47. height: "100%",
  48. justify_content: "center",
  49. align_items: "center",
  50. background_color: "{q2_color_str}",
  51. onmouseenter: move |m| q2_color.set([get_brightness(m.data); 3]),
  52. onmousedown: move |m| q2_color.set([get_brightness(m.data); 3]),
  53. onmouseup: move |m| q2_color.set([get_brightness(m.data); 3]),
  54. onwheel: move |w| q2_color.set([q2_color[0] + (10.0*w.delta_y) as i32;3]),
  55. onmouseleave: move |_| q2_color.set([200; 3]),
  56. "click me"
  57. }
  58. }
  59. div {
  60. width: "100%",
  61. height: "50%",
  62. flex_direction: "row",
  63. div {
  64. width: "50%",
  65. height: "100%",
  66. justify_content: "center",
  67. align_items: "center",
  68. background_color: "{q3_color_str}",
  69. onmouseenter: move |m| q3_color.set([0, get_brightness(m.data), 0]),
  70. onmousedown: move |m| q3_color.set([0, get_brightness(m.data), 0]),
  71. onmouseup: move |m| q3_color.set([0, get_brightness(m.data), 0]),
  72. onwheel: move |w| q3_color.set([0, q3_color[1] + (10.0*w.delta_y) as i32, 0]),
  73. onmouseleave: move |_| q3_color.set([200; 3]),
  74. "click me"
  75. }
  76. div {
  77. width: "50%",
  78. height: "100%",
  79. justify_content: "center",
  80. align_items: "center",
  81. background_color: "{q4_color_str}",
  82. onmouseenter: move |m| q4_color.set([0, 0, get_brightness(m.data)]),
  83. onmousedown: move |m| q4_color.set([0, 0, get_brightness(m.data)]),
  84. onmouseup: move |m| q4_color.set([0, 0, get_brightness(m.data)]),
  85. onwheel: move |w| q4_color.set([0, 0, q4_color[2] + (10.0*w.delta_y) as i32]),
  86. onmouseleave: move |_| q4_color.set([200; 3]),
  87. "click me"
  88. }
  89. }
  90. }
  91. })
  92. }