use std::rc::Rc;
use cosmic_text::{Affinity, FontSystem};
use crabshell_reactive::Signal;
use crate::cx::Cx;
use crate::edit::{Edit, Keyed, grapheme_start, graphemes_before};
use crate::geometry::{Point, Rect};
use crate::input::Handler;
use crate::keyboard::{Key, KeyEvent};
use crate::prop::{IntoProp, Prop};
use crate::select::{cursor_at, highlights, index_of};
use crate::style::Style;
use crate::text::{TextNode, TextRun};
use crate::tree::{NodeKind, borrow};
use crate::widgets::{Common, Element, ElementKind, Widget, visibility_methods};
use crate::window::Window;
const DOT: char = '•';
pub(crate) const DEFAULT_WIDTH: f32 = 200.0;
pub(crate) type AcceptHandler = Rc<Handler<dyn Fn(&Cx, &str)>>;
pub(crate) struct InputProps {
pub(crate) text: Signal<String>,
pub(crate) placeholder: Option<Prop<String>>,
pub(crate) password: Option<Prop<bool>>,
pub(crate) focus: Option<Prop<bool>>,
pub(crate) on_accept: Option<AcceptHandler>,
pub(crate) multiline: bool,
pub(crate) wrap: Option<Prop<bool>>,
}
pub struct TextInput {
input: InputProps,
common: Common,
}
impl TextInput {
pub fn new(text: Signal<String>) -> TextInput {
TextInput {
input: InputProps {
text,
placeholder: None,
password: None,
focus: None,
on_accept: None,
multiline: false,
wrap: None,
},
common: Common::default(),
}
}
pub fn placeholder(mut self, text: impl IntoProp<String>) -> TextInput {
self.input.placeholder = Some(text.into_prop());
self
}
pub fn password(mut self, password: impl IntoProp<bool>) -> TextInput {
self.input.password = Some(password.into_prop());
self
}
pub fn focus(mut self, focus: impl IntoProp<bool>) -> TextInput {
self.input.focus = Some(focus.into_prop());
self
}
pub fn on_accept(mut self, handler: impl Fn(&Cx, &str) + 'static) -> TextInput {
self.input.on_accept = Some(Rc::new(Handler::new(handler)));
self
}
pub fn style(mut self, style: impl IntoProp<Style>) -> TextInput {
self.common.style = Some(style.into_prop());
self
}
visibility_methods!();
}
impl Widget for TextInput {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "TextInput",
common: self.common,
kind: ElementKind::Input(Box::new(self.input)),
children: Vec::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Preedit {
pub text: String,
pub cursor: Option<(usize, usize)>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FieldState {
pub field: u64,
pub text: String,
pub cursor: usize,
pub anchor: usize,
pub caret: Rect,
pub multiline: bool,
}
impl Window {
pub fn field(&self) -> Option<FieldState> {
let tree = self.tree.borrow();
if !tree.keyboard.entered {
return None;
}
let id = tree.keyboard.focused?;
let NodeKind::Input(input) = &tree.node(id)?.kind else {
return None;
};
(!input.password).then(|| FieldState {
field: u64::from(id),
text: input.edit.text.clone(),
cursor: input.edit.cursor,
anchor: input.edit.anchor.unwrap_or(input.edit.cursor),
caret: tree.window_rect(id, input.caret),
multiline: input.edit.multiline,
})
}
pub fn commit_text(&self, text: &str) {
self.edit_field(|edit| edit.insert(text));
}
pub fn paste(&self, text: &str) {
if text.is_empty() {
return;
}
self.change_field(true, |edit| edit.insert(text));
}
pub fn delete_around(&self, before: usize, after: usize) {
self.edit_field(|edit| {
let selected = edit.selection().unwrap_or(edit.cursor..edit.cursor);
let start = selected.start.saturating_sub(before);
let end = selected.end.saturating_add(after).min(edit.text.len());
if !(edit.text.is_char_boundary(start) && edit.text.is_char_boundary(end)) {
return;
}
edit.text.replace_range(selected.end..end, "");
edit.text.replace_range(start..selected.start, "");
let removed = selected.start - start;
edit.cursor -= removed;
edit.anchor = edit.anchor.map(|anchor| anchor - removed);
});
}
pub fn set_preedit(&self, preedit: Option<Preedit>) {
let preedit = preedit.filter(|preedit| !preedit.text.is_empty());
if preedit.is_some() {
self.edit_field(|edit| {
if edit.selection().is_some() {
edit.insert("");
}
});
}
let mut tree = borrow(&self.tree);
let Some(id) = tree.keyboard.focused else {
return;
};
if matches!(tree.node(id).map(|node| &node.kind), Some(NodeKind::Input(input)) if input.password)
{
return;
}
tree.update_input(id, |input| {
input.preedit = preedit;
input.refresh();
});
}
fn edit_field(&self, change: impl FnOnce(&mut Edit)) {
self.change_field(false, change);
}
fn change_field(&self, passwords: bool, change: impl FnOnce(&mut Edit)) {
let write = {
let mut tree = borrow(&self.tree);
let Some(id) = tree.keyboard.focused else {
return;
};
let before = match tree.node(id).map(|node| &node.kind) {
Some(NodeKind::Input(input)) if passwords || !input.password => input.edit.text.clone(),
_ => return,
};
let mut write = None;
tree.update_input(id, |input| {
change(&mut input.edit);
input.refresh();
if input.edit.text != before {
write = Some((input.signal, input.edit.text.clone()));
}
});
write
};
if let Some((signal, text)) = write {
signal.try_set(text);
}
}
}
pub(crate) struct InputNode {
pub(crate) text: TextNode,
pub(crate) edit: Edit,
pub(crate) signal: Signal<String>,
pub(crate) placeholder: String,
pub(crate) password: bool,
pub(crate) focused: bool,
pub(crate) wants_focus: bool,
pub(crate) scroll: f32,
pub(crate) caret: Rect,
pub(crate) selection: Vec<Rect>,
pub(crate) area: Rect,
pub(crate) preedit: Option<Preedit>,
pub(crate) underline: Vec<Rect>,
pub(crate) goal: Option<(usize, f32)>,
pub(crate) upstream: Option<usize>,
pub(crate) on_accept: Signal<Option<AcceptHandler>>,
}
impl InputNode {
pub(crate) fn new(
signal: Signal<String>,
on_accept: Signal<Option<AcceptHandler>>,
multiline: bool,
) -> InputNode {
let mut text = TextNode::new();
if multiline {
text.set_max_lines(u32::MAX);
}
InputNode {
text,
edit: Edit {
multiline,
..Edit::default()
},
signal,
placeholder: String::new(),
password: false,
focused: false,
wants_focus: false,
scroll: 0.0,
caret: Rect::default(),
selection: Vec::new(),
area: Rect::default(),
preedit: None,
underline: Vec::new(),
goal: None,
upstream: None,
on_accept,
}
}
pub(crate) fn showing_placeholder(&self) -> bool {
self.edit.text.is_empty() && self.preedit.is_none()
}
fn preedit_len(&self) -> usize {
self
.preedit
.as_ref()
.map_or(0, |preedit| preedit.text.len())
}
pub(crate) fn refresh(&mut self) -> bool {
let shown = if self.showing_placeholder() {
self.placeholder.clone()
} else if self.password {
DOT
.to_string()
.repeat(graphemes_before(&self.edit.text, self.edit.text.len()))
} else if let Some(preedit) = &self.preedit {
let (before, after) = self.edit.text.split_at(self.edit.cursor);
format!("{before}{}{after}", preedit.text)
} else {
self.edit.text.clone()
};
self.text.set_content(shown)
}
fn shown_index(&self, at: usize) -> usize {
if self.showing_placeholder() {
0
} else if self.password {
graphemes_before(&self.edit.text, at) * DOT.len_utf8()
} else if at > self.edit.cursor {
at + self.preedit_len()
} else {
at
}
}
fn text_index(&self, at: usize) -> usize {
if self.showing_placeholder() {
0
} else if self.password {
grapheme_start(&self.edit.text, at / DOT.len_utf8())
} else if at <= self.edit.cursor {
at
} else {
let after = at.saturating_sub(self.preedit_len()).max(self.edit.cursor);
after.min(self.edit.text.len())
}
}
fn caret_run(&self, at: usize, upstream: bool) -> Option<TextRun<'_>> {
let cursor = cursor_at(&self.text.buffer, at);
let mut runs = self.text.runs().filter(|run| {
run.line_i == cursor.line
&& (run.glyphs.is_empty() || {
let start = run.glyphs.iter().map(|glyph| glyph.start).min();
let end = run.glyphs.iter().map(|glyph| glyph.end).max();
start.is_some_and(|start| start <= cursor.index)
&& end.is_some_and(|end| cursor.index <= end)
})
});
if upstream { runs.next() } else { runs.last() }
}
fn caret_spot(&self, at: usize, upstream: bool) -> Option<(f32, f32, f32)> {
let run = self
.caret_run(at, upstream)
.or_else(|| self.text.runs().next())?;
let x = if run.glyphs.is_empty() || self.showing_placeholder() {
self.text.empty_line_place(run.rtl) * self.text.layout_width()
} else {
run
.layout()
.cursor_position(&cursor_at(&self.text.buffer, at))
.unwrap_or(0.0)
};
Some((x, run.line_top, run.line_height))
}
fn glyph_box(&self, top: f32, height: f32) -> (f32, f32) {
let natural = (1.2 * self.text.buffer.metrics().font_size).round();
let glyphs = natural.min(height);
(top + ((height - glyphs) / 2.0).round(), glyphs)
}
pub(crate) fn finish(&mut self, content: Rect, fonts: &mut FontSystem) {
let room = (content.width - 1.0).max(0.0);
let width = match (self.text.aligned(), self.text.wraps()) {
(false, wraps) => wraps.then_some(content.width),
(true, true) => Some(room),
(true, false) if self.showing_placeholder() => Some(room),
(true, false) => Some(self.text.width().max(room)),
};
self.text.finish_at(content, width, fonts);
}
pub(crate) fn place(&mut self, content: Rect) {
let at = self.shown_index(self.edit.cursor);
let caret = match &self.preedit {
Some(preedit) => {
at + preedit
.cursor
.map_or(preedit.text.len(), |(start, _)| start)
}
None => at,
};
let upstream = self.upstream == Some(self.edit.cursor) && self.preedit.is_none();
let (x, top, height) = self
.caret_spot(caret, upstream)
.unwrap_or((0.0, 0.0, content.height));
let width = self.text.runs().map(|run| run.line_w).fold(0.0, f32::max);
let room = (content.width - 1.0).max(0.0);
let mut scroll = self.scroll.min((width - room).max(0.0));
if x - scroll > room {
scroll = x - room;
}
if x < scroll {
scroll = x;
}
self.scroll = scroll.max(0.0);
self.text.origin.x = content.x - self.scroll;
let origin = self.text.origin;
let (top, height) = self.glyph_box(top, height);
self.caret = Rect::new(origin.x + x, origin.y + top, 1.0, height);
self.area = content;
self.selection = match self.edit.selection() {
Some(range) => highlights(
&self.text,
self.shown_index(range.start)..self.shown_index(range.end),
),
None => Vec::new(),
};
self.underline = match &self.preedit {
Some(preedit) => highlights(&self.text, at..at + preedit.text.len())
.into_iter()
.map(|span| {
let (top, height) = self.glyph_box(span.y, span.height);
Rect::new(span.x, top + height - 1.0, span.width, 1.0)
})
.collect(),
None => Vec::new(),
};
}
pub(crate) fn hit(&self, point: Point) -> (usize, bool) {
let origin = self.text.origin;
let Some(cursor) = self.text.buffer.hit(point.x - origin.x, point.y - origin.y) else {
return (0, false);
};
let at = self.text_index(index_of(&self.text.buffer, cursor));
(at, cursor.affinity == Affinity::Before)
}
pub(crate) fn line_key(&mut self, event: &KeyEvent, page: f32) -> Option<Keyed> {
let ctrl = event.modifiers.ctrl;
let lines = match event.key {
Key::Up => -1.0,
Key::Down => 1.0,
Key::PageUp => -page,
Key::PageDown => page,
Key::Home | Key::End if !ctrl => 0.0,
_ => return None,
};
let at = self.shown_index(self.edit.cursor);
let upstream = self.upstream == Some(self.edit.cursor);
let (target, upstream, goal) = if lines == 0.0 {
let run = self.caret_run(at, upstream)?;
let ends = run.glyphs.iter().map(|glyph| (glyph.start, glyph.end));
let index = if event.key == Key::Home {
ends.map(|(start, _)| start).min()
} else {
ends.map(|(_, end)| end).max()
};
let line = cosmic_text::Cursor::new(run.line_i, index.unwrap_or(0));
(
index_of(&self.text.buffer, line),
event.key == Key::End,
None,
)
} else {
let (x, top, height) = self.caret_spot(at, upstream)?;
let x = self
.goal
.filter(|&(cursor, _)| cursor == self.edit.cursor)
.map_or(x, |(_, x)| x);
let step = if matches!(event.key, Key::Up | Key::Down) {
lines * height
} else {
lines
};
let cursor = self.text.buffer.hit(x, top + height / 2.0 + step)?;
(
index_of(&self.text.buffer, cursor),
cursor.affinity == Affinity::Before,
Some(x),
)
};
let target = self.text_index(target);
self.edit.move_to(event.modifiers.shift, target);
self.upstream = upstream.then_some(target);
self.goal = goal.map(|x| (target, x));
Some(Keyed::Moved)
}
}