tui_buttons.rs 2.7 KB

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