overlay.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, HotKeyState, 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 |state| {
  19. if state == HotKeyState::Pressed {
  20. show_overlay.toggle();
  21. }
  22. });
  23. rsx! {
  24. document::Link {
  25. rel: "stylesheet",
  26. href: asset!("/examples/assets/overlay.css"),
  27. }
  28. if show_overlay() {
  29. div {
  30. width: "100%",
  31. height: "100%",
  32. background_color: "red",
  33. border: "1px solid black",
  34. div {
  35. width: "100%",
  36. height: "10px",
  37. background_color: "black",
  38. onmousedown: move |_| dioxus::desktop::window().drag(),
  39. }
  40. "This is an overlay!"
  41. }
  42. }
  43. }
  44. }
  45. fn make_config() -> dioxus::desktop::Config {
  46. dioxus::desktop::Config::default().with_window(make_window())
  47. }
  48. fn make_window() -> WindowBuilder {
  49. WindowBuilder::new()
  50. .with_transparent(true)
  51. .with_decorations(false)
  52. .with_resizable(false)
  53. .with_always_on_top(true)
  54. .with_position(PhysicalPosition::new(0, 0))
  55. .with_max_inner_size(LogicalSize::new(100000, 50))
  56. }