Skip to main content

editor/
editor_settings.rs

1use std::fs;
2use std::path::PathBuf;
3
4const EDITOR_SETTINGS_FILE: &str = "editor_settings.ron";
5
6/// Editor-wide preferences that aren't tied to any one project, persisted next to the editor
7/// executable — same pattern as [`crate::recent_projects::RecentProjects`], just a different
8/// piece of state. Currently just the external program "Open Script" (see
9/// `ui::draw_script_tile`) launches scripts in, picked once and remembered.
10#[derive(Default, serde::Serialize, serde::Deserialize)]
11pub struct EditorSettings {
12    pub preferred_editor: Option<PathBuf>,
13}
14
15impl EditorSettings {
16    fn file_path() -> PathBuf {
17        std::env::current_exe()
18            .ok()
19            .and_then(|exe| exe.parent().map(|dir| dir.join(EDITOR_SETTINGS_FILE)))
20            .unwrap_or_else(|| PathBuf::from(EDITOR_SETTINGS_FILE))
21    }
22
23    pub fn load() -> Self {
24        let Ok(text) = fs::read_to_string(Self::file_path()) else { return Self::default() };
25        ron::from_str(&text).unwrap_or_default()
26    }
27
28    pub fn save(&self) -> anyhow::Result<()> {
29        fs::write(Self::file_path(), ron::ser::to_string_pretty(self, Default::default())?)?;
30        Ok(())
31    }
32}