From 82c0414f66f4998c3d61764a1c504e06e62e7ab0 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Tue, 27 Oct 2026 12:20:55 +0100 Subject: web: Find Peak dialog, live sliders, graduated fit, load-race fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - click away from a dot now opens a Find Peak list (candidates within 60 px, sorted by distance, name/height/pixel-offset labels) — the desktop dialog equivalent for crowded ridgelines - sliders update on input, not on release - fit is graduated like the original: 1 marker fits the center angle, 2 add scale, 4+ the full viewpoint (removed the >=4 gate) - image load now re-fits once more 120 ms after the first paint (same nudge as the desktop app) so the picture never stays hidden --- crates/gipfel-web/Cargo.toml | 1 + crates/gipfel-web/src/app.rs | 168 +++++++++++++++++++++++++++++++++------ crates/gipfel-web/src/bridge.rs | 7 ++ crates/gipfel-web/src/markers.rs | 6 +- crates/gipfel-web/src/widgets.rs | 9 ++- crates/gipfel-web/www/style.css | 38 +++++++++ 6 files changed, 199 insertions(+), 30 deletions(-) (limited to 'crates/gipfel-web') diff --git a/crates/gipfel-web/Cargo.toml b/crates/gipfel-web/Cargo.toml index 4cb78a7..303750a 100644 --- a/crates/gipfel-web/Cargo.toml +++ b/crates/gipfel-web/Cargo.toml @@ -31,6 +31,7 @@ features = [ "HtmlElement", "HtmlInputElement", "ImageBitmap", + "InputEvent", "MouseEvent", "PointerEvent", "Request", diff --git a/crates/gipfel-web/src/app.rs b/crates/gipfel-web/src/app.rs index 0874e9a..30704e4 100644 --- a/crates/gipfel-web/src/app.rs +++ b/crates/gipfel-web/src/app.rs @@ -50,6 +50,23 @@ impl Default for UiParams { } } +/// one entry of the Find Peak candidate list +#[derive(Clone, PartialEq)] +struct FpItem { + hill: usize, + label: String, +} + +/// open Find Peak popup: anchor (screen + image coords) and candidates +#[derive(Clone, PartialEq)] +struct FindPeak { + sx: f64, + sy: f64, + ix: f64, + iy: f64, + items: Vec, +} + fn gunzip(gz: &[u8]) -> Result, String> { let mut raw = Vec::new(); GzDecoder::new(gz) @@ -74,6 +91,7 @@ pub fn app() -> Html { let hover = use_state(|| Option::::None); let vp = use_state(UiParams::default); let img_size = use_state(|| (0u32, 0u32)); + let find_peak = use_state(|| Option::::None); // force a redraw when the window is resized so the canvas backing // store tracks its CSS size (listener lives as long as the app) @@ -143,17 +161,20 @@ pub fn app() -> Html { let fit_info = fit_info.clone(); let fit_distortion = fit_distortion.clone(); Rc::new(move |mk: &[Marker]| { + if mk.is_empty() { + fit_info.set(String::new()); + return; + } let info = { let mut p = pan.borrow_mut(); match markers::refit(&mut p, mk, *fit_distortion) { - Ok(()) if mk.len() >= 4 => format!( + Ok(()) => format!( "fit: az {:.2}° · pitch {:.2}° · roll {:.2}° · scale {:.1}", p.get_center_angle(), p.get_pitch_angle(), p.get_roll_angle(), p.get_scale() ), - Ok(()) => format!("{} markers — place at least 4 to fit", mk.len()), Err(e) => format!("fit failed: {e}"), } }; @@ -161,6 +182,19 @@ pub fn app() -> Html { }) }; + // place a marker for a hill at an image position and refit + let mark_peak = { + let markers = markers.clone(); + let do_refit = do_refit.clone(); + Callback::from(move |(idx, ix, iy): (usize, f64, f64)| { + let mut mk = (*markers).clone(); + mk.retain(|m| m.hill != idx); + mk.push(Marker { hill: idx, x: ix, y: iy }); + do_refit(&mk); + markers.set(mk); + }) + }; + // Decode selected/dropped bytes: display, read metadata, reset markers. let load_bytes = { let bitmap = bitmap.clone(); @@ -171,11 +205,13 @@ pub fn app() -> Html { let fit_info = fit_info.clone(); let vp = vp.clone(); let img_size = img_size.clone(); + let find_peak = find_peak.clone(); Callback::from(move |(bytes, name): (Vec, String)| { let (bitmap, status, fit, pan, markers, fit_info) = (bitmap.clone(), status.clone(), fit.clone(), pan.clone(), markers.clone(), fit_info.clone()); let vp = vp.clone(); let img_size = img_size.clone(); + let find_peak = find_peak.clone(); spawn_local(async move { match bridge::decode_bitmap(&bytes).await { Ok(bmp) => { @@ -208,8 +244,22 @@ pub fn app() -> Html { status.set(format!("{name} — {w}×{h} px · {vp}")); markers.set(Vec::new()); fit_info.set(String::new()); + find_peak.set(None); bitmap.set(Some(bmp)); fit.emit(MouseEvent::new("fit").unwrap()); + // same nudge as the desktop app: re-fit shortly + // after the first paint so the image is guaranteed + // to appear even if layout was not ready yet + { + let fit = fit.clone(); + spawn_local(async move { + let _ = wasm_bindgen_futures::JsFuture::from( + bridge::delay_promise(120), + ) + .await; + fit.emit(MouseEvent::new("fit").unwrap()); + }); + } } Err(e) => status.set(format!("Failed to load {name}: {e}")), } @@ -346,6 +396,48 @@ pub fn app() -> Html { }) }; + let fp_close = { + let find_peak = find_peak.clone(); + Callback::from(move |_: MouseEvent| find_peak.set(None)) + }; + + // Find Peak popup (built outside the main html! for macro simplicity) + let findpeak_popup = if let Some(fp) = &*find_peak { + let fp2 = fp.clone(); + let items: Html = fp2 + .items + .iter() + .map(|it| { + let onclick = { + let mark = mark_peak.clone(); + let find_peak = find_peak.clone(); + let (hill, ix, iy) = (it.hill, fp2.ix, fp2.iy); + Callback::from(move |_: MouseEvent| { + mark.emit((hill, ix, iy)); + find_peak.set(None); + }) + }; + html! { + + } + }) + .collect(); + html! { + <> +
+
+
{ "Find Peak" }
+ { items } +
+ + } + } else { + Html::default() + }; + let callbacks = ViewCallbacks { pan: { let view = view.clone(); @@ -371,30 +463,57 @@ pub fn app() -> Html { }, click: { let pan = pan.clone(); - let markers = markers.clone(); let view = view.clone(); let status = status.clone(); - let do_refit = do_refit.clone(); + let mark = mark_peak.clone(); + let find_peak = find_peak.clone(); + let img_size = img_size.clone(); Callback::from(move |(ix, iy): (f64, f64)| { - let p = pan.borrow_mut(); - match markers::nearest_hill(&p, ix, iy, 20.0 / view.scale) { - None => status.set( - "No peak near click — click on a green dot (zoom in to separate them)." - .to_string(), - ), - Some(idx) => { - let mut mk = (*markers).clone(); - mk.retain(|m| m.hill != idx); - mk.push(Marker { - hill: idx, - x: ix, - y: iy, - }); - drop(p); - do_refit(&mk); - markers.set(mk); - } + let scale = view.scale; + let p = pan.borrow(); + // unambiguous click right on a peak: mark directly + if let Some(idx) = markers::nearest_hill(&p, ix, iy, 10.0 / scale) { + drop(p); + find_peak.set(None); + mark.emit((idx, ix, iy)); + return; + } + // otherwise offer a Find Peak list, like the desktop dialog + let r = 60.0 / scale; + let mut cands: Vec<(f64, usize)> = p + .visible + .iter() + .map(|&i| ((p.hills[i].x - ix).hypot(p.hills[i].y - iy), i)) + .filter(|(d, _)| *d <= r) + .collect(); + if cands.is_empty() { + drop(p); + status.set("No peaks near click — zoom in or adjust the view.".to_string()); + find_peak.set(None); + return; } + cands.sort_by(|a, b| a.0.total_cmp(&b.0)); + cands.truncate(15); + let items: Vec = cands + .iter() + .map(|(d, i)| FpItem { + hill: *i, + label: format!( + "{} · {:.0} m · {:.0} px", + p.hills[*i].name, + p.hills[*i].height, + d * scale + ), + }) + .collect(); + let (w, h) = (img_size.0 as f64, img_size.1 as f64); + find_peak.set(Some(FindPeak { + sx: (ix + w / 2.0) * scale + view.ox, + sy: (iy + h / 2.0) * scale + view.oy, + ix, + iy, + items, + })); }) }, marker_drag: { @@ -525,10 +644,10 @@ pub fn app() -> Html { }

- { "Hover a green dot to see the peak · click to mark it · drag to adjust · right-click to remove." } + { "Hover a dot to see the peak · click to mark it (or pick from the list) · drag to adjust · right-click to remove." }

- { "The view is fitted automatically once 4+ markers are placed." } + { "The view is fitted as soon as markers are placed; more markers improve the fit." }

@@ -543,6 +662,7 @@ pub fn app() -> Html { candidates={candidates} label_font={vp.label_font} /> + { findpeak_popup.clone() }
if bitmap.is_some() { diff --git a/crates/gipfel-web/src/bridge.rs b/crates/gipfel-web/src/bridge.rs index a60b1f6..6dbba93 100644 --- a/crates/gipfel-web/src/bridge.rs +++ b/crates/gipfel-web/src/bridge.rs @@ -53,6 +53,13 @@ pub async fn fetch_bytes(url: &str) -> Result, String> { Ok(Uint8Array::new(&buf).to_vec()) } +/// setTimeout as an awaitable Promise. +pub fn delay_promise(ms: i32) -> js_sys::Promise { + js_sys::Promise::new(&mut |resolve, _reject| { + let _ = window().set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, ms); + }) +} + pub fn ctx2d(canvas: &HtmlCanvasElement) -> Option { canvas .get_context("2d") diff --git a/crates/gipfel-web/src/markers.rs b/crates/gipfel-web/src/markers.rs index 279c62c..d1bf8cb 100644 --- a/crates/gipfel-web/src/markers.rs +++ b/crates/gipfel-web/src/markers.rs @@ -15,9 +15,11 @@ pub struct Marker { } /// Refit the projection parameters to the current markers. -/// With fewer than 4 markers nothing can be fitted (same rule as desktop). +/// Same graduated behavior as the original: 1 marker fits the center +/// angle, 2 add the scale, 4+ the full viewpoint (+ distortion when +/// unlocked and enough markers). pub fn refit(pan: &mut Panorama, markers: &[Marker], fit_distortion: bool) -> Result<(), String> { - if markers.len() < 4 { + if markers.is_empty() { return Ok(()); } let mut obs: Vec = Vec::with_capacity(markers.len()); diff --git a/crates/gipfel-web/src/widgets.rs b/crates/gipfel-web/src/widgets.rs index f93f424..1d22bac 100644 --- a/crates/gipfel-web/src/widgets.rs +++ b/crates/gipfel-web/src/widgets.rs @@ -1,7 +1,7 @@ //! Small form controls: full-width slider with value readout above //! (mirrors the desktop `slider_item` layout) and a numeric input row. -use web_sys::HtmlInputElement; +use web_sys::{HtmlInputElement, InputEvent}; use yew::prelude::*; #[derive(Properties, PartialEq)] @@ -18,9 +18,10 @@ pub struct SliderProps { #[function_component(SliderItem)] pub fn slider_item(p: &SliderProps) -> Html { - let onchange = { + // live update while dragging (oninput), not only on release + let oninput = { let onchange = p.onchange.clone(); - Callback::from(move |e: Event| { + Callback::from(move |e: InputEvent| { let input: HtmlInputElement = e.target_unchecked_into(); if let Ok(v) = input.value().parse::() { onchange.emit(v); @@ -39,7 +40,7 @@ pub fn slider_item(p: &SliderProps) -> Html { max={p.max.to_string()} step={p.step.to_string()} value={p.value.to_string()} - {onchange} + {oninput} /> } diff --git a/crates/gipfel-web/www/style.css b/crates/gipfel-web/www/style.css index 523ee2b..9e7e8a0 100644 --- a/crates/gipfel-web/www/style.css +++ b/crates/gipfel-web/www/style.css @@ -144,3 +144,41 @@ canvas.compass { border: 1px solid #34343e; border-radius: 4px; } +.fpbackdrop { + position: absolute; + inset: 0; + z-index: 9; +} +.findpeak { + position: absolute; + z-index: 10; + background: #1e1e26; + border: 1px solid #3a3a46; + border-radius: 6px; + box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5); + max-height: 320px; + overflow-y: auto; + min-width: 220px; +} +.fphead { + padding: 6px 10px; + font-size: 11px; + text-transform: uppercase; + letter-spacing: 1px; + color: #8a8a94; + border-bottom: 1px solid #2c2c34; +} +.fpitem { + display: block; + width: 100%; + text-align: left; + padding: 6px 10px; + background: none; + border: 0; + color: #e8e8ea; + font-size: 13px; + cursor: pointer; +} +.fpitem:hover { + background: #2f6fd4; +} -- cgit v1.2.3