colorpicker.rs 1.3 KB

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