feat: multiwindow support

This commit is contained in:
Jonathan Kelley committed 2022-12-30 22:05:15 -05:00
1 parent 3cfaaea7ea
commit 880aa737a6
5 files changed
+182 -76

No files matched your search

+29 -3
View File
@@ -1,14 +1,19 @@
use std::cell::RefCell;
use std::rc::Rc;
use crate::eval::EvalResult;
use crate::events::IpcMessage;
use crate::Config;
use dioxus_core::Component;
use dioxus_core::ScopeState;
use dioxus_core::VirtualDom;
use serde_json::Value;
use wry::application::event_loop::EventLoopProxy;
#[cfg(target_os = "ios")]
use wry::application::platform::ios::WindowExtIOS;
use wry::application::window::Fullscreen as WryFullscreen;
use wry::application::window::Window;
use wry::application::window::WindowId;
use wry::webview::WebView;
pub type ProxyType = EventLoopProxy<UserWindowEvent>;
@@ -20,6 +25,8 @@ pub fn use_window(cx: &ScopeState) -> &DesktopContext {
.unwrap()
}
pub type WebviewQueue = Rc<RefCell<Vec<(VirtualDom, crate::cfg::Config)>>>;
/// An imperative interface to the current window.
///
/// To get a handle to the current window, use the [`use_window`] hook.
@@ -43,6 +50,8 @@ pub struct DesktopContext {
/// The receiver for eval results since eval is async
pub(super) eval: tokio::sync::broadcast::Sender<Value>,
pub(super) pending_windows: WebviewQueue,
#[cfg(target_os = "ios")]
pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
}
@@ -57,16 +66,26 @@ impl std::ops::Deref for DesktopContext {
}
impl DesktopContext {
pub(crate) fn new(webview: Rc<WebView>, proxy: ProxyType) -> Self {
pub(crate) fn new(webview: Rc<WebView>, proxy: ProxyType, webviews: WebviewQueue) -> Self {
Self {
webview,
proxy,
eval: tokio::sync::broadcast::channel(8).0,
pending_windows: webviews,
#[cfg(target_os = "ios")]
views: Default::default(),
}
}
/// Create a new window using the props and window builder
pub fn new_window<T: 'static>(&self, app: Component<T>, props: T, cfg: Config) {
let dom = VirtualDom::new_with_props(app, props);
self.pending_windows.borrow_mut().push((dom, cfg));
self.proxy
.send_event(UserWindowEvent(EventData::NewWindow, self.id()))
.unwrap();
}
/// trigger the drag-window event
///
/// Moves the window with the left mouse button until the button is released.
@@ -91,7 +110,9 @@ impl DesktopContext {
/// close window
pub fn close(&self) {
let _ = self.proxy.send_event(UserWindowEvent::CloseWindow);
let _ = self
.proxy
.send_event(UserWindowEvent(EventData::CloseWindow, self.id()));
}
/// change window to fullscreen
@@ -189,11 +210,16 @@ impl DesktopContext {
}
#[derive(Debug)]
pub enum UserWindowEvent {
pub struct UserWindowEvent(pub EventData, pub WindowId);
#[derive(Debug)]
pub enum EventData {
Poll,
Ipc(IpcMessage),
NewWindow,
CloseWindow,
}