summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop/src-tauri/src/linux_display.rs
blob: 0179cf8bbb3720c7c33d644793bd35c1cee1e9a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::path::PathBuf;
use tauri::AppHandle;
use tauri_plugin_store::StoreExt;

use crate::constants::SETTINGS_STORE;

pub const LINUX_DISPLAY_CONFIG_KEY: &str = "linuxDisplayConfig";

#[derive(Default, Serialize, Deserialize)]
struct DisplayConfig {
    wayland: Option<bool>,
}

fn dir() -> Option<PathBuf> {
    Some(dirs::data_dir()?.join(if cfg!(debug_assertions) {
        "ai.opencode.desktop.dev"
    } else {
        "ai.opencode.desktop"
    }))
}

fn path() -> Option<PathBuf> {
    dir().map(|dir| dir.join(SETTINGS_STORE))
}

pub fn read_wayland() -> Option<bool> {
    let raw = std::fs::read_to_string(path()?).ok()?;
    let root = serde_json::from_str::<serde_json::Value>(&raw)
        .ok()?
        .get(LINUX_DISPLAY_CONFIG_KEY)
        .cloned()?;
    serde_json::from_value::<DisplayConfig>(root).ok()?.wayland
}

pub fn write_wayland(app: &AppHandle, value: bool) -> Result<(), String> {
    let store = app
        .store(SETTINGS_STORE)
        .map_err(|e| format!("Failed to open settings store: {}", e))?;

    store.set(
        LINUX_DISPLAY_CONFIG_KEY,
        json!(DisplayConfig {
            wayland: Some(value),
        }),
    );
    store
        .save()
        .map_err(|e| format!("Failed to save settings store: {}", e))?;

    Ok(())
}