1
0

overlay.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. LaunchBuilder::desktop().with_cfg(make_config()).launch(app);
  13. }
  14. fn app() -> Element {
  15. let mut show_overlay = use_signal(|| true);
  16. _ = use_global_shortcut("cmd+g", move || show_overlay.toggle());
  17. rsx! {
  18. head::Link {
  19. rel: "stylesheet",
  20. href: asset!("./examples/assets/overlay.css"),
  21. }
  22. if show_overlay() {
  23. div {
  24. width: "100%",
  25. height: "100%",
  26. background_color: "red",
  27. border: "1px solid black",
  28. div {
  29. width: "100%",
  30. height: "10px",
  31. background_color: "black",
  32. onmousedown: move |_| dioxus::desktop::window().drag(),
  33. }
  34. "This is an overlay!"
  35. }
  36. }
  37. }
  38. }
  39. fn make_config() -> dioxus::desktop::Config {
  40. dioxus::desktop::Config::default().with_window(make_window())
  41. }
  42. fn make_window() -> WindowBuilder {
  43. WindowBuilder::new()
  44. .with_transparent(true)
  45. .with_decorations(false)
  46. .with_resizable(false)
  47. .with_always_on_top(true)
  48. .with_position(PhysicalPosition::new(0, 0))
  49. .with_max_inner_size(LogicalSize::new(100000, 50))
  50. }