eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Config-related action handlers.

use crate::app::{Action, reducer};
use crate::config;
use crate::errors::ApplicationError;
use crate::ui::font;
use super::super::HandlerContext;

/// Handle config-related actions. Returns true if handled.
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) => {
            // Reload all configuration values
            // Update state from settings
            ctx.app.state.theme = settings.theme.clone();
            ctx.app.state.available_themes = config::list_themes(); // This line was not in the edit, keeping it.
            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;
            
            // Re-apply auto fetch settings
            ctx.app.state.auto_fetch_interval_secs = settings.core.auto_fetch_interval;
            ctx.app.state.auto_fetch_remote = settings.core.auto_fetch_remote.clone();
            
            // Reload Background Manager
            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))));
                }
            }
            
            // Update Font
            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)
}