use std::sync::Arc;
use super::LayoutEngine;
use crate::{
layout::{Size, layout_result::LayoutResult},
node::{
node::Node,
node_kind::{NodeKind, TextInputOverflow, TextInputOverflowAnchor, TextInputState, TextState},
text_selection::CaretPositions,
},
};
pub(crate) enum TextLayoutOutput {
Text(TextOutput),
TextInput(TextInputOutput),
}
pub(crate) struct TextOutput {
pub(super) display_text: Option<Arc<str>>,
pub(super) render_wrap: bool,
pub(super) caret_positions: Option<CaretPositions>,
}
impl TextLayoutOutput {
pub(crate) fn estimated_memory_bytes(&self) -> usize {
let heap = match self {
Self::Text(output) => {
output.display_text.as_ref().map_or(0, |text| text.len())
+ output
.caret_positions
.as_ref()
.map_or(0, CaretPositions::estimated_shared_memory_bytes)
}
Self::TextInput(output) => output.caret_positions.estimated_shared_memory_bytes(),
};
2 * std::mem::size_of::<usize>() + std::mem::size_of::<Self>() + heap
}
}
impl TextOutput {
fn apply(&self, state: &TextState) {
state.set_layout_output(
self.display_text.as_ref(),
self.render_wrap,
self.caret_positions.as_ref(),
);
}
}
pub(crate) struct TextInputOutput {
pub(super) caret_positions: CaretPositions,
pub(super) size: Size,
pub(super) text_width: f32,
pub(super) text_height: f32,
pub(super) line_height: f32,
pub(super) text_band: Option<(f32, f32)>,
}
impl TextInputOutput {
fn apply(&self, state: &TextInputState) {
state.set_caret_positions(self.caret_positions.clone());
state.set_caret_height(self.line_height);
state.set_text_band(self.text_band);
state.sync_caret_metrics_to_position(self.line_height);
match state.overflow() {
TextInputOverflow::Scroll => {
state.set_scroll_x(self.horizontal_scroll(state));
state.set_scroll_y(0.0);
}
TextInputOverflow::Multiline => {
state.set_scroll_x(0.0);
state.set_scroll_y(self.vertical_scroll(state));
}
}
}
fn horizontal_scroll(&self, state: &TextInputState) -> f32 {
let caret_x = state.caret_x() + state.scroll_x();
let caret_width = 1.0;
let max_scroll = (self.text_width + caret_width - self.size.width).max(0.0);
if !state.is_focused() {
return match state.unfocused_overflow_anchor() {
TextInputOverflowAnchor::Start => 0.0,
TextInputOverflowAnchor::End => max_scroll,
};
}
let scroll_x = state.scroll_x().min(max_scroll);
if caret_x < scroll_x {
caret_x
} else if caret_x + caret_width > scroll_x + self.size.width {
(caret_x + caret_width - self.size.width).min(max_scroll)
} else {
scroll_x
}
}
fn vertical_scroll(&self, state: &TextInputState) -> f32 {
let caret_y = state.caret_y() + state.scroll_y();
let max_scroll = (self.text_height - self.size.height).max(0.0);
let scroll_y = state.scroll_y().min(max_scroll);
if caret_y < scroll_y {
caret_y
} else if caret_y + self.line_height > scroll_y + self.size.height {
(caret_y + self.line_height - self.size.height).min(max_scroll)
} else {
scroll_y
}
}
}
impl LayoutEngine {
pub(super) fn restore_text_layout_tree(node: &Node, result: &LayoutResult) {
Self::restore_text_layout_output(node, result);
for (child, child_layout) in node.children().iter().zip(&result.children) {
Self::restore_text_layout_tree(child, &child_layout.result);
}
}
pub(super) fn restore_text_layout_output(node: &Node, result: &LayoutResult) {
let Some(output) = result.text_layout.as_deref() else {
return;
};
match (node.node_kind(), output) {
(NodeKind::Text { state, .. }, TextLayoutOutput::Text(output)) => output.apply(state),
#[cfg(feature = "markdown")]
(NodeKind::RichText { state, .. }, TextLayoutOutput::Text(output)) => output.apply(state),
(NodeKind::TextInput { state, .. }, TextLayoutOutput::TextInput(output)) => output.apply(state),
_ => {}
}
}
pub(super) fn text_result(state: &TextState, size: Size, output: TextOutput) -> LayoutResult {
output.apply(state);
Self::leaf_with_output(size, TextLayoutOutput::Text(output))
}
pub(super) fn text_input_result(state: &TextInputState, output: TextInputOutput) -> LayoutResult {
output.apply(state);
Self::leaf_with_output(output.size, TextLayoutOutput::TextInput(output))
}
fn leaf_with_output(size: Size, output: TextLayoutOutput) -> LayoutResult {
LayoutResult {
size,
children: vec![],
text_layout: Some(Arc::new(output)),
dropped: false,
}
}
}