buttons.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use dioxus::prelude::*;
  2. use dioxus_html::input_data::keyboard_types::Code;
  3. fn main() {
  4. dioxus_tui::launch(app);
  5. }
  6. #[derive(PartialEq, Props)]
  7. struct ButtonProps {
  8. color_offset: u32,
  9. layer: u16,
  10. }
  11. #[allow(non_snake_case)]
  12. fn Button(cx: Scope<ButtonProps>) -> Element {
  13. let toggle = use_signal(|| false);
  14. let hovered = use_signal(|| false);
  15. let hue = cx.props.color_offset % 255;
  16. let saturation = if *toggle.get() { 50 } else { 25 } + if *hovered.get() { 50 } else { 25 };
  17. let brightness = saturation / 2;
  18. let color = format!("hsl({hue}, {saturation}, {brightness})");
  19. rsx! {
  20. div{
  21. width: "100%",
  22. height: "100%",
  23. background_color: "{color}",
  24. tabindex: "{cx.props.layer}",
  25. onkeydown: move |e| {
  26. if let Code::Space = e.inner().code() {
  27. toggle.modify(|f| !f);
  28. }
  29. },
  30. onclick: move |_| {
  31. toggle.modify(|f| !f);
  32. },
  33. onmouseenter: move |_| {
  34. hovered.set(true);
  35. },
  36. onmouseleave: move |_|{
  37. hovered.set(false);
  38. },
  39. justify_content: "center",
  40. align_items: "center",
  41. display: "flex",
  42. flex_direction: "column",
  43. p{ "tabindex: {cx.props.layer}" }
  44. }
  45. }
  46. }
  47. fn app() -> Element {
  48. rsx! {
  49. div {
  50. display: "flex",
  51. flex_direction: "column",
  52. width: "100%",
  53. height: "100%",
  54. for y in 1..8 {
  55. div {
  56. display: "flex",
  57. flex_direction: "row",
  58. width: "100%",
  59. height: "100%",
  60. for x in 1..8 {
  61. if (x + y) % 2 == 0 {
  62. div {
  63. width: "100%",
  64. height: "100%",
  65. background_color: "rgb(100, 100, 100)",
  66. }
  67. } else {
  68. Button {
  69. color_offset: x * y,
  70. layer: ((x + y) % 3) as u16,
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }