#[macro_export]
macro_rules! components {
( $( $x:expr $( => $t:ty )* ),* ) => {
{
let mut temp_vec = Vec::new();
$(
temp_vec.push(
Box::new($x)
as Box<dyn matetui::Component $( $t + )* >
);
)*
temp_vec
}
};
}
#[macro_export]
macro_rules! children {
( $( $name:expr => $value:expr ),* ) => {
{
let mut map = std::collections::HashMap::new();
$(
map.insert(
$name.to_string(),
Box::new($value) as Box<dyn matetui::Component>
);
)*
map
}
};
}
#[macro_export]
macro_rules! component {
(
$(#[$outer:meta])*
$vis:vis struct $name:ident { $($fieldname:ident: $ty:ty),* $(,)? }
) => {
component! {
$(#[$outer])*
$vis struct $name { $($fieldname: $ty),* },
children => {}
}
};
(
$(#[$outer:meta])*
$vis:vis struct $name:ident { $($fieldname:ident: $ty:ty),* $(,)? },
children => {
$($childname:literal => $childval:expr),* $(,)?
}
) => {
$(#[$outer])*
$vis struct $name {
is_active: bool,
action_sender: Option<tokio::sync::mpsc::UnboundedSender<String>>,
children: matetui::Children,
$($fieldname: $ty),*
}
impl Default for $name {
fn default() -> Self {
Self {
is_active: false,
action_sender: None,
children: matetui::children!( $($childname => $childval),* ),
$($fieldname: Default::default()),*
}
}
}
impl matetui::ComponentAccessors for $name {
fn name(&self) -> String {
stringify!($name).to_string()
}
fn is_active(&self) -> bool {
self.is_active
}
fn set_active(&mut self, active: bool) {
self.is_active = active;
self.on_active_changed(active);
}
fn register_action_handler(&mut self, tx: tokio::sync::mpsc::UnboundedSender<String>) {
self.action_sender = Some(tx.clone());
}
fn send(&self, action: &str) {
if let Some(tx) = &self.action_sender {
tx.send(action.to_string()).unwrap();
}
}
fn send_action(&self, action: matetui::Action) {
if let Some(tx) = &self.action_sender {
tx.send(action.to_string()).unwrap();
}
}
fn as_active(mut self) -> Self {
self.set_active(true);
self.on_active_changed(true);
self
}
fn get_children(&mut self) -> Option<&mut matetui::Children> {
Some(&mut self.children)
}
}
};
}
#[macro_export]
macro_rules! kb {
($($key:expr => $action:expr),* $(,)?) => {
[
$(($key, $crate::ActionKind::from($action))),*
]
};
($(($key:expr, $action:expr)),* $(,)?) => {
[
$(($key, $crate::ActionKind::from($action))),*
]
};
}