mattrguerra/cortexmainPrivate on GitHub, published here in full

state.rs

Language Rust1.1 KB35 lines

src-tauri/src/state.rs

  1. use std::collections::HashMap;
  2. use std::path::PathBuf;
  3. use std::sync::{Mutex, MutexGuard, PoisonError};
  4. use std::time::Instant;
  5. use cortex_core::secrets::KeyCache;
  6. use cortex_core::storage::Db;
  7. use tokio_util::sync::CancellationToken;
  8. pub struct AppState {
  9. pub db: Mutex<Db>,
  10. pub keys: KeyCache,
  11. pub streams: Mutex<HashMap<String, CancellationToken>>,
  12. pub data_dir: PathBuf,
  13. /// When the quick window was last shown; blur right after showing is ignored.
  14. pub quick_shown_at: Mutex<Option<Instant>>,
  15. /// While a native dialog is open, losing focus must not hide the quick window.
  16. pub dialog_open: Mutex<bool>,
  17. pub tray_spend: Mutex<Option<tauri::menu::MenuItem<tauri::Wry>>>,
  18. }
  19. /// Lock even if a panic elsewhere poisoned the mutex, so one failed command
  20. /// does not fail every later one until the app restarts.
  21. pub fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
  22. m.lock().unwrap_or_else(PoisonError::into_inner)
  23. }
  24. impl AppState {
  25. pub fn db(&self) -> MutexGuard<'_, Db> {
  26. lock(&self.db)
  27. }
  28. pub fn attachments_dir(&self) -> PathBuf {
  29. self.data_dir.join("attachments")
  30. }
  31. }