forked from 1b-agent/fieldcraft-upstream
Navigation: orbit about the point under the cursor, zoom toward it, presets in the display menu
An orbit turns about the model point under the press (the pick ray's first hit on the plot, the mesh or the design), or the centre of what is drawn when it starts on empty space, keeps that point still on screen and marks it while orbiting. The wheel and a pinch zoom toward the point under the cursor; the wheel's delta is read as it arrives, so a notch zooms in full in orthographic and perspective views alike (#273). Mouse presets and reverse zoom come from SolidCraft (prefs.rs NavPreset, NAV_PRESETS; MIT OR Apache-2.0): FieldCraft's own (left drag per the navigation bar) stays the default, Ctrl + left drag from empty space orbits in every preset. They are chosen in the display menu, settable with ui.set and kept in prefs.json beside recent.json (the browser build keeps them for the page's run); an older prefs.json still loads. Fixes #262, fixes #273. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UhhZMBTviDQxZvwnyP89fo
This commit is contained in:
1 parent
f074e10442
commit
3aab588b8b
8 files changed
+480
-20
No files matched your search
@@ -42,6 +42,8 @@ pub struct Services {
|
||||
pub pick_save: Option<PickSave>,
|
||||
/// The recent-files list's file (JSON); `None` keeps the list for this run only.
|
||||
pub recent_path: Option<PathBuf>,
|
||||
/// The user's preferences file (JSON, [`Prefs`]); `None` keeps them for this run only.
|
||||
pub prefs_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
const MODEL: Filter = Filter { name: "FieldCraft model", extensions: &["fieldcraft"] };
|
||||
@@ -164,6 +166,50 @@ impl Recent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest preferences file read.
|
||||
const MAX_PREFS_BYTES: u64 = 64 * 1024;
|
||||
|
||||
/// What the window remembers for the user between runs (`prefs.json` beside `recent.json`).
|
||||
/// Keys it no longer knows (`show_setup`, from before the setup card went) are ignored.
|
||||
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Prefs {
|
||||
/// The mouse navigation preset ([`crate::navigation::NAV_PRESETS`] id).
|
||||
pub nav_preset: String,
|
||||
/// The wheel zooms the other way.
|
||||
pub reverse_zoom: bool,
|
||||
}
|
||||
|
||||
impl Default for Prefs {
|
||||
fn default() -> Self {
|
||||
Prefs { nav_preset: crate::navigation::DEFAULT_PRESET.into(), reverse_zoom: false }
|
||||
}
|
||||
}
|
||||
|
||||
impl Prefs {
|
||||
/// The preferences kept in `file` (an unreadable or malformed file gives the defaults).
|
||||
pub fn load(file: Option<&Path>) -> Prefs {
|
||||
file.filter(|f| std::fs::metadata(f).is_ok_and(|m| m.is_file() && m.len() <= MAX_PREFS_BYTES))
|
||||
.and_then(|f| std::fs::read(f).ok())
|
||||
.and_then(|b| serde_json::from_slice(&b).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Write them to `file`; a failure is logged (the preferences stay for this run).
|
||||
pub fn save(&self, file: Option<&Path>) {
|
||||
let Some(file) = file else { return };
|
||||
let write = || -> std::io::Result<()> {
|
||||
if let Some(dir) = file.parent() {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
std::fs::write(file, serde_json::to_vec_pretty(self).unwrap_or_default())
|
||||
};
|
||||
if let Err(e) = write() {
|
||||
log::warn!("preferences {}: {e}", file.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A command running in the background on a copy of the session: a file opening, a solve, a
|
||||
/// mesher started from its form.
|
||||
pub struct Loading {
|
||||
@@ -1026,6 +1072,24 @@ mod tests {
|
||||
dir
|
||||
}
|
||||
|
||||
/// The navigation preset and the wheel's direction are remembered in the preferences file;
|
||||
/// an older file with keys no longer used still loads, a malformed one gives the defaults.
|
||||
#[test]
|
||||
fn navigation_preferences_are_remembered() {
|
||||
let file = temp("prefs").join("prefs.json");
|
||||
std::fs::write(&file, br#"{"show_setup": false}"#).unwrap();
|
||||
assert_eq!(Prefs::load(Some(&file)), Prefs::default());
|
||||
let services = || Services { prefs_path: Some(file.clone()), ..Default::default() };
|
||||
let mut app = ModelApp::new(Session::default()).with_services(services());
|
||||
app.ui.nav_preset = "solidworks".into();
|
||||
app.ui.reverse_zoom = true;
|
||||
app.save_prefs();
|
||||
let again = ModelApp::new(Session::default()).with_services(services());
|
||||
assert_eq!((again.ui.nav_preset.as_str(), again.ui.reverse_zoom), ("solidworks", true));
|
||||
std::fs::write(&file, b"{nope").unwrap();
|
||||
assert_eq!(Prefs::load(Some(&file)), Prefs::default());
|
||||
}
|
||||
|
||||
/// A picker that answers `path` and records the filters it was offered.
|
||||
fn answering(path: Option<String>, offered: Rc<RefCell<Vec<Vec<Filter>>>>) -> PickOpen {
|
||||
Box::new(move |f: &[Filter]| {
|
||||
|
||||
Reference in new issue
Block a user