use crate::prelude::*;
pub struct Label {
describing: Option<String>,
}
impl Label {
pub fn new<T>(cx: &mut Context, text: impl Res<T> + Clone + 'static) -> Handle<Self>
where
T: ToStringLocalized + 'static,
{
Self { describing: None }.build(cx, |_| {}).text(text.clone()).role(Role::Label).name(text)
}
pub fn rich<T>(
cx: &mut Context,
text: impl Res<T> + Clone + 'static,
children: impl Fn(&mut Context),
) -> Handle<Self>
where
T: ToStringLocalized + 'static,
{
Self { describing: None }
.build(cx, |cx| {
children(cx);
})
.text(text.clone())
.role(Role::Label)
.name(text)
}
}
impl Handle<'_, Label> {
pub fn describing(self, entity_identifier: impl Into<String>) -> Self {
let identifier = entity_identifier.into();
if let Some(id) = self.cx.resolve_entity_identifier(&identifier) {
let label_identifier = format!("{}", self.entity);
self.cx.entity_identifiers.insert(label_identifier.clone(), self.entity);
self.cx.style.labelled_by.insert(id, label_identifier);
}
self.modify(|label| label.describing = Some(identifier)).class("describing").hidden(true)
}
}
impl View for Label {
fn element(&self) -> Option<&'static str> {
Some("label")
}
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event {
WindowEvent::Press { .. } | WindowEvent::PressDown { .. } => {
if cx.current() == cx.mouse.left.pressed && meta.target == cx.current() {
if let Some(describing) = self
.describing
.as_ref()
.and_then(|identity| cx.resolve_entity_identifier(identity))
{
let old = cx.current;
cx.current = describing;
cx.focus_with_visibility(false);
let message = if matches!(window_event, WindowEvent::Press { .. }) {
WindowEvent::Press { mouse: false }
} else {
WindowEvent::PressDown { mouse: false }
};
cx.emit_to(describing, message);
cx.current = old;
}
}
}
_ => {}
});
}
}
pub struct TextSpan {}
impl TextSpan {
pub fn new<'a>(
cx: &'a mut Context,
text: &str,
children: impl Fn(&mut Context),
) -> Handle<'a, Self> {
Self {}
.build(cx, |cx| {
cx.style.text_span.insert(cx.current(), true);
children(cx);
})
.text(text.to_string())
.display(Display::None)
.pointer_events(PointerEvents::None)
}
}
impl View for TextSpan {
fn element(&self) -> Option<&'static str> {
Some("text-span")
}
}