Skip to main content

console_ui_engine_null/
ui_element.rs

1pub use std::any::Any;
2
3use crate::buffer::SizedBuffer;
4use crate::console::ConsoleUpdateInfo;
5
6pub trait UiElement {
7    fn update(&mut self, _console: &mut ConsoleUpdateInfo) {}
8    fn render(&self, _buffer: &mut SizedBuffer) {}
9    fn get_name(&self) -> &str;
10    fn as_any(&self) -> &dyn Any;
11    fn as_any_mut(&mut self) -> &mut dyn Any;
12    fn is_clicked(&self, _x: u16, _y: u16) -> bool { false }
13
14    fn is_focusable(&self) -> bool {false}
15    fn has_focus(&self) -> bool;
16    fn on_focus(&mut self) {}
17    fn on_focus_removed(&mut self) {}
18}
19    #[macro_export]
20    macro_rules! ui_component_struct {
21        (pub struct $name:ident { $( $vis:vis $field:ident: $ty:ty ),* $(,)* }) => {
22            pub struct $name {
23                pub name: &'static str,
24                focused: bool,
25                $( $vis $field: $ty ),*
26            }
27        };
28    }
29
30    #[macro_export]
31    macro_rules! ui_component_impl {
32        () => {
33            fn get_name(&self) -> &str {
34                self.name.clone()
35            }
36            fn has_focus(&self) -> bool { self.focused }
37
38            fn as_any(&self) -> &dyn Any { self }
39            fn as_any_mut(&mut self) -> &mut dyn Any { self }
40        };
41    }