1
0

overlay.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if show_overlay() {
  19. div {
  20. width: "100%",
  21. height: "100%",
  22. background_color: "red",
  23. border: "1px solid black",
  24. div {
  25. width: "100%",
  26. height: "10px",
  27. background_color: "black",
  28. onmousedown: move |_| dioxus::desktop::window().drag(),
  29. }
  30. "This is an overlay!"
  31. }
  32. }
  33. }
  34. }
  35. fn make_config() -> dioxus::desktop::Config {
  36. dioxus::desktop::Config::default()
  37. .with_window(make_window())
  38. .with_custom_head(
  39. r#"
  40. <style type="text/css">
  41. html, body {
  42. height: 100px;
  43. margin: 0;
  44. overscroll-behavior-y: none;
  45. overscroll-behavior-x: none;
  46. overflow: hidden;
  47. }
  48. #main, #bodywrap {
  49. height: 100%;
  50. margin: 0;
  51. overscroll-behavior-x: none;
  52. overscroll-behavior-y: none;
  53. }
  54. </style>
  55. "#
  56. .to_owned(),
  57. )
  58. }
  59. fn make_window() -> WindowBuilder {
  60. WindowBuilder::new()
  61. .with_transparent(true)
  62. .with_decorations(false)
  63. .with_resizable(false)
  64. .with_always_on_top(true)
  65. .with_position(PhysicalPosition::new(0, 0))
  66. .with_max_inner_size(LogicalSize::new(100000, 50))
  67. }