dioxus/examples/window_event.rs

137 lines
4.5 KiB
Rust
Raw Normal View History

2024-02-14 20:33:07 +00:00
//! This example demonstrates how to handle window events and change window properties.
//!
//! We're able to do things like:
//! - implement window dragging
//! - toggle fullscreen
//! - toggle always on top
//! - toggle window decorations
//! - change the window title
//!
//! The entire featuresuite of wry and tao is available to you
2024-01-20 00:36:40 +00:00
use dioxus::desktop::{window, Config, WindowBuilder};
2022-02-11 03:21:25 +00:00
use dioxus::prelude::*;
fn main() {
2024-01-18 20:32:01 +00:00
LaunchBuilder::desktop()
.with_cfg(
Config::new().with_window(
WindowBuilder::new()
.with_title("Borderless Window")
.with_decorations(false),
),
2024-01-15 21:06:05 +00:00
)
2024-01-18 20:32:01 +00:00
.launch(app)
2022-02-11 03:21:25 +00:00
}
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx!(
head::Link { href: "https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css", rel: "stylesheet" }
2024-02-14 20:33:07 +00:00
Header {}
div { class: "container mx-auto",
div { class: "grid grid-cols-5",
SetOnTop {}
SetDecorations {}
SetTitle {}
}
}
2024-02-14 20:33:07 +00:00
)
}
#[component]
fn Header() -> Element {
let mut fullscreen = use_signal(|| false);
rsx! {
header { class: "text-gray-400 bg-gray-900 body-font", onmousedown: move |_| window().drag(),
2024-01-15 21:06:05 +00:00
div { class: "container mx-auto flex flex-wrap p-5 flex-col md:flex-row items-center",
2022-02-11 03:21:25 +00:00
a { class: "flex title-font font-medium items-center text-white mb-4 md:mb-0",
span { class: "ml-3 text-xl", "Dioxus" }
2022-02-11 03:21:25 +00:00
}
nav { class: "md:ml-auto flex flex-wrap items-center text-base justify-center" }
2024-02-14 20:33:07 +00:00
// Set the window to minimized
2022-02-11 03:21:25 +00:00
button {
class: "inline-flex items-center bg-gray-800 border-0 py-1 px-3 focus:outline-none hover:bg-gray-700 rounded text-base mt-4 md:mt-0",
2023-01-01 20:41:18 +00:00
onmousedown: |evt| evt.stop_propagation(),
2024-01-15 21:06:05 +00:00
onclick: move |_| window().set_minimized(true),
2022-02-11 03:21:25 +00:00
"Minimize"
}
2024-02-14 20:33:07 +00:00
// Toggle fullscreen
2022-02-11 03:21:25 +00:00
button {
class: "inline-flex items-center bg-gray-800 border-0 py-1 px-3 focus:outline-none hover:bg-gray-700 rounded text-base mt-4 md:mt-0",
2023-01-01 20:41:18 +00:00
onmousedown: |evt| evt.stop_propagation(),
2022-02-11 03:21:25 +00:00
onclick: move |_| {
window().set_fullscreen(!fullscreen());
window().set_resizable(fullscreen());
2024-01-15 21:06:05 +00:00
fullscreen.toggle();
2022-02-11 03:21:25 +00:00
},
"Fullscreen"
}
2024-02-14 20:33:07 +00:00
// Close the window
// If the window is the last window open, the app will close, if you configured the close behavior to do so
2022-02-11 03:21:25 +00:00
button {
class: "inline-flex items-center bg-gray-800 border-0 py-1 px-3 focus:outline-none hover:bg-gray-700 rounded text-base mt-4 md:mt-0",
2023-01-01 20:41:18 +00:00
onmousedown: |evt| evt.stop_propagation(),
2024-01-15 21:06:05 +00:00
onclick: move |_| window().close(),
2022-02-11 03:21:25 +00:00
"Close"
}
}
}
2024-02-14 20:33:07 +00:00
}
}
#[component]
fn SetOnTop() -> Element {
let mut always_on_top = use_signal(|| false);
rsx! {
div {
button {
class: "inline-flex items-center text-white bg-green-500 border-0 py-1 px-3 hover:bg-green-700 rounded",
onmousedown: |evt| evt.stop_propagation(),
onclick: move |_| {
window().set_always_on_top(!always_on_top());
always_on_top.toggle();
},
"Always On Top"
2022-02-11 03:21:25 +00:00
}
}
2024-02-14 20:33:07 +00:00
}
}
#[component]
fn SetDecorations() -> Element {
let mut decorations = use_signal(|| false);
rsx! {
div {
button {
class: "inline-flex items-center text-white bg-blue-500 border-0 py-1 px-3 hover:bg-green-700 rounded",
onmousedown: |evt| evt.stop_propagation(),
onclick: move |_| {
window().set_decorations(!decorations());
decorations.toggle();
},
"Set Decorations"
}
}
}
}
#[component]
fn SetTitle() -> Element {
rsx! {
div {
button {
class: "inline-flex items-center text-white bg-blue-500 border-0 py-1 px-3 hover:bg-green-700 rounded",
onmousedown: |evt| evt.stop_propagation(),
onclick: move |_| window().set_title("Dioxus Application"),
"Change Title"
}
}
}
2022-02-11 03:21:25 +00:00
}