use crate::{
action::Action,
cli::CliArgs,
configs::custom::{
app_custom::AppConfig, keymap_custom::KeymapConfig, palette_custom::PaletteConfig,
telegram_custom::TelegramConfig, theme_custom::ThemeConfig,
},
tg::tg_context::TgContext,
};
use ratatui::style::Style;
use std::sync::{atomic::AtomicBool, Arc, Mutex, MutexGuard};
use std::{io, sync::atomic::Ordering};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
macro_rules! theme_style_generate {
($fn_name: ident, $map: ident, $attr_name: ident) => {
#[inline]
pub fn $fn_name(&self) -> Style {
if self.app_config().theme_enable {
self.theme_config()
.$map
.get(stringify!($attr_name))
.unwrap()
.as_style()
} else {
Style::default()
}
}
};
}
#[derive(Debug)]
pub struct AppContext {
app_config: Mutex<AppConfig>,
keymap_config: Mutex<KeymapConfig>,
theme_config: Mutex<ThemeConfig>,
palette_config: Mutex<PaletteConfig>,
tg_config: Mutex<TelegramConfig>,
action_tx: Mutex<UnboundedSender<Action>>,
action_rx: Mutex<UnboundedReceiver<Action>>,
quit: AtomicBool,
tg_context: Arc<TgContext>,
cli_args: Mutex<CliArgs>,
}
impl AppContext {
pub fn new(
app_config: AppConfig,
keymap_config: KeymapConfig,
theme_config: ThemeConfig,
palette_config: PaletteConfig,
telegram_config: TelegramConfig,
tg_context: TgContext,
cli_args: CliArgs,
) -> Result<Self, io::Error> {
let (action_tx, action_rx) = tokio::sync::mpsc::unbounded_channel::<Action>();
let quit = false;
Ok(Self {
app_config: Mutex::new(app_config),
keymap_config: Mutex::new(keymap_config),
palette_config: Mutex::new(palette_config),
theme_config: Mutex::new(theme_config),
tg_config: Mutex::new(telegram_config),
action_rx: Mutex::new(action_rx),
action_tx: Mutex::new(action_tx),
quit: AtomicBool::new(quit),
tg_context: Arc::new(tg_context),
cli_args: Mutex::new(cli_args),
})
}
pub fn app_config(&self) -> MutexGuard<'_, AppConfig> {
self.app_config.lock().unwrap()
}
pub fn keymap_config(&self) -> MutexGuard<'_, KeymapConfig> {
self.keymap_config.lock().unwrap()
}
pub fn theme_config(&self) -> MutexGuard<'_, ThemeConfig> {
self.theme_config.lock().unwrap()
}
pub fn palette_config(&self) -> MutexGuard<'_, PaletteConfig> {
self.palette_config.lock().unwrap()
}
pub fn telegram_config(&self) -> MutexGuard<'_, TelegramConfig> {
self.tg_config.lock().unwrap()
}
pub fn action_rx(&self) -> MutexGuard<'_, UnboundedReceiver<Action>> {
self.action_rx.lock().unwrap()
}
pub fn action_tx(&self) -> MutexGuard<'_, UnboundedSender<Action>> {
self.action_tx.lock().unwrap()
}
pub fn quit_acquire(&self) -> bool {
self.quit.load(Ordering::Acquire)
}
pub fn quit_store(&self, value: bool) {
self.quit.store(value, Ordering::Release);
}
pub fn tg_context(&self) -> Arc<TgContext> {
Arc::clone(&self.tg_context)
}
pub fn cli_args(&self) -> MutexGuard<'_, CliArgs> {
self.cli_args.lock().unwrap()
}
theme_style_generate!(
style_border_component_focused,
common,
border_component_focused
);
theme_style_generate!(style_item_selected, common, item_selected);
theme_style_generate!(style_timestamp, common, timestamp);
theme_style_generate!(style_chat_list, chat_list, self);
theme_style_generate!(style_chat_list_item_selected, chat_list, item_selected);
theme_style_generate!(style_chat_list_item_chat_name, chat_list, item_chat_name);
theme_style_generate!(
style_chat_list_item_message_content,
chat_list,
item_message_content
);
theme_style_generate!(
style_chat_list_item_unread_counter,
chat_list,
item_unread_counter
);
theme_style_generate!(style_chat, chat, self);
theme_style_generate!(style_chat_chat_name, chat, chat_name);
theme_style_generate!(style_chat_message_myself_name, chat, message_myself_name);
theme_style_generate!(style_chat_message_other_name, chat, message_other_name);
theme_style_generate!(
style_chat_message_myself_content,
chat,
message_myself_content
);
theme_style_generate!(
style_chat_message_other_content,
chat,
message_other_content
);
theme_style_generate!(style_chat_message_reply_text, chat, message_reply_text);
theme_style_generate!(
style_chat_message_myself_reply_name,
chat,
message_myself_reply_name
);
theme_style_generate!(
style_chat_message_myself_reply_content,
chat,
message_myself_reply_content
);
theme_style_generate!(
style_chat_message_other_reply_name,
chat,
message_other_reply_name
);
theme_style_generate!(
style_chat_message_other_reply_content,
chat,
message_other_reply_content
);
theme_style_generate!(style_prompt, prompt, self);
theme_style_generate!(style_prompt_message_text, prompt, message_text);
theme_style_generate!(
style_prompt_message_text_selected,
prompt,
message_text_selected
);
theme_style_generate!(
style_prompt_message_preview_text,
prompt,
message_preview_text
);
theme_style_generate!(style_reply_message, reply_message, self);
theme_style_generate!(
style_reply_message_message_text,
reply_message,
message_text
);
theme_style_generate!(style_status_bar, status_bar, self);
theme_style_generate!(style_status_bar_size_info_text, status_bar, size_info_text);
theme_style_generate!(
style_status_bar_size_info_numbers,
status_bar,
size_info_numbers
);
theme_style_generate!(style_status_bar_press_key_text, status_bar, press_key_text);
theme_style_generate!(style_status_bar_press_key_key, status_bar, press_key_key);
theme_style_generate!(
style_status_bar_message_quit_text,
status_bar,
message_quit_text
);
theme_style_generate!(
style_status_bar_message_quit_key,
status_bar,
message_quit_key
);
theme_style_generate!(style_status_bar_open_chat_text, status_bar, open_chat_text);
theme_style_generate!(style_status_bar_open_chat_name, status_bar, open_chat_name);
theme_style_generate!(style_title_bar, title_bar, self);
theme_style_generate!(style_title_bar_title1, title_bar, title1);
theme_style_generate!(style_title_bar_title2, title_bar, title2);
theme_style_generate!(style_title_bar_title3, title_bar, title3);
}