feat: add DesktopContext
This commit is contained in:
1 parent
169028705d
commit
31a2f8f63a
2 files changed
+41
-9
No files matched your search
@@ -4,25 +4,48 @@ use crate::UserWindowEvent;
|
|||||||
|
|
||||||
type ProxyType = EventLoopProxy<UserWindowEvent>;
|
type ProxyType = EventLoopProxy<UserWindowEvent>;
|
||||||
|
|
||||||
|
/// Desktop-Window handle api context
|
||||||
|
///
|
||||||
|
/// you can use this context control some window event
|
||||||
|
///
|
||||||
|
/// you can use `cx.consume_context::<DesktopContext>` to get this context
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// let desktop = cx.consume_context::<DesktopContext>().unwrap();
|
||||||
|
/// ```
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DesktopContext {
|
pub struct DesktopContext {
|
||||||
proxy: ProxyType,
|
proxy: ProxyType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DesktopContext {
|
impl DesktopContext {
|
||||||
|
|
||||||
pub fn new(proxy: ProxyType) -> Self {
|
pub fn new(proxy: ProxyType) -> Self {
|
||||||
Self { proxy }
|
Self { proxy }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// trigger the drag-window event
|
||||||
|
///
|
||||||
|
/// Moves the window with the left mouse button until the button is released.
|
||||||
|
///
|
||||||
|
/// you need use it in `onmousedown` event:
|
||||||
|
/// ```rust
|
||||||
|
/// onmousedown: move |_| { desktop.drag_window(); }
|
||||||
|
/// ```
|
||||||
pub fn drag_window(&self) {
|
pub fn drag_window(&self) {
|
||||||
let _ = self.proxy.send_event(UserWindowEvent::DragWindow);
|
let _ = self.proxy.send_event(UserWindowEvent::DragWindow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn minimized(&self, minimized: bool) {
|
pub fn minimize(&self, minimized: bool) {
|
||||||
let _ = self.proxy.send_event(UserWindowEvent::Minimized(minimized));
|
let _ = self.proxy.send_event(UserWindowEvent::Minimize(minimized));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn maximized(&self, maximized: bool) {
|
pub fn maximize(&self, maximized: bool) {
|
||||||
let _ = self.proxy.send_event(UserWindowEvent::Maximized(maximized));
|
let _ = self.proxy.send_event(UserWindowEvent::Maximize(maximized));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn close(&self) {
|
||||||
|
let _ = self.proxy.send_event(UserWindowEvent::CloseWindow);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -288,20 +288,28 @@ pub fn launch_with_props<P: 'static + Send>(
|
|||||||
// this loop just run once, because dioxus-desktop is unsupport multi-window.
|
// this loop just run once, because dioxus-desktop is unsupport multi-window.
|
||||||
for webview in desktop.webviews.values() {
|
for webview in desktop.webviews.values() {
|
||||||
let window = webview.window();
|
let window = webview.window();
|
||||||
window.drag_window().unwrap();
|
// start to drag the window.
|
||||||
|
// if the drag_window have any err. we don't do anything.
|
||||||
|
let _ = window.drag_window();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UserWindowEvent::Minimized(state) => {
|
UserWindowEvent::CloseWindow => {
|
||||||
|
// close window
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
}
|
||||||
|
UserWindowEvent::Minimize(state) => {
|
||||||
// this loop just run once, because dioxus-desktop is unsupport multi-window.
|
// this loop just run once, because dioxus-desktop is unsupport multi-window.
|
||||||
for webview in desktop.webviews.values() {
|
for webview in desktop.webviews.values() {
|
||||||
let window = webview.window();
|
let window = webview.window();
|
||||||
|
// change window minimized state.
|
||||||
window.set_minimized(state);
|
window.set_minimized(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UserWindowEvent::Maximized(state) => {
|
UserWindowEvent::Maximize(state) => {
|
||||||
// this loop just run once, because dioxus-desktop is unsupport multi-window.
|
// this loop just run once, because dioxus-desktop is unsupport multi-window.
|
||||||
for webview in desktop.webviews.values() {
|
for webview in desktop.webviews.values() {
|
||||||
let window = webview.window();
|
let window = webview.window();
|
||||||
|
// change window maximized state.
|
||||||
window.set_maximized(state);
|
window.set_maximized(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -320,8 +328,9 @@ pub fn launch_with_props<P: 'static + Send>(
|
|||||||
pub enum UserWindowEvent {
|
pub enum UserWindowEvent {
|
||||||
Update,
|
Update,
|
||||||
DragWindow,
|
DragWindow,
|
||||||
Minimized(bool),
|
CloseWindow,
|
||||||
Maximized(bool),
|
Minimize(bool),
|
||||||
|
Maximize(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DesktopController {
|
pub struct DesktopController {
|
||||||
|
|||||||
Reference in new issue
Block a user