linux_display.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(if cfg!(debug_assertions) {
  14. "ai.opencode.desktop.dev"
  15. } else {
  16. "ai.opencode.desktop"
  17. }))
  18. }
  19. fn path() -> Option<PathBuf> {
  20. dir().map(|dir| dir.join(SETTINGS_STORE))
  21. }
  22. pub fn read_wayland() -> Option<bool> {
  23. let raw = std::fs::read_to_string(path()?).ok()?;
  24. let root = serde_json::from_str::<serde_json::Value>(&raw)
  25. .ok()?
  26. .get(LINUX_DISPLAY_CONFIG_KEY)
  27. .cloned()?;
  28. serde_json::from_value::<DisplayConfig>(root).ok()?.wayland
  29. }
  30. pub fn write_wayland(app: &AppHandle, value: bool) -> Result<(), String> {
  31. let store = app
  32. .store(SETTINGS_STORE)
  33. .map_err(|e| format!("Failed to open settings store: {}", e))?;
  34. store.set(
  35. LINUX_DISPLAY_CONFIG_KEY,
  36. json!(DisplayConfig {
  37. wayland: Some(value),
  38. }),
  39. );
  40. store
  41. .save()
  42. .map_err(|e| format!("Failed to save settings store: {}", e))?;
  43. Ok(())
  44. }