diff --git a/apps/rustgis-mobile/README.md b/apps/rustgis-mobile/README.md index 0c4629de35..3e762e0013 100644 --- a/apps/rustgis-mobile/README.md +++ b/apps/rustgis-mobile/README.md @@ -95,11 +95,18 @@ Rust calls Java and never the other way: each frame the app collects what happen ## The touch interface A phone starts in the touch interface: the map, a bar of four large buttons under it (Layers, Add -Data, Capture, More) and sheets that rise over the map. The Layers sheet shows and hides layers, -zooms to one and makes one the active layer; the More sheet has Full Extent, the project, undo -and redo, and **Use the Desktop Interface**, which brings back the ribbon and panes; **Touch +Data, Capture, More) and sheets that rise over the map. The Layers sheet switches between the +project's maps and layouts when it has more than one, shows and hides layers, zooms to one and +makes one the active layer; the More sheet has Full Extent, undo and redo, the project, the +position, and **Use the Desktop Interface**, which brings back the ribbon and panes; **Touch Interface**, on the View tab (or Use the Touch Interface in Command Search), returns to the phone's -interface. +interface. The map has no zoom buttons or view tabs there: a finger pinches to zoom. + +A sheet is lowered by its **×**, a swipe down on its heading, a tap on the map above it, or +Android's Back. Android 16 on sends an app targeting it no Back key, so the activity's Java half +registers a callback while a sheet is up (`takesBack`), and asks the window for a frame when Back +comes; older Androids send the key, which the app reads. With no sheet up, Back does what it did +before. **Capture** adds a feature to one of the map's point, line or polygon layers. A cross marks the middle of the map: the map is moved under it, and the sheet's fields are filled in (one for each diff --git a/apps/rustgis-mobile/java/org/rustgis/mobile/MainActivity.java b/apps/rustgis-mobile/java/org/rustgis/mobile/MainActivity.java index 276caf1b63..747d180c7d 100644 --- a/apps/rustgis-mobile/java/org/rustgis/mobile/MainActivity.java +++ b/apps/rustgis-mobile/java/org/rustgis/mobile/MainActivity.java @@ -85,6 +85,12 @@ public class MainActivity extends NativeActivity { /** Why TrackService couldn't run, until takeEvents says so. */ private static final java.util.concurrent.atomic.AtomicReference refusal = new java.util.concurrent.atomic.AtomicReference<>(); + /** The callback Back comes to while the Rust side takes it (the main thread's alone). */ + private Object backCallback; + /** Back came to backCallback and takeBack hasn't said so yet. */ + private final java.util.concurrent.atomic.AtomicBoolean back = new java.util.concurrent.atomic.AtomicBoolean(); + /** The window's surface while it has one (the main thread's alone): a frame is asked of it. */ + private android.view.SurfaceHolder surface; /** The activity, while it is created: TrackService's refusal reaches it at once. */ private static volatile MainActivity current; /** The listener positions arrive on, while they are asked for. */ @@ -163,6 +169,54 @@ public void barsLight(final boolean light) { }); } + /** + * Takes Back from the system while the Rust side has a sheet it lowers (on), or gives it + * back, so Back does what the system does with it. Needed from Android 16, which sends an + * app targeting it no Back key; older ones send the key, and the Rust side reads that. + */ + public void takesBack(final boolean on) { + if (android.os.Build.VERSION.SDK_INT < 36) { + return; + } + runOnUiThread(() -> { + if (on && backCallback == null) { + android.window.OnBackInvokedCallback callback = () -> { + back.set(true); + // The Rust side draws only when something happens: ask it for a frame. + if (surface != null) { + surfaceRedrawNeeded(surface); + } + }; + getOnBackInvokedDispatcher().registerOnBackInvokedCallback( + android.window.OnBackInvokedDispatcher.PRIORITY_DEFAULT, callback); + backCallback = callback; + } else if (!on && backCallback != null) { + getOnBackInvokedDispatcher() + .unregisterOnBackInvokedCallback((android.window.OnBackInvokedCallback) backCallback); + backCallback = null; + // One not read yet lowered nothing: it isn't kept for the next sheet. + back.set(false); + } + }); + } + + /** Whether Back came since this was last asked. */ + public boolean takeBack() { + return back.getAndSet(false); + } + + @Override + public void surfaceCreated(android.view.SurfaceHolder holder) { + super.surfaceCreated(holder); + surface = holder; + } + + @Override + public void surfaceDestroyed(android.view.SurfaceHolder holder) { + surface = null; + super.surfaceDestroyed(holder); + } + /** * Starts or stops reporting the device's position: each fix arrives as a "position" event * (longitude, latitude, accuracy in metres). Starting asks for the location permission where diff --git a/apps/rustgis-mobile/src/android.rs b/apps/rustgis-mobile/src/android.rs index 63020207e5..c610ef648a 100644 --- a/apps/rustgis-mobile/src/android.rs +++ b/apps/rustgis-mobile/src/android.rs @@ -35,6 +35,14 @@ impl shell::Phone for Phone { fn copy_text(&self, text: &str) -> Result<(), String> { Phone::copy_text(self, text) } + + fn takes_back(&self, on: bool) { + Phone::takes_back(self, on); + } + + fn take_back(&self) -> bool { + Phone::take_back(self) + } } /// Points the app's folders (`rustgis_data::dirs`) at the app's own storage: Android has no diff --git a/apps/rustgis-mobile/src/phone.rs b/apps/rustgis-mobile/src/phone.rs index a6f89b2e26..35a6a689ee 100644 --- a/apps/rustgis-mobile/src/phone.rs +++ b/apps/rustgis-mobile/src/phone.rs @@ -101,6 +101,25 @@ impl Phone { } } + /// Takes Android's Back from the system (`on`) or gives it up, where the system keeps it from + /// an app that doesn't ask (Android 16 on). + pub fn takes_back(&self, on: bool) { + let asked = self.java(|env, activity| { + env.call_method(activity, jni_str!("takesBack"), jni_sig!((on: boolean) -> void), &[JValue::Bool(on)])?.v() + }); + if let Err(e) = asked { + log::warn!("{e}"); + } + } + + /// Whether Back, taken from the system, came since this was last asked. A refusal is logged, + /// as `takes_back`'s is, and counts as no Back. + pub fn take_back(&self) -> bool { + self.java(|env, activity| env.call_method(activity, jni_str!("takeBack"), jni_sig!(() -> boolean), &[])?.z()) + .inspect_err(|e| log::warn!("{e}")) + .unwrap_or(false) + } + /// Whether a picker for opening is on the screen and hasn't answered. pub fn picking(&self) -> bool { self.java(|env, activity| env.call_method(activity, jni_str!("picking"), jni_sig!(() -> boolean), &[])?.z()).unwrap_or(false) diff --git a/apps/rustgis-mobile/src/shell.rs b/apps/rustgis-mobile/src/shell.rs index dfa703ad1d..03474cc07d 100644 --- a/apps/rustgis-mobile/src/shell.rs +++ b/apps/rustgis-mobile/src/shell.rs @@ -65,6 +65,17 @@ pub trait Phone: Platform { let _ = text; Ok(()) } + + /// Asks the system for its Back (`on`), or leaves it to the system, where it keeps Back from + /// an app that doesn't ask (Android 16 on). Where Back comes as a key, or there is none (iOS), + /// this does nothing. + fn takes_back(&self, _on: bool) {} + + /// Whether the system's Back asked for came since the last frame, where it comes as no key egui + /// knows (Android 16 on). The shell gives egui the Back key for it. + fn take_back(&self) -> bool { + false + } } /// What a service couldn't do, kept until the next frame says it in the window (a service has @@ -122,6 +133,9 @@ struct App { keeping: bool, /// Whether the soft keyboard was up, where the phone says. keyboard: Option, + /// Whether the interface has something Back lowers (`rustgis_ui_touch::takes_back`), as + /// the phone was last told. + back: bool, /// Until when the window looks again for the soft keyboard's room: a moment after the /// last frame a field wanted the keyboard. typing: Option, @@ -670,6 +684,13 @@ impl eframe::App for App

{ }); copy_out(&mut self.app, phone.as_ref(), copied); } + let back = rustgis_ui_touch::takes_back(&self.app, &ctx); + if back != self.back { + self.back = back; + if let Some(phone) = &self.phone { + phone.takes_back(back); + } + } // A second picker asked for while one was up was refused: what the front end kept for // the first is put back, so its file is read as it was asked for. if self.refused_picker.replace(false) @@ -718,15 +739,11 @@ impl eframe::App for App

{ // The soft keyboard's Return, as the Enter key egui knows: each field does with it what // it does with Enter. if self.phone.as_ref().is_some_and(|phone| phone.take_return()) { - for pressed in [true, false] { - raw_input.events.push(egui::Event::Key { - key: egui::Key::Enter, - physical_key: None, - pressed, - repeat: false, - modifiers: egui::Modifiers::NONE, - }); - } + press(raw_input, egui::Key::Enter); + } + // The system's Back, as the key older Androids send for it. + if self.back && self.phone.as_ref().is_some_and(|phone| phone.take_back()) { + press(raw_input, egui::Key::BrowserBack); } if !self.phone.as_ref().is_some_and(|phone| phone.picking()) { self.kept.borrow_mut().take(); @@ -743,6 +760,13 @@ impl eframe::App for App

{ } } +/// Gives egui a press and release of `key`. +fn press(raw_input: &mut egui::RawInput, key: egui::Key) { + for pressed in [true, false] { + raw_input.events.push(egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }); + } +} + /// A pushed or picked file, handed to the front end as a file dropped on the window: by its path. #[derive(Debug)] struct Pushed(PathBuf); @@ -884,6 +908,7 @@ pub fn run( keeping: false, typing: None, keyboard: None, + back: false, bars_light: None, pushed, phone, @@ -991,6 +1016,7 @@ mod tests { keeping: false, keyboard: None, typing: None, + back: false, bars_light: None, phone: Some(phone), inbox: Inbox::default(), diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index ab4f70631f..56dd706e6e 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -637,6 +637,47 @@ pub fn status_bar(app: &mut RustGisApp, ui: &mut Ui) { }); } +/// How the strip of work running off the frame is drawn: in the desktop's status bar, or at a +/// finger's size for a shell with its own interface ([`RustGisApp::chrome`]). +#[derive(Clone, Copy)] +pub(crate) struct StripSize { + /// The size of its words. + pub(crate) text: f32, + finger: bool, +} + +impl StripSize { + pub(crate) fn of(app: &RustGisApp) -> StripSize { + let finger = app.chrome.is_some(); + StripSize { text: if finger { 14.0 } else { 11.5 }, finger } + } + + /// A job's progress bar, `fraction` of the way. + pub(crate) fn progress(self, ui: &mut Ui, fraction: f32) { + let height = if self.finger { 20.0 } else { 12.0 }; + ui.add(egui::ProgressBar::new(fraction).show_percentage().desired_width(120.0).desired_height(height)); + } + + /// A job's Cancel where a finger's strip has it, before the job's words, so it is in sight + /// however long they are. Says whether it was tapped; nothing in the status bar. + pub(crate) fn cancel_first(self, ui: &mut Ui, enabled: bool) -> bool { + let size = vec2(FINGER_CANCEL, FINGER); + self.finger && ui.add_enabled(enabled, egui::Button::new(egui::RichText::new(tl!("Cancel")).size(16.0)).min_size(size)).clicked() + } + + /// A job's Cancel where the status bar has it, after the job's words. Says whether it was + /// clicked; nothing in a finger's strip. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) fn cancel_last(self, ui: &mut Ui, enabled: bool) -> bool { + !self.finger && ui.add_enabled(enabled, egui::Button::new(tl!("Cancel")).small()).clicked() + } +} + +/// The height of what a finger taps, in points (Android's guidance is 48). +pub(crate) const FINGER: f32 = 48.0; +/// The width of a finger's strip's Cancel. +const FINGER_CANCEL: f32 = 88.0; + /// Restored local FlatGeobuf layers classify dates off the frame, including hidden maps. /// This status keeps progress and Cancel available until those scans finish. #[cfg(not(target_arch = "wasm32"))] @@ -653,15 +694,19 @@ fn source_open_status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { if sources.is_empty() { return; } + let size = StripSize::of(app); + let cancellable = sources.iter().any(|(_, (_, _, cancelled))| !cancelled); sep(ui, t); ui.spinner(); - ui.label(egui::RichText::new(tl!("Reading dates…")).size(11.5).color(t.text_dim)); + let mut cancel = size.cancel_first(ui, cancellable); + ui.label(egui::RichText::new(tl!("Reading dates…")).size(size.text).color(t.text_dim)); let (done, total) = sources.iter().fold((0u64, 0u64), |(done, total), (_, (d, n, _))| (done.saturating_add(*d), total.saturating_add(*n))); if total > 0 && sources.iter().all(|(_, (_, n, _))| *n > 0) { - ui.add(egui::ProgressBar::new((done as f32 / total as f32).min(1.0)).show_percentage().desired_width(120.0).desired_height(12.0)); + size.progress(ui, (done as f32 / total as f32).min(1.0)); } - if ui.add_enabled(sources.iter().any(|(_, (_, _, cancelled))| !cancelled), egui::Button::new(tl!("Cancel")).small()).clicked() { + cancel |= size.cancel_last(ui, cancellable); + if cancel { for (source, _) in sources { source.cancel_opening(); } @@ -718,16 +763,23 @@ fn window_reads_under_way(app: &RustGisApp) -> bool { /// (`RustGisApp::cancel_download`): one in the bar itself, several as a menu of them. fn downloads_status(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let under_way = app.downloads_under_way(); + let size = StripSize::of(app); let mut cancel = None; ui.spinner(); match under_way.as_slice() { [] => {} - [one] => download_row(ui, t, one, &mut cancel), + [one] => download_row(ui, t, size, one, &mut cancel), many => { - ui.menu_button(crate::i18n::fmt("{n} downloads", &[("n", &many.len().to_string())]), |ui| { - for d in many { - ui.horizontal(|ui| download_row(ui, t, d, &mut cancel)); + ui.scope(|ui| { + // A finger opens the menu too; what the strip draws after it keeps its own size. + if size.finger { + ui.spacing_mut().interact_size.y = FINGER; } + ui.menu_button(crate::i18n::fmt("{n} downloads", &[("n", &many.len().to_string())]), |ui| { + for d in many { + ui.horizontal(|ui| download_row(ui, t, size, d, &mut cancel)); + } + }); }); } } @@ -742,7 +794,11 @@ fn downloads_status(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { /// progress and its Cancel. A file's whole download shows a bar once the server said its size and /// the megabytes read before; a window's read ("Reading…", as its start notice says) isn't /// counted, nor is a download in the browser. -fn download_row(ui: &mut Ui, t: &Tokens, d: &crate::UnderWay, cancel: &mut Option) { +fn download_row(ui: &mut Ui, t: &Tokens, size: StripSize, d: &crate::UnderWay, cancel: &mut Option) { + let hint = tl!("Stop this download; nothing is added"); + if size.cancel_first(ui, true) { + *cancel = Some(d.id); + } let received = d.progress.as_ref().map_or(0, |p| p.received()); let fraction = d.progress.as_ref().and_then(|p| p.fraction()); let doing = match (&d.progress, fraction) { @@ -752,14 +808,14 @@ fn download_row(ui: &mut Ui, t: &Tokens, d: &crate::UnderWay, cancel: &mut Optio } (Some(_), _) => tl!("Downloading…").to_string(), }; - ui.label(egui::RichText::new(doing).size(11.5).color(t.text_dim)); + ui.label(egui::RichText::new(doing).size(size.text).color(t.text_dim)); let short: String = if d.shown.chars().count() > 48 { format!("{}…", d.shown.chars().take(47).collect::()) } else { d.shown.clone() }; - ui.label(egui::RichText::new(short).size(11.5)).on_hover_text(&d.shown); + ui.label(egui::RichText::new(short).size(size.text)).on_hover_text(&d.shown); if let Some(f) = fraction { - ui.add(egui::ProgressBar::new(f).show_percentage().desired_width(120.0).desired_height(12.0)); + size.progress(ui, f); } - if ui.small_button(tl!("Cancel")).on_hover_text(tl!("Stop this download; nothing is added")).clicked() { + if !size.finger && ui.small_button(tl!("Cancel")).on_hover_text(hint).clicked() { *cancel = Some(d.id); } } @@ -1751,11 +1807,24 @@ pub fn notices(app: &mut RustGisApp, ctx: &egui::Context) { } ctx.request_repaint_after(std::time::Duration::from_millis(250)); let screen = ctx.content_rect(); - let mut y = screen.max.y - 40.0; - for toast in app.map.toasts.iter_mut().rev().take(3) { + // A shell with its own interface has its bars below the map, the work strip's Cancel among + // them: the toasts stand at the map's foot, clear of them, each on the one before by its own + // height, as many of the last three as the map above has room for (the newest always, at the + // top when a tall sheet leaves the map none). + let over_map = app.chrome.and(app.map.view_rect).map(|(map, _)| map.max.y); + let mut y = over_map.unwrap_or(screen.max.y).max(screen.min.y + 84.0) - 40.0; + for (i, toast) in app.map.toasts.iter_mut().rev().take(3).enumerate() { // Keyed by the notice, not the slot, so a selection stays with its text as toasts come and go. - let shown = egui::Area::new(egui::Id::unique(("toast", toast.id))) - .fixed_pos(pos2(screen.max.x - 380.0, y - 44.0)) + let id = egui::Id::unique(("toast", toast.id)); + // Its height when last drawn (a one-line toast's before it is measured): an older one that + // wouldn't fit below the top waits for room, as the window would push it down over the rest. + let height = egui::AreaState::load(ctx, id).and_then(|a| a.size).map_or(44.0, |s| s.y); + if i > 0 && y - height < screen.min.y { + break; + } + let shown = egui::Area::new(id) + .pivot(egui::Align2::LEFT_BOTTOM) + .fixed_pos(pos2(screen.max.x - 380.0, y)) .order(egui::Order::Tooltip) .show(ctx, |ui| { egui::Frame::NONE @@ -1776,7 +1845,7 @@ pub fn notices(app: &mut RustGisApp, ctx: &egui::Context) { if shown.response.contains_pointer() { toast.at = now; } - y -= 52.0; + y = shown.response.rect.min.y - 8.0; } } diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index 3059a98b45..b2af2dce4d 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -126,15 +126,19 @@ pub fn stop(app: &mut RustGisApp, wait: std::time::Duration) { /// The export under way in the status bar: its name, progress and Cancel. pub fn status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { let Some(job) = &app.export_job else { return }; + let size = crate::chrome::StripSize::of(app); + let cancellable = !job.control.is_cancelled(); ui.spinner(); + let mut cancel = size.cancel_first(ui, cancellable); // Two labels, not one sentence put together from translated pieces. - ui.label(egui::RichText::new(tl!("Running…")).size(11.5).color(t.text_dim)); - ui.label(egui::RichText::new(tl!(job.name)).size(11.5).strong()); + ui.label(egui::RichText::new(tl!("Running…")).size(size.text).color(t.text_dim)); + ui.label(egui::RichText::new(tl!(job.name)).size(size.text).strong()); // How far its current step has come, when its writer counts. if let Some((done, total)) = job.control.progress() { - ui.add(egui::ProgressBar::new(done as f32 / total.max(1) as f32).show_percentage().desired_width(120.0).desired_height(12.0)); + size.progress(ui, done as f32 / total.max(1) as f32); } - if ui.add_enabled(!job.control.is_cancelled(), egui::Button::new(tl!("Cancel")).small()).clicked() { + cancel |= size.cancel_last(ui, cancellable); + if cancel { job.control.cancel(); } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 4952735ef8..6f2e680bd0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3196,8 +3196,9 @@ impl RustGisApp { readjob::stop(self, std::time::Duration::from_secs(5)); } - /// Draws the map into `ui`, with its own controls (zoom, compass, scale bar): for a shell - /// that lays out its own interface ([`RustGisApp::chrome`]). + /// Draws the map into `ui`, with its compass and scale bar: for a shell that lays out its own + /// interface ([`RustGisApp::chrome`]). With one in place, the map has no zoom buttons and no + /// view tabs: the shell offers what it needs of them. pub fn map_view(&mut self, ui: &mut egui::Ui) { mapview::show(self, ui); } @@ -3217,7 +3218,8 @@ impl RustGisApp { chrome::background_busy(self) } - /// Draws that work's progress and Cancel into `ui`, as the desktop's status bar does. + /// Draws that work's progress and Cancel into `ui`, as the desktop's status bar does; with a + /// shell's interface in place ([`RustGisApp::chrome`]), at a finger's size. pub fn background_status(&mut self, ui: &mut egui::Ui) { chrome::background_status(self, ui); } diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index d81f55ed21..7b25517409 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -846,7 +846,10 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui) { app.map.follow_project(app.session.opened); let t = Tokens::get(ui.ctx()); crate::modelbuilder::follow_view(app); - view_tabs(app, ui, &t); + // A shell with its own interface (the touch interface) switches views itself. + if app.chrome.is_none() { + view_tabs(app, ui, &t); + } crate::vertexedit::cancel_stale_drag(app); crate::vertexedit::say_unplaced(app); if app.mb.shown() { @@ -4083,7 +4086,15 @@ fn overlays(app: &mut RustGisApp, ui: &mut Ui, p: &egui::Painter, rect: Rect, t: p.rect_filled(r, CornerRadius::same(3), t.ribbon.gamma_multiply(0.75)); p.galley(r.min + vec2(5.0, 2.0), g, t.text_dim); } - // Navigation buttons (zoom in/out/full) in the top-right corner. + // Navigation buttons (zoom in/out/full) in the top-right corner. A shell with its own + // interface has none: a finger pinches to zoom, and the shell offers Full Extent. + if app.chrome.is_none() { + navigation(app, ui, p, rect, t); + } +} + +/// The map's Zoom In, Zoom Out and Full Extent buttons, in the top-right corner of `rect`. +fn navigation(app: &mut RustGisApp, ui: &mut Ui, p: &egui::Painter, rect: Rect, t: &Tokens) { let mut y = rect.min.y + 12.0; for (icon, id, tip) in [("zoom_in", "map.zoomIn", "Zoom in"), ("zoom_out", "map.zoomOut", "Zoom out"), ("zoom_full", "map.zoomFull", "Full extent")] diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index a39d5a0b53..cf2bd24b62 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -624,7 +624,9 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui) { let t = Tokens::get(ui.ctx()); let full = ui.available_rect_before_wrap(); ui.painter().rect_filled(full, 0.0, t.pane); - let bar = Rect::from_min_size(full.min, vec2(full.width(), 40.0)); + // A shell with its own interface puts a finger-sized Back to the map in the toolbar. + let bar_h = if app.chrome.is_some() { crate::chrome::FINGER + 12.0 } else { 40.0 }; + let bar = Rect::from_min_size(full.min, vec2(full.width(), bar_h)); let body = Rect::from_min_max(pos2(full.min.x, bar.max.y), full.max); let narrow = body.width() < 760.0; let palette_w = if narrow { 0.0 } else { PALETTE_W }; @@ -661,6 +663,17 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui) { fn toolbar(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, problems: &[model::Problem]) { ui.horizontal_centered(|ui| { + // A shell with its own interface has no view tabs, and on a phone the buttons at the far + // end are past the window's edge: the way back to the map comes first. + let back = egui::Button::new(tl!("Back to the map")).min_size(vec2(0.0, crate::chrome::FINGER)); + if app.chrome.is_some() && ui.add(back).clicked() { + app.mb.active = false; + // Opened over a layout, the layout would show in its place. A close that fails is + // said by `run`, as any command's failure is. + if app.session.active_layout.is_some() { + let _ = app.run("layout.close", serde_json::Value::Null); + } + } let (r, _) = ui.allocate_exact_size(vec2(22.0, 22.0), Sense::hover()); icons::paint(ui.painter(), r, "modelbuilder", t.icon, t.accent); ui.add(egui::TextEdit::singleline(&mut app.mb.name).hint_text(tl!("Untitled model")).desired_width(170.0).font(semibold(13.0))); diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index a27598bb93..e522901dee 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -774,19 +774,23 @@ pub fn showing(app: &RustGisApp) -> bool { /// The read under way in the status bar: its name, progress, how many wait, and Cancel. pub fn status(app: &RustGisApp, ui: &mut Ui, t: &Tokens) { let Some(job) = &app.reads.job else { return }; + let size = crate::chrome::StripSize::of(app); + let cancellable = !job.control.is_cancelled(); ui.spinner(); + let mut cancel = size.cancel_first(ui, cancellable); // Two labels, not one sentence put together from translated pieces. - ui.label(egui::RichText::new(tl!("Running…")).size(11.5).color(t.text_dim)); - ui.label(egui::RichText::new(tl!(job.name)).size(11.5).strong()); + ui.label(egui::RichText::new(tl!("Running…")).size(size.text).color(t.text_dim)); + ui.label(egui::RichText::new(tl!(job.name)).size(size.text).strong()); // How far its current step has come, when its reader counts. if let Some((done, total)) = job.control.progress() { - ui.add(egui::ProgressBar::new(done as f32 / total.max(1) as f32).show_percentage().desired_width(120.0).desired_height(12.0)); + size.progress(ui, done as f32 / total.max(1) as f32); } let n = app.reads.waiting.len(); if n > 0 { - ui.label(egui::RichText::new(crate::i18n::fmt("{n} waiting", &[("n", &n.to_string())])).size(11.5).color(t.text_dim)); + ui.label(egui::RichText::new(crate::i18n::fmt("{n} waiting", &[("n", &n.to_string())])).size(size.text).color(t.text_dim)); } - if ui.add_enabled(!job.control.is_cancelled(), egui::Button::new(tl!("Cancel")).small()).clicked() { + cancel |= size.cancel_last(ui, cancellable); + if cancel { job.control.cancel(); } } diff --git a/crates/ui-touch/src/lib.rs b/crates/ui-touch/src/lib.rs index 6ca4d33ffa..75ec4a16d8 100644 --- a/crates/ui-touch/src/lib.rs +++ b/crates/ui-touch/src/lib.rs @@ -6,12 +6,14 @@ //! the desktop interface's View › Touch Interface can bring it back. Dialogs, notices and the //! Project page stay the front end's. //! -//! What is here: the map, the bar (Layers, Add Data, Capture, More), the Layers sheet (show, -//! hide, zoom to, the active layer), the Capture sheet (a point, line or polygon placed by the cross -//! on the map, with a form for its layer's attributes that follows the layer's data design: value -//! lists, ranges and feature templates) and the More sheet (full extent, project, undo, the -//! desktop interface, the device's position). Where a shell reports the device's position -//! (`RustGisApp::position`), it is drawn on the map and the capture cross can be put on it. +//! What is here: the map, the bar (Layers, Add Data, Capture, More), the Layers sheet (the +//! project's maps and layouts; show, hide, zoom to, the active layer), the Capture sheet (a +//! point, line or polygon placed by the cross on the map, with a form for its layer's attributes +//! that follows the layer's data design: value lists, ranges and feature templates) and the More +//! sheet (full extent, undo, the project, the device's position, the desktop interface). A sheet +//! is lowered by its ×, a swipe down on its heading, a tap on the map above it, or the system's +//! Back. Where a shell reports the device's position (`RustGisApp::position`), it is drawn on the +//! map and the capture cross can be put on it. //! The form marks the fields the layer's constraint rules require, narrows a field to the values //! its contingent value sets leave it, has a calendar for dates, and keeps the field being typed //! in above the soft keyboard (see [`form`]). @@ -19,7 +21,7 @@ //! deletes it; with Snap on, the cross's place snaps to the map's features (see [`edit`]). #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] -use egui::{Align, Color32, CornerRadius, Layout, Rect, RichText, Sense, Stroke, Ui, Vec2}; +use egui::{Align, Color32, CornerRadius, Key, Layout, Modifiers, Rect, RichText, Sense, Stroke, Ui, Vec2}; use rustgis_ui_egui::rustgis_engine::platform::{TextEntry, TextKind, Typed}; use rustgis_ui_egui::theme::Tokens; use rustgis_ui_egui::{RustGisApp, i18n, icons, tl}; @@ -45,6 +47,10 @@ const TYPING_SHARE: f32 = 0.9; const CROWDED_ROWS: f32 = 7.0; /// The room above and below a sheet's rows. const SHEET_MARGIN: f32 = 4.0; +/// A heading over a group of the More sheet's rows. +const GROUP: f32 = 28.0; +/// The More sheet's groups: Map, Project, Position and Tools. +const MORE_GROUPS: usize = 4; /// The More sheet's rows with the position hidden. const MORE_ROWS: usize = 9; /// The More sheet's rows shown only with the position on: Go to My Position, and the track. @@ -79,11 +85,27 @@ pub fn removing_copies(ctx: &egui::Context) -> bool { copies::removing(ctx) } +/// Whether the system's Back would lower something here: a sheet of the touch interface that is +/// in view, with no dialog over it. A shell whose system keeps Back unless asked for it (Android +/// 16 on) asks only then, so the rest of the time Back does what the system does with it. +pub fn takes_back(app: &RustGisApp, ctx: &egui::Context) -> bool { + // A dialog window isn't a modal layer: it is asked for by name. + app.chrome.is_some() + && !app.ui.backstage + && sheet(ctx) != Sheet::None + && app.dialogs.open.is_none() + && ctx.memory(|m| m.top_modal_layer().is_none()) +} + /// The touch interface (a `rustgis_ui_egui::Chrome`): the bar, the sheet asked for, and the map /// in what is left. pub fn chrome(app: &mut RustGisApp, ui: &mut Ui) { let ctx = ui.ctx().clone(); let t = Tokens::get(&ctx); + // Back (Android's key, which a shell also gives for the system's Back) lowers the sheet. + if takes_back(app, &ctx) && ctx.input_mut(|i| i.consume_key(Modifiers::NONE, Key::BrowserBack)) { + set_sheet(&ctx, Sheet::None); + } let shown = sheet(&ctx); // Each position the shell reported goes to the average and the track being taken; while a // track is being recorded, positions keep coming with the app out of sight. @@ -143,16 +165,18 @@ pub fn chrome(app: &mut RustGisApp, ui: &mut Ui) { // Work running off the window's thread (a file being read, an export) shows its progress // and Cancel over the bar, where the desktop's status bar would. if app.background_busy() { - egui::Panel::bottom("touch_busy").exact_size(TARGET).frame(frame.inner_margin(egui::Margin::symmetric(12, 0))).show(ui, |ui| { - // Scrolled sideways when it is wider than the phone (a long address): its Cancel is - // then reached, not cut off. + let margin = egui::Margin::symmetric(12, 0); + // A little over a finger tall, so its Cancel stands clear of the strip's edges. + egui::Panel::bottom("touch_busy").exact_size(TARGET + 8.0).frame(frame.inner_margin(margin)).show(ui, |ui| { + // Scrolled sideways when it is wider than the phone (a long address); each Cancel + // comes before its job's words, so the first is in sight without scrolling. egui::ScrollArea::horizontal().show(ui, |ui| ui.horizontal_centered(|ui| app.background_status(ui))); }); } if shown != Sheet::None { // As tall as its rows, up to its share of the window: a longer list scrolls. let rows = match shown { - Sheet::Layers => (app.session.map().layers.len() + app.session.map().tiles.len()).max(1), + Sheet::Layers => (app.session.map().layers.len() + app.session.map().tiles.len()).max(1) + usize::from(has_views(app)), // Go to My Position is there only while the position is shown. // The position rows while it is shown; the track's row alone while one is recorded // with it off. @@ -172,7 +196,8 @@ pub fn chrome(app: &mut RustGisApp, ui: &mut Ui) { Sheet::None => 0, }; let spacing = ui.spacing().item_spacing.y; - let wanted = (rows + 1) as f32 * (TARGET + spacing) + 2.0 * SHEET_MARGIN; + let groups = if shown == Sheet::More { MORE_GROUPS } else { 0 }; + let wanted = (rows + 1) as f32 * (TARGET + spacing) + groups as f32 * (GROUP + spacing) + 2.0 * SHEET_MARGIN; let share = if ctx.egui_wants_keyboard_input() { TYPING_SHARE } else { SHEET_SHARE }; let height = wanted.min(ui.available_height() * share); let margin = egui::Margin::symmetric(8, SHEET_MARGIN as i8); @@ -187,7 +212,18 @@ pub fn chrome(app: &mut RustGisApp, ui: &mut Ui) { ); } egui::CentralPanel::default().frame(egui::Frame::NONE.fill(t.canvas)).show(ui, |ui| { + let above = ui.available_rect_before_wrap(); + // ModelBuilder, when it is up in the map's place, takes its own taps. + let map = !app.mb.shown(); app.map_view(ui); + // With a sheet up, a tap on the map above it lowers the sheet and does nothing to the + // map. It senses taps alone, so a drag or a pinch moves the map as ever. + if shown != Sheet::None && map { + let pinched = pinched(ui); + if ui.interact(above, egui::Id::unique("touch_dismiss"), Sense::click()).clicked() && !pinched { + set_sheet(ui.ctx(), Sheet::None); + } + } // While capturing, a cross marks the middle of the map: the point goes there, so the // map is moved under the cross, which a finger does more exactly than a tap. // Where the device is, while its position is shown. @@ -251,6 +287,27 @@ pub fn chrome(app: &mut RustGisApp, ui: &mut Ui) { }); } +/// Whether a second finger has touched since the first one went down. Such a gesture is a +/// pinch, though egui counts the first finger's lift as a tap when that finger stayed still. +/// Read from the touches themselves, as a second finger can come and go between two frames. +/// Looked at every frame it is to be known for. +fn pinched(ui: &Ui) -> bool { + let id = egui::Id::unique("touch_pinched"); + let began = ui.input(|i| i.pointer.primary_pressed()); + // The first finger down since the press, and whether another has touched since. + let kept = if began { None } else { ui.data(|d| d.get_temp::<(Option, bool)>(id)) }; + let (mut first, mut pinched) = kept.unwrap_or_default(); + ui.input(|i| { + for event in &i.events { + if let egui::Event::Touch { id: finger, .. } = event { + pinched |= *first.get_or_insert(*finger) != *finger; + } + } + }); + ui.data_mut(|d| d.insert_temp(id, (first, pinched))); + pinched +} + /// Starts recording a track, turning the device's position on when it is off. fn start_track(app: &mut RustGisApp, ctx: &egui::Context) { let mut state = track::field(ctx); @@ -369,17 +426,56 @@ fn bar_button(ui: &mut Ui, t: &Tokens, icon: &str, label: &str, on: bool) -> boo response.clicked() } -/// A sheet's heading, with the button that lowers it. +/// A sheet's heading, with its drag handle and the button that lowers it. A swipe down on the +/// heading lowers the sheet too. fn heading(ui: &mut Ui, t: &Tokens, title: &str) { - ui.horizontal(|ui| { - ui.set_min_height(TARGET); - ui.label(RichText::new(title).size(17.0).strong().color(t.text)); - ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - if icon_button(ui, t, "close", tl!("Close")) { - set_sheet(ui.ctx(), Sheet::None); + // The drag is sensed under the heading's widgets, so the button still takes its tap. + let row = ui + .scope_builder(egui::UiBuilder::new().id_salt("touch_heading").sense(Sense::drag()), |ui| { + ui.horizontal(|ui| { + ui.set_min_height(TARGET); + // Not selectable, or a drag starting on the title would select it instead. + ui.add(egui::Label::new(RichText::new(title).size(17.0).strong().color(t.text)).selectable(false)); + ui.with_layout(Layout::right_to_left(Align::Center), |ui| { + if icon_button(ui, t, "close", tl!("Close")) { + set_sheet(ui.ctx(), Sheet::None); + } + }); + }); + }) + .response; + let handle = Rect::from_center_size(egui::pos2(row.rect.center().x, row.rect.min.y + 4.0), Vec2::new(36.0, 4.0)); + ui.painter().rect_filled(handle, CornerRadius::same(2), t.border_strong); + if swiped_down(ui, &row) { + set_sheet(ui.ctx(), Sheet::None); + } +} + +/// Whether a drag on `row` ended this frame a finger's height or more below where it began, and +/// further down than sideways. +fn swiped_down(ui: &Ui, row: &egui::Response) -> bool { + let id = egui::Id::unique("touch_swipe"); + // Where the finger went down on the row, kept from the press: egui lets go of it as the + // finger lifts. A drag the system takes away (a call, an edge gesture) ends only with the + // next finger lifted, so each press starts over. + let (pressed, origin) = ui.input(|i| (i.pointer.primary_pressed(), i.pointer.press_origin())); + if pressed { + let from = origin.filter(|at| row.interact_rect.contains(*at)); + ui.data_mut(|d| { + d.remove::(id); + if let Some(from) = from { + d.insert_temp(id, from); } }); - }); + } + if !row.drag_stopped() { + return false; + } + let from = ui.data_mut(|d| d.remove_temp::(id)); + from.zip(row.interact_pointer_pos()).is_some_and(|(from, to)| { + let moved = to - from; + moved.y >= TARGET && moved.y > moved.x.abs() + }) } /// A square button of the smallest size a finger taps, showing an icon. Says whether it was @@ -401,11 +497,79 @@ enum Row { Tile(usize), } -/// The Layers sheet: the active map's layers, then its tile layers, each list top first. A row's -/// eye shows or hides its layer, its name makes it the one acted on, and its last button zooms -/// to it. +/// Whether the project has more than one view (maps, layouts and an open ModelBuilder) to switch +/// between. +fn has_views(app: &RustGisApp) -> bool { + app.session.project.maps.len() + app.session.project.layouts.len() + usize::from(app.mb.open) > 1 +} + +/// What a view chip brings into view. +enum View { + Map, + Layout, + /// ModelBuilder, left open with its model (by Back to the map, say). + Model, +} + +/// The project's views, to switch between them where the desktop's tab strip isn't drawn: a chip +/// for each map, then each layout, then ModelBuilder while it is open, the one in view marked. +/// Nothing with one map, no layout and no ModelBuilder. +fn views(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { + if !has_views(app) { + return; + } + let (project, layout) = (&app.session.project, app.session.active_layout); + // ModelBuilder, when it is up, covers every view: none is in view, so any chip leaves it. + let covered = app.mb.shown(); + // Each view's name, what it is, and whether it is in view. Read first: a tap runs a command, + // which changes them. + let mut views: Vec<(String, View, bool)> = project + .maps + .iter() + .enumerate() + .map(|(i, m)| (m.name.clone(), View::Map, !covered && layout.is_none() && i == project.active_map)) + .collect(); + views.extend(project.layouts.iter().enumerate().map(|(i, l)| (l.name.clone(), View::Layout, !covered && layout == Some(i)))); + if app.mb.open { + // As the desktop's tab names it. + let model = if app.mb.name.trim().is_empty() { tl!("Untitled model").to_string() } else { app.mb.name.trim().to_string() }; + let name = rustgis_ui_egui::i18n::fmt("ModelBuilder: {name}", &[("name", &model)]); + views.push((name, View::Model, covered)); + } + egui::ScrollArea::horizontal().id_salt("touch_views").show(ui, |ui| { + ui.horizontal(|ui| { + ui.set_min_height(TARGET); + for (name, view, on) in views { + let text = RichText::new(&name).size(15.0).color(if on { t.accent_text } else { t.text }); + let chip = egui::Button::new(text).fill(if on { t.checked } else { t.input }).min_size(Vec2::new(TARGET, TARGET - 8.0)); + if !ui.add(chip).clicked() || on { + continue; + } + // As the desktop's tab row does: the view chosen comes out from under ModelBuilder, + // and ModelBuilder's chip brings its model back as it was left, over the view now + // in sight (`ui.modelBuilder` with no name). + app.mb.active = false; + match view { + View::Model => run(app, "ui.modelBuilder", Value::Null), + View::Layout => run(app, "layout.activate", json!({"name": name})), + View::Map => { + if app.session.active_layout.is_some() { + run(app, "layout.close", Value::Null); + } + run(app, "map.activate", json!({"name": name})); + } + } + } + }); + }); +} + +/// The Layers sheet: the project's views ([`views`]), then the active map's layers and its tile +/// layers, each list top first. A row's eye shows or hides its layer, its name makes it the one +/// acted on, and its last button zooms to it. fn layers(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { heading(ui, t, tl!("Layers")); + views(app, ui, t); // Read first: a row's button runs a command, which changes the lists it is drawn from. let map = app.session.map(); let active = app.session.active_layer; @@ -1570,34 +1734,31 @@ fn edit_sheet( }); } -/// The More sheet: what is done now and then, each a row as tall as a finger. +/// The More sheet: what is done now and then, grouped under headings, each a row as tall as a +/// finger. fn more(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { heading(ui, t, tl!("More")); - let rows: [(&str, &str, &str); 7] = [ - ("zoom_full", tl!("Full Extent"), "map.zoomFull"), - ("open", tl!("Open Project"), "ui.open"), - ("save", tl!("Save Project"), "ui.save"), - // Save writes back where the project came from: this asks where. - ("save", tl!("Save Project As…"), "ui.saveAs"), - ("new", tl!("New Project"), "project.new"), - ("undo", tl!("Undo"), "edit.undo"), - ("redo", tl!("Redo"), "edit.redo"), - ]; egui::ScrollArea::vertical().auto_shrink([false, true]).show(ui, |ui| { - for (icon, label, command) in rows { - if row(ui, t, icon, label) { - // While unused copies are being removed, nothing may bring back or open a layer - // reading one: the project was looked at once, as the removal began. - if copies::removing(ui.ctx()) && ["edit.undo", "edit.redo", "ui.open", "project.new"].contains(&command) { - app.session.notify(tl!("Wait for the unused copies to be removed."), false); - continue; - } - // Open and New ask first when the project has unsaved changes: the front end's - // own question, as for the ribbon's buttons. - set_sheet(ui.ctx(), Sheet::None); - run(app, command, Value::Null); - } + group(ui, t, tl!("Map")); + let map = + [("zoom_full", tl!("Full Extent"), "map.zoomFull"), ("undo", tl!("Undo"), "edit.undo"), ("redo", tl!("Redo"), "edit.redo")]; + command_rows(app, ui, t, &map); + group(ui, t, tl!("Project")); + let project = [ + ("open", tl!("Open Project"), "ui.open"), + ("save", tl!("Save Project"), "ui.save"), + // Save writes back where the project came from: this asks where. + ("save", tl!("Save Project As…"), "ui.saveAs"), + ("new", tl!("New Project"), "project.new"), + ]; + command_rows(app, ui, t, &project); + // A map opened from a field package goes back as one: its edits and photos, for Bring Back + // on the computer it came from. + if can_send_field_package(app) && row(ui, t, "package", tl!("Send Field Package")) { + set_sheet(ui.ctx(), Sheet::None); + run(app, "ui.saveItem", json!({"kind": "fieldPackage"})); } + group(ui, t, tl!("Position")); // The device's position, where the shell can report one (it asks the user's leave first). let ctx = ui.ctx().clone(); let locate = if app.locate { tl!("Hide My Position") } else { tl!("Show My Position") }; @@ -1626,12 +1787,7 @@ fn more(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } } - // A map opened from a field package goes back as one: its edits and photos, for Bring Back - // on the computer it came from. - if can_send_field_package(app) && row(ui, t, "package", tl!("Send Field Package")) { - set_sheet(&ctx, Sheet::None); - run(app, "ui.saveItem", json!({"kind": "fieldPackage"})); - } + group(ui, t, tl!("Tools")); // The copies of picked files nothing in the project names, removed once agreed. if copies::folder().is_some_and(|f| f.is_dir()) { copies_row(app, ui, t, &ctx); @@ -1653,6 +1809,37 @@ fn more(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { }); } +/// A heading over a group of a sheet's rows. +fn group(ui: &mut Ui, t: &Tokens, title: &str) { + let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), GROUP), Sense::hover()); + ui.painter().text( + egui::pos2(rect.min.x + 12.0, rect.max.y - 4.0), + egui::Align2::LEFT_BOTTOM, + title, + egui::FontId::proportional(13.0), + t.text_dim, + ); +} + +/// A row for each of `rows` (icon, label, command): tapped, the sheet is lowered and the command +/// run. +fn command_rows(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, rows: &[(&str, &str, &str)]) { + for &(icon, label, command) in rows { + if row(ui, t, icon, label) { + // While unused copies are being removed, nothing may bring back or open a layer + // reading one: the project was looked at once, as the removal began. + if copies::removing(ui.ctx()) && ["edit.undo", "edit.redo", "ui.open", "project.new"].contains(&command) { + app.session.notify(tl!("Wait for the unused copies to be removed."), false); + continue; + } + // Open and New ask first when the project has unsaved changes: the front end's own + // question, as for the ribbon's buttons. + set_sheet(ui.ctx(), Sheet::None); + run(app, command, Value::Null); + } + } +} + /// How many rows Remove Unused Copies takes in the More sheet: none on a computer, two while it /// asks. fn copies_rows(ctx: &egui::Context) -> usize { @@ -1920,6 +2107,370 @@ mod tests { assert!(app.session.map().layers.is_empty()); } + /// The whole app in the touch interface, in a phone's window, worked by one finger. + struct Held { + app: RustGisApp, + ctx: egui::Context, + time: f64, + } + + impl Held { + fn new() -> Held { + let mut held = Held { app: RustGisApp::with_sample(), ctx: egui::Context::default(), time: 0.0 }; + held.app.chrome = Some(chrome); + // The fonts and the panels take a few frames to settle. + for _ in 0..5 { + held.frame(Vec::new()); + } + held + } + + /// Runs a frame with `events`, returning the words painted in sight, each with its place. + fn frame(&mut self, events: Vec) -> Vec<(String, Rect)> { + self.time += 0.1; + let screen_rect = Some(Rect::from_min_size(egui::Pos2::ZERO, Vec2::new(411.0, 800.0))); + let input = egui::RawInput { screen_rect, time: Some(self.time), events, ..Default::default() }; + let app = &mut self.app; + let output = self.ctx.run_ui(input, |ui| { + app.logic(ui.ctx()); + app.ui(ui); + }); + let words = output + .shapes + .iter() + .filter_map(|painted| match &painted.shape { + egui::epaint::Shape::Text(text) => { + let at = text.visual_bounding_rect(); + painted.clip_rect.intersects(at).then(|| (text.galley.text().to_string(), at)) + } + _ => None, + }) + .collect(); + output.drop_without_applying_deltas(); + words + } + + /// A finger put down at the first of `path`, moved through the rest and lifted, a frame + /// for each step. + fn touch(&mut self, path: &[egui::Pos2]) { + use egui::TouchPhase::{End, Move, Start}; + let Some((&first, &last)) = path.first().zip(path.last()) else { return }; + let touch = + |phase, pos| egui::Event::Touch { device_id: egui::TouchDeviceId(0), id: egui::TouchId(0), phase, pos, force: None }; + let button = |pos, pressed| egui::Event::PointerButton { + pos, + button: egui::PointerButton::Primary, + pressed, + modifiers: egui::Modifiers::NONE, + }; + self.frame(vec![touch(Start, first), egui::Event::PointerMoved(first), button(first, true)]); + for &pos in path.iter().skip(1) { + self.frame(vec![touch(Move, pos), egui::Event::PointerMoved(pos)]); + } + self.frame(vec![touch(End, last), button(last, false), egui::Event::PointerGone]); + // What the tap did is drawn, and past the time of a double tap. + self.time += 1.0; + self.frame(Vec::new()); + } + + /// Two fingers: the first, which egui takes for the pointer, stays at `still` while a + /// second moves from `from` to `to`; then both lift, the first one first. + fn pinch(&mut self, still: egui::Pos2, from: egui::Pos2, to: egui::Pos2) { + use egui::TouchPhase::{End, Move, Start}; + let touch = + |id, phase, pos| egui::Event::Touch { device_id: egui::TouchDeviceId(0), id: egui::TouchId(id), phase, pos, force: None }; + let button = |pressed| egui::Event::PointerButton { + pos: still, + button: egui::PointerButton::Primary, + pressed, + modifiers: egui::Modifiers::NONE, + }; + self.frame(vec![touch(0, Start, still), egui::Event::PointerMoved(still), button(true)]); + self.frame(vec![touch(1, Start, from)]); + self.frame(vec![touch(1, Move, from.lerp(to, 0.5))]); + self.frame(vec![touch(1, Move, to)]); + self.frame(vec![touch(0, End, still), button(false), egui::Event::PointerGone]); + self.frame(vec![touch(1, End, to)]); + self.time += 1.0; + self.frame(Vec::new()); + } + + /// A finger moved along `path` and then taken away by the system, not lifted. + fn cancelled(&mut self, path: &[egui::Pos2]) { + use egui::TouchPhase::{Cancel, Move, Start}; + let Some((&first, &last)) = path.first().zip(path.last()) else { return }; + let touch = + |phase, pos| egui::Event::Touch { device_id: egui::TouchDeviceId(0), id: egui::TouchId(0), phase, pos, force: None }; + let press = egui::Event::PointerButton { + pos: first, + button: egui::PointerButton::Primary, + pressed: true, + modifiers: egui::Modifiers::NONE, + }; + self.frame(vec![touch(Start, first), egui::Event::PointerMoved(first), press]); + for &pos in path.iter().skip(1) { + self.frame(vec![touch(Move, pos), egui::Event::PointerMoved(pos)]); + } + // As egui-winit reports it: the touch cancelled and the pointer gone. + self.frame(vec![touch(Cancel, last), egui::Event::PointerGone]); + self.time += 1.0; + self.frame(Vec::new()); + } + + /// A finger held at `still` and lifted, with `second` arriving in one frame between. + fn quick_second_finger(&mut self, still: egui::Pos2, second: Vec) { + use egui::TouchPhase::{End, Start}; + let touch = + |phase| egui::Event::Touch { device_id: egui::TouchDeviceId(0), id: egui::TouchId(0), phase, pos: still, force: None }; + let button = |pressed| egui::Event::PointerButton { + pos: still, + button: egui::PointerButton::Primary, + pressed, + modifiers: egui::Modifiers::NONE, + }; + self.frame(vec![touch(Start), egui::Event::PointerMoved(still), button(true)]); + self.frame(second); + self.frame(vec![touch(End), button(false), egui::Event::PointerGone]); + self.time += 1.0; + self.frame(Vec::new()); + } + + /// Where `word` is painted. + fn find(&mut self, word: &str) -> Option { + self.frame(Vec::new()).into_iter().find(|(text, _)| text == word).map(|(_, at)| at) + } + + /// Raises `to` and returns the middle of its heading's title. + fn raise(&mut self, to: Sheet, title: &str) -> egui::Pos2 { + set_sheet(&self.ctx, to); + // The bar has a button of the same word, below the heading: the heading comes first. + self.frame(Vec::new()); + let words = self.frame(Vec::new()); + let heading = words.iter().filter(|(text, _)| text == title).map(|(_, at)| at.center()); + heading.min_by(|a, b| a.y.total_cmp(&b.y)).unwrap() + } + } + + #[test] + fn a_swipe_down_on_a_sheets_heading_lowers_it() { + let mut held = Held::new(); + for (sheet_up, title) in [(Sheet::Layers, "Layers"), (Sheet::Capture, "Capture"), (Sheet::More, "More")] { + let title_at = held.raise(sheet_up, title); + // The title itself, and the bare heading beside it. + for from in [title_at, egui::pos2(205.0, title_at.y)] { + held.raise(sheet_up, title); + let to = |dx: f32, dy: f32| [from, from + Vec2::new(dx / 2.0, dy / 2.0), from + Vec2::new(dx, dy)]; + // Short, sideways or upward is no swipe down. + for (dx, dy) in [(0.0, 30.0), (150.0, 60.0), (0.0, -120.0)] { + held.touch(&to(dx, dy)); + assert_eq!(sheet(&held.ctx), sheet_up, "{title}: {dx}, {dy}"); + } + // Taken away by the system, however far it had gone, it is none either, nor + // does it make one of a tap further down afterwards. + held.cancelled(&to(0.0, 20.0)); + // (At the sheet's edge, where no row takes the tap.) + held.touch(&[egui::pos2(1.0, from.y + 120.0)]); + assert_eq!(sheet(&held.ctx), sheet_up, "{title}: cancelled"); + held.touch(&to(10.0, 120.0)); + assert_eq!(sheet(&held.ctx), Sheet::None, "{title}"); + } + } + } + + #[test] + fn a_tap_on_the_map_lowers_the_sheet_and_a_drag_or_a_pinch_moves_the_map() { + let mut held = Held::new(); + run(&mut held.app, "tool.set", json!({"tool": "measure"})); + held.raise(Sheet::Layers, "Layers"); + let at = egui::pos2(200.0, 150.0); + let before = held.app.session.map().camera; + held.touch(&[at, at + Vec2::new(30.0, 20.0), at + Vec2::new(60.0, 40.0)]); + let dragged = held.app.session.map().camera; + assert_ne!(dragged.center, before.center, "the drag panned the map"); + held.pinch(at, at + Vec2::new(40.0, 40.0), at + Vec2::new(120.0, 120.0)); + assert!(held.app.session.map().camera.res < dragged.res, "the pinch zoomed the map"); + assert_eq!(sheet(&held.ctx), Sheet::Layers); + // Nor is a second finger that comes and goes between two frames a tap. + let other = |phase| egui::Event::Touch { + device_id: egui::TouchDeviceId(0), + id: egui::TouchId(1), + phase, + pos: at + Vec2::new(60.0, 60.0), + force: None, + }; + use egui::TouchPhase::{End, Start}; + held.quick_second_finger(at, vec![other(Start), other(End)]); + assert_eq!(sheet(&held.ctx), Sheet::Layers); + // The tap only lowers the sheet: no point is measured from it. The next one is. + held.touch(&[at]); + assert_eq!(sheet(&held.ctx), Sheet::None); + assert_eq!(held.app.map.sketch_len(), 0); + held.touch(&[at]); + assert_eq!(held.app.map.sketch_len(), 1); + } + + #[test] + fn back_lowers_a_sheet_and_is_not_taken_without_one() { + let mut held = Held::new(); + let back = + || egui::Event::Key { key: Key::BrowserBack, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }; + assert!(!takes_back(&held.app, &held.ctx)); + held.raise(Sheet::More, "More"); + assert!(takes_back(&held.app, &held.ctx)); + held.frame(vec![back()]); + assert_eq!(sheet(&held.ctx), Sheet::None); + assert!(!takes_back(&held.app, &held.ctx)); + // Nor with a dialog over the sheet. + held.raise(Sheet::More, "More"); + run(&mut held.app, "ui.dialog", json!({"name": "addService"})); + held.frame(Vec::new()); + assert!(!takes_back(&held.app, &held.ctx)); + held.frame(vec![back()]); + assert_eq!(sheet(&held.ctx), Sheet::More); + held.app.dialogs.open = None; + held.frame(vec![back()]); + assert_eq!(sheet(&held.ctx), Sheet::None); + // With a question up, Back is not the sheet's. + held.raise(Sheet::More, "More"); + let first = held.app.session.map().layers.first().map(|l| l.id); + run(&mut held.app, "layer.rename", json!({"layer": first, "name": "Renamed"})); + run(&mut held.app, "project.new", Value::Null); + held.frame(Vec::new()); + held.frame(Vec::new()); + assert!(!takes_back(&held.app, &held.ctx)); + held.frame(vec![back()]); + assert_eq!(sheet(&held.ctx), Sheet::More); + assert!(held.app.session.dirty(), "the changes are kept"); + } + + #[test] + fn the_touch_map_has_no_tabs_or_zoom_buttons_and_the_layers_sheet_switches_views() { + let mut held = Held::new(); + let first = held.app.session.map().name.clone(); + assert_eq!(held.find(&first), None, "no view tab"); + // Where the desktop's Zoom In button is. + let res = held.app.session.map().camera.res; + held.touch(&[egui::pos2(411.0 - 28.0, 28.0)]); + assert_eq!(held.app.session.map().camera.res, res); + run(&mut held.app, "map.new", json!({"name": "Second"})); + run(&mut held.app, "layout.new", json!({"name": "Sheet A"})); + run(&mut held.app, "layout.close", Value::Null); + held.raise(Sheet::Layers, "Layers"); + for (name, map, layout) in [(first.as_str(), 0, None), ("Sheet A", 0, Some(0)), ("Second", 1, None)] { + let chip = held.find(name).unwrap().center(); + held.touch(&[chip]); + assert_eq!((held.app.session.project.active_map, held.app.session.active_layout), (map, layout), "{name}"); + assert_eq!(sheet(&held.ctx), Sheet::Layers); + } + // Over ModelBuilder, the map in view's own chip brings it back out. + run(&mut held.app, "ui.modelBuilder", json!({})); + held.raise(Sheet::Layers, "Layers"); + let chip = held.find("Second").unwrap().center(); + held.touch(&[chip]); + assert!(!held.app.mb.shown(), "the map is in view again"); + assert_eq!((held.app.session.project.active_map, held.app.session.active_layout), (1, None)); + } + + /// Toasts stand above the map's foot, as many of them as fit there, never over the sheet. + #[test] + fn toasts_stand_over_the_map_and_off_the_sheet() { + let mut held = Held::new(); + held.raise(Sheet::Layers, "Layers"); + for i in 0..3 { + // Several lines each: taller than a one-line toast. + held.app.session.notify(format!("Toast {i}\nline 2\nline 3\nline 4"), false); + } + // A toast's first frame measures it, unseen. + held.frame(Vec::new()); + let words = held.frame(Vec::new()); + let foot = held.app.map.view_rect.unwrap().0.max.y; + let toasts: Vec = words.iter().filter(|(text, _)| text.starts_with("Toast ")).map(|(_, at)| *at).collect(); + assert!(toasts.len() > 1, "the map above the sheet has room for more than one: {toasts:?}"); + for (i, at) in toasts.iter().enumerate() { + assert!(at.max.y <= foot, "{at:?} is over the sheet, below {foot}"); + assert!(toasts[i + 1..].iter().all(|other| !other.intersects(*at)), "{at:?} overlaps another"); + } + } + + /// An older toast too tall for the room left above the newer ones waits, rather than being + /// pushed down over them. + #[test] + fn a_toast_too_tall_for_the_room_left_waits() { + let mut held = Held::new(); + held.raise(Sheet::Layers, "Layers"); + let tall: Vec = (0..30).map(|i| format!("line {i}")).collect(); + held.app.session.notify(format!("Toast tall\n{}", tall.join("\n")), false); + held.app.session.notify("Toast short 1", false); + held.app.session.notify("Toast short 2", false); + for _ in 0..3 { + held.frame(Vec::new()); + } + let words = held.frame(Vec::new()); + let toasts: Vec<(String, Rect)> = words.into_iter().filter(|(text, _)| text.starts_with("Toast ")).collect(); + assert!(toasts.iter().any(|(text, _)| text.starts_with("Toast short 2")), "the newest is shown: {toasts:?}"); + for (i, (_, at)) in toasts.iter().enumerate() { + assert!(at.min.y >= 0.0, "{at:?} runs off the top"); + assert!(toasts[i + 1..].iter().all(|(_, other)| !other.intersects(*at)), "{at:?} overlaps another"); + } + } + + /// ModelBuilder, left open in the desktop interface, has no tab here: its own button leaves it. + #[test] + fn model_builder_is_left_for_the_map_by_a_button_in_sight() { + let mut held = Held::new(); + run(&mut held.app, "ui.modelBuilder", json!({})); + // A model on the canvas, never saved: every way back keeps it. + held.app.mb.graph = serde_json::from_value(json!({"nodes": [{"id": "in", "kind": "input", "layer": "Cities"}]})).unwrap(); + held.frame(Vec::new()); + assert_eq!(held.find("France"), None, "the model is in view, not the map"); + let back = held.find("Back to the map").unwrap(); + assert!(back.max.x <= 411.0, "{back:?}"); + held.touch(&[back.center()]); + assert!(held.find("France").is_some()); + // Left open, its model is a view in the Layers sheet, which brings it back as it was. + held.raise(Sheet::Layers, "Layers"); + let chip = held.find("ModelBuilder: Model").unwrap().center(); + held.touch(&[chip]); + assert!(held.app.mb.shown()); + // With the sheet still up, Back to the map takes the first tap. + assert_eq!(sheet(&held.ctx), Sheet::Layers); + let back = held.find("Back to the map").unwrap(); + held.touch(&[back.center()]); + assert!(!held.app.mb.shown(), "one tap goes back to the map"); + // Opened over a layout, it still goes back to the map. + run(&mut held.app, "layout.new", json!({"name": "Sheet A"})); + run(&mut held.app, "ui.modelBuilder", json!({})); + held.frame(Vec::new()); + let back = held.find("Back to the map").unwrap(); + held.touch(&[back.center()]); + assert_eq!(held.app.session.active_layout, None); + assert!(held.find("France").is_some()); + // The layout it was opened over is gone, and the chip still brings it back to stay. + held.raise(Sheet::Layers, "Layers"); + let chip = held.find("ModelBuilder: Model").unwrap().center(); + held.touch(&[chip]); + held.frame(Vec::new()); + held.frame(Vec::new()); + assert!(held.app.mb.shown()); + assert_eq!(held.find("France"), None, "the model is in view, not the map"); + let nodes: Vec<_> = held.app.mb.graph.nodes.iter().map(|n| n.id.as_str()).collect(); + assert_eq!(nodes, ["in"]); + } + + #[test] + fn a_finger_above_the_words_of_cancel_stops_a_download() { + // Never answered, so the download stays under way until it is cancelled. + let server = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}/pin.geojson", server.local_addr().unwrap()); + let mut held = Held::new(); + run(&mut held.app, "ui.addUrl", json!({"url": url})); + assert!(held.app.background_busy()); + let words = held.find("Cancel").unwrap(); + // Well outside a desktop button around the words, inside a finger's. + held.touch(&[words.center() - Vec2::new(0.0, 19.0)]); + assert!(!held.app.background_busy()); + } + /// The sample's layer of `kind`, made the active one, as the sheet resolves it. fn target(app: &mut RustGisApp, kind: GeometryType) -> Target { let id = app.session.map().layers.iter().find(|l| l.geometry_type() == kind).map(|l| l.id).unwrap(); diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index 8a1a58acd7..7093dfe448 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -18,14 +18,15 @@ A phone opens in the touch interface: the map, with a bar of four large buttons | Button | What it does | | --- | --- | -| **Layers** | Shows and hides layers, zooms to one, and makes one the active layer | +| **Layers** | Shows and hides layers, zooms to one, makes one the active layer, and switches between the project's maps and layouts | | **Add Data** | Opens files with the system's picker (Android's Files, iOS's document picker) | | **Capture** | Adds and edits features at the cross in the middle of the map | -| **More** | Full Extent, the project, Undo and Redo, the position, tracks and the rest | +| **More** | Full Extent, Undo and Redo, the project, the position, tracks and the rest | -Each button opens a sheet that rises over the map. **Use the Desktop Interface**, in the More -sheet, brings back the ribbon and panes; **Touch Interface**, on the View tab (or Use the Touch -Interface in Command Search), returns to the phone's interface. +Each button opens a sheet that rises over the map. To close it, tap its **×**, swipe down on its +heading, tap the map above it, or use Android's Back. Pinch the map to zoom. **Use the Desktop +Interface**, in the More sheet, brings back the ribbon and panes; **Touch Interface**, on the View +tab (or Use the Touch Interface in Command Search), returns to the phone's interface. ## Opening data