use std::{
cell::Cell,
fmt,
marker::PhantomData,
rc::{Rc, Weak},
};
use super::{
Answer, Component,
task::{Action, Executor},
};
use crate::{Command, JsonValue, Measurement, NodeId};
#[derive(Clone)]
pub struct ElementRef {
node: Rc<Cell<NodeId>>,
executor: Weak<Executor>,
}
impl ElementRef {
pub(crate) fn new(executor: &Rc<Executor>) -> Self {
Self {
node: Rc::new(Cell::new(0)),
executor: Rc::downgrade(executor),
}
}
pub fn node(&self) -> Option<NodeId> {
Some(self.node.get()).filter(|&n| n != 0)
}
pub(crate) fn attach(&self, node: NodeId) {
self.node.set(node);
}
pub(crate) fn detach(&self, node: NodeId) {
if self.node.get() == node {
self.node.set(0);
}
}
fn send(&self, command: Command) {
if let (Some(node), Some(executor)) = (self.node(), self.executor.upgrade()) {
executor.act(Action::Command(node, command));
}
}
pub fn focus(&self) {
self.send(Command::Focus);
}
pub fn blur(&self) {
self.send(Command::Blur);
}
pub fn set_accessibility_focus(&self) {
self.send(Command::AccessibilityFocus);
}
pub fn set_selection(&self, start: u32, end: u32) {
self.send(Command::SetSelection { start, end });
}
pub fn scroll_to(&self, x: f32, y: f32, animated: bool) {
self.send(Command::ScrollTo { x, y, animated });
}
pub fn scroll_to_offset(&self, offset: f32, animated: bool) {
self.send(Command::ScrollTo {
x: offset,
y: offset,
animated,
});
}
pub fn scroll_to_end(&self, animated: bool) {
self.send(Command::ScrollToEnd { animated });
}
pub fn scroll_to_index(
&self,
index: u32,
view_position: f32,
view_offset: f32,
animated: bool,
) {
self.send(Command::ScrollToIndex {
index,
animated,
view_position,
view_offset,
});
}
pub fn zoom_to(&self, scale: f32, center: Option<(f32, f32)>, animated: bool) {
self.send(Command::ZoomTo {
scale,
center,
animated,
});
}
pub fn reload(&self) {
self.send(Command::Reload);
}
pub fn go_back(&self) {
self.send(Command::GoBack);
}
pub fn go_forward(&self) {
self.send(Command::GoForward);
}
pub fn stop_loading(&self) {
self.send(Command::StopLoading);
}
pub fn inject_java_script(&self, script: impl Into<String>) {
self.send(Command::InjectJavaScript(script.into()));
}
pub fn post_message(&self, data: impl Into<String>) {
self.send(Command::PostMessage(data.into()));
}
pub fn seek(&self, seconds: f64) {
self.send(Command::Seek(seconds));
}
pub fn command(&self, name: &str, args: JsonValue) {
self.send(Command::Native {
name: name.into(),
args,
});
}
pub fn measure(&self) -> Answer<Option<Measurement>> {
let (answer, slot) = Answer::new();
match (self.node(), self.executor.upgrade()) {
(Some(node), Some(executor)) => executor.act(Action::Measure(node, slot)),
_ => slot.borrow_mut().deliver(None),
}
answer
}
}
impl PartialEq for ElementRef {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.node, &other.node)
}
}
impl fmt::Debug for ElementRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ElementRef").field(&self.node.get()).finish()
}
}
pub struct ComponentRef<C: Component> {
element: ElementRef,
_component: PhantomData<fn() -> C>,
}
impl<C: Component> ComponentRef<C> {
pub(crate) fn new(executor: &Rc<Executor>) -> Self {
Self {
element: ElementRef::new(executor),
_component: PhantomData,
}
}
pub(crate) fn element(&self) -> &ElementRef {
&self.element
}
pub fn node(&self) -> Option<NodeId> {
self.element.node()
}
pub fn api<A: super::Api>(&self) -> Option<Rc<A>> {
super::reactive::api_of::<A>(self.node()?)
}
}
impl<C: Component> Clone for ComponentRef<C> {
fn clone(&self) -> Self {
Self {
element: self.element.clone(),
_component: PhantomData,
}
}
}
impl<C: Component> PartialEq for ComponentRef<C> {
fn eq(&self, other: &Self) -> bool {
self.element == other.element
}
}
impl<C: Component> fmt::Debug for ComponentRef<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ComponentRef")
.field(&self.element.node.get())
.finish()
}
}