use std::sync::{Mutex};
use alchemy_styles::styles::{Appearance, Layout};
use alchemy_lifecycle::ComponentKey;
use alchemy_lifecycle::error::Error;
use alchemy_lifecycle::rsx::RSX;
use alchemy_lifecycle::traits::{Component, Props, PlatformSpecificNodeType};
#[cfg(feature = "cocoa")]
use alchemy_cocoa::text::{Text as PlatformTextBridge};
pub struct TextProps;
pub struct Text(Mutex<PlatformTextBridge>);
impl Text {
pub fn default_props() -> TextProps { TextProps {} }
}
impl Props for Text {
fn set_props(&mut self, _: &mut std::any::Any) {}
}
impl Component for Text {
fn new(_: ComponentKey) -> Text {
Text(Mutex::new(PlatformTextBridge::new()))
}
fn has_native_backing_node(&self) -> bool { true }
fn borrow_native_backing_node(&self) -> Option<PlatformSpecificNodeType> {
let bridge = self.0.lock().unwrap();
Some(bridge.borrow_native_backing_node())
}
fn apply_styles(&self, appearance: &Appearance, layout: &Layout) {
let mut bridge = self.0.lock().unwrap();
bridge.apply_styles(appearance, layout);
}
fn component_did_mount(&mut self) {
let mut bridge = self.0.lock().unwrap();
bridge.render();
}
fn render(&self, children: Vec<RSX>) -> Result<RSX, Error> {
let text = children.iter().map(|child| match child {
RSX::VirtualText(s) => s.0.to_owned(),
_ => String::new()
}).collect::<String>();
let mut bridge = self.0.lock().unwrap();
bridge.set_text(text);
Ok(RSX::None)
}
}