Files
gpx.studio/gpx-rs/src/engine/command/tools/move_waypoint.rs
T
vcoppeandvcoppe 8fe7d8d64d Migration to new Rust-based engine (#401)
* wasm first steps

* parsing start

* parsing continued

* progress

* progress

* progress

* progress

* progress

* progress

* progress

* progress

* fixes

* reorganize

* change type

* fix

* progress

* refactoring

* progress

* progress

* progress

* keep style info

* progress

* progress

* remove (copy) in name

* progress

* merge methods into 1

* splice

* progress

* progress

* progress

* cleaning

* progress

* progress

* progress

* visibility

* progress

* progress

* progress

* progress

* progress

* fixes

* progress

* fix markers

* fix

* progress

* progress

* progress

* progress

* progress

* progress

* small fix

* use engine info

* fix popups

* small fix

* progress

* progress

* progress

* clean tool

* handle self closing elements

* reduce data exchanges across boundary

* copy on wasm side to avoid any data corruption

* speed fix

* routing

* refactoring

* reverse

* reverse

* crop

* split

* merge

* progress

* time tool

* fix bounds for waypoints

* reduce

* fix reactivity

* handle corrupt time data

* export

* progress

* remove gpx js package

* remove todo

* update readme

* cleaning

* progress

* fix link fields

* remove unused fields

* refactoring and tests

* import and export of routes, ref #192

* per-chunk categories storage

* cross-tab sync

* outdated state

* refactoring

* reorder methods

* refactoring

* fix

* improve flow

* renaming

* progress

* progress

* fix

* progress

* per file history

* keep original flag

* use history variant

* fix

* progress

* file explorer

* progress

* fix

* sticky folders

* improved sticky folders

* fix sortable

* fix sortable

* fix

* fix tab size

* no file delete on desktop

* fix routing on other file

* fix sorting

* remember language

* open links in browser

* commit missing changes

* avoid selecting on resize

* clarify filename

* explorer actions

* fix folder styling

* drop anywhere in folder

* cleaning

* fixes

* remove focus-ring

* full screen

* small fix

* consistent highlight color

* improved preview

* performance improvements

* sparser data transfers

* sparser data transfers

* show popup once data is loaded

* fix routing control size

* fix

* sparse desktop build

* remove custom ign styles

* use new endpoint for openhikingmap

* strip native menu

* reorder context menu items

* route full support

* route - track conversion

* create route file action

* route export, filter out anchors at high zoom level

* clarify name handling

* move hide & center to view

* routes continued

* update docs

* fix power format, closes #378

* lint fixes

* fix display of additional data with gaps, closes #361

* fix linting issues

* fix import modules

* fix multidrag

* change base to resolve

* fix type errors

* fix layer typings

* fix last type error

* each keys

* improve typings

* deploy to cloudflare

---------

Co-authored-by: vcoppe <vcoppe@hexaly.com>
2026-10-31 18:08:22 +01:00

108 lines
3.2 KiB
Rust

use crate::{Apply, CommandError, FileId, LngLat, State, WaypointId, update_waypoint};
/// Moves a waypoint of a file, whatever is selected (what a drag does): only its position and
/// elevation change.
#[derive(Debug)]
pub struct MoveWaypoint {
pub file_id: FileId,
pub waypoint_id: WaypointId,
pub lng: f64,
pub lat: f64,
pub ele: f64,
}
impl Apply for MoveWaypoint {
fn apply(self, state: &mut State) -> Result<(), CommandError> {
update_waypoint(state, self.file_id, self.waypoint_id, |wpt| {
wpt.coordinates = LngLat {
lng: self.lng,
lat: self.lat,
};
wpt.ele = self.ele;
})
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use crate::{
File, Selection, Waypoint, WaypointChunk, engine::command::fixture::Fixture, waypoint_ids,
};
use super::*;
fn fixture() -> (Fixture, crate::FileId, Vec<crate::WaypointId>) {
let mut fx = Fixture::default();
let mut file = File::default();
file.wpt.push(WaypointChunk {
wpt: (0..3)
.map(|i| Waypoint {
name: Some(format!("w{i}")),
..Default::default()
})
.collect(),
..Default::default()
});
let id = file.id;
let ids = waypoint_ids(&file).collect();
fx.files.insert(id, Rc::new(file));
fx.order.0.push(id);
(fx, id, ids)
}
#[test]
fn test_only_the_given_waypoint_moves_whatever_is_selected() {
let (mut fx, id, ids) = fixture();
let rev = fx.files[&id].wpt.rev_id;
MoveWaypoint {
file_id: id,
waypoint_id: ids[1],
lng: 4.0,
lat: 50.0,
ele: 42.0,
}
.apply(&mut fx.state())
.unwrap();
let wpts: Vec<_> = fx.files[&id].wpt.iter().collect();
assert_eq!(wpts.len(), 3);
assert_eq!(
(
wpts[1].coordinates.lng,
wpts[1].coordinates.lat,
wpts[1].ele
),
(4.0, 50.0, 42.0)
);
// the other fields and the other waypoints are as they were
assert_eq!(wpts[1].name.as_deref(), Some("w1"));
assert_eq!(wpts[1].id, ids[1]);
assert_eq!((wpts[0].coordinates.lng, wpts[0].ele), (0.0, 0.0));
assert_eq!((wpts[2].coordinates.lat, wpts[2].ele), (0.0, 0.0));
// the coordinates changed, which is noticed by what is derived from them
assert_ne!(fx.files[&id].wpt.rev_id, rev);
assert_eq!(fx.selection, Selection::Empty);
}
#[test]
fn test_unknown_waypoint_or_file_is_nothing_to_do() {
let (mut fx, id, ids) = fixture();
let before = fx.files[&id].clone();
for (file_id, waypoint_id) in [(id, WaypointId::default()), (FileId::default(), ids[0])] {
let result = MoveWaypoint {
file_id,
waypoint_id,
lng: 1.0,
lat: 1.0,
ele: 1.0,
}
.apply(&mut fx.state());
assert!(matches!(result, Err(CommandError::NothingToDo)));
}
assert!(Rc::ptr_eq(&fx.files[&id], &before));
}
}