Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions apps/rustgis-mobile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions apps/rustgis-mobile/java/org/rustgis/mobile/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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. */
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions apps/rustgis-mobile/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions apps/rustgis-mobile/src/phone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 35 additions & 9 deletions apps/rustgis-mobile/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: takes_back (ask the system to hand Back over) and take_back (did Back come?) differ by one letter, which is easy to misread at the call site in update. Consider names such as keep_back and back_came. The method also makes a JNI call every frame while a sheet is up, even on Androids that never use it. That cost is small.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass.


/// 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
Expand Down Expand Up @@ -122,6 +133,9 @@ struct App<P: Phone> {
keeping: bool,
/// Whether the soft keyboard was up, where the phone says.
keyboard: Option<bool>,
/// 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<std::time::Instant>,
Expand Down Expand Up @@ -670,6 +684,13 @@ impl<P: Phone> eframe::App for App<P> {
});
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)
Expand Down Expand Up @@ -718,15 +739,11 @@ impl<P: Phone> eframe::App for App<P> {
// 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()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.back is true on every frame a sheet is up, so phone.take_back() runs a JNI call on each of those frames. On Android below 16 MainActivity.takeBack can only return false, because takesBack returns early there, so the call is wasted work on the render path. The cost is one small JNI round trip per frame, so this is low priority. If it matters, the shell could ask only where Back arrives as a callback (for example, have takes_back on the Java side report whether the callback was ever registered, once).

Confidence: medium-low (measured cost not checked on a device).

Category: performance

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this as it is. The call is made only on frames that are drawn, which the window does when something happens, and on the Java side it is one atomic read (back.getAndSet(false)) with no I/O or allocation. Asking for the Android version on the Rust side to skip it below 16 would add a second answer to keep in step for a cost well under the frame's own JNI traffic (takeEvents is called each frame too).

press(raw_input, egui::Key::BrowserBack);
}
if !self.phone.as_ref().is_some_and(|phone| phone.picking()) {
self.kept.borrow_mut().take();
Expand All @@ -743,6 +760,13 @@ impl<P: Phone> eframe::App for App<P> {
}
}

/// 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);
Expand Down Expand Up @@ -884,6 +908,7 @@ pub fn run<P: Phone + 'static>(
keeping: false,
typing: None,
keyboard: None,
back: false,
bars_light: None,
pushed,
phone,
Expand Down Expand Up @@ -991,6 +1016,7 @@ mod tests {
keeping: false,
keyboard: None,
typing: None,
back: false,
bars_light: None,
phone: Some(phone),
inbox: Inbox::default(),
Expand Down
103 changes: 86 additions & 17 deletions crates/ui-egui/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand All @@ -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();
}
Expand Down Expand Up @@ -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));
}
});
});
}
}
Expand All @@ -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<u64>) {
fn download_row(ui: &mut Ui, t: &Tokens, size: StripSize, d: &crate::UnderWay, cancel: &mut Option<u64>) {
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) {
Expand All @@ -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::<String>()) } 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);
}
}
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also changes the desktop's toasts, although the PR says the desktop interface is unchanged. On the desktop over_map is None, so the newest toast's bottom now sits 40 points above the window's bottom (before, its top sat 84 points up, anchored top-left at y - 44). Each older toast stacks on the measured height plus 8 points instead of a fixed 52. The stacking fix for multi-line notices looks like a real improvement, but the PR's "desktop unchanged" claim is wrong for this, and no desktop before/after screenshot is included, although AGENTS.md asks for one on any visible change.

Either limit the new placement to the touch interface (app.chrome.is_some()), or keep it for both and say so in the PR with a desktop before/after. Confidence: high that the placement changed; medium on how visible it is.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept for both: the PR body now says the desktop's toasts stack by their own heights too. A one-line toast sits where it did (bottom 40 points above the window's foot, as before); only notices of several lines no longer overlap the one above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the placement uses several unnamed constants (84.0, 40.0, 44.0, 8.0) that describe the toast's layout. Naming them, for example TOAST_FOOT and TOAST_HEIGHT_ONE_LINE, would make the bug above easier to see and fix. This is for the quality pass.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass.

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
Expand All @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-raising the desktop toast placement, since the earlier reply says a one-line toast "sits where it did". Before this change the toast's top was at screen.max.y - 84 (y - 44 with y = foot - 40), so its bottom was at foot - 84 + h. Now its bottom is at foot - 40. Those match only when h is 44. A one-line toast here is a 16 pt row inside 8 pt top and bottom margins, with 1 pt strokes, so h is about 34 pt. That moves the newest desktop toast about 10 pt lower than before. This comes from reading the layout code, not from a measurement, so my confidence is medium-low. A before/after desktop screenshot would settle it, and the earlier request for one still stands.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass.

}
}

Expand Down
Loading
Loading