colorpicker.rs 1.3 KB

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