Mature ltk to host an externally-laid-out Android view tree
Some checks failed
CI / build + test (push) Has been cancelled
CI / cargo audit (push) Has been cancelled

Add the primitives rustdroid needs to project an Android view hierarchy onto an ltk surface, all kept general rather than Android-specific.
Canvas gains arbitrary vector path fill and stroke: `Canvas::fill_path` / `stroke_path` over a new `PathCmd` command list (MoveTo/LineTo/QuadTo/CubicTo/Close in surface coordinates). The software backend rasterises directly with tiny-skia; the GLES backend rasterises into a tiny-skia pixmap and blits it (CPU fallback, no GPU path shader). This is what renders an Android `Path`, a `VectorDrawable`, or a Lottie frame.
`ExternalSource::Cpu` (and the `External::cpu` constructor) adds an immediate-mode CPU drawing closure, invoked once per frame with the canvas and the widget's laid-out rect, working on both the GLES and software backends. It hosts a custom `View.onDraw` straight onto the ltk canvas without a GL texture round-trip, unlike the existing `Texture` source which only renders on GLES.
`Stack::push_placed` appends a child at an exact rect, bypassing alignment and intrinsic sizing. This lets a view tree whose geometry is computed elsewhere — Android's measure/layout pass, which yields an absolute rect per view — be projected onto a Stack in paint order.
New `RichText` widget: wrapped paragraph text carrying a message per clickable link range, with the layout pass emitting one hit rect per link line so taps land on the link rather than the whole paragraph. It is the ltk side of an Android `Spanned` carrying `URLSpan` / `ClickableSpan`.
gesture: only drive the horizontal pager once the axis locks horizontal
`on_move` emitted horizontal swipe progress whenever `swipe_axis != Vertical`, which includes the pre-lock window where `swipe_axis` is still `None` (the first ~8 px of travel). But `horizontal_drag_started` only flips once `dx.abs() > 8`. A vertical gesture that opened with a few pixels of lateral drift — a swipe-up to the launcher, a scroll — therefore emitted a tiny horizontal progress sample, which armed the consumer's pager, then locked vertical without `dx` ever passing 8, so `horizontal_drag_started` stayed false. On release the horizontal branch was skipped, no `on_swipe_horizontal_progress(0.0)` fired, and the pager stayed stuck active — on the crustace homescreen that froze the surface (the main stays motion-only behind stale page subsurfaces) until an unrelated gesture reset it.
Emit horizontal progress only once `swipe_axis == Some(Horizontal)`. Locking onto the horizontal axis already implies `dx.abs() > 8`, so `horizontal_drag_started` is set in the same step, restoring the invariant that a frame which drives the pager always has a matching release event to settle it. The cost is that the first ~8 px of a horizontal drag no longer move the page, which is the same deadband the axis lock already imposes on the vertical panels. Adds `pre_lock_lateral_drift_does_not_drive_horizontal_pager`.
This commit is contained in:
2026-06-12 22:14:02 +02:00
parent df8fcbf757
commit b00cf460bb
21 changed files with 840 additions and 43 deletions

View File

@@ -27,7 +27,7 @@
use glow::HasContext;
use crate::theme::{ gradient_lut, BlendMode, InsetShadow, LinearGradient, RadialGradient, Shadow };
use crate::types::{ Color, Corners, Rect };
use crate::types::{ Color, Corners, PathCmd, Rect };
use super::helpers::ortho_rect;
use super::GlesCanvas;
@@ -51,6 +51,69 @@ impl GlesCanvas
r_x1 <= clip.x || c_x1 <= r_x0 || r_y1 <= clip.y || c_y1 <= r_y0
}
pub fn fill_path( &mut self, cmds: &[ PathCmd ], color: Color )
{
self.rasterise_path( cmds, color, None );
}
pub fn stroke_path( &mut self, cmds: &[ PathCmd ], color: Color, width: f32 )
{
self.rasterise_path( cmds, color, Some( width ) );
}
/// CPU fallback for vector paths on the GPU backend: rasterise into a
/// tiny-skia pixmap sized to the path's bounding box, then blit it as a
/// texture via `draw_image_data`. No GPU path shader.
fn rasterise_path( &mut self, cmds: &[ PathCmd ], color: Color, stroke: Option<f32> )
{
let Some( path ) = crate::render::helpers::build_ts_path( cmds ) else { return };
let bounds = path.bounds();
let pad = stroke.map_or( 1.0, |w| w * 0.5 + 1.0 );
let x0 = ( bounds.left() - pad ).floor();
let y0 = ( bounds.top() - pad ).floor();
let x1 = ( bounds.right() + pad ).ceil();
let y1 = ( bounds.bottom() + pad ).ceil();
let pw = ( x1 - x0 ).max( 1.0 ) as u32;
let ph = ( y1 - y0 ).max( 1.0 ) as u32;
if pw > 8192 || ph > 8192 { return; }
let Some( mut pixmap ) = tiny_skia::Pixmap::new( pw, ph ) else { return };
let mut paint = tiny_skia::Paint::default();
paint.set_color( Color::rgba( color.r, color.g, color.b, color.a * self.global_alpha ).to_tiny_skia() );
paint.anti_alias = true;
let tf = tiny_skia::Transform::from_translate( -x0, -y0 );
match stroke
{
Some( w ) =>
{
let mut s = tiny_skia::Stroke::default();
s.width = w;
pixmap.stroke_path( &path, &paint, &s, tf, None );
}
None => { pixmap.fill_path( &path, &paint, tiny_skia::FillRule::Winding, tf, None ); }
}
// tiny-skia stores premultiplied RGBA; the texture shader expects straight.
let mut rgba = pixmap.data().to_vec();
for px in rgba.chunks_exact_mut( 4 )
{
let a = px[ 3 ] as u32;
if a > 0 && a < 255
{
px[ 0 ] = ( px[ 0 ] as u32 * 255 / a ).min( 255 ) as u8;
px[ 1 ] = ( px[ 1 ] as u32 * 255 / a ).min( 255 ) as u8;
px[ 2 ] = ( px[ 2 ] as u32 * 255 / a ).min( 255 ) as u8;
}
}
// Transient texture (upload → draw → delete): a path's pixels change
// every frame for animated vectors, which would make the content-keyed
// image cache in `draw_image_data` grow without bound and exhaust GL.
let dest = Rect { x: x0, y: y0, width: pw as f32, height: ph as f32 };
let tex = super::helpers::upload_rgba_texture( &self.gl, self.version, &rgba, pw as i32, ph as i32 );
self.draw_external_texture( tex, dest, 1.0 );
unsafe { self.gl.delete_texture( tex ); }
}
pub fn fill_rect( &mut self, rect: Rect, color: Color, corners: Corners )
{
if self.rect_culled( rect, 1.0 ) { return; }