pub struct TextInput {
pub handle: ControlHandle,
/* private fields */
}
Expand description
An edit control is a rectangular control window to permit the user to enter and edit text by typing on the keyboard
This control only allow a single line input. For block of text, use TextBox
.
Winapi documentation: https://docs.microsoft.com/en-us/windows/win32/controls/about-edit-controls#text-and-input-styles
TextInput is not behind any features.
Builder parameters:
parent
: Required. The text input parent container.text
: The text input text.size
: The text input size.position
: The text input position.flags
: A combination of the TextInputFlags values.ex_flags
: A combination of win32 window extended flags. Unlikeflags
, ex_flags must be used straight from winapifont
: The font used for the text input textlimit
: The maximum number of character that can be inserted in the controlreadonly
: If the text input should allow user input or notpassword
: The password character. If set to None, the textinput is a regular control.align
: The alignment of the text in the text inputbackground_color
: The color of the textinput top and bottom padding. This is not the white background under the text.focus
: The control receive focus after being created
Control events:
OnTextInput
: When a TextInput value is changedMousePress(_)
: Generic mouse press events on the buttonOnMouseMove
: Generic mouse mouse eventOnMouseWheel
: Generic mouse wheel event
use native_windows_gui as nwg;
fn build_box(tbox: &mut nwg::TextInput, window: &nwg::Window, font: &nwg::Font) {
nwg::TextInput::builder()
.text("Hello")
.font(Some(font))
.parent(window)
.build(tbox);
}
Fields§
§handle: ControlHandle
Implementations§
Source§impl TextInput
impl TextInput
Sourcepub fn builder<'a>() -> TextInputBuilder<'a>
pub fn builder<'a>() -> TextInputBuilder<'a>
Examples found in repository?
89 fn build_partial<W: Into<nwg::ControlHandle>>(data: &mut SubmitForm, parent: Option<W>) -> Result<(), nwg::NwgError> {
90 let parent = parent.unwrap().into();
91
92 nwg::TextInput::builder()
93 .text(&data.form_data)
94 .parent(&parent)
95 .build(&mut data.value)?;
96
97 nwg::Button::builder()
98 .text("Save")
99 .parent(&parent)
100 .build(&mut data.sumbit_button)?;
101
102 nwg::GridLayout::builder()
103 .child(0, 0, &data.value)
104 .child(0, 1, &data.sumbit_button)
105 .parent(&parent)
106 .build(&data.layout)?;
107
108 Ok(())
109 }
More examples
12fn main() {
13 nwg::init().expect("Failed to init Native Windows GUI");
14 nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
15
16 let mut window = Default::default();
17 let mut name_edit = Default::default();
18 let mut hello_button = Default::default();
19 let layout = Default::default();
20
21 nwg::Window::builder()
22 .size((300, 115))
23 .position((300, 300))
24 .title("Basic example")
25 .build(&mut window)
26 .unwrap();
27
28 nwg::TextInput::builder()
29 .text("Heisenberg")
30 .focus(true)
31 .parent(&window)
32 .build(&mut name_edit)
33 .unwrap();
34
35 nwg::Button::builder()
36 .text("Say my name")
37 .parent(&window)
38 .build(&mut hello_button)
39 .unwrap();
40
41 nwg::GridLayout::builder()
42 .parent(&window)
43 .spacing(1)
44 .child(0, 0, &name_edit)
45 .child_item(nwg::GridLayoutItem::new(&hello_button, 0, 1, 1, 2))
46 .build(&layout)
47 .unwrap();
48
49 let window = Rc::new(window);
50 let events_window = window.clone();
51
52 let handler = nwg::full_bind_event_handler(&window.handle, move |evt, _evt_data, handle| {
53 use nwg::Event as E;
54
55 match evt {
56 E::OnWindowClose =>
57 if &handle == &events_window as &nwg::Window {
58 nwg::modal_info_message(&events_window.handle, "Goodbye", &format!("Goodbye {}", name_edit.text()));
59 nwg::stop_thread_dispatch();
60 },
61 E::OnButtonClick =>
62 if &handle == &hello_button {
63 nwg::modal_info_message(&events_window.handle, "Hello", &format!("Hello {}", name_edit.text()));
64 },
65 _ => {}
66 }
67 });
68
69 nwg::dispatch_thread_events();
70 nwg::unbind_event_handler(&handler);
71}
46 fn build_ui(mut data: BasicApp) -> Result<BasicAppUi, nwg::NwgError> {
47 use nwg::Event as E;
48
49 // Controls
50 nwg::Window::builder()
51 .flags(nwg::WindowFlags::WINDOW | nwg::WindowFlags::VISIBLE)
52 .size((300, 135))
53 .position((300, 300))
54 .title("Basic example")
55 .build(&mut data.window)?;
56
57 nwg::TextInput::builder()
58 .size((280, 35))
59 .position((10, 10))
60 .text("Heisenberg")
61 .parent(&data.window)
62 .focus(true)
63 .build(&mut data.name_edit)?;
64
65 nwg::Button::builder()
66 .size((280, 70))
67 .position((10, 50))
68 .text("Say my name")
69 .parent(&data.window)
70 .build(&mut data.hello_button)?;
71
72 // Wrap-up
73 let ui = BasicAppUi {
74 inner: Rc::new(data),
75 default_handler: Default::default(),
76 };
77
78 // Events
79 let evt_ui = Rc::downgrade(&ui.inner);
80 let handle_events = move |evt, _evt_data, handle| {
81 if let Some(ui) = evt_ui.upgrade() {
82 match evt {
83 E::OnButtonClick =>
84 if &handle == &ui.hello_button {
85 BasicApp::say_hello(&ui);
86 },
87 E::OnWindowClose =>
88 if &handle == &ui.window {
89 BasicApp::say_goodbye(&ui);
90 },
91 _ => {}
92 }
93 }
94 };
95
96 *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events));
97
98 return Ok(ui);
99 }
47 fn build_ui(mut data: BasicApp) -> Result<BasicAppUi, nwg::NwgError> {
48 use nwg::Event as E;
49
50 // Controls
51 nwg::Window::builder()
52 .flags(nwg::WindowFlags::WINDOW | nwg::WindowFlags::VISIBLE)
53 .size((300, 115))
54 .position((300, 300))
55 .title("Basic example")
56 .build(&mut data.window)?;
57
58 nwg::TextInput::builder()
59 .text("Heisenberg")
60 .parent(&data.window)
61 .focus(true)
62 .build(&mut data.name_edit)?;
63
64 nwg::Button::builder()
65 .text("Say my name")
66 .parent(&data.window)
67 .build(&mut data.hello_button)?;
68
69 // Wrap-up
70 let ui = BasicAppUi {
71 inner: Rc::new(data),
72 default_handler: Default::default(),
73 };
74
75 // Events
76 let evt_ui = Rc::downgrade(&ui.inner);
77 let handle_events = move |evt, _evt_data, handle| {
78 if let Some(evt_ui) = evt_ui.upgrade() {
79 match evt {
80 E::OnButtonClick =>
81 if &handle == &evt_ui.hello_button {
82 BasicApp::say_hello(&evt_ui);
83 },
84 E::OnWindowClose =>
85 if &handle == &evt_ui.window {
86 BasicApp::say_goodbye(&evt_ui);
87 },
88 _ => {}
89 }
90 }
91 };
92
93 *ui.default_handler.borrow_mut() = Some(nwg::full_bind_event_handler(&ui.window.handle, handle_events));
94
95 // Layouts
96 nwg::GridLayout::builder()
97 .parent(&ui.window)
98 .spacing(1)
99 .child(0, 0, &ui.name_edit)
100 .child_item(nwg::GridLayoutItem::new(&ui.hello_button, 0, 1, 1, 2))
101 .build(&ui.layout)?;
102
103 return Ok(ui);
104 }
250 fn build_partial<W: Into<ControlHandle>>(data: &mut PeopleUi, parent: Option<W>) -> Result<(), NwgError> {
251 let parent = parent.unwrap().into();
252
253 nwg::Label::builder()
254 .text("Name:")
255 .h_align(nwg::HTextAlign::Right)
256 .parent(&parent)
257 .build(&mut data.label1)?;
258
259 nwg::Label::builder()
260 .text("Age:")
261 .h_align(nwg::HTextAlign::Right)
262 .parent(&parent)
263 .build(&mut data.label2)?;
264
265 nwg::Label::builder()
266 .text("Job:")
267 .h_align(nwg::HTextAlign::Right)
268 .parent(&parent)
269 .build(&mut data.label3)?;
270
271 nwg::TextInput::builder()
272 .text("John Doe")
273 .parent(&parent)
274 .build(&mut data.name_input)?;
275
276 nwg::TextInput::builder()
277 .text("75")
278 .flags(nwg::TextInputFlags::VISIBLE | nwg::TextInputFlags::NUMBER)
279 .parent(&parent)
280 .build(&mut data.age_input)?;
281
282 nwg::TextInput::builder()
283 .text("Programmer")
284 .parent(&parent)
285 .build(&mut data.job_input)?;
286
287 nwg::Button::builder()
288 .text("Save")
289 .parent(&parent)
290 .build(&mut data.save_btn)?;
291
292 nwg::GridLayout::builder()
293 .parent(&parent)
294 .max_size([1000, 150])
295 .min_size([100, 120])
296 .child(0, 0, &data.label1)
297 .child(0, 1, &data.label2)
298 .child(0, 2, &data.label3)
299 .child(1, 0, &data.name_input)
300 .child(1, 1, &data.age_input)
301 .child(1, 2, &data.job_input)
302 .build(&data.layout)?;
303
304 nwg::GridLayout::builder()
305 .min_size([100, 200])
306 .max_column(Some(2))
307 .max_row(Some(6))
308 .child(1, 5, &data.save_btn)
309 .parent(&parent)
310 .build(&data.layout2)?;
311
312 Ok(())
313 }
314
315 fn process_event<'a>(&self, _evt: nwg::Event, _evt_data: &nwg::EventData, _handle: ControlHandle) {
316 }
317
318 fn handles(&self) -> Vec<&ControlHandle> {
319 Vec::new()
320 }
321 }
322}
323
324mod partial_animal_ui {
325 use native_windows_gui as nwg;
326 use self::nwg::{PartialUi, NwgError, ControlHandle};
327 use super::*;
328
329 impl PartialUi for AnimalUi {
330
331 fn build_partial<W: Into<ControlHandle>>(data: &mut AnimalUi, parent: Option<W>) -> Result<(), NwgError> {
332 let parent = parent.unwrap().into();
333
334 nwg::Label::builder()
335 .text("Name:")
336 .h_align(nwg::HTextAlign::Right)
337 .parent(&parent)
338 .build(&mut data.label1)?;
339
340 nwg::Label::builder()
341 .text("Race:")
342 .h_align(nwg::HTextAlign::Right)
343 .parent(&parent)
344 .build(&mut data.label2)?;
345
346 nwg::Label::builder()
347 .text("Is fluffy:")
348 .h_align(nwg::HTextAlign::Right)
349 .parent(&parent)
350 .build(&mut data.label3)?;
351
352 nwg::TextInput::builder()
353 .text("Mittens")
354 .parent(&parent)
355 .build(&mut data.name_input)?;
356
357 nwg::ComboBox::builder()
358 .collection(vec!["Cat", "Dog", "Pidgeon", "Monkey"])
359 .selected_index(Some(0))
360 .parent(&parent)
361 .build(&mut data.race_input)?;
362
363 nwg::CheckBox::builder()
364 .text("")
365 .check_state(nwg::CheckBoxState::Checked)
366 .parent(&parent)
367 .build(&mut data.is_soft_input)?;
368
369 nwg::Button::builder()
370 .text("Save")
371 .parent(&parent)
372 .build(&mut data.save_btn)?;
373
374 nwg::GridLayout::builder()
375 .parent(&parent)
376 .max_size([1000, 150])
377 .min_size([100, 120])
378 .child(0, 0, &data.label1)
379 .child(0, 1, &data.label2)
380 .child(0, 2, &data.label3)
381 .child(1, 0, &data.name_input)
382 .child(1, 1, &data.race_input)
383 .child(1, 2, &data.is_soft_input)
384 .build(&data.layout)?;
385
386 nwg::GridLayout::builder()
387 .min_size([100, 200])
388 .max_column(Some(2))
389 .max_row(Some(6))
390 .child(1, 5, &data.save_btn)
391 .parent(&parent)
392 .build(&data.layout2)?;
393
394 Ok(())
395 }
396
397 fn process_event<'a>(&self, _evt: nwg::Event, _evt_data: &nwg::EventData, _handle: ControlHandle) {
398 }
399
400 fn handles(&self) -> Vec<&ControlHandle> {
401 Vec::new()
402 }
403 }
404}
405
406mod partial_food_ui {
407 use native_windows_gui as nwg;
408 use self::nwg::{PartialUi, NwgError, ControlHandle};
409 use super::*;
410
411 impl PartialUi for FoodUi {
412 fn build_partial<W: Into<ControlHandle>>(data: &mut FoodUi, parent: Option<W>) -> Result<(), NwgError> {
413 let parent = parent.unwrap().into();
414
415 nwg::Label::builder()
416 .text("Name:")
417 .h_align(nwg::HTextAlign::Right)
418 .parent(&parent)
419 .build(&mut data.label1)?;
420
421 nwg::Label::builder()
422 .text("Tasty:")
423 .h_align(nwg::HTextAlign::Right)
424 .parent(&parent)
425 .build(&mut data.label2)?;
426
427 nwg::TextInput::builder()
428 .text("Banana")
429 .parent(&parent)
430 .build(&mut data.name_input)?;
431
432 nwg::CheckBox::builder()
433 .text("")
434 .check_state(nwg::CheckBoxState::Checked)
435 .parent(&parent)
436 .build(&mut data.tasty_input)?;
437
438 nwg::Button::builder()
439 .text("Save")
440 .parent(&parent)
441 .build(&mut data.save_btn)?;
442
443 nwg::GridLayout::builder()
444 .parent(&parent)
445 .max_size([1000, 90])
446 .min_size([100, 80])
447 .child(0, 0, &data.label1)
448 .child(0, 1, &data.label2)
449 .child(1, 0, &data.name_input)
450 .child(1, 1, &data.tasty_input)
451 .build(&data.layout)?;
452
453 nwg::GridLayout::builder()
454 .min_size([100, 200])
455 .max_column(Some(2))
456 .max_row(Some(6))
457 .child(1, 5, &data.save_btn)
458 .parent(&parent)
459 .build(&data.layout2)?;
460
461 Ok(())
462 }
88 fn build_ui(mut data: MessageBank) -> Result<MessageBankUi, nwg::NwgError> {
89 use nwg::Event as E;
90
91 // Controls
92 nwg::Window::builder()
93 .flags(nwg::WindowFlags::MAIN_WINDOW | nwg::WindowFlags::VISIBLE)
94 .size((400, 300))
95 .position((800, 300))
96 .title("My message bank")
97 .build(&mut data.window)?;
98
99 nwg::TextInput::builder()
100 .text("Hello World!")
101 .focus(true)
102 .parent(&data.window)
103 .build(&mut data.message_content)?;
104
105 nwg::Button::builder()
106 .text("Save")
107 .parent(&data.window)
108 .build(&mut data.add_message_btn)?;
109
110 nwg::TextInput::builder()
111 .text("Title")
112 .parent(&data.window)
113 .build(&mut data.message_title)?;
114
115 // Wrap-up
116 let ui = MessageBankUi {
117 inner: Rc::new(data),
118 default_handler: Default::default(),
119 };
120
121 // Events
122 let window_handles = [&ui.window.handle];
123
124 for handle in window_handles.iter() {
125 let evt_ui = Rc::downgrade(&ui.inner);
126 let handle_events = move |evt, _evt_data, handle| {
127 if let Some(evt_ui) = evt_ui.upgrade() {
128 match evt {
129 E::OnButtonClick => {
130 if &handle == &evt_ui.add_message_btn { MessageBank::add_message(&evt_ui); }
131 },
132 E::OnWindowClose => {
133 if &handle == &evt_ui.window { MessageBank::exit(&evt_ui); }
134 },
135 _ => {}
136 }
137 }
138 };
139
140 ui.default_handler.borrow_mut().push(
141 nwg::full_bind_event_handler(handle, handle_events)
142 );
143 }
144
145 // Layout
146 nwg::GridLayout::builder()
147 .parent(&ui.window)
148 .max_row(Some(6))
149 .child(0, 0, &ui.add_message_btn)
150 .child_item(nwg::GridLayoutItem::new(&ui.message_title, 1, 0, 2, 1))
151 .child_item(nwg::GridLayoutItem::new(&ui.message_content, 3, 0, 3, 1))
152 .build(&ui.layout)?;
153
154 return Ok(ui);
155 }
Sourcepub fn password_char(&self) -> Option<char>
pub fn password_char(&self) -> Option<char>
Return the password character displayed by the text input. If the input is not a password, return None.
Sourcepub fn set_password_char(&self, c: Option<char>)
pub fn set_password_char(&self, c: Option<char>)
Set or Remove the password character displayed by the text input. If the input is not a password all character are re-rendered with the new character
Sourcepub fn set_limit(&self, limit: usize)
pub fn set_limit(&self, limit: usize)
Set the number of maximum character allowed in this text input
If limit
is 0, the text length is set to 0x7FFFFFFE characters
Sourcepub fn modified(&self) -> bool
pub fn modified(&self) -> bool
Check if the content of the text input was modified after it’s creation
Sourcepub fn set_modified(&self, e: bool)
pub fn set_modified(&self, e: bool)
Manually set modified flag of the text input
Sourcepub fn selection(&self) -> Range<u32>
pub fn selection(&self) -> Range<u32>
Return the selected range of characters by the user in the text input
Sourcepub fn set_selection(&self, r: Range<u32>)
pub fn set_selection(&self, r: Range<u32>)
Return the selected range of characters by the user in the text input
Sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Return the length of the user input in the control. This is better than input.text().len()
as it
does not allocate a string in memory
Sourcepub fn readonly(&self) -> bool
pub fn readonly(&self) -> bool
Return true if the TextInput value cannot be edited. Retrurn false otherwise. A user can still copy text from a readonly TextEdit (unlike disabled)
Sourcepub fn set_readonly(&self, r: bool)
pub fn set_readonly(&self, r: bool)
Set the readonly flag of the text input A user can still copy text from a readonly TextEdit (unlike disabled)
Sourcepub fn enabled(&self) -> bool
pub fn enabled(&self) -> bool
Return true if the control user can interact with the control, return false otherwise
Sourcepub fn set_enabled(&self, v: bool)
pub fn set_enabled(&self, v: bool)
Enable or disable the control
Sourcepub fn visible(&self) -> bool
pub fn visible(&self) -> bool
Return true if the control is visible to the user. Will return true even if the control is outside of the parent client view (ex: at the position (10000, 10000))
Sourcepub fn set_visible(&self, v: bool)
pub fn set_visible(&self, v: bool)
Show or hide the control to the user
Sourcepub fn set_position(&self, x: i32, y: i32)
pub fn set_position(&self, x: i32, y: i32)
Set the position of the button in the parent window
Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Return the text displayed in the TextInput
Examples found in repository?
More examples
48 fn number(&self, button: &nwg::Button) {
49 let text = self.input.text();
50 self.input.set_text(&format!("{}{}", text, button.text()));
51 }
52
53 fn clear(&self) {
54 self.input.set_text("");
55 }
56
57 fn compute(&self) {
58 use Token::*;
59 static SYMBOLS: &'static [char] = &['+', '-', '*', '/'];
60
61 let eq = self.input.text();
62 if eq.len() == 0 {
63 return;
64 }
65
66 let mut tokens: Vec<Token> = Vec::with_capacity(5);
67 let mut last = 0;
68
69 for (i, chr) in eq.char_indices() {
70 if SYMBOLS.iter().any(|&s| s == chr) {
71 let left = &eq[last..i];
72 match left.parse::<i32>() {
73 Ok(i) => tokens.push(Token::Number(i)),
74 _ => {
75 nwg::error_message("Error", "Invalid equation!");
76 self.input.set_text("");
77 return
78 }
79 }
80
81 let tk = match chr {
82 '+' => Plus,
83 '-' => Minus,
84 '*' => Mult,
85 '/' => Div,
86 _ => unreachable!()
87 };
88
89 tokens.push(tk);
90
91 last = i+1;
92 }
93 }
94
95 let right = &eq[last..];
96 match right.parse::<i32>() {
97 Ok(i) => tokens.push(Token::Number(i)),
98 _ => {
99 nwg::error_message("Error", "Invalid equation!");
100 self.input.set_text("");
101 return
102 }
103 }
104
105 let mut i = 1;
106 let mut result = match &tokens[0] { Token::Number(n) => *n, _ => unreachable!() };
107 while i < tokens.len() {
108 match [&tokens[i], &tokens[i+1]] {
109 [Plus, Number(n)] => { result += n; },
110 [Minus, Number(n)] => { result -= n;},
111 [Mult, Number(n)] => { result *= n; },
112 [Div, Number(n)] => { result /= n; },
113 _ => unreachable!()
114 }
115 i += 2;
116 }
117
118 self.input.set_text(&result.to_string());
119 }
26 fn add_message(&self) {
27 let title = self.message_title.text();
28 let content = self.message_content.text();
29
30 let mut new_button = Default::default();
31 nwg::Button::builder()
32 .text(&title)
33 .parent(&self.window)
34 .build(&mut new_button)
35 .expect("Failed to build button");
36
37 let mut buttons = self.buttons.borrow_mut();
38 let mut handlers = self.handlers.borrow_mut();
39
40 let blen = buttons.len() as u32;
41 let (x, y) = (blen % 6, blen / 6);
42 self.layout.add_child(x, y+1, &new_button);
43
44 // You can share controls handle with events handlers
45 let new_button_handle = new_button.handle;
46 let handler = nwg::bind_event_handler(&new_button.handle, &self.window.handle, move |evt, _evt_data, handle| {
47 match evt {
48 nwg::Event::OnButtonClick => {
49 if handle == new_button_handle {
50 nwg::simple_message(&title, &content);
51 }
52 },
53 _ => {}
54 }
55 });
56
57 buttons.push(new_button);
58 handlers.push(handler);
59 }
12fn main() {
13 nwg::init().expect("Failed to init Native Windows GUI");
14 nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
15
16 let mut window = Default::default();
17 let mut name_edit = Default::default();
18 let mut hello_button = Default::default();
19 let layout = Default::default();
20
21 nwg::Window::builder()
22 .size((300, 115))
23 .position((300, 300))
24 .title("Basic example")
25 .build(&mut window)
26 .unwrap();
27
28 nwg::TextInput::builder()
29 .text("Heisenberg")
30 .focus(true)
31 .parent(&window)
32 .build(&mut name_edit)
33 .unwrap();
34
35 nwg::Button::builder()
36 .text("Say my name")
37 .parent(&window)
38 .build(&mut hello_button)
39 .unwrap();
40
41 nwg::GridLayout::builder()
42 .parent(&window)
43 .spacing(1)
44 .child(0, 0, &name_edit)
45 .child_item(nwg::GridLayoutItem::new(&hello_button, 0, 1, 1, 2))
46 .build(&layout)
47 .unwrap();
48
49 let window = Rc::new(window);
50 let events_window = window.clone();
51
52 let handler = nwg::full_bind_event_handler(&window.handle, move |evt, _evt_data, handle| {
53 use nwg::Event as E;
54
55 match evt {
56 E::OnWindowClose =>
57 if &handle == &events_window as &nwg::Window {
58 nwg::modal_info_message(&events_window.handle, "Goodbye", &format!("Goodbye {}", name_edit.text()));
59 nwg::stop_thread_dispatch();
60 },
61 E::OnButtonClick =>
62 if &handle == &hello_button {
63 nwg::modal_info_message(&events_window.handle, "Hello", &format!("Hello {}", name_edit.text()));
64 },
65 _ => {}
66 }
67 });
68
69 nwg::dispatch_thread_events();
70 nwg::unbind_event_handler(&handler);
71}
Sourcepub fn set_text<'a>(&self, v: &'a str)
pub fn set_text<'a>(&self, v: &'a str)
Set the text displayed in the TextInput
Examples found in repository?
48 fn number(&self, button: &nwg::Button) {
49 let text = self.input.text();
50 self.input.set_text(&format!("{}{}", text, button.text()));
51 }
52
53 fn clear(&self) {
54 self.input.set_text("");
55 }
56
57 fn compute(&self) {
58 use Token::*;
59 static SYMBOLS: &'static [char] = &['+', '-', '*', '/'];
60
61 let eq = self.input.text();
62 if eq.len() == 0 {
63 return;
64 }
65
66 let mut tokens: Vec<Token> = Vec::with_capacity(5);
67 let mut last = 0;
68
69 for (i, chr) in eq.char_indices() {
70 if SYMBOLS.iter().any(|&s| s == chr) {
71 let left = &eq[last..i];
72 match left.parse::<i32>() {
73 Ok(i) => tokens.push(Token::Number(i)),
74 _ => {
75 nwg::error_message("Error", "Invalid equation!");
76 self.input.set_text("");
77 return
78 }
79 }
80
81 let tk = match chr {
82 '+' => Plus,
83 '-' => Minus,
84 '*' => Mult,
85 '/' => Div,
86 _ => unreachable!()
87 };
88
89 tokens.push(tk);
90
91 last = i+1;
92 }
93 }
94
95 let right = &eq[last..];
96 match right.parse::<i32>() {
97 Ok(i) => tokens.push(Token::Number(i)),
98 _ => {
99 nwg::error_message("Error", "Invalid equation!");
100 self.input.set_text("");
101 return
102 }
103 }
104
105 let mut i = 1;
106 let mut result = match &tokens[0] { Token::Number(n) => *n, _ => unreachable!() };
107 while i < tokens.len() {
108 match [&tokens[i], &tokens[i+1]] {
109 [Plus, Number(n)] => { result += n; },
110 [Minus, Number(n)] => { result -= n;},
111 [Mult, Number(n)] => { result *= n; },
112 [Div, Number(n)] => { result /= n; },
113 _ => unreachable!()
114 }
115 i += 2;
116 }
117
118 self.input.set_text(&result.to_string());
119 }
Sourcepub fn placeholder_text<'a>(&self, text_length: usize) -> String
pub fn placeholder_text<'a>(&self, text_length: usize) -> String
Return the placeholder text displayed in the TextInput when it is empty and does not have focus. The string returned will be as long as the user specified, however it might be longer or shorter than the actual placeholder text.
Sourcepub fn set_placeholder_text<'a>(&self, v: Option<&'a str>)
pub fn set_placeholder_text<'a>(&self, v: Option<&'a str>)
Set the placeholder text displayed in the TextInput when it is empty and does not have focus
Sourcepub fn class_name(&self) -> &'static str
pub fn class_name(&self) -> &'static str
Winapi class name used during control creation
Sourcepub fn forced_flags(&self) -> u32
pub fn forced_flags(&self) -> u32
Winapi flags required by the control