use super::{
edit_buffer::{boundaries, EditBuffer},
edit_history::EditHistory,
Response, Ui, Widget,
};
use crate::{
context::{HitAction, HitRegion, Paint, TextEditInput},
Border, Color, CornerRadius, Id, Padding, Rect, Shape, Vec2,
};
use std::{hash::Hash, ops::Range, panic::Location, time::Duration};
use text_input::Fingerprint;
use unicode_segmentation::UnicodeSegmentation;
use winit::keyboard::KeyCode;
mod access;
mod area;
mod area_paint;
mod blink;
mod chrome;
pub(crate) mod doc;
mod doc_layout;
mod doc_rich;
mod events;
mod geometry;
mod options;
mod scroll;
mod show;
mod single;
mod text_input;
use geometry::{display_line, line, positions, x_at};
use options::{AreaHeight, AreaOptions};
pub(crate) type EditEventHandler<'a> = dyn FnMut(&mut String, &TextEditInput) -> bool + 'a;
pub struct TextEdit<'a> {
text: &'a mut String,
placeholder: String,
id: Option<Id>,
source: &'static Location<'static>,
enabled: bool,
status: super::SemanticStatus,
read_only: bool,
width: Option<f32>,
height: Option<f32>,
size: Option<f32>,
padding: Option<Padding>,
rounding: Option<CornerRadius>,
fill: Option<Color>,
pub(crate) hovered_fill: Option<Color>,
text_color: Option<Color>,
placeholder_color: Option<Color>,
selection_color: Option<Color>,
style: super::theme::TextEditStyle,
hover_style: Option<super::HoverStyle>,
pub(crate) event_handler: Option<&'a mut EditEventHandler<'a>>,
pub(crate) exact_id: Option<Id>,
pub(crate) affixes: (String, String),
pub(crate) select_all: bool,
pub(crate) accept: Option<fn(char) -> bool>,
pub(crate) whole_value: bool,
max_chars: Option<usize>,
area: Option<AreaOptions>,
family: Option<crate::TextFamily>,
tabular: Option<bool>,
}
impl<'a> TextEdit<'a> {
#[track_caller]
pub fn new(text: &'a mut String) -> Self {
Self {
text,
placeholder: String::new(),
id: None,
source: Location::caller(),
enabled: true,
status: Default::default(),
read_only: false,
width: None,
height: None,
size: None,
padding: None,
rounding: None,
fill: None,
hovered_fill: None,
text_color: None,
placeholder_color: None,
selection_color: None,
style: Default::default(),
hover_style: None,
event_handler: None,
exact_id: None,
affixes: (String::new(), String::new()),
select_all: false,
accept: None,
whole_value: false,
max_chars: None,
area: None,
family: None,
tabular: None,
}
}
pub fn style(mut self, style: super::theme::TextEditStyle) -> Self {
self.style = style;
self
}
pub fn monospace(mut self) -> Self {
self.family = Some(crate::TextFamily::Monospace);
self
}
pub fn family(mut self, family: crate::TextFamily) -> Self {
self.family = Some(family);
self
}
pub fn tabular_numbers(mut self, tabular: bool) -> Self {
self.tabular = Some(tabular);
self
}
pub fn caret_color(mut self, color: Color) -> Self {
self.style.caret = Some(color);
self
}
pub fn hover_style(mut self, style: super::HoverStyle) -> Self {
self.hover_style = Some(style);
self
}
pub fn id_source(mut self, source: impl Hash) -> Self {
self.id = Some(Id::new(source));
self
}
pub fn placeholder(mut self, value: impl Into<String>) -> Self {
self.placeholder = value.into();
self
}
pub fn enabled(mut self, value: bool) -> Self {
self.enabled = value;
self
}
pub fn status(mut self, status: super::SemanticStatus) -> Self {
self.status = status;
self
}
#[deprecated(
note = "use `.enabled(!disabled)`; `enabled` is the one way to set a control's availability"
)]
pub fn disabled(self, value: bool) -> Self {
self.enabled(!value)
}
pub fn read_only(mut self, value: bool) -> Self {
self.read_only = value;
self
}
#[track_caller]
pub fn width(mut self, value: f32) -> Self {
self.width = super::sanitize::positive("TextEdit::width", value).or(self.width);
self
}
#[track_caller]
pub fn height(mut self, value: f32) -> Self {
self.height = super::sanitize::positive("TextEdit::height", value).or(self.height);
self
}
#[track_caller]
pub fn font_size(mut self, value: f32) -> Self {
self.size = super::sanitize::positive("TextEdit::font_size", value).or(self.size);
self
}
pub fn padding(mut self, value: Padding) -> Self {
self.padding = Some(value);
self
}
pub fn corner_radius(mut self, radius: impl Into<CornerRadius>) -> Self {
self.rounding = Some(radius.into());
self
}
#[deprecated(
note = "use `.corner_radius(..)`; one name for the corner radius of every component"
)]
pub fn rounding(self, value: CornerRadius) -> Self {
self.corner_radius(value)
}
pub fn fill(mut self, value: Color) -> Self {
self.fill = Some(value);
self
}
pub fn text_color(mut self, value: Color) -> Self {
self.text_color = Some(value);
self
}
pub fn placeholder_color(mut self, value: Color) -> Self {
self.placeholder_color = Some(value);
self
}
pub fn selection_color(mut self, value: Color) -> Self {
self.selection_color = Some(value);
self
}
pub fn max_chars(mut self, count: usize) -> Self {
self.max_chars = Some(count);
self
}
}
pub(crate) fn body_id(id: Id) -> Id {
id.with("body")
}
pub struct TextEditState {
pub buffer: EditBuffer,
pub scroll: f32,
fingerprint: Fingerprint,
history: EditHistory,
word_drag: Option<Range<usize>>,
drag_paragraphs: bool,
focused: bool,
blink_interval: Duration,
pub preedit: Option<(String, Option<(usize, usize)>)>,
area: Option<Box<area::AreaState>>,
access: access::LineCache,
}
impl Ui<'_> {
#[track_caller]
pub fn text_edit(&mut self, text: &mut String) -> Response {
self.add(TextEdit::new(text))
}
#[track_caller]
pub fn text_area(&mut self, text: &mut String) -> Response {
self.add(TextEdit::new(text).multiline())
}
}