use unicode_width::UnicodeWidthChar;
#[derive(Debug, Default)]
pub struct Ime {
enabled: bool,
preedit: Option<Preedit>,
}
impl Ime {
pub fn new() -> Self {
Default::default()
}
#[inline]
pub fn set_enabled(&mut self, is_enabled: bool) {
if is_enabled {
self.enabled = is_enabled
} else {
*self = Default::default();
}
}
#[inline]
#[allow(unused)]
pub fn is_enabled(&self) -> bool {
self.enabled
}
#[inline]
pub fn set_preedit(&mut self, preedit: Option<Preedit>) {
self.preedit = preedit;
}
#[inline]
pub fn preedit(&self) -> Option<&Preedit> {
self.preedit.as_ref()
}
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Preedit {
pub text: String,
pub cursor_byte_offset: Option<usize>,
pub cursor_end_offset: Option<usize>,
}
impl Preedit {
pub fn new(text: String, cursor_byte_offset: Option<usize>) -> Self {
let cursor_end_offset = if let Some(byte_offset) = cursor_byte_offset {
let cursor_end_offset = text[byte_offset..]
.chars()
.fold(0, |acc, ch| acc + ch.width().unwrap_or(1));
Some(cursor_end_offset)
} else {
None
};
Self {
text,
cursor_byte_offset,
cursor_end_offset,
}
}
}