1
0

overlay.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //! This example demonstrates how to create an overlay window with dioxus.
  2. //!
  3. //! Basically, we just create a new window with a transparent background and no decorations, size it to the screen, and
  4. //! then we can draw whatever we want on it. In this case, we're drawing a simple overlay with a draggable header.
  5. //!
  6. //! We also add a global shortcut to toggle the overlay on and off, so you could build a raycast-type app with this.
  7. use dioxus::desktop::{
  8. tao::dpi::PhysicalPosition, use_global_shortcut, LogicalSize, WindowBuilder,
  9. };
  10. use dioxus::prelude::*;
  11. fn main() {
  12. dioxus::LaunchBuilder::desktop()
  13. .with_cfg(make_config())
  14. .launch(app);
  15. }
  16. fn app() -> Element {
  17. let mut show_overlay = use_signal(|| true);
  18. _ = use_global_shortcut("cmd+g", move || show_overlay.toggle());
  19. rsx! {
  20. head::Link {
  21. rel: "stylesheet",
  22. href: asset!("./examples/assets/overlay.css"),
  23. }
  24. if show_overlay() {
  25. div {
  26. width: "100%",
  27. height: "100%",
  28. background_color: "red",
  29. border: "1px solid black",
  30. div {
  31. width: "100%",
  32. height: "10px",
  33. background_color: "black",
  34. onmousedown: move |_| dioxus::desktop::window().drag(),
  35. }
  36. "This is an overlay!"
  37. }
  38. }
  39. }
  40. }
  41. fn make_config() -> dioxus::desktop::Config {
  42. dioxus::desktop::Config::default().with_window(make_window())
  43. }
  44. fn make_window() -> WindowBuilder {
  45. WindowBuilder::new()
  46. .with_transparent(true)
  47. .with_decorations(false)
  48. .with_resizable(false)
  49. .with_always_on_top(true)
  50. .with_position(PhysicalPosition::new(0, 0))
  51. .with_max_inner_size(LogicalSize::new(100000, 50))
  52. }