use std::sync::Arc;
use crate::mutex::Mutex;
use crate::{
text_selection::{CCursorRange, CursorRange, TextCursorState},
Context, Galley, Id,
};
pub type TextEditUndoer = crate::util::undoer::Undoer<(CCursorRange, String)>;
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct TextEditState {
pub cursor: TextCursorState,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) undoer: Arc<Mutex<TextEditUndoer>>,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) ime_enabled: bool,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) ime_cursor_range: CursorRange,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) singleline_offset: f32,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) last_edit_time: f64,
}
impl TextEditState {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
ctx.data_mut(|d| d.get_persisted(id))
}
pub fn store(self, ctx: &Context, id: Id) {
ctx.data_mut(|d| d.insert_persisted(id, self));
}
#[deprecated = "Use `self.cursor.char_range` instead"]
pub fn ccursor_range(&self) -> Option<CCursorRange> {
self.cursor.char_range()
}
#[deprecated = "Use `self.cursor.set_char_range` instead"]
pub fn set_ccursor_range(&mut self, ccursor_range: Option<CCursorRange>) {
self.cursor.set_char_range(ccursor_range);
}
#[deprecated = "Use `self.cursor.set_range` instead"]
pub fn set_cursor_range(&mut self, cursor_range: Option<CursorRange>) {
self.cursor.set_range(cursor_range);
}
pub fn undoer(&self) -> TextEditUndoer {
self.undoer.lock().clone()
}
pub fn set_undoer(&mut self, undoer: TextEditUndoer) {
*self.undoer.lock() = undoer;
}
pub fn clear_undoer(&mut self) {
self.set_undoer(TextEditUndoer::default());
}
#[deprecated = "Use `self.cursor.range` instead"]
pub fn cursor_range(&self, galley: &Galley) -> Option<CursorRange> {
self.cursor.range(galley)
}
}