use super::*;
use crate::node::text_selection::{trailing_break_len, trailing_break_row_y};
impl LayoutEngine {
pub(super) fn layout_text_input(
&self,
glyph_engine: &mut GlyphEngine,
state: &crate::node::node_kind::TextInputState,
text: &str,
style: &TextStyle,
placeholder_style: Option<&TextStyle>,
constraints: Constraints,
padding: ResolvedPadding,
objects: &[Option<Size>],
) -> LayoutResult {
#[cfg(feature = "markdown")]
if let Some(styled) = self.styled_input_styles(state, style, objects) {
return self.layout_decorated_input(glyph_engine, state, style, constraints, styled, padding);
}
#[cfg(not(feature = "markdown"))]
let _ = objects;
if let Some(tokens) = state.tokens().filter(|tokens| !tokens.tokens.is_empty()) {
return self.layout_input_tokens(glyph_engine, state, style, constraints, tokens, padding);
}
let display_style = text_input_display_style(state, style, placeholder_style);
let overflow = state.overflow();
let text_constraints = Constraints {
min_width: 0.0,
min_height: 0.0,
..constraints
};
let multiline = overflow == crate::node::node_kind::TextInputOverflow::Multiline;
let wraps = state.wraps_lines();
let text_result = self.layout_text(glyph_engine, text, display_style, text_constraints, wraps);
let line_height = (style.font_size * style.line_height).max(1.0);
let preferred_width = if wraps {
text_result.size.width.max(DEFAULT_TEXT_INPUT_WIDTH)
} else if multiline {
text_result.size.width + self.caret.borrow().width().max(TEXT_INPUT_CARET_WIDTH)
} else {
DEFAULT_TEXT_INPUT_WIDTH
};
let width = constraints.constrain(Size::new(preferred_width, 0.0)).width;
let caret_width = if wraps || display_style.text_align != crate::layout::text_style::TextAlign::Left {
width
} else {
f32::MAX
};
let caret_source = state.caret_layout_text();
let mut caret_positions = glyph_engine.caret_positions(&caret_source, style, caret_width, wraps);
state.remap_caret_positions(&mut caret_positions);
let mut text_height = text_result
.size
.height
.max(explicit_multiline_height(text, line_height));
if multiline && trailing_break_len(&caret_source) > 0 {
let empty_row = trailing_break_row_y(&caret_positions, &caret_source, line_height);
text_height = text_height.max(empty_row + line_height);
}
let preferred = if multiline {
Size::new(
preferred_width,
multiline_preferred_height(state, text_height, line_height),
)
} else {
Size::new(preferred_width, line_height)
};
let size = constraints.constrain(preferred);
let output = TextInputOutput {
token_layout: None,
caret_positions,
size,
text_width: text_result.size.width,
text_height,
line_height,
text_band: glyph_engine.line_content_band(style),
caret_width: self.caret.borrow().width().max(TEXT_INPUT_CARET_WIDTH),
padding,
};
Self::text_input_result(state, output)
}
pub(crate) fn text_input_scrollbar_lane(&self, node: &Node) -> f32 {
let NodeKind::TextInput { state, .. } = node.node_kind() else {
return 0.0;
};
if state.overflow() != TextInputOverflow::Multiline {
return 0.0;
}
let theme_style = self.scrollbar.borrow().clone();
let resting = node.scrollbar_style_when(theme_style.clone(), false);
if !should_reserve_scrollbar(&resting, ScrollDirection::Vertical, ScrollAxis::Vertical) {
return 0.0;
}
let hovered = node.scrollbar_style_when(theme_style, true);
reserved_scrollbar_size(&resting).max(reserved_scrollbar_size(&hovered))
}
}