feat: add use_desktop_context hook
This commit is contained in:
1 parent
e4eb982a36
commit
366cf75887
2 files changed
+13
-7
No files matched your search
@@ -1,5 +1,4 @@
|
|||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use dioxus_desktop::desktop_context::DesktopContext;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dioxus::desktop::launch_cfg(app, |cfg| {
|
dioxus::desktop::launch_cfg(app, |cfg| {
|
||||||
@@ -8,16 +7,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: Scope) -> Element {
|
fn app(cx: Scope) -> Element {
|
||||||
let desktop = cx.consume_context::<DesktopContext>().unwrap();
|
let desktop = dioxus::desktop::desktop_context::use_desktop_context(&cx);
|
||||||
|
|
||||||
let drag = desktop.clone();
|
|
||||||
let close = desktop.clone();
|
|
||||||
|
|
||||||
cx.render(rsx!(
|
cx.render(rsx!(
|
||||||
link { href:"https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css", rel:"stylesheet" }
|
link { href:"https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css", rel:"stylesheet" }
|
||||||
header {
|
header {
|
||||||
class: "text-gray-400 bg-gray-900 body-font",
|
class: "text-gray-400 bg-gray-900 body-font",
|
||||||
onmousedown: move |_| drag.drag_window(),
|
onmousedown: move |_| desktop.drag_window(),
|
||||||
div {
|
div {
|
||||||
class: "container mx-auto flex flex-wrap p-5 flex-col md:flex-row items-center",
|
class: "container mx-auto flex flex-wrap p-5 flex-col md:flex-row items-center",
|
||||||
a { class: "flex title-font font-medium items-center text-white mb-4 md:mb-0",
|
a { class: "flex title-font font-medium items-center text-white mb-4 md:mb-0",
|
||||||
@@ -33,7 +29,7 @@ fn app(cx: Scope) -> Element {
|
|||||||
button {
|
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",
|
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",
|
||||||
onmousedown: |evt| evt.cancel_bubble(),
|
onmousedown: |evt| evt.cancel_bubble(),
|
||||||
onclick: move |_| close.close(),
|
onclick: move |_| desktop.close(),
|
||||||
"Close"
|
"Close"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use dioxus_core::ScopeState;
|
||||||
use wry::application::event_loop::EventLoopProxy;
|
use wry::application::event_loop::EventLoopProxy;
|
||||||
|
|
||||||
use crate::UserWindowEvent;
|
use crate::UserWindowEvent;
|
||||||
@@ -50,3 +53,10 @@ impl DesktopContext {
|
|||||||
let _ = self.proxy.send_event(UserWindowEvent::CloseWindow);
|
let _ = self.proxy.send_event(UserWindowEvent::CloseWindow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// use this function can get the `DesktopContext` context.
|
||||||
|
pub fn use_desktop_context(cx: &ScopeState) -> &Rc<DesktopContext> {
|
||||||
|
cx.use_hook(|_| cx.consume_context::<DesktopContext>())
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
Reference in new issue
Block a user