refactor: tags

This commit is contained in:
zhom
2025-08-15 00:04:10 +04:00
parent 334f894e68
commit 7c2ed1e0fc
4 changed files with 256 additions and 157 deletions
+20 -27
View File
@@ -142,11 +142,9 @@ impl BrowserRunner {
let profile_manager = ProfileManager::instance();
let result = profile_manager.save_profile(profile);
// Update tag suggestions after any save
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
result
}
@@ -154,11 +152,9 @@ impl BrowserRunner {
let profile_manager = ProfileManager::instance();
let profiles = profile_manager.list_profiles();
if let Ok(ref ps) = profiles {
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(ps);
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(ps);
});
}
profiles
}
@@ -264,12 +260,10 @@ impl BrowserRunner {
println!("Updated proxy PID mapping from temp (0) to actual PID: {process_id}");
}
// Save the updated profile
self.save_process_info(&updated_profile)?;
// Ensure tag suggestions include any tags from this profile
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
// Save the updated profile
self.save_process_info(&updated_profile)?;
// Ensure tag suggestions include any tags from this profile
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
println!(
@@ -471,11 +465,9 @@ impl BrowserRunner {
updated_profile.last_launch = Some(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs());
self.save_process_info(&updated_profile)?;
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
// Apply proxy settings if needed (for Firefox-based browsers)
if profile.proxy_id.is_some()
@@ -842,11 +834,9 @@ impl BrowserRunner {
}
// Rebuild tags after deletion
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
Ok(())
}
@@ -1642,7 +1632,10 @@ pub async fn update_profile_proxy(
}
#[tauri::command]
pub fn update_profile_tags(profile_name: String, tags: Vec<String>) -> Result<BrowserProfile, String> {
pub fn update_profile_tags(
profile_name: String,
tags: Vec<String>,
) -> Result<BrowserProfile, String> {
let profile_manager = ProfileManager::instance();
profile_manager
.update_profile_tags(&profile_name, tags)
+12 -20
View File
@@ -287,11 +287,9 @@ impl ProfileManager {
self.save_profile(&profile)?;
// Keep tag suggestions up to date after name change (rebuild from all profiles)
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
Ok(profile)
}
@@ -331,11 +329,9 @@ impl ProfileManager {
println!("Profile '{profile_name}' deleted successfully");
// Rebuild tag suggestions after deletion
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
Ok(())
}
@@ -412,11 +408,9 @@ impl ProfileManager {
}
// Rebuild tag suggestions after group changes just in case
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
Ok(())
}
@@ -440,11 +434,9 @@ impl ProfileManager {
self.save_profile(&profile)?;
// Update global tag suggestions from all profiles
let _ = crate::tag_manager::TAG_MANAGER
.lock()
.map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
let _ = crate::tag_manager::TAG_MANAGER.lock().map(|tm| {
let _ = tm.rebuild_from_profiles(&self.list_profiles().unwrap_or_default());
});
Ok(profile)
}
+11 -28
View File
@@ -19,7 +19,9 @@ impl TagManager {
pub fn new() -> Self {
Self {
base_dirs: BaseDirs::new().expect("Failed to get base directories"),
data_dir_override: std::env::var("DONUTBROWSER_DATA_DIR").ok().map(PathBuf::from),
data_dir_override: std::env::var("DONUTBROWSER_DATA_DIR")
.ok()
.map(PathBuf::from),
}
}
@@ -40,7 +42,11 @@ impl TagManager {
}
let mut path = self.base_dirs.data_local_dir().to_path_buf();
path.push(if cfg!(debug_assertions) { "DonutBrowserDev" } else { "DonutBrowser" });
path.push(if cfg!(debug_assertions) {
"DonutBrowserDev"
} else {
"DonutBrowser"
});
path.push("data");
path.push("tags.json");
path
@@ -74,21 +80,6 @@ impl TagManager {
Ok(all)
}
pub fn suggest_tags(&self, query: Option<&str>) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let all = self.get_all_tags()?;
if let Some(q) = query {
let q_lower = q.to_lowercase();
Ok(
all
.into_iter()
.filter(|t| t.to_lowercase().contains(&q_lower))
.collect(),
)
} else {
Ok(all)
}
}
pub fn rebuild_from_profiles(
&self,
profiles: &[BrowserProfile],
@@ -102,21 +93,13 @@ impl TagManager {
}
}
let combined: Vec<String> = set.into_iter().collect();
self.save_tags_data(&TagsData { tags: combined.clone() })?;
self.save_tags_data(&TagsData {
tags: combined.clone(),
})?;
Ok(combined)
}
pub fn add_tags(&self, tags: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let mut data = self.load_tags_data()?;
data.tags.extend(tags.iter().cloned());
data.tags.sort();
data.tags.dedup();
self.save_tags_data(&data)
}
}
lazy_static::lazy_static! {
pub static ref TAG_MANAGER: std::sync::Mutex<TagManager> = std::sync::Mutex::new(TagManager::new());
}