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

@@ -6,12 +6,31 @@
use tiny_skia::{ Path, PathBuilder };
use crate::types::Corners;
use crate::types::{ Corners, PathCmd };
/// Cubic bezier control-point factor for a quarter-circle approximation
/// (`(4/3) * (sqrt(2) - 1) ≈ 0.5523`).
const KAPPA: f32 = 0.5523_f32;
/// Build a tiny-skia path from a list of [`PathCmd`]s. Shared by both backends'
/// `fill_path` / `stroke_path`. Returns None for an empty or degenerate path.
pub ( crate ) fn build_ts_path( cmds: &[ PathCmd ] ) -> Option<Path>
{
let mut pb = PathBuilder::new();
for cmd in cmds
{
match *cmd
{
PathCmd::MoveTo( x, y ) => pb.move_to( x, y ),
PathCmd::LineTo( x, y ) => pb.line_to( x, y ),
PathCmd::QuadTo( x1, y1, x, y ) => pb.quad_to( x1, y1, x, y ),
PathCmd::CubicTo( x1, y1, x2, y2, x, y ) => pb.cubic_to( x1, y1, x2, y2, x, y ),
PathCmd::Close => pb.close(),
}
}
pb.finish()
}
/// Build a rounded rectangle path with independent per-corner radii
/// using cubic bezier curves. Each corner is clamped against the
/// inscribed-circle limit `min(width, height) / 2` before drawing,

View File

@@ -326,6 +326,9 @@ impl Canvas
/// Install a theme font registry on the active backend.
pub fn set_font_registry( &mut self, registry: Arc<FontRegistry> )
{
// Drop the shaped-line cache: a new registry can swap the faces the
// resolver leads with, so cached glyph runs may no longer match.
crate::text_shaping::clear_shape_cache();
match self
{
Canvas::Software( c ) => c.set_font_registry( registry ),
@@ -518,6 +521,29 @@ impl Canvas
}
}
/// Fill an arbitrary vector path (commands in surface coordinates) with a
/// solid colour. The software backend rasterises with tiny-skia directly;
/// the GLES backend rasterises into a tiny-skia pixmap and blits it (the
/// CPU fallback — no path shader on the GPU path).
pub fn fill_path( &mut self, cmds: &[ crate::types::PathCmd ], color: Color )
{
match self
{
Canvas::Software( c ) => c.fill_path( cmds, color ),
Canvas::Gles( c ) => c.fill_path( cmds, color ),
}
}
/// Stroke an arbitrary vector path (commands in surface coordinates).
pub fn stroke_path( &mut self, cmds: &[ crate::types::PathCmd ], color: Color, width: f32 )
{
match self
{
Canvas::Software( c ) => c.stroke_path( cmds, color, width ),
Canvas::Gles( c ) => c.stroke_path( cmds, color, width ),
}
}
/// Paint an outer drop shadow behind the rounded rect `target`.
///
/// On the GPU backend this runs an analytic soft-shadow shader

View File

@@ -9,9 +9,9 @@
use tiny_skia::{ Paint, PathBuilder, Stroke, Transform };
use crate::types::{ Color, Corners, Rect };
use crate::types::{ Color, Corners, PathCmd, Rect };
use super::helpers::build_rounded_rect;
use super::helpers::{ build_rounded_rect, build_ts_path };
use super::SoftwareCanvas;
impl SoftwareCanvas
@@ -100,4 +100,24 @@ impl SoftwareCanvas
stroke.width = width;
self.pixmap.stroke_path( &path, &paint, &stroke, Transform::identity(), self.clip_mask.as_ref() );
}
pub fn fill_path( &mut self, cmds: &[ PathCmd ], color: Color )
{
let Some( path ) = build_ts_path( cmds ) else { return };
let mut paint = 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;
self.pixmap.fill_path( &path, &paint, tiny_skia::FillRule::Winding, Transform::identity(), self.clip_mask.as_ref() );
}
pub fn stroke_path( &mut self, cmds: &[ PathCmd ], color: Color, width: f32 )
{
let Some( path ) = build_ts_path( cmds ) else { return };
let mut paint = 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 mut stroke = Stroke::default();
stroke.width = width;
self.pixmap.stroke_path( &path, &paint, &stroke, Transform::identity(), self.clip_mask.as_ref() );
}
}

View File

@@ -115,7 +115,9 @@ impl SoftwareCanvas
// refused the font, missing bytes for the preferred face)
// falls through to no-op — we'd rather not paint than risk
// a corrupted line.
let shaped = crate::text_shaping::shape_line( text, scaled, resolve );
let shaped = crate::text_shaping::shape_line_cached(
text, scaled, Arc::as_ptr( &canvas_handle.font ) as usize, resolve,
);
if shaped.is_empty() { return; }
// Resolve every glyph's font into an `Arc<Font>` for
@@ -128,7 +130,7 @@ impl SoftwareCanvas
{
fonts.push( ( primary_id, Arc::clone( &canvas_handle.font ) ) );
}
for g in &shaped
for g in shaped.iter()
{
if fonts.iter().any( |( id, _ )| *id == g.font_id ) { continue; }
// Walk the fallback chain to find an Arc<Font> with this
@@ -155,7 +157,7 @@ impl SoftwareCanvas
let mut layout: Vec<( GlyphKey, f32, f32 )> = Vec::with_capacity( shaped.len() );
{
let mut cursor_x = x;
for g in &shaped
for g in shaped.iter()
{
let Some( ( _, font_arc ) ) = fonts.iter().find( |( id, _ )| *id == g.font_id ) else
{
@@ -251,7 +253,9 @@ impl SoftwareCanvas
if !canvas_handle.bytes.is_empty() { Some( canvas_handle.clone() ) } else { None }
)
};
let shaped = crate::text_shaping::shape_line( text, scaled, resolve );
let shaped = crate::text_shaping::shape_line_cached(
text, scaled, Arc::as_ptr( &canvas_handle.font ) as usize, resolve,
);
if shaped.is_empty()
{
// Fallback: rustybuzz could not shape (no bytes for the