use crate::*;
impl AsNode for VirtualNode {
fn as_node(&self) -> Option<VirtualNode> {
Some(self.clone())
}
}
impl AsNode for &VirtualNode {
fn as_node(&self) -> Option<VirtualNode> {
Some((*self).clone())
}
}
impl AsNode for String {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.clone(), None)))
}
}
impl AsNode for &str {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl AsNode for i32 {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl AsNode for i64 {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl AsNode for usize {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl AsNode for f32 {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl AsNode for f64 {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl AsNode for bool {
fn as_node(&self) -> Option<VirtualNode> {
Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
}
}
impl<T> AsNode for Signal<T>
where
T: Clone + PartialEq + std::fmt::Display + 'static,
{
fn as_node(&self) -> Option<VirtualNode> {
Some(self.as_reactive_text())
}
}
impl IntoNode for VirtualNode {
fn into_node(self) -> VirtualNode {
self
}
}
impl<F> IntoNode for F
where
F: FnMut() -> VirtualNode + 'static,
{
fn into_node(self) -> VirtualNode {
VirtualNode::Dynamic(DynamicNode {
render_fn: Rc::new(RefCell::new(self)),
hook_context: crate::reactive::create_hook_context(),
})
}
}
impl IntoNode for String {
fn into_node(self) -> VirtualNode {
VirtualNode::Text(TextNode::new(self, None))
}
}
impl IntoNode for &str {
fn into_node(self) -> VirtualNode {
VirtualNode::Text(TextNode::new(self.to_string(), None))
}
}
impl IntoNode for i32 {
fn into_node(self) -> VirtualNode {
VirtualNode::Text(TextNode::new(self.to_string(), None))
}
}
impl IntoNode for usize {
fn into_node(self) -> VirtualNode {
VirtualNode::Text(TextNode::new(self.to_string(), None))
}
}
impl IntoNode for bool {
fn into_node(self) -> VirtualNode {
VirtualNode::Text(TextNode::new(self.to_string(), None))
}
}
impl<T> IntoNode for Signal<T>
where
T: Clone + PartialEq + std::fmt::Display + 'static,
{
fn into_node(self) -> VirtualNode {
self.as_reactive_text()
}
}
impl<T> AsReactiveText for Signal<T>
where
T: Clone + PartialEq + std::fmt::Display + 'static,
{
fn as_reactive_text(&self) -> VirtualNode {
let signal: Signal<T> = *self;
let initial: String = signal.get().to_string();
let string_signal: Signal<String> = {
let boxed: Box<SignalInner<String>> = Box::new(SignalInner::new(initial.clone()));
Signal::from_inner(Box::leak(boxed) as *mut SignalInner<String>)
};
let source_signal: Signal<T> = *self;
let string_signal_clone: Signal<String> = string_signal;
source_signal.subscribe({
let source_signal: Signal<T> = source_signal;
move || {
let new_value: String = source_signal.get().to_string();
string_signal_clone.set(new_value);
}
});
VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
}
}
impl<T> EventAdapter<T> {
pub fn new(inner: T) -> Self {
EventAdapter { inner }
}
pub(crate) fn into_inner(self) -> T {
self.inner
}
}
impl<F> EventAdapter<F>
where
F: FnMut(NativeEvent) + 'static,
{
pub fn into_attribute(self, event_name: NativeEventName) -> AttributeValue {
AttributeValue::Event(NativeEventHandler::new(event_name, self.into_inner()))
}
}
impl EventAdapter<NativeEventHandler> {
pub fn into_attribute(self, event_name: NativeEventName) -> AttributeValue {
let handler: NativeEventHandler = self.into_inner();
AttributeValue::Event(NativeEventHandler::new(
event_name,
move |event: NativeEvent| {
handler.handle(event);
},
))
}
}
impl EventAdapter<Option<NativeEventHandler>> {
pub fn into_attribute(self, event_name: NativeEventName) -> AttributeValue {
match self.into_inner() {
Some(handler) => EventAdapter::new(handler).into_attribute(event_name),
None => AttributeValue::Text(String::new()),
}
}
}
impl<T> AttrValueAdapter<T> {
pub fn new(inner: T) -> Self {
AttrValueAdapter { inner }
}
pub(crate) fn into_inner(self) -> T {
self.inner
}
}
impl<F> AttrValueAdapter<F>
where
F: FnMut(NativeEvent) + 'static,
{
pub fn into_callback_attribute_value(self) -> AttributeValue {
self.into_inner().into_callback_attribute()
}
pub fn into_callback_attribute_value_with_name(self, name: String) -> AttributeValue {
AttributeValue::Event(NativeEventHandler::new(
NativeEventName::Other(name),
self.into_inner(),
))
}
}
impl AttrValueAdapter<NativeEventHandler> {
pub fn into_callback_attribute_value(self) -> AttributeValue {
let handler: NativeEventHandler = self.into_inner();
AttributeValue::Event(NativeEventHandler::new(
NativeEventName::Other("callback".to_string()),
move |event: NativeEvent| {
handler.handle(event);
},
))
}
pub fn into_callback_attribute_value_with_name(self, name: String) -> AttributeValue {
let handler: NativeEventHandler = self.into_inner();
AttributeValue::Event(NativeEventHandler::new(
NativeEventName::Other(name),
move |event: NativeEvent| {
handler.handle(event);
},
))
}
}
impl AttrValueAdapter<Option<NativeEventHandler>> {
pub fn into_callback_attribute_value(self) -> AttributeValue {
match self.into_inner() {
Some(handler) => AttrValueAdapter::new(handler).into_callback_attribute_value(),
None => AttributeValue::Text(String::new()),
}
}
pub fn into_callback_attribute_value_with_name(self, name: String) -> AttributeValue {
match self.into_inner() {
Some(handler) => {
AttrValueAdapter::new(handler).into_callback_attribute_value_with_name(name)
}
None => AttributeValue::Text(String::new()),
}
}
}
impl<T> AttrValueAdapter<T>
where
T: IntoReactiveValue,
{
pub fn into_reactive_attribute_value(self) -> AttributeValue {
self.into_inner().into_reactive_value()
}
}