use std::sync::{
Arc, Mutex,
atomic::{AtomicBool, Ordering},
};
pub(crate) use crate::node::select_state::{SelectChangeCallback, SelectState};
use crate::{
app::{
events::{KeyboardEvent, TextInputEvent},
theme::{ThemePalette, ThemeTypography, TypographyStyle},
},
core::Signal,
layout::{
layout_kind::ScrollState,
text_style::{TextAlign, TextStyle},
},
node::{
CheckboxStyle, EventHandler, IntoTextInputEventHandler, SliderPartStyle, TextColor, TextTransformMode,
text_selection::{
CaretPosition, CaretPositions, TextSelectionRange, caret_x_for_index, caret_y_for_index, clamp_to_char_boundary,
closest_caret_on_adjacent_row, closest_caret_to_point, line_bounds, next_char_boundary, next_delete_boundary,
next_word_boundary, previous_char_boundary, previous_delete_boundary, previous_word_boundary,
selection_range_indices, selection_ranges_for_positions, trailing_break_len, trailing_break_row_y,
word_selection_bounds,
},
},
};
mod checkbox;
mod slider;
pub(crate) mod text_input;
pub(crate) use checkbox::CheckboxState;
pub(crate) use slider::{SliderAxis, SliderChangeHandler, SliderPartRect, SliderState, SliderThumb};
pub(crate) use text_input::{TextInputState, WeakTextInputState};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextInputOverflow {
Multiline,
#[default]
Scroll,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum KeyboardKind {
#[default]
Text,
Email,
Url,
Number,
Decimal,
Phone,
Search,
None,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ContentKind {
#[default]
Auto,
None,
Name,
Email,
Username,
Password,
NewPassword,
OneTimeCode,
Phone,
Url,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ReturnKey {
#[default]
Default,
Done,
Go,
Next,
Search,
Send,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextInputOverflowAnchor {
#[default]
Start,
End,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextOverflow {
#[default]
Clip,
Elipsis,
}
#[cfg(feature = "image")]
pub(crate) struct LoadingImage {
pub(crate) key: crate::images::ImageKey,
pub(crate) cap: Option<crate::images::ImageSizeCap>,
pub(crate) source: crate::images::ImageSource,
pub(crate) fit: crate::node::BackgroundSize,
pub(crate) error_view: Option<crate::node::node_kind::ErrorViewCallback>,
pub(crate) placeholder_view: Option<crate::node::node_kind::PlaceholderViewCallback>,
pub(crate) failed: bool,
}
#[cfg(feature = "image")]
impl Clone for LoadingImage {
fn clone(&self) -> Self {
crate::app::runtime::note_loading_image_built();
Self {
key: self.key,
cap: self.cap,
source: self.source.clone(),
fit: self.fit,
error_view: self.error_view.clone(),
placeholder_view: self.placeholder_view.clone(),
failed: self.failed,
}
}
}
#[cfg(feature = "image")]
pub(crate) type ErrorViewCallback = Arc<dyn Fn(&crate::images::ImageDecodeError) -> crate::node::Element + Send + Sync>;
#[cfg(feature = "image")]
pub(crate) type PlaceholderViewCallback = Arc<dyn Fn() -> crate::node::Element + Send + Sync>;
#[derive(Clone)]
pub(crate) enum NodeKind {
Empty,
#[cfg(feature = "canvas")]
Canvas {
canvas: crate::canvas::CanvasHandle,
painter: Option<crate::canvas::CanvasPainter>,
},
Text {
state: TextState,
style: TextStyleSource,
transform_mode: TextTransformMode,
},
RichText {
state: TextState,
spans: Vec<crate::layout::quad::RichTextSpan>,
transform_mode: TextTransformMode,
generated: std::sync::Arc<[std::ops::Range<usize>]>,
},
#[cfg(feature = "markdown")]
InlineLink {
link: crate::node::inline_link::InlineLink,
},
TextInput {
state: TextInputState,
style: TextStyle,
placeholder_style: Option<Box<TextStyle>>,
},
Checkbox {
state: CheckboxState,
},
Slider {
state: SliderState,
},
Select {
state: SelectState,
},
#[cfg(feature = "raster")]
Image {
data: crate::images::ImageData,
fit: crate::node::BackgroundSize,
},
#[cfg(feature = "image")]
LoadingImage {
request: Box<LoadingImage>,
},
#[cfg(feature = "raster")]
Video {
data: crate::images::ImageData,
fit: crate::node::BackgroundSize,
},
#[cfg(feature = "image")]
ResourceImage {
path: Arc<str>,
fit: crate::node::BackgroundSize,
},
#[cfg(feature = "canvas")]
Svg {
data: crate::svg::SvgData,
},
#[cfg(all(feature = "canvas", feature = "http"))]
ResourceSvg {
path: Arc<str>,
},
}
impl NodeKind {
pub(crate) fn paints_hover(&self) -> bool {
match self {
Self::Checkbox { .. } | Self::Slider { .. } | Self::Select { .. } => true,
#[cfg(feature = "markdown")]
Self::InlineLink { .. } => true,
Self::Empty | Self::Text { .. } | Self::TextInput { .. } => false,
#[cfg(feature = "canvas")]
Self::Canvas { .. } => false,
Self::RichText { .. } => false,
#[cfg(feature = "raster")]
Self::Image { .. } | Self::Video { .. } => false,
#[cfg(feature = "image")]
Self::LoadingImage { .. } | Self::ResourceImage { .. } => false,
#[cfg(feature = "canvas")]
Self::Svg { .. } => false,
#[cfg(all(feature = "canvas", feature = "http"))]
Self::ResourceSvg { .. } => false,
}
}
}
#[derive(Clone, PartialEq)]
pub(crate) struct TextStyleSource {
base: TextStyleBase,
color: Option<TextColor>,
text_align: Option<TextAlign>,
rare: Option<Box<RareTextStyle>>,
}
#[derive(Clone, Default, PartialEq)]
struct RareTextStyle {
letter_spacing: Option<f32>,
font_features: Option<crate::layout::text_style::FontFeatures>,
shadow: Option<crate::layout::text_style::TextShadow>,
underline: Option<crate::layout::text_style::TextUnderline>,
strikethrough: Option<crate::layout::text_style::TextStrikethrough>,
max_lines: Option<u32>,
}
#[derive(Clone, PartialEq)]
enum TextStyleBase {
Default,
Typography(TypographyStyle),
Explicit(TextStyle),
}
impl TextStyleSource {
pub(crate) fn default_style() -> Self {
Self {
base: TextStyleBase::Default,
color: None,
text_align: None,
rare: None,
}
}
pub(crate) fn explicit(style: TextStyle) -> Self {
Self {
base: TextStyleBase::Explicit(style),
color: None,
text_align: None,
rare: None,
}
}
pub(crate) fn set_variant(&mut self, style: impl Into<TypographyStyle>) {
self.base = TextStyleBase::Typography(style.into());
}
pub(crate) fn set_color(&mut self, color: impl Into<TextColor>) {
self.color = Some(color.into());
}
pub(crate) fn names_color(&self) -> bool {
self.color.is_some()
|| matches!(&self.base, TextStyleBase::Explicit(style) if style.color != crate::layout::text_style::DEFAULT_TEXT_COLOR)
}
pub(crate) fn set_text_align(&mut self, align: impl Into<TextAlign>) {
self.text_align = Some(align.into());
}
pub(crate) fn set_letter_spacing(&mut self, letter_spacing: f32) {
self.rare_mut().letter_spacing = Some(letter_spacing);
}
pub(crate) fn set_font_features(&mut self, font_features: crate::layout::text_style::FontFeatures) {
self.rare_mut().font_features = Some(font_features);
}
pub(crate) fn set_shadow(&mut self, shadow: crate::layout::text_style::TextShadow) {
self.rare_mut().shadow = Some(shadow);
}
fn rare_mut(&mut self) -> &mut RareTextStyle {
self.rare.get_or_insert_with(Box::default)
}
pub(crate) fn set_underline(&mut self, underline: crate::layout::text_style::TextUnderline) {
self.rare_mut().underline = Some(underline);
}
pub(crate) fn set_strikethrough(&mut self, strikethrough: crate::layout::text_style::TextStrikethrough) {
self.rare_mut().strikethrough = Some(strikethrough);
}
pub(crate) fn set_max_lines(&mut self, max_lines: u32) {
self.rare_mut().max_lines = Some(max_lines);
}
pub(crate) fn max_lines(&self) -> Option<u32> {
self.rare.as_ref().and_then(|rare| rare.max_lines)
}
pub(crate) fn resolve(&self, typography: &ThemeTypography, palette: &ThemePalette) -> TextStyle {
let mut style = match &self.base {
TextStyleBase::Default => typography.default_style().clone(),
TextStyleBase::Typography(style) => typography.resolve_or_default(*style),
TextStyleBase::Explicit(style) => style.clone(),
};
style.color = palette.text_color(&style);
if let Some(color) = self.color.as_ref().and_then(|color| color.resolve(palette)) {
style.color = color;
}
if let Some(text_align) = self.text_align {
style.text_align = text_align;
}
if let Some(rare) = &self.rare {
if let Some(letter_spacing) = rare.letter_spacing {
style.letter_spacing = letter_spacing;
}
if let Some(font_features) = &rare.font_features {
style.font_features = font_features.clone();
}
if let Some(shadow) = rare.shadow {
style.shadow = Some(shadow);
}
if let Some(underline) = rare.underline {
style.underline = Some(underline);
}
if let Some(strikethrough) = rare.strikethrough {
style.strikethrough = Some(strikethrough);
}
}
style
}
}
#[derive(Clone)]
pub(crate) struct TextState {
inner: Arc<Mutex<TextInner>>,
}
static TEXT_STATE_WRITES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub(crate) fn note_text_state_write() {
TEXT_STATE_WRITES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn text_state_writes() -> u64 {
TEXT_STATE_WRITES.load(std::sync::atomic::Ordering::Relaxed)
}
#[derive(Clone)]
pub(crate) struct PaintedCarets {
pub(crate) positions: CaretPositions,
pub(crate) scale: f32,
}
impl PaintedCarets {
fn unmeasured() -> Self {
Self {
positions: vec![CaretPosition {
index: 0,
x: 0.0,
y: 0.0,
}]
.into(),
scale: 1.0,
}
}
}
struct TextInner {
selectable: bool,
sensitive: bool,
caret: usize,
selection_anchor: Option<usize>,
carets: PaintedCarets,
text_top: f32,
display_text: Option<Arc<str>>,
render_wrap: bool,
highlights: Arc<[crate::node::text_highlight::TextHighlight]>,
}
impl TextInner {
fn line_ranges(&self, value: &str, start: usize, end: usize) -> Vec<TextSelectionRange> {
let scale = self.carets.scale;
selection_ranges_for_positions(&self.carets.positions, Some(value), start, end, 0.0, 0.0)
.into_iter()
.map(|range| TextSelectionRange {
x: range.x / scale,
y: range.y / scale + self.text_top,
width: range.width / scale,
})
.collect()
}
}
impl TextState {
pub(crate) fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(TextInner {
selectable: false,
sensitive: false,
caret: 0,
selection_anchor: None,
carets: PaintedCarets::unmeasured(),
text_top: 0.0,
display_text: None,
render_wrap: false,
highlights: Arc::from([]),
})),
}
}
pub(crate) fn set_selectable(&self, selectable: bool) {
let mut inner = self.inner.lock().unwrap();
inner.selectable = selectable;
if !selectable {
inner.selection_anchor = None;
inner.caret = 0;
}
}
pub(crate) fn selectable(&self) -> bool {
self.inner.lock().unwrap().selectable
}
pub(crate) fn set_highlights(&self, highlights: Arc<[crate::node::text_highlight::TextHighlight]>) {
self.inner.lock().unwrap().highlights = highlights;
}
pub(crate) fn measures_carets(&self) -> bool {
let inner = self.inner.lock().unwrap();
inner.selectable || !inner.highlights.is_empty()
}
pub(crate) fn set_sensitive(&self) {
self.inner.lock().unwrap().sensitive = true;
}
pub(crate) fn is_sensitive(&self) -> bool {
self.inner.lock().unwrap().sensitive
}
pub(crate) fn update_selection_to_point(&self, value: &str, x: f32, y: f32) {
if !self.selectable() {
return;
}
let caret = self.caret_index_at_point(value, x, y);
self.inner.lock().unwrap().caret = caret;
}
pub(crate) fn clear_selection_at_point(&self, value: &str, x: f32, y: f32) {
let caret = self.caret_index_at_point(value, x, y);
let mut inner = self.inner.lock().unwrap();
inner.caret = caret;
inner.selection_anchor = None;
}
pub(crate) fn clear_selection(&self) {
self.inner.lock().unwrap().selection_anchor = None;
}
pub(crate) fn set_selection_indices(&self, value: &str, anchor: usize, caret: usize) {
if !self.selectable() {
return;
}
let mut inner = self.inner.lock().unwrap();
inner.selection_anchor = Some(clamp_to_char_boundary(value, anchor));
inner.caret = clamp_to_char_boundary(value, caret);
}
pub(crate) fn select_word_at_point(&self, value: &str, x: f32, y: f32) {
if !self.selectable() {
return;
}
let caret = self.caret_index_at_point(value, x, y);
let (start, end) = word_selection_bounds(value, caret);
let mut inner = self.inner.lock().unwrap();
inner.selection_anchor = Some(start);
inner.caret = end;
}
pub(crate) fn select_line_at_point(&self, value: &str, x: f32, y: f32) {
if !self.selectable() {
return;
}
let caret = self.caret_index_at_point(value, x, y);
let (start, end) = line_bounds(value, caret);
let mut inner = self.inner.lock().unwrap();
inner.selection_anchor = Some(start);
inner.caret = end;
}
pub(crate) fn has_selection(&self, value: &str) -> bool {
let inner = self.inner.lock().unwrap();
selection_range_indices(value, inner.selection_anchor, inner.caret).is_some()
}
pub(crate) fn selection_anchor(&self) -> Option<usize> {
self.inner.lock().unwrap().selection_anchor
}
pub(crate) fn selected_text(&self, value: &str) -> Option<String> {
self.selected_range(value).map(|range| value[range].to_owned())
}
pub(crate) fn selected_range(&self, value: &str) -> Option<std::ops::Range<usize>> {
let inner = self.inner.lock().unwrap();
let (start, end) = selection_range_indices(value, inner.selection_anchor, inner.caret)?;
Some(start..end)
}
pub(crate) fn selection_ranges(&self, value: &str) -> Vec<TextSelectionRange> {
if !self.selectable() {
return Vec::new();
}
let inner = self.inner.lock().unwrap();
let Some((start, end)) = selection_range_indices(value, inner.selection_anchor, inner.caret) else {
return Vec::new();
};
inner.line_ranges(value, start, end)
}
pub(crate) fn highlight_ranges(
&self,
value: &str,
) -> Vec<(crate::node::text_highlight::TextHighlight, Vec<TextSelectionRange>)> {
let inner = self.inner.lock().unwrap();
inner
.highlights
.iter()
.filter_map(|highlight| {
let start = clamp_to_char_boundary(value, highlight.range.start);
let end = clamp_to_char_boundary(value, highlight.range.end);
(start < end).then(|| (highlight.clone(), inner.line_ranges(value, start, end)))
})
.collect()
}
pub(crate) fn range_lines(&self, value: &str, range: std::ops::Range<usize>) -> Vec<TextSelectionRange> {
let start = clamp_to_char_boundary(value, range.start);
let end = clamp_to_char_boundary(value, range.end);
if start >= end {
return Vec::new();
}
self.inner.lock().unwrap().line_ranges(value, start, end)
}
pub(crate) fn set_layout_output(
&self,
display_text: Option<&Arc<str>>,
render_wrap: bool,
carets: Option<&PaintedCarets>,
text_top: f32,
) {
let mut inner = self.inner.lock().unwrap();
let mut changed = inner.render_wrap != render_wrap || inner.text_top != text_top;
if inner.display_text.as_ref().map(Arc::as_ptr) != display_text.map(Arc::as_ptr) {
inner.display_text = display_text.cloned();
changed = true;
}
inner.render_wrap = render_wrap;
inner.text_top = text_top;
if let Some(carets) = carets {
changed |= !inner.carets.positions.same(&carets.positions) || inner.carets.scale != carets.scale;
inner.carets = carets.clone();
}
if changed {
note_text_state_write();
}
}
pub(crate) fn display_text(&self) -> Option<Arc<str>> {
self.inner.lock().unwrap().display_text.clone()
}
pub(crate) fn render_wrap(&self) -> bool {
self.inner.lock().unwrap().render_wrap
}
pub(crate) fn copy_runtime_state_from(&self, old: &Self, value: &str, preserve_display_text: bool) {
if Arc::ptr_eq(&self.inner, &old.inner) {
return;
}
let old_inner = old.inner.lock().unwrap();
let selectable = self.selectable();
let len = value.len();
let mut inner = self.inner.lock().unwrap();
inner.selectable = selectable;
inner.display_text = if preserve_display_text {
old_inner.display_text.clone()
} else {
None
};
inner.render_wrap = old_inner.render_wrap;
inner.text_top = old_inner.text_top;
if selectable {
inner.caret = old_inner.caret.min(len);
inner.selection_anchor = old_inner.selection_anchor.map(|anchor| anchor.min(len));
} else {
inner.caret = 0;
inner.selection_anchor = None;
}
if selectable || !inner.highlights.is_empty() {
inner.carets = old_inner.carets.clone();
}
}
pub(crate) fn caret_index_at_point(&self, value: &str, x: f32, y: f32) -> usize {
let inner = self.inner.lock().unwrap();
let scale = inner.carets.scale;
clamp_to_char_boundary(
value,
closest_caret_to_point(&inner.carets.positions, x * scale, (y - inner.text_top) * scale),
)
}
}