Keep Android exit lists scrolling and test exits by network
Deliver the primary touch release to egui one frame before clearing the pointer. This preserves the velocity that ScrollArea uses for a fling. Cancellations clear it immediately and a new primary touch takes precedence. Add a speedometer to each network heading in the exit chooser. It reuses the Devices batch command and capability checks. Show queued or measured results in the existing rows and let the same control stop the batch without selecting a different exit. Verified scroll deceleration, cancellation, consecutive gestures and per-network start/stop with egui input tests. The Android ARM64 check, formatting and workspace Clippy passed. The macOS workspace suite passed 673 tests with 7 ignored.
This commit is contained in:
1 parent
7b83aec3be
commit
8e0cfc97ac
9 files changed
+359
-88
No files matched your search
@@ -1,8 +1,9 @@
|
||||
//! Android owns the GLES surface, keyboard and clipboard. egui owns the widgets.
|
||||
use crate::view::{Phone, SystemAction};
|
||||
use egui::{
|
||||
Event, ImeEvent, Key, Modifiers, PointerButton, Pos2, TouchDeviceId, TouchId, TouchPhase,
|
||||
use crate::{
|
||||
touch::TouchInput,
|
||||
view::{Phone, SystemAction},
|
||||
};
|
||||
use egui::{Event, ImeEvent, Key, Modifiers, Pos2, TouchPhase};
|
||||
use egui_glow::{Painter, glow};
|
||||
use jni::{
|
||||
JNIEnv,
|
||||
@@ -26,7 +27,7 @@ struct Display {
|
||||
ctx: egui::Context,
|
||||
ui: Phone,
|
||||
events: Vec<Event>,
|
||||
primary: Option<i32>,
|
||||
touch: TouchInput,
|
||||
start: Instant,
|
||||
ime: i32,
|
||||
exit_active: bool,
|
||||
@@ -81,7 +82,7 @@ extern "system" fn Java_cy_hexor_tsunagi_Native_surface(
|
||||
ctx,
|
||||
ui,
|
||||
events: Vec::new(),
|
||||
primary: None,
|
||||
touch: TouchInput::default(),
|
||||
start: Instant::now(),
|
||||
ime: -1,
|
||||
exit_active: false,
|
||||
@@ -104,7 +105,7 @@ extern "system" fn Java_cy_hexor_tsunagi_Native_draw(
|
||||
let size = egui::vec2(w as f32, h as f32) / scale;
|
||||
let mut input = egui::RawInput {
|
||||
screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, size)),
|
||||
events: std::mem::take(&mut d.events),
|
||||
events: d.touch.take_frame(&mut d.events),
|
||||
time: Some(d.start.elapsed().as_secs_f64()),
|
||||
..Default::default()
|
||||
};
|
||||
@@ -122,6 +123,9 @@ extern "system" fn Java_cy_hexor_tsunagi_Native_draw(
|
||||
}
|
||||
d.exit_active = exit_active;
|
||||
let output = d.ctx.run(input, |ctx| {
|
||||
if !d.events.is_empty() {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
action = d.ui.draw(ctx, &state, super::send).or(action);
|
||||
});
|
||||
d.ui.poll_monitor(|| super::submit(crate::service::Request::WatchTraffic));
|
||||
@@ -201,37 +205,7 @@ extern "system" fn Java_cy_hexor_tsunagi_Native_touch(
|
||||
2 => TouchPhase::End,
|
||||
_ => TouchPhase::Cancel,
|
||||
};
|
||||
d.events.push(Event::Touch {
|
||||
device_id: TouchDeviceId(0),
|
||||
id: TouchId(id as u64),
|
||||
phase,
|
||||
pos,
|
||||
force: None,
|
||||
});
|
||||
if phase == TouchPhase::Start && d.primary.is_none() {
|
||||
d.primary = Some(id);
|
||||
}
|
||||
if d.primary != Some(id) {
|
||||
return;
|
||||
}
|
||||
if phase != TouchPhase::Cancel {
|
||||
d.events.push(Event::PointerMoved(pos));
|
||||
}
|
||||
if phase != TouchPhase::Move {
|
||||
if phase == TouchPhase::Cancel {
|
||||
d.events.push(Event::PointerGone);
|
||||
}
|
||||
d.events.push(Event::PointerButton {
|
||||
pos,
|
||||
button: PointerButton::Primary,
|
||||
pressed: phase == TouchPhase::Start,
|
||||
modifiers: Modifiers::NONE,
|
||||
});
|
||||
}
|
||||
if matches!(phase, TouchPhase::Cancel | TouchPhase::End) {
|
||||
d.events.push(Event::PointerGone);
|
||||
d.primary = None;
|
||||
}
|
||||
d.touch.push(&mut d.events, phase, id, pos);
|
||||
});
|
||||
}
|
||||
#[unsafe(no_mangle)]
|
||||
|
||||
@@ -9,6 +9,8 @@ mod android;
|
||||
pub mod service;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(any(target_os = "android", test))]
|
||||
mod touch;
|
||||
#[cfg(unix)]
|
||||
pub mod tun;
|
||||
pub mod view;
|
||||
@@ -0,0 +1,161 @@
|
||||
//! Translate Android fingers into egui touch events and one primary pointer.
|
||||
use egui::{Event, Modifiers, PointerButton, Pos2, TouchDeviceId, TouchId, TouchPhase};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct TouchInput {
|
||||
primary: Option<i32>,
|
||||
clear_after_release: bool,
|
||||
}
|
||||
|
||||
impl TouchInput {
|
||||
pub(crate) fn push(&mut self, events: &mut Vec<Event>, phase: TouchPhase, id: i32, pos: Pos2) {
|
||||
events.push(Event::Touch {
|
||||
device_id: TouchDeviceId(0),
|
||||
id: TouchId(id as u64),
|
||||
phase,
|
||||
pos,
|
||||
force: None,
|
||||
});
|
||||
if phase == TouchPhase::Start && self.primary.is_none() {
|
||||
self.primary = Some(id);
|
||||
self.clear_after_release = false;
|
||||
}
|
||||
if self.primary != Some(id) {
|
||||
return;
|
||||
}
|
||||
if phase != TouchPhase::Cancel {
|
||||
events.push(Event::PointerMoved(pos));
|
||||
}
|
||||
if phase != TouchPhase::Move {
|
||||
events.push(Event::PointerButton {
|
||||
pos,
|
||||
button: PointerButton::Primary,
|
||||
pressed: phase == TouchPhase::Start,
|
||||
modifiers: Modifiers::NONE,
|
||||
});
|
||||
}
|
||||
match phase {
|
||||
TouchPhase::End => {
|
||||
self.primary = None;
|
||||
self.clear_after_release = true;
|
||||
}
|
||||
TouchPhase::Cancel => {
|
||||
self.primary = None;
|
||||
events.push(Event::PointerGone);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn take_frame(&mut self, events: &mut Vec<Event>) -> Vec<Event> {
|
||||
let frame = std::mem::take(events);
|
||||
// ScrollArea reads the release velocity in this frame. PointerGone clears
|
||||
// egui's position history, so clear the touch hover in the following frame.
|
||||
if std::mem::take(&mut self.clear_after_release) {
|
||||
events.push(Event::PointerGone);
|
||||
}
|
||||
frame
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use egui::{CentralPanel, Context, RawInput, Rect, ScrollArea, pos2, vec2};
|
||||
|
||||
#[test]
|
||||
fn a_flick_coasts_and_slows_while_a_cancelled_drag_stops() {
|
||||
for end in [TouchPhase::End, TouchPhase::Cancel] {
|
||||
let ctx = Context::default();
|
||||
ctx.set_os(egui::os::OperatingSystem::Android);
|
||||
let mut input = TouchInput::default();
|
||||
let mut events = Vec::new();
|
||||
let mut frame_no = 0;
|
||||
let mut frame = |touch| {
|
||||
if let Some((phase, y)) = touch {
|
||||
input.push(&mut events, phase, 0, pos2(80., y));
|
||||
}
|
||||
frame_no += 1;
|
||||
let mut offset = 0.;
|
||||
let _ = ctx.run(
|
||||
RawInput {
|
||||
time: Some(f64::from(frame_no) / 60.),
|
||||
events: input.take_frame(&mut events),
|
||||
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(320., 320.))),
|
||||
..Default::default()
|
||||
},
|
||||
|ctx| {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
offset = ScrollArea::vertical()
|
||||
.auto_shrink([false, false])
|
||||
.show(ui, |ui| {
|
||||
for i in 0..60 {
|
||||
ui.add_sized(
|
||||
[270., 40.],
|
||||
egui::Button::new(format!("Peer {i}")),
|
||||
);
|
||||
}
|
||||
})
|
||||
.state
|
||||
.offset
|
||||
.y;
|
||||
});
|
||||
},
|
||||
);
|
||||
offset
|
||||
};
|
||||
// The first layout measures the content. The next registers its drag area.
|
||||
frame(None);
|
||||
frame(None);
|
||||
frame(Some((TouchPhase::Start, 220.)));
|
||||
for y in [208., 190., 168., 146.] {
|
||||
frame(Some((TouchPhase::Move, y)));
|
||||
}
|
||||
let released = frame(Some((end, 126.)));
|
||||
let first = frame(None);
|
||||
let second = frame(None);
|
||||
assert!(released > 0., "{end:?} should follow a drag");
|
||||
if end == TouchPhase::End {
|
||||
assert!(
|
||||
second > first && first > released,
|
||||
"the scroll should coast after release"
|
||||
);
|
||||
assert!(
|
||||
second - first < first - released,
|
||||
"the fling should decelerate"
|
||||
);
|
||||
} else {
|
||||
assert_eq!((released, first), (first, second));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_primary_touch_supersedes_a_release_in_the_same_frame() {
|
||||
let mut input = TouchInput::default();
|
||||
let mut events = Vec::new();
|
||||
for (phase, id) in [
|
||||
(TouchPhase::Start, 1),
|
||||
(TouchPhase::End, 1),
|
||||
(TouchPhase::Start, 2),
|
||||
] {
|
||||
input.push(&mut events, phase, id, Pos2::ZERO);
|
||||
}
|
||||
let frame = input.take_frame(&mut events);
|
||||
assert!(matches!(
|
||||
frame.last(),
|
||||
Some(Event::PointerButton { pressed: true, .. })
|
||||
));
|
||||
assert!(events.is_empty());
|
||||
input.push(&mut events, TouchPhase::End, 2, Pos2::ZERO);
|
||||
let release = input.take_frame(&mut events);
|
||||
assert!(matches!(
|
||||
release.last(),
|
||||
Some(Event::PointerButton { pressed: false, .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
input.take_frame(&mut events).as_slice(),
|
||||
[Event::PointerGone]
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -493,7 +493,7 @@ impl Phone {
|
||||
if ui.button(format!("{} Paste secret", icon::CLIPBOARD)).clicked() {system = Some(SystemAction::PasteSecret);}
|
||||
}
|
||||
Page::Exits => {
|
||||
if let Some(choice) = devices::choose_exit_from_networks(ui, &report.networks, &report.endpoint_id) {
|
||||
if let Some(choice) = devices::choose_exit_with_tests(ui, &report.networks, &report.endpoint_id, &actions) {
|
||||
self.forms.exit_confirm = Some(choice);
|
||||
}
|
||||
if report.networks.is_empty() && ui.button("Join or create a network").clicked() {
|
||||
|
||||
Reference in new issue
Block a user