You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pubstructAutoSave{last_content_hash:u64,last_save_time:Instant,save_delay:Duration,pending_save:bool,}implAutoSave{pubfnnew(save_delay:Duration) -> Self{Self{last_content_hash:0,last_save_time:Instant::now(),
save_delay,pending_save:false,}}/// Appelé quand le contenu changepubfncontent_changed(&mutself,content:&impl std::hash::Hash){use std::hash::{Hash,Hasher};use std::collections::hash_map::DefaultHasher;letmut hasher = DefaultHasher::new();
content.hash(&mut hasher);let new_hash = hasher.finish();if new_hash != self.last_content_hash{self.last_content_hash = new_hash;self.pending_save = true;}}/// Retourne true si on doit sauvegarder maintenantpubfnshould_save(&self) -> bool{self.pending_save && self.last_save_time.elapsed() >= self.save_delay}/// Marquer comme sauvegardépubfnmark_saved(&mutself){self.pending_save = false;self.last_save_time = Instant::now();}/// Indicateur pour l'UIpubfnstatus(&self) -> AutoSaveStatus{ifself.pending_save{AutoSaveStatus::Pending}else{AutoSaveStatus::Saved(self.last_save_time)}}}pubenumAutoSaveStatus{Saved(Instant),Pending,Saving,Error(String),}implAutoSaveStatus{pubfnrender(&self,ui:&mutUi,tokens:&DesignTokens){let(icon, text, color) = matchself{AutoSaveStatus::Saved(time) => {let elapsed = time.elapsed();let text = if elapsed < Duration::from_secs(5){"Sauvegardé".to_string()}elseif elapsed < Duration::from_secs(60){format!("Sauvegardé il y a {}s", elapsed.as_secs())}else{format!("Sauvegardé il y a {}m", elapsed.as_secs() / 60)};("✓", text, tokens.colors.success)}AutoSaveStatus::Pending => {("○","Modifications non sauvegardées".to_string(), tokens.colors.warning)}AutoSaveStatus::Saving => {("↻","Sauvegarde...".to_string(), tokens.colors.text_muted)}AutoSaveStatus::Error(msg) => {("✗",format!("Erreur: {}", msg), tokens.colors.error)}};
ui.horizontal(|ui| {
ui.label(
egui::RichText::new(icon).size(tokens.typography.size_sm).color(color));
ui.label(
egui::RichText::new(text).size(tokens.typography.size_xs).color(tokens.colors.text_muted));});}}
Usage
letmut autosave = AutoSave::new(Duration::from_secs(2));// Quand le contenu change
autosave.content_changed(&document);// Dans la boucle principaleif autosave.should_save(){matchsave_document().await{Ok(_) => autosave.mark_saved(),Err(e) => {// Gérer l'erreur}}}// Afficher le statut dans l'UI
autosave.status().render(ui,&tokens);
Résumé
Détection de changement : Hash du contenu
Délai : Sauvegarde après 2 secondes d'inactivité
Indicateur visuel : Statut dans la barre de statut
Gestion d'erreur : Afficher les erreurs de sauvegarde