use std::sync::Arc;
use super::{LayoutEngine, ResolvedPadding};
use crate::{
layout::{
Size,
layout_result::{LayoutResult, SubtreeHolds},
},
node::{
node::Node,
node_kind::{NodeKind, PaintedCarets, TextInputOverflow, TextInputOverflowAnchor, TextInputState, TextState},
text_selection::CaretPositions,
},
};
pub(crate) enum TextLayoutOutput {
Text(TextOutput),
TextInput(TextInputOutput),
#[cfg(feature = "markdown")]
InlineLink(super::inline_links::InlineLinkOutput),
}
pub(crate) struct TextOutput {
pub(super) display_text: Option<Arc<str>>,
pub(super) render_wrap: bool,
pub(super) carets: Option<PaintedCarets>,
pub(super) text_top: f32,
pub(super) decoration: Option<Arc<super::text_decorations::TextDecorationLayout>>,
pub(super) inline: Option<Arc<super::inline_links::InlineTextLayout>>,
}
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
.carets
.as_ref()
.map_or(0, |carets| carets.positions.estimated_shared_memory_bytes())
+ output
.decoration
.as_ref()
.map_or(0, |decoration| decoration.estimated_memory_bytes())
+ output.inline_memory_bytes()
}
#[cfg(feature = "markdown")]
Self::InlineLink(output) => output.estimated_memory_bytes(),
Self::TextInput(output) => {
output.caret_positions.estimated_shared_memory_bytes()
+ output
.token_layout
.as_ref()
.map_or(0, super::input_token_layout::TokenLayout::estimated_shared_memory_bytes)
}
};
2 * std::mem::size_of::<usize>() + std::mem::size_of::<Self>() + heap
}
}
impl TextOutput {
pub(super) fn with_inline(&self, inline: Arc<super::inline_links::InlineTextLayout>) -> Self {
Self {
display_text: self.display_text.clone(),
render_wrap: self.render_wrap,
carets: self.carets.clone(),
text_top: inline.top,
decoration: self.decoration.clone(),
inline: Some(inline),
}
}
pub(super) fn with_decoration(&self, decoration: Arc<super::text_decorations::TextDecorationLayout>) -> Self {
Self {
display_text: self.display_text.clone(),
render_wrap: self.render_wrap,
carets: self.carets.clone(),
text_top: self.text_top,
decoration: Some(decoration),
inline: self.inline.clone(),
}
}
pub(super) fn with_selection(&self, carets: PaintedCarets, text_top: f32) -> Self {
Self {
display_text: self.display_text.clone(),
render_wrap: self.render_wrap,
carets: Some(carets),
text_top,
decoration: self.decoration.clone(),
inline: self.inline.clone(),
}
}
fn inline_memory_bytes(&self) -> usize {
self.inline.as_ref().map_or(0, |inline| inline.estimated_memory_bytes())
}
fn apply(&self, state: &TextState) {
state.set_layout_output(
self.display_text.as_ref(),
self.render_wrap,
self.carets.as_ref(),
self.text_top,
);
}
}
pub(crate) struct TextInputOutput {
pub(super) token_layout: Option<Arc<super::input_token_layout::TokenLayout>>,
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)>,
pub(super) caret_width: f32,
pub(super) padding: ResolvedPadding,
}
impl TextInputOutput {
fn apply(&self, state: &TextInputState) {
crate::node::node_kind::note_text_state_write();
state.set_token_layout(self.token_layout.clone());
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);
let (width, height) = (self.size.width, self.size.height);
match state.overflow() {
TextInputOverflow::Scroll => {
state.set_scroll_x(self.horizontal_scroll(state));
state.set_vertical_scroll(width, height, height, 0.0);
}
TextInputOverflow::Multiline => {
state.set_scroll_x(0.0);
let scroll_y = self.vertical_scroll(state);
let padding = self.padding;
let vertical_padding = padding.top + padding.bottom;
state.set_vertical_scroll(
width + padding.left + padding.right,
height + vertical_padding,
self.text_height + vertical_padding,
scroll_y,
);
}
}
}
fn horizontal_scroll(&self, state: &TextInputState) -> f32 {
let caret_x = state.caret_x() + state.scroll_x();
let caret_width = self.caret_width;
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 line_height = state.caret_height();
let max_scroll = (self.text_height - self.size.height).max(0.0);
let scroll_y = state.scroll_y().min(max_scroll);
if !state.follows_caret() {
scroll_y
} else if caret_y < scroll_y {
caret_y
} else if caret_y + line_height > scroll_y + self.size.height {
(caret_y + 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) {
if child_layout.result.holds.text {
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),
(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,
holds: SubtreeHolds::UNKNOWN,
generation: 0,
sticky_shifts: None,
reach: crate::layout::layout_result::Reach::UNKNOWN,
}
}
}