implement on mounted for desktop

This commit is contained in:
Evan Almloff committed 2023-03-20 16:10:34 -05:00
1 parent cb5cb56ad3
commit 7636c046fa
8 files changed
+450 -24

No files matched your search

+24 -12
View File
@@ -5,12 +5,15 @@ use euclid::Rect;
use std::{
any::Any,
fmt::{Display, Formatter},
future::Future,
pin::Pin,
rc::Rc,
};
/// An Element that has been rendered and allows reading and modifying information about it.
///
/// Different platforms will have different implementations and different levels of support for this trait. Renderers that do not support specific features will return `None` for those queries.
// we can not use async_trait here because it does not create a trait that is object safe
pub trait RenderedElementBacking {
/// Get the renderer specific element for the given id
fn get_raw_element(&self) -> MountedResult<&dyn Any> {
@@ -18,26 +21,35 @@ pub trait RenderedElementBacking {
}
/// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
fn get_client_rect(&self) -> MountedResult<Rect<f64, f64>> {
Err(MountedError::NotSupported)
#[allow(clippy::type_complexity)]
fn get_client_rect(&self) -> Pin<Box<dyn Future<Output = MountedResult<Rect<f64, f64>>>>> {
Box::pin(async { Err(MountedError::NotSupported) })
}
/// Scroll to make the element visible
fn scroll_to(&self, _behavior: ScrollBehavior) -> MountedResult<()> {
Err(MountedError::NotSupported)
fn scroll_to(
&self,
_behavior: ScrollBehavior,
) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
Box::pin(async { Err(MountedError::NotSupported) })
}
/// Set the focus on the element
fn set_focus(&self, _focus: bool) -> MountedResult<()> {
Err(MountedError::NotSupported)
fn set_focus(&self, _focus: bool) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
Box::pin(async { Err(MountedError::NotSupported) })
}
}
impl RenderedElementBacking for () {}
/// The way that scrolling should be performed
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum ScrollBehavior {
/// Scroll to the element immediately
#[cfg_attr(feature = "serialize", serde(rename = "instant"))]
Instant,
/// Scroll to the element smoothly
#[cfg_attr(feature = "serialize", serde(rename = "smooth"))]
Smooth,
}
@@ -62,18 +74,18 @@ impl MountedData {
}
/// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
pub fn get_client_rect(&self) -> MountedResult<Rect<f64, f64>> {
self.inner.get_client_rect()
pub async fn get_client_rect(&self) -> MountedResult<Rect<f64, f64>> {
self.inner.get_client_rect().await
}
/// Scroll to make the element visible
pub fn scroll_to(&self, behavior: ScrollBehavior) -> MountedResult<()> {
self.inner.scroll_to(behavior)
pub async fn scroll_to(&self, behavior: ScrollBehavior) -> MountedResult<()> {
self.inner.scroll_to(behavior).await
}
/// Set the focus on the element
pub fn set_focus(&self, focus: bool) -> MountedResult<()> {
self.inner.set_focus(focus)
pub async fn set_focus(&self, focus: bool) -> MountedResult<()> {
self.inner.set_focus(focus).await
}
}