use super::{gesture::ClickCounter, Context, EventResponse, HitAction, Id};
use crate::{components::text_edit::TextEditState, Vec2};
use std::collections::{HashMap, HashSet};
use winit::keyboard::{KeyCode, ModifiersState};
pub(crate) enum TextEditInput {
Text(String),
Commit(String),
Key(KeyCode, ModifiersState),
Pointer(Vec2, bool, u8),
Focus(bool),
Preedit(String, Option<(usize, usize)>),
SetValue(String),
Select(usize, usize),
}
#[derive(Default)]
pub(crate) struct TextFields {
pub(crate) states: HashMap<Id, TextEditState>,
queue: HashMap<Id, Vec<TextEditInput>>,
tabs: HashSet<Id>,
tabs_previous: HashSet<Id>,
pub(super) clicks: ClickCounter,
pub(super) composing: bool,
}
impl TextFields {
pub(super) fn push(&mut self, id: Id, input: TextEditInput) {
self.queue.entry(id).or_default().push(input);
}
pub(super) fn focus_changed(&mut self, lost: Option<Id>, gained: Option<Id>) {
for (id, gained) in [(lost, false), (gained, true)] {
if let Some(id) = id {
self.push(id, TextEditInput::Focus(gained));
}
}
self.clicks.reset();
self.composing = false;
}
pub(crate) fn take_tab(&mut self, id: Id) {
self.tabs.insert(id);
}
pub(super) fn publish_tabs(&mut self) {
self.tabs_previous = std::mem::take(&mut self.tabs);
}
pub(super) fn clear_queue(&mut self) {
self.queue.clear();
}
pub(super) fn retire(&mut self, painted: impl Fn(Id) -> bool) {
self.states
.retain(|id, _| painted(crate::components::text_edit::body_id(*id)));
}
}
impl Context {
pub fn on_text_event(&mut self, text: &str) -> EventResponse {
self.on_committed_text(text, false)
}
pub(super) fn on_committed_text(&mut self, text: &str, ime: bool) -> EventResponse {
self.input.text.push_str(text);
let target = self.interaction.focused_as(HitAction::TextEdit);
if let Some(id) = target {
self.text_fields.push(
id,
if ime {
TextEditInput::Commit(text.to_owned())
} else {
TextEditInput::Text(text.to_owned())
},
);
}
self.request_repaint();
EventResponse {
consumed: target.is_some(),
repaint: true,
}
}
pub(super) fn ime_preedit(&mut self, text: &str, cursor: Option<(usize, usize)>) -> bool {
self.text_fields.composing = !text.is_empty();
let Some(id) = self.interaction.focused_as(HitAction::TextEdit) else {
return false;
};
self.text_fields
.push(id, TextEditInput::Preedit(text.to_owned(), cursor));
true
}
pub(super) fn ime_disabled(&mut self) {
self.text_fields.composing = false;
if let Some(id) = self.interaction.focused {
self.text_fields
.push(id, TextEditInput::Preedit(String::new(), None));
}
}
pub(super) fn text_key(&mut self, id: Id, code: KeyCode, pressed: bool) -> bool {
let tab_input =
self.text_fields.tabs_previous.contains(&id) && !self.input.modifiers.shift_key();
if code == KeyCode::Tab && !tab_input {
return false;
}
self.input.record_key(code, pressed);
if pressed {
self.text_fields
.push(id, TextEditInput::Key(code, self.input.modifiers));
}
true
}
pub(super) fn text_press(&mut self, id: Id, pointer: Vec2) {
let shift = self.input.modifiers.shift_key();
let now = crate::time::Instant::now();
let count = self.text_fields.clicks.count(id, pointer, now, 3, !shift);
self.text_fields
.push(id, TextEditInput::Pointer(pointer, shift, count));
}
pub(super) fn text_drag(&mut self, id: Id, pointer: Vec2) {
self.text_fields.clicks.moved(pointer);
self.text_fields
.push(id, TextEditInput::Pointer(pointer, true, 0));
}
pub(crate) fn text_edit_pointer(&self, id: Id) -> Option<Vec2> {
let transform = self.visuals.to_local(id);
self.input.pointer.map(|p| transform.point(p))
}
pub(crate) fn take_text_edit_input(&mut self, id: Id) -> Vec<TextEditInput> {
let transform = self.visuals.to_local(id);
self.text_fields
.queue
.remove(&id)
.unwrap_or_default()
.into_iter()
.map(|input| match input {
TextEditInput::Pointer(p, extend, count) => {
TextEditInput::Pointer(transform.point(p), extend, count)
}
input => input,
})
.collect()
}
}