use std::fmt;
use kurbo::Rect;
use super::key::EditingState;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ImeEvent {
Compose {
text: String,
cursor: Option<(usize, usize)>,
},
Commit(String),
ApplyEditingState(EditingState),
Enabled,
Disabled,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ImeContentType {
#[default]
Normal,
Password,
NoSuggestions,
Terminal,
}
impl ImeContentType {
pub fn is_secret(self) -> bool {
match self {
Self::Password => true,
Self::Normal | Self::NoSuggestions | Self::Terminal => false,
}
}
pub fn suppresses_suggestions(self) -> bool {
match self {
Self::Password | Self::NoSuggestions | Self::Terminal => true,
Self::Normal => false,
}
}
}
#[derive(Clone, PartialEq)]
pub struct ImeState {
pub active: bool,
pub editing: EditingState,
pub caret: Option<Rect>,
pub content_type: ImeContentType,
pub suppress_soft_keyboard: bool,
}
impl Default for ImeState {
fn default() -> Self {
Self {
active: false,
editing: EditingState::default(),
caret: None,
content_type: ImeContentType::Normal,
suppress_soft_keyboard: false,
}
}
}
impl fmt::Debug for ImeState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct Redacted<'a>(&'a EditingState);
impl fmt::Debug for Redacted<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EditingState")
.field("text", &"<redacted>")
.field("selection_base", &self.0.selection_base)
.field("selection_extent", &self.0.selection_extent)
.field("composing_base", &self.0.composing_base)
.field("composing_extent", &self.0.composing_extent)
.finish()
}
}
let mut s = f.debug_struct("ImeState");
s.field("active", &self.active);
if self.content_type.is_secret() {
s.field("editing", &Redacted(&self.editing));
} else {
s.field("editing", &self.editing);
}
s.field("caret", &self.caret)
.field("content_type", &self.content_type)
.field("suppress_soft_keyboard", &self.suppress_soft_keyboard)
.finish()
}
}