buttons.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_state(cx, || false);
  14. let hovered = use_state(cx, || 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. cx.render(rsx! {
  20. div{
  21. width: "100%",
  22. height: "100%",
  23. background_color: "{color}",
  24. tabindex: "{cx.props.layer}",
  25. onkeydown: |e| {
  26. if let Code::Space = e.inner().code() {
  27. toggle.modify(|f| !f);
  28. }
  29. },
  30. onclick: |_| {
  31. toggle.modify(|f| !f);
  32. },
  33. onmouseenter: |_|{
  34. hovered.set(true);
  35. },
  36. onmouseleave: |_|{
  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(cx: Scope) -> Element {
  48. cx.render(rsx! {
  49. div {
  50. display: "flex",
  51. flex_direction: "column",
  52. width: "100%",
  53. height: "100%",
  54. (1..8).map(|y|
  55. rsx!{
  56. div{
  57. display: "flex",
  58. flex_direction: "row",
  59. width: "100%",
  60. height: "100%",
  61. (1..8).map(|x|{
  62. if (x + y) % 2 == 0{
  63. rsx!{
  64. div{
  65. width: "100%",
  66. height: "100%",
  67. background_color: "rgb(100, 100, 100)",
  68. }
  69. }
  70. }
  71. else{
  72. let layer = (x + y) % 3;
  73. rsx!{
  74. Button{
  75. color_offset: x * y,
  76. layer: layer as u16,
  77. }
  78. }
  79. }
  80. })
  81. }
  82. }
  83. )
  84. }
  85. })
  86. }