hover.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use std::{convert::TryInto, sync::Arc};
  2. use dioxus::{events::MouseData, prelude::*};
  3. fn main() {
  4. rink::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 mb = m.buttons;
  12. let b: i32 = m.buttons.count_ones().try_into().unwrap();
  13. 127 * b
  14. }
  15. let (q1_color, set_q1_color) = use_state(&cx, || [200; 3]);
  16. let (q2_color, set_q2_color) = use_state(&cx, || [200; 3]);
  17. let (q3_color, set_q3_color) = use_state(&cx, || [200; 3]);
  18. let (q4_color, set_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. cx.render(rsx! {
  24. div {
  25. width: "100%",
  26. height: "100%",
  27. flex_direction: "column",
  28. div {
  29. width: "100%",
  30. height: "50%",
  31. flex_direction: "row",
  32. div {
  33. border_width: "1px",
  34. width: "50%",
  35. height: "100%",
  36. justify_content: "center",
  37. align_items: "center",
  38. background_color: "{q1_color_str}",
  39. onmouseenter: move |m| set_q1_color([get_brightness(m.data), 0, 0]),
  40. onmousedown: move |m| set_q1_color([get_brightness(m.data), 0, 0]),
  41. onmouseup: move |m| set_q1_color([get_brightness(m.data), 0, 0]),
  42. onwheel: move |w| set_q1_color([q1_color[0] + (10.0*w.delta_y) as i32, 0, 0]),
  43. onmouseleave: move |_| set_q1_color([200; 3]),
  44. "click me"
  45. }
  46. div {
  47. width: "50%",
  48. height: "100%",
  49. justify_content: "center",
  50. align_items: "center",
  51. background_color: "{q2_color_str}",
  52. onmouseenter: move |m| set_q2_color([get_brightness(m.data); 3]),
  53. onmousedown: move |m| set_q2_color([get_brightness(m.data); 3]),
  54. onmouseup: move |m| set_q2_color([get_brightness(m.data); 3]),
  55. onwheel: move |w| set_q2_color([q2_color[0] + (10.0*w.delta_y) as i32;3]),
  56. onmouseleave: move |_| set_q2_color([200; 3]),
  57. "click me"
  58. }
  59. }
  60. div {
  61. width: "100%",
  62. height: "50%",
  63. flex_direction: "row",
  64. div {
  65. width: "50%",
  66. height: "100%",
  67. justify_content: "center",
  68. align_items: "center",
  69. background_color: "{q3_color_str}",
  70. onmouseenter: move |m| set_q3_color([0, get_brightness(m.data), 0]),
  71. onmousedown: move |m| set_q3_color([0, get_brightness(m.data), 0]),
  72. onmouseup: move |m| set_q3_color([0, get_brightness(m.data), 0]),
  73. onwheel: move |w| set_q3_color([0, q3_color[1] + (10.0*w.delta_y) as i32, 0]),
  74. onmouseleave: move |_| set_q3_color([200; 3]),
  75. "click me"
  76. }
  77. div {
  78. width: "50%",
  79. height: "100%",
  80. justify_content: "center",
  81. align_items: "center",
  82. background_color: "{q4_color_str}",
  83. onmouseenter: move |m| set_q4_color([0, 0, get_brightness(m.data)]),
  84. onmousedown: move |m| set_q4_color([0, 0, get_brightness(m.data)]),
  85. onmouseup: move |m| set_q4_color([0, 0, get_brightness(m.data)]),
  86. onwheel: move |w| set_q4_color([0, 0, q4_color[2] + (10.0*w.delta_y) as i32]),
  87. onmouseleave: move |_| set_q4_color([200; 3]),
  88. "click me"
  89. }
  90. }
  91. }
  92. })
  93. }