use crate::app::{Action, reducer};
use crate::config;
use crate::errors::ApplicationError;
use crate::ui::font;
use super::super::HandlerContext;
pub async fn handle(action: &Action, ctx: &mut HandlerContext<'_>) -> Result<bool, ApplicationError> {
if !matches!(action, Action::ReloadConfig) {
return Ok(false);
}
match config::load() {
Ok(settings) => {
ctx.app.state.theme = settings.theme.clone();
ctx.app.state.available_themes = config::list_themes(); ctx.app.state.commit_template = settings.commit_template.clone();
ctx.app.state.custom_commands = settings.custom_commands.clone();
ctx.app.state.background_image_path = settings.ui.background_image_path.clone();
ctx.app.state.background_opacity = settings.ui.background_opacity;
ctx.app.state.font_family = settings.ui.font_family.clone();
ctx.app.state.font_size = settings.ui.font_size;
ctx.app.state.auto_fetch_interval_secs = settings.core.auto_fetch_interval;
ctx.app.state.auto_fetch_remote = settings.core.auto_fetch_remote.clone();
ctx.bg_manager.set_opacity(ctx.app.state.background_opacity);
if let Some(path) = &ctx.app.state.background_image_path {
if let Err(e) = ctx.bg_manager.load(path) {
tracing::error!("Failed to reload background image: {}", e);
ctx.app.state = reducer(ctx.app.state.clone(), Action::SetStatusError(Some(format!("Background reload failed: {}", e))));
}
}
ctx.app.state.font_family = settings.ui.font_family.clone();
ctx.app.state.font_size = settings.ui.font_size;
if let Some(f) = &ctx.app.state.font_family {
font::set_font(f);
} else {
font::reset_font();
}
ctx.app.state = reducer(ctx.app.state.clone(), Action::SetFeedback(Some("Configuration reloaded".to_string())));
}
Err(e) => {
ctx.app.state = reducer(
ctx.app.state.clone(),
Action::SetStatusError(Some(format!("config reload failed: {e}")))
);
}
}
Ok(true)
}