* progress: reworked don't run this, it'll kill your web browser * feat: use_eval but with comms * revision: async recv & recv_sync * revision: use_eval * revision: standard eval interface * revision: use serde_json::Value instead of JsValue * revision: docs * revision: error message * create: desktop eval (wip) * fix: desktop eval * revision: wrap use_eval in Rc<RefCell<_>> * fix: fmt, clippy * fix: desktop tests * revision: change to channel system - fixes clippy errors - fixes playwright tests * fix: tests * fix: eval example * fix: fmt * fix: tests, desktop stuff * fix: tests * feat: drop handler * fix: tests * fix: rustfmt * revision: web promise/callback system * fix: recv error * revision: IntoFuture, functionation * fix: ci * revision: playwright web * remove: unescessary code * remove dioxus-html from public examples * prototype-patch * fix web eval * fix: rustfmt * fix: CI * make use_eval more efficient * implement eval for liveview as well * fix playwright tests * fix clippy * more clippy fixes * fix clippy * fix stack overflow * fix desktop mock * fix clippy --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
121 lines
3.7 KiB
Rust
121 lines
3.7 KiB
Rust
use dioxus_core::ElementId;
|
|
use dioxus_html::{geometry::euclid::Rect, MountedResult, RenderedElementBacking};
|
|
|
|
use crate::{desktop_context::DesktopContext, query::QueryEngine};
|
|
|
|
/// A mounted element passed to onmounted events
|
|
pub struct DesktopElement {
|
|
id: ElementId,
|
|
webview: DesktopContext,
|
|
query: QueryEngine,
|
|
}
|
|
|
|
impl DesktopElement {
|
|
pub(crate) fn new(id: ElementId, webview: DesktopContext, query: QueryEngine) -> Self {
|
|
Self { id, webview, query }
|
|
}
|
|
}
|
|
|
|
impl RenderedElementBacking for DesktopElement {
|
|
fn get_raw_element(&self) -> dioxus_html::MountedResult<&dyn std::any::Any> {
|
|
Ok(self)
|
|
}
|
|
|
|
fn get_client_rect(
|
|
&self,
|
|
) -> std::pin::Pin<
|
|
Box<
|
|
dyn futures_util::Future<
|
|
Output = dioxus_html::MountedResult<dioxus_html::geometry::euclid::Rect<f64, f64>>,
|
|
>,
|
|
>,
|
|
> {
|
|
let script = format!("return window.interpreter.GetClientRect({});", self.id.0);
|
|
|
|
let fut = self
|
|
.query
|
|
.new_query::<Option<Rect<f64, f64>>>(&script, self.webview.clone())
|
|
.resolve();
|
|
Box::pin(async move {
|
|
match fut.await {
|
|
Ok(Some(rect)) => Ok(rect),
|
|
Ok(None) => MountedResult::Err(dioxus_html::MountedError::OperationFailed(
|
|
Box::new(DesktopQueryError::FailedToQuery),
|
|
)),
|
|
Err(err) => {
|
|
MountedResult::Err(dioxus_html::MountedError::OperationFailed(Box::new(err)))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
fn scroll_to(
|
|
&self,
|
|
behavior: dioxus_html::ScrollBehavior,
|
|
) -> std::pin::Pin<Box<dyn futures_util::Future<Output = dioxus_html::MountedResult<()>>>> {
|
|
let script = format!(
|
|
"return window.interpreter.ScrollTo({}, {});",
|
|
self.id.0,
|
|
serde_json::to_string(&behavior).expect("Failed to serialize ScrollBehavior")
|
|
);
|
|
|
|
let fut = self
|
|
.query
|
|
.new_query::<bool>(&script, self.webview.clone())
|
|
.resolve();
|
|
Box::pin(async move {
|
|
match fut.await {
|
|
Ok(true) => Ok(()),
|
|
Ok(false) => MountedResult::Err(dioxus_html::MountedError::OperationFailed(
|
|
Box::new(DesktopQueryError::FailedToQuery),
|
|
)),
|
|
Err(err) => {
|
|
MountedResult::Err(dioxus_html::MountedError::OperationFailed(Box::new(err)))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
fn set_focus(
|
|
&self,
|
|
focus: bool,
|
|
) -> std::pin::Pin<Box<dyn futures_util::Future<Output = dioxus_html::MountedResult<()>>>> {
|
|
let script = format!(
|
|
"return window.interpreter.SetFocus({}, {});",
|
|
self.id.0, focus
|
|
);
|
|
|
|
let fut = self
|
|
.query
|
|
.new_query::<bool>(&script, self.webview.clone())
|
|
.resolve();
|
|
|
|
Box::pin(async move {
|
|
match fut.await {
|
|
Ok(true) => Ok(()),
|
|
Ok(false) => MountedResult::Err(dioxus_html::MountedError::OperationFailed(
|
|
Box::new(DesktopQueryError::FailedToQuery),
|
|
)),
|
|
Err(err) => {
|
|
MountedResult::Err(dioxus_html::MountedError::OperationFailed(Box::new(err)))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum DesktopQueryError {
|
|
FailedToQuery,
|
|
}
|
|
|
|
impl std::fmt::Display for DesktopQueryError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
DesktopQueryError::FailedToQuery => write!(f, "Failed to query the element"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for DesktopQueryError {}
|