forked from 1b-agent/opencadstudio-upstream
refactor(config): consolidate settings into one grouped settings.json
App preferences were spread across six flat plain-text files (settings.txt,
recent.txt, recent_limit.txt, statusbar.txt, ribbon.txt, plot.txt), each with a
bespoke line parser and no grouping. Replace them with a single grouped JSON,
serialized via serde (enabling the derive feature; serde_json was already a
dependency).
- New app/config.rs: AppConfig { settings, recent, statusbar, ribbon, plot } →
<config>/settings.json (pretty JSON), load()/save().
- Make UserSettings, StatusBarConfig/StatusPill, CollapseMode and
PlotDialogState serde-derived; #[serde(skip)] marks the plot dialog's runtime
fields so only print preferences persist. Their bespoke .txt load/save is
removed.
- App gains current_config/apply_config/save_config: one AppConfig::load at
startup distributes into live state; every settings-change site
(persist_settings_if_changed, recent add/remove/limit, status-bar pill
toggle, ribbon density, plot commit) routes through save_config, which diffs
against the last write to avoid thrashing.
- Clean start: the old flat files are ignored (no migration). ocad.pgp stays a
separate hand-editable file.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
256f9a282b
commit
dc7117f203
12 files changed
+228
-411
No files matched your search
@@ -54,7 +54,9 @@ const MAX_PANEL_SQUEEZE: f32 = 8.0;
|
||||
/// How the ribbon tool panels are sized. `Auto` adapts to the window width (the
|
||||
/// step-by-step degradation); the others pin every panel to one density so the
|
||||
/// user can override the automatic choice. The selection is persisted.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
||||
#[derive(
|
||||
Clone, Copy, PartialEq, Eq, Debug, Default, serde::Serialize, serde::Deserialize,
|
||||
)]
|
||||
pub enum CollapseMode {
|
||||
/// Size panels to the window: degrade from the right as space runs out.
|
||||
#[default]
|
||||
@@ -86,19 +88,6 @@ impl CollapseMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Stable identifier used for persistence.
|
||||
fn id(self) -> &'static str {
|
||||
match self {
|
||||
CollapseMode::Auto => "auto",
|
||||
CollapseMode::Full => "full",
|
||||
CollapseMode::Compact => "compact",
|
||||
CollapseMode::Collapsed => "collapsed",
|
||||
}
|
||||
}
|
||||
|
||||
fn from_id(s: &str) -> Option<Self> {
|
||||
CollapseMode::ALL.iter().copied().find(|m| m.id() == s)
|
||||
}
|
||||
|
||||
/// The degradation level every panel is pinned to, or `None` for `Auto`.
|
||||
fn forced_level(self) -> Option<u8> {
|
||||
@@ -110,27 +99,6 @@ impl CollapseMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Load the saved mode, defaulting to `Auto`.
|
||||
pub fn load() -> Self {
|
||||
config_path()
|
||||
.and_then(|p| std::fs::read_to_string(p).ok())
|
||||
.and_then(|s| CollapseMode::from_id(s.trim()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Persist this mode (best-effort; silent on failure).
|
||||
pub fn save(self) {
|
||||
let Some(path) = config_path() else { return };
|
||||
if let Some(dir) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(dir);
|
||||
}
|
||||
let _ = std::fs::write(path, self.id());
|
||||
}
|
||||
}
|
||||
|
||||
/// `<config-dir>/OpenCADStudio/ribbon.txt`, matching the other settings stores.
|
||||
fn config_path() -> Option<std::path::PathBuf> {
|
||||
Some(crate::config::config_dir()?.join("ribbon.txt"))
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CollapseMode {
|
||||
|
||||
@@ -161,15 +161,20 @@ impl Ribbon {
|
||||
active_table_style: String::new(),
|
||||
tab_bar_h: Arc::new(AtomicU32::new(28.0f32.to_bits())),
|
||||
tool_bar_h: Arc::new(AtomicU32::new(TOOL_BAR_H.to_bits())),
|
||||
collapse_mode: CollapseMode::load(),
|
||||
collapse_mode: CollapseMode::default(),
|
||||
collapse_tight: Arc::new(AtomicBool::new(false)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the tool-panel density and persist the choice.
|
||||
/// Change the tool-panel density. Persistence is handled by the caller via
|
||||
/// the consolidated app config (`save_config`).
|
||||
pub fn set_collapse_mode(&mut self, mode: CollapseMode) {
|
||||
self.collapse_mode = mode;
|
||||
mode.save();
|
||||
}
|
||||
|
||||
/// The current tool-panel density (for saving into the app config).
|
||||
pub fn collapse_mode(&self) -> CollapseMode {
|
||||
self.collapse_mode
|
||||
}
|
||||
|
||||
/// Current tab-bar height as last measured by the `WrapBar` widget.
|
||||
|
||||
Reference in new issue
Block a user