use crate::*;
impl IntoNode for VirtualNode {
fn into_node(self) -> VirtualNode {
self
}
}
impl IntoNode for Vec<VirtualNode> {
fn into_node(self) -> VirtualNode {
if self.is_empty() {
VirtualNode::Empty
} else {
VirtualNode::Fragment(self)
}
}
}
impl IntoNode for Option<VirtualNode> {
fn into_node(self) -> VirtualNode {
match self {
Some(node) => node,
None => VirtualNode::Empty,
}
}
}
impl IntoNode for Option<Vec<VirtualNode>> {
fn into_node(self) -> VirtualNode {
match self {
Some(nodes) => nodes.into_node(),
None => VirtualNode::Empty,
}
}
}
impl<F> IntoNode for F
where
F: FnMut() -> VirtualNode + 'static,
{
fn into_node(self) -> VirtualNode {
let render_inner: Rc<RefCell<RenderFnInner>> =
Rc::new(RefCell::new(RenderFnInner::new(Box::new(self))));
VirtualNode::Dynamic(DynamicNode::new(
render_inner,
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 + Display + 'static,
{
fn into_node(self) -> VirtualNode {
self.as_reactive_text()
}
}
impl<T> AsReactiveText for Signal<T>
where
T: Clone + PartialEq + Display + 'static,
{
fn as_reactive_text(&self) -> VirtualNode {
let initial: String = self.get().to_string();
let string_signal: Signal<String> = Signal::create(initial.clone());
let string_signal_clone: Signal<String> = string_signal;
self.replace_subscribe({
let source: Signal<T> = *self;
move || {
let new_value: String = source.get().to_string();
string_signal_clone.set_silent(new_value);
}
});
VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
}
}
impl<T> EventAdapter<T> {
pub(crate) fn into_inner(self) -> T {
self.inner
}
}
impl<F> EventAdapter<F>
where
F: FnMut(Event) + 'static,
{
pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
AttributeValue::Event(NativeEventHandler::create(event_name, self.into_inner()))
}
}
impl EventAdapter<NativeEventHandler> {
pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
let mut handler: NativeEventHandler = self.into_inner();
handler.set_event_name(event_name);
AttributeValue::Event(handler)
}
}
impl EventAdapter<Option<NativeEventHandler>> {
pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
match self.into_inner() {
Some(handler) => EventAdapter::new(handler).into_attribute(event_name),
None => AttributeValue::Text(String::new()),
}
}
}
impl EventAdapter<Option<Rc<dyn Fn(Event)>>> {
pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
match self.into_inner() {
Some(callback) => {
let wrapper = move |event: Event| {
callback(event);
};
AttributeValue::Event(NativeEventHandler::create(event_name, wrapper))
}
None => AttributeValue::Text(String::new()),
}
}
}
impl<T> AttrValueAdapter<T> {
pub(crate) fn into_inner(self) -> T {
self.inner
}
}
impl<F> AttrValueAdapter<F>
where
F: FnMut(Event) + '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: &'static str) -> AttributeValue {
AttributeValue::Event(NativeEventHandler::create(name, self.into_inner()))
}
}
impl AttrValueAdapter<NativeEventHandler> {
pub fn into_callback_attribute_value(self) -> AttributeValue {
AttributeValue::Event(self.into_inner())
}
pub fn into_callback_attribute_value_with_name(self, name: &'static str) -> AttributeValue {
let mut handler: NativeEventHandler = self.into_inner();
handler.set_event_name(name);
AttributeValue::Event(handler)
}
}
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: &'static str) -> 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()
}
}