fix: retain instruction layouts across countdown transitions
This commit is contained in:
104
tests/ui_rendering.rs
Normal file
104
tests/ui_rendering.rs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use slint::{
|
||||||
|
ComponentHandle, PhysicalSize, Rgb8Pixel,
|
||||||
|
platform::{
|
||||||
|
Platform, WindowAdapter,
|
||||||
|
software_renderer::{MinimalSoftwareWindow, RepaintBufferType},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
slint::include_modules!();
|
||||||
|
|
||||||
|
struct TestPlatform(Rc<MinimalSoftwareWindow>);
|
||||||
|
impl Platform for TestPlatform {
|
||||||
|
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, slint::PlatformError> {
|
||||||
|
Ok(self.0.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Frames {
|
||||||
|
buffers: Vec<Vec<Rgb8Pixel>>,
|
||||||
|
next: usize,
|
||||||
|
width: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Frames {
|
||||||
|
fn render(&mut self, ui: &AppWindow, window: &MinimalSoftwareWindow) -> Vec<Rgb8Pixel> {
|
||||||
|
slint::platform::update_timers_and_animations();
|
||||||
|
ui.window().request_redraw();
|
||||||
|
let pixels = &mut self.buffers[self.next];
|
||||||
|
assert!(window.draw_if_needed(|renderer| {
|
||||||
|
renderer.render(pixels, self.width as usize);
|
||||||
|
}));
|
||||||
|
let result = pixels.clone();
|
||||||
|
self.next = (self.next + 1) % self.buffers.len();
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_same_frame(first: &[Rgb8Pixel], settled: &[Rgb8Pixel], context: &str) {
|
||||||
|
let differences = first.iter().zip(settled).filter(|(a, b)| a != b).count();
|
||||||
|
assert_eq!(differences, 0, "{context}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_transitions(repaint: RepaintBufferType, buffer_count: usize) {
|
||||||
|
let window = MinimalSoftwareWindow::new(repaint);
|
||||||
|
slint::platform::set_platform(Box::new(TestPlatform(window.clone()))).unwrap();
|
||||||
|
let ui = AppWindow::new().unwrap();
|
||||||
|
ui.show().unwrap();
|
||||||
|
// Native kiosk, portrait fallback, and a larger monitor.
|
||||||
|
for (width, height) in [(1280, 1024), (480, 800), (1920, 1080)] {
|
||||||
|
ui.window().set_size(PhysicalSize::new(width, height));
|
||||||
|
let mut frames = Frames {
|
||||||
|
buffers: vec![vec![Rgb8Pixel::default(); (width * height) as usize]; buffer_count],
|
||||||
|
next: 0,
|
||||||
|
width,
|
||||||
|
};
|
||||||
|
for _ in 0..4 {
|
||||||
|
frames.render(&ui, &window);
|
||||||
|
}
|
||||||
|
let instructions = frames.render(&ui, &window);
|
||||||
|
for _ in 0..2 {
|
||||||
|
ui.set_countdown(3);
|
||||||
|
ui.set_flash_opacity(0.75);
|
||||||
|
for _ in 0..3 {
|
||||||
|
frames.render(&ui, &window);
|
||||||
|
}
|
||||||
|
ui.set_countdown(0);
|
||||||
|
ui.set_flash_opacity(0.0);
|
||||||
|
ui.set_awaiting_release(true);
|
||||||
|
let first_release = frames.render(&ui, &window);
|
||||||
|
for _ in 0..4 {
|
||||||
|
assert_same_frame(
|
||||||
|
&first_release,
|
||||||
|
&frames.render(&ui, &window),
|
||||||
|
"release prompt moved after appearing",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ui.set_awaiting_release(false);
|
||||||
|
for _ in 0..4 {
|
||||||
|
assert_same_frame(
|
||||||
|
&instructions,
|
||||||
|
&frames.render(&ui, &window),
|
||||||
|
"instructions moved after reappearing",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn panels_appear_in_one_piece_with_fresh_buffers() {
|
||||||
|
check_transitions(RepaintBufferType::NewBuffer, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn panels_appear_in_one_piece_with_reused_buffer() {
|
||||||
|
check_transitions(RepaintBufferType::ReusedBuffer, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn panels_appear_in_one_piece_with_swapped_buffers() {
|
||||||
|
check_transitions(RepaintBufferType::SwappedBuffers, 2);
|
||||||
|
}
|
||||||
@@ -111,7 +111,8 @@ export component AppWindow inherits Window {
|
|||||||
|
|
||||||
// Wrap the text before sizing the panel, with consistent scaled padding.
|
// Wrap the text before sizing the panel, with consistent scaled padding.
|
||||||
panel := Rectangle {
|
panel := Rectangle {
|
||||||
visible: root.countdown == 0 && !root.awaiting-release;
|
// Keep layout alive while hidden so wrapped text does not reflow on return.
|
||||||
|
opacity: root.countdown == 0 && !root.awaiting-release ? 1 : 0;
|
||||||
x: root.safe-area-insets.left + (root.width - root.safe-area-insets.left - root.safe-area-insets.right - self.width) / 2;
|
x: root.safe-area-insets.left + (root.width - root.safe-area-insets.left - root.safe-area-insets.right - self.width) / 2;
|
||||||
y: root.height - self.height - root.inset - root.safe-area-insets.bottom;
|
y: root.height - self.height - root.inset - root.safe-area-insets.bottom;
|
||||||
width: min(1040px * root.ui-scale, root.width - root.inset * 2 - root.safe-area-insets.left - root.safe-area-insets.right);
|
width: min(1040px * root.ui-scale, root.width - root.inset * 2 - root.safe-area-insets.left - root.safe-area-insets.right);
|
||||||
@@ -152,7 +153,9 @@ export component AppWindow inherits Window {
|
|||||||
vertical-alignment: center;
|
vertical-alignment: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
if root.awaiting-release: Rectangle {
|
Rectangle {
|
||||||
|
// Allocate and measure this prompt once, before the first countdown.
|
||||||
|
opacity: root.awaiting-release ? 1 : 0;
|
||||||
x: (root.width - self.width) / 2;
|
x: (root.width - self.width) / 2;
|
||||||
y: root.height - self.height - root.inset - root.safe-area-insets.bottom;
|
y: root.height - self.height - root.inset - root.safe-area-insets.bottom;
|
||||||
width: panel.width;
|
width: panel.width;
|
||||||
|
|||||||
Reference in New Issue
Block a user