linux_display.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use serde::{Deserialize, Serialize};
  2. use serde_json::json;
  3. use std::path::PathBuf;
  4. use tauri::AppHandle;
  5. use tauri_plugin_store::StoreExt;
  6. use crate::constants::SETTINGS_STORE;
  7. pub const LINUX_DISPLAY_CONFIG_KEY: &str = "linuxDisplayConfig";
  8. #[derive(Default, Serialize, Deserialize)]
  9. struct DisplayConfig {
  10. wayland: Option<bool>,
  11. }
  12. fn dir() -> Option<PathBuf> {
  13. Some(dirs::data_dir()?.join("ai.opencode.desktop"))
  14. }
  15. fn path() -> Option<PathBuf> {
  16. dir().map(|dir| dir.join(SETTINGS_STORE))
  17. }
  18. pub fn read_wayland() -> Option<bool> {
  19. let path = path()?;
  20. let raw = std::fs::read_to_string(path).ok()?;
  21. let config = serde_json::from_str::<DisplayConfig>(&raw).ok()?;
  22. config.wayland
  23. }
  24. pub fn write_wayland(app: &AppHandle, value: bool) -> Result<(), String> {
  25. let store = app
  26. .store(SETTINGS_STORE)
  27. .map_err(|e| format!("Failed to open settings store: {}", e))?;
  28. store.set(
  29. LINUX_DISPLAY_CONFIG_KEY,
  30. json!(DisplayConfig {
  31. wayland: Some(value),
  32. }),
  33. );
  34. store
  35. .save()
  36. .map_err(|e| format!("Failed to save settings store: {}", e))?;
  37. Ok(())
  38. }