colorpicker.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. use dioxus::core::RenderReturn;
  2. use dioxus::prelude::*;
  3. use dioxus_tui::query::Query;
  4. use dioxus_tui::Size;
  5. fn main() {
  6. dioxus_tui::launch(app);
  7. }
  8. fn app(cx: Scope) -> Element {
  9. let hue = use_state(cx, || 0.0);
  10. let brightness = use_state(cx, || 0.0);
  11. let tui_query: Query = cx.consume_context().unwrap();
  12. // disable templates so that every node has an id and can be queried
  13. cx.render(rsx! {
  14. div{
  15. width: "100%",
  16. background_color: "hsl({hue}, 70%, {brightness}%)",
  17. onmousemove: move |evt| {
  18. if let RenderReturn::Ready(node) = cx.root_node() {
  19. if let Some(id) = node.root_ids.get(0){
  20. let node = tui_query.get(id);
  21. let Size{width, height} = node.size().unwrap();
  22. let pos = evt.inner().element_coordinates();
  23. hue.set((pos.x as f32/width as f32)*255.0);
  24. brightness.set((pos.y as f32/height as f32)*100.0);
  25. }
  26. }
  27. },
  28. "hsl({hue}, 70%, {brightness}%)",
  29. }
  30. })
  31. }