use std::num::TryFromIntError;
use ohos_ime_sys::types::{InputMethod_EnterKeyType, InputMethod_TextInputType};
#[derive(Clone)]
pub struct TextSelection {
pub(crate) start: i32,
pub(crate) end: i32,
}
pub struct InvalidSelection(());
impl From<TryFromIntError> for InvalidSelection {
fn from(_: TryFromIntError) -> Self {
InvalidSelection(())
}
}
impl TextSelection {
pub fn new(start: usize, end: usize) -> Result<TextSelection, InvalidSelection> {
Ok(TextSelection {
start: start.try_into()?,
end: end.try_into()?,
})
}
}
pub struct TextConfig {
pub(crate) input_type: InputMethod_TextInputType,
pub(crate) enterkey_type: InputMethod_EnterKeyType,
pub(crate) preview_text_support: bool,
pub(crate) selection: Option<TextSelection>,
pub(crate) window_id: Option<i32>,
}
impl Default for TextConfig {
fn default() -> TextConfig {
TextConfigBuilder::new().build()
}
}
pub struct TextConfigBuilder {
input_type: InputMethod_TextInputType,
enterkey_type: InputMethod_EnterKeyType,
preview_text_support: bool,
selection: Option<TextSelection>,
window_id: Option<i32>,
}
impl TextConfigBuilder {
pub const fn new() -> TextConfigBuilder {
TextConfigBuilder {
input_type: InputMethod_TextInputType::IME_TEXT_INPUT_TYPE_TEXT,
enterkey_type: InputMethod_EnterKeyType::IME_ENTER_KEY_UNSPECIFIED,
preview_text_support: false,
selection: None,
window_id: None,
}
}
pub fn build(&self) -> TextConfig {
TextConfig {
input_type: self.input_type,
enterkey_type: self.enterkey_type,
preview_text_support: self.preview_text_support,
selection: self.selection.clone(),
window_id: self.window_id,
}
}
pub fn input_type(mut self, input_type: InputMethod_TextInputType) -> TextConfigBuilder {
self.input_type = input_type;
self
}
pub fn enterkey_type(mut self, enterkey_type: InputMethod_EnterKeyType) -> TextConfigBuilder {
self.enterkey_type = enterkey_type;
self
}
pub fn preview_text_support(mut self, preview_text_support: bool) -> TextConfigBuilder {
self.preview_text_support = preview_text_support;
self
}
pub fn selection(mut self, selection: TextSelection) -> TextConfigBuilder {
self.selection = Some(selection);
self
}
pub fn window_id(mut self, window_id: i32) -> TextConfigBuilder {
self.window_id = Some(window_id);
self
}
}
impl Default for TextConfigBuilder {
fn default() -> TextConfigBuilder {
Self::new()
}
}