use std::{rc::Rc, sync::Arc};
use cranpose_core::{MutableState, NodeId, State};
use cranpose_foundation::modifier_element;
use crate::{
density::Density,
modifier::Modifier,
text::{TextLayoutOptions, TextOptions, TextOverflow, TextStyle},
text_modifier_node::{TextModifierElement, TextModifierNode, shared_text_style},
widgets::{layout::NodeModifier, nodes::LayoutNode},
};
#[derive(Clone)]
pub struct DynamicTextSource(Rc<dyn Fn() -> crate::text::AnnotatedString>);
impl DynamicTextSource {
pub fn new<F>(resolver: F) -> Self
where
F: Fn() -> crate::text::AnnotatedString + 'static,
{
Self(Rc::new(resolver))
}
fn resolve(&self) -> crate::text::AnnotatedString {
(self.0)()
}
}
impl PartialEq for DynamicTextSource {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
#[derive(Clone, PartialEq)]
pub enum TextSource {
Static(crate::text::AnnotatedString),
Dynamic(DynamicTextSource),
}
impl TextSource {
fn resolve(&self) -> crate::text::AnnotatedString {
match self {
Self::Static(text) => text.clone(),
Self::Dynamic(dynamic) => dynamic.resolve(),
}
}
}
#[doc(hidden)]
#[derive(Clone, Copy)]
pub enum StaticText<'a> {
Plain(&'a str),
Annotated(&'a crate::text::AnnotatedString),
}
impl StaticText<'_> {
pub(crate) fn is(self, shown: &crate::text::AnnotatedString) -> bool {
match self {
Self::Plain(text) => shown.is_plain(text),
Self::Annotated(text) => shown == text,
}
}
}
#[doc(hidden)]
pub trait IntoTextSource {
fn into_text_source(self) -> TextSource;
fn static_text(&self) -> Option<StaticText<'_>> {
None
}
}
impl IntoTextSource for String {
fn into_text_source(self) -> TextSource {
TextSource::Static(crate::text::AnnotatedString::from(self))
}
fn static_text(&self) -> Option<StaticText<'_>> {
Some(StaticText::Plain(self))
}
}
impl IntoTextSource for &str {
fn into_text_source(self) -> TextSource {
TextSource::Static(crate::text::AnnotatedString::from(self))
}
fn static_text(&self) -> Option<StaticText<'_>> {
Some(StaticText::Plain(self))
}
}
impl IntoTextSource for crate::text::AnnotatedString {
fn into_text_source(self) -> TextSource {
TextSource::Static(self)
}
fn static_text(&self) -> Option<StaticText<'_>> {
Some(StaticText::Annotated(self))
}
}
impl<T> IntoTextSource for State<T>
where
T: ToString + Clone + 'static,
{
fn into_text_source(self) -> TextSource {
let state = self;
TextSource::Dynamic(DynamicTextSource::new(move || {
crate::text::AnnotatedString::from(state.value().to_string())
}))
}
}
impl<T> IntoTextSource for MutableState<T>
where
T: ToString + Clone + 'static,
{
fn into_text_source(self) -> TextSource {
let state = self;
TextSource::Dynamic(DynamicTextSource::new(move || {
crate::text::AnnotatedString::from(state.value().to_string())
}))
}
}
impl<F> IntoTextSource for F
where
F: Fn() -> String + 'static,
{
fn into_text_source(self) -> TextSource {
TextSource::Dynamic(DynamicTextSource::new(move || {
crate::text::AnnotatedString::from(self())
}))
}
}
impl IntoTextSource for DynamicTextSource {
fn into_text_source(self) -> TextSource {
TextSource::Dynamic(self)
}
}
fn text_node_modifier(modifier: Modifier, element: TextModifierElement) -> Modifier {
modifier.then(Modifier::from_dyn_element(modifier_element(element)))
}
fn text_layout_inputs(
style: Arc<TextStyle>,
options: TextLayoutOptions,
) -> (Arc<TextStyle>, TextLayoutOptions) {
#[cfg(feature = "localization")]
let style = crate::localization::apply_shared_text_locale(style);
(style, options.normalized())
}
#[track_caller]
fn compose_text<S>(
text: S,
modifier: Modifier,
style: Arc<TextStyle>,
options: TextLayoutOptions,
) -> NodeId
where
S: IntoTextSource + Clone + PartialEq + 'static,
{
if let Some(fixed) = text.static_text()
&& crate::widgets::selection_container::current_selection_registrar().is_none()
{
return static_text::Text(fixed, modifier, style, options);
}
text_group::Text(text, modifier, style, options)
}
struct StaticTextModifier<'a> {
modifier: Modifier,
text: StaticText<'a>,
style: Arc<TextStyle>,
options: TextLayoutOptions,
density: Density,
}
impl StaticTextModifier<'_> {
fn shared_text(&self) -> crate::text::AnnotatedString {
match self.text {
StaticText::Plain(text) => crate::render_state::current_app_context()
.map_or_else(|| text.into(), |context| context.text.plain_text(text)),
StaticText::Annotated(text) => text.clone(),
}
}
fn element(&self) -> TextModifierElement {
TextModifierElement::with_shared_style(
self.shared_text(),
Arc::clone(&self.style),
self.options,
self.density,
)
}
fn show_in(&self, node: &LayoutNode) -> Option<bool> {
let last = node.node_after(&self.modifier)?;
let mut last = last.borrow_mut();
let text_node = last.as_any_mut().downcast_mut::<TextModifierNode>()?;
if text_node.shows(self.text, &self.style, self.options, self.density) {
return Some(false);
}
if cfg!(feature = "inspection") {
return None;
}
text_node.show(&self.shared_text(), &self.style, self.options, self.density);
Some(true)
}
}
impl NodeModifier for StaticTextModifier<'_> {
fn provided_locals(&self) -> Vec<cranpose_core::ProvidedValue> {
self.modifier.provided_composition_locals()
}
fn to_modifier(&self) -> Modifier {
text_node_modifier(self.modifier.clone(), self.element())
}
fn set_on(&mut self, node: &mut LayoutNode) {
match self.show_in(node) {
Some(true) => node.note_last_node_updated(),
Some(false) => {}
None => node.set_modifier(self.to_modifier()),
}
}
}
mod static_text {
use std::sync::Arc;
use cranpose_core::NodeId;
use super::{StaticText, StaticTextModifier, text_layout_inputs};
use crate::{
composable,
modifier::Modifier,
text::{TextLayoutOptions, TextStyle},
widgets::layout::{LayoutEnvironment, compose_empty_layout},
};
#[composable(no_skip)]
pub(super) fn Text(
text: StaticText<'_>,
modifier: Modifier,
style: Arc<TextStyle>,
options: TextLayoutOptions,
) -> NodeId {
let (style, options) = text_layout_inputs(style, options);
let environment = LayoutEnvironment::current();
compose_empty_layout(
StaticTextModifier {
modifier,
text,
style,
options,
density: environment.density,
},
environment,
)
}
}
mod text_group {
use std::sync::Arc;
use cranpose_core::NodeId;
use super::{
IntoTextSource, TextModifierElement, TextSource, text_layout_inputs, text_node_modifier,
};
use crate::{
composable,
modifier::Modifier,
text::{TextLayoutOptions, TextStyle},
widgets::layout::{LayoutEnvironment, compose_empty_layout},
};
#[composable]
pub(super) fn Text<S>(
text: S,
modifier: Modifier,
style: Arc<TextStyle>,
options: TextLayoutOptions,
) -> NodeId
where
S: IntoTextSource + Clone + PartialEq + 'static,
{
text_node(&text.into_text_source(), modifier, style, options)
}
#[inline(never)]
fn text_node(
source: &TextSource,
modifier: Modifier,
style: Arc<TextStyle>,
options: TextLayoutOptions,
) -> NodeId {
let (style, options) = text_layout_inputs(style, options);
let environment = LayoutEnvironment::current();
let text = source.resolve();
let modifier = match crate::widgets::selection_container::current_selection_registrar() {
Some(registrar) => modifier.then(crate::widgets::selection_container::selectable_text(
registrar,
text.clone(),
Arc::clone(&style),
options,
)),
None => modifier,
};
compose_empty_layout(
text_node_modifier(
modifier,
TextModifierElement::with_shared_style(text, style, options, environment.density),
),
environment,
)
}
}
#[track_caller]
#[expect(non_snake_case, reason = "names follow Compose's casing")]
pub fn BasicTextWithOptions<S>(
text: S,
modifier: Modifier,
style: TextStyle,
options: TextLayoutOptions,
) -> NodeId
where
S: IntoTextSource + Clone + PartialEq + 'static,
{
compose_text(text, modifier, shared_text_style(style), options)
}
#[track_caller]
#[expect(non_snake_case, reason = "names follow Compose's casing")]
pub fn BasicText<S>(
text: S,
modifier: Modifier,
style: TextStyle,
overflow: TextOverflow,
soft_wrap: bool,
max_lines: usize,
min_lines: usize,
) -> NodeId
where
S: IntoTextSource + Clone + PartialEq + 'static,
{
compose_text(
text,
modifier,
shared_text_style(style),
TextLayoutOptions {
overflow,
soft_wrap,
max_lines,
min_lines,
},
)
}
#[track_caller]
#[expect(non_snake_case, reason = "names follow Compose's casing")]
pub fn TextWithOptions<S>(
value: S,
modifier: Modifier,
style: TextStyle,
options: TextOptions,
) -> NodeId
where
S: IntoTextSource + Clone + PartialEq + 'static,
{
compose_text(
value,
modifier,
shared_text_style(style),
TextLayoutOptions::from(options),
)
}
#[track_caller]
#[expect(non_snake_case, reason = "names follow Compose's casing")]
pub fn Text<S>(value: S, modifier: Modifier, style: TextStyle) -> NodeId
where
S: IntoTextSource + Clone + PartialEq + 'static,
{
compose_text(
value,
modifier,
shared_text_style(style),
TextLayoutOptions::from(TextOptions::default()),
)
}
#[cfg(test)]
#[path = "tests/text_tests.rs"]
mod tests;