1use std::num::TryFromIntError;
2use ohos_ime_sys::types::{InputMethod_EnterKeyType, InputMethod_TextInputType};
5
6#[derive(Clone)]
7pub struct TextSelection {
8 pub(crate) start: i32,
9 pub(crate) end: i32,
10}
11
12pub struct InvalidSelection(());
13
14impl From<TryFromIntError> for InvalidSelection {
15 fn from(_: TryFromIntError) -> Self {
16 InvalidSelection(())
17 }
18}
19
20impl TextSelection {
21 pub fn new(start: usize, end: usize) -> Result<TextSelection, InvalidSelection> {
27 Ok(TextSelection {
28 start: start.try_into()?,
29 end: end.try_into()?,
30 })
31 }
32}
33
34pub struct TextConfig {
35 pub(crate) input_type: InputMethod_TextInputType,
36 pub(crate) enterkey_type: InputMethod_EnterKeyType,
37 pub(crate) preview_text_support: bool,
38 pub(crate) selection: Option<TextSelection>,
39 pub(crate) window_id: Option<i32>,
40}
41
42impl Default for TextConfig {
43 fn default() -> TextConfig {
44 TextConfigBuilder::new().build()
45 }
46}
47
48pub struct TextConfigBuilder {
49 input_type: InputMethod_TextInputType,
50 enterkey_type: InputMethod_EnterKeyType,
51 preview_text_support: bool,
52 selection: Option<TextSelection>,
53 window_id: Option<i32>,
54}
55
56impl TextConfigBuilder {
57 pub const fn new() -> TextConfigBuilder {
58 TextConfigBuilder {
59 input_type: InputMethod_TextInputType::IME_TEXT_INPUT_TYPE_TEXT,
60 enterkey_type: InputMethod_EnterKeyType::IME_ENTER_KEY_UNSPECIFIED,
61 preview_text_support: false,
62 selection: None,
63 window_id: None,
64 }
65 }
66
67 pub fn build(&self) -> TextConfig {
68 TextConfig {
69 input_type: self.input_type,
71 enterkey_type: self.enterkey_type,
72 preview_text_support: self.preview_text_support,
73 selection: self.selection.clone(),
74 window_id: self.window_id,
75 }
76 }
77
78 pub fn input_type(mut self, input_type: InputMethod_TextInputType) -> TextConfigBuilder {
79 self.input_type = input_type;
80 self
81 }
82
83 pub fn enterkey_type(mut self, enterkey_type: InputMethod_EnterKeyType) -> TextConfigBuilder {
84 self.enterkey_type = enterkey_type;
85 self
86 }
87
88 pub fn preview_text_support(mut self, preview_text_support: bool) -> TextConfigBuilder {
89 self.preview_text_support = preview_text_support;
90 self
91 }
92
93 pub fn selection(mut self, selection: TextSelection) -> TextConfigBuilder {
94 self.selection = Some(selection);
95 self
96 }
97
98 pub fn window_id(mut self, window_id: i32) -> TextConfigBuilder {
99 self.window_id = Some(window_id);
100 self
101 }
102}
103
104impl Default for TextConfigBuilder {
105 fn default() -> TextConfigBuilder {
106 Self::new()
107 }
108}