overlay.rs 2.1 KB

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