One proxy, and a macro instead of a shared test module
`Proxy` is the task queue as well as the way an application sends its own events, so `ProxyQueue` is gone. `schedule_redraw` becomes `request_redraw_if_needed`, which says what the comment beside it was saying. `assert_corners!` replaces the region helper, so `tests/common` goes with it, and the scroll test now states both corners rather than one number.
This commit is contained in:
1 parent
3a74a04a5b
commit
7eb2b85425
5 files changed
+33
-37
No files matched your search
+5
-9
@@ -49,9 +49,7 @@ pub enum DefaultEvent<State: DefaultAppState> {
|
|||||||
Update(Box<dyn TaskUpdate<DefaultRsc<State>>>),
|
Update(Box<dyn TaskUpdate<DefaultRsc<State>>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ProxyQueue<State: DefaultAppState>(EventLoopProxy<DefaultEvent<State>>);
|
impl<State: DefaultAppState> TaskQueue<DefaultRsc<State>> for Proxy<State> {
|
||||||
|
|
||||||
impl<State: DefaultAppState> TaskQueue<DefaultRsc<State>> for ProxyQueue<State> {
|
|
||||||
fn send(&self, update: Box<dyn TaskUpdate<DefaultRsc<State>>>) {
|
fn send(&self, update: Box<dyn TaskUpdate<DefaultRsc<State>>>) {
|
||||||
let _ = self.0.send_event(DefaultEvent::Update(update));
|
let _ = self.0.send_event(DefaultEvent::Update(update));
|
||||||
}
|
}
|
||||||
@@ -212,7 +210,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
.create_window(State::window_attributes())
|
.create_window(State::window_attributes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let default_state = DefaultUiState::new(window);
|
let default_state = DefaultUiState::new(window);
|
||||||
let mut rsc = DefaultRsc::init(Arc::new(ProxyQueue(proxy.clone())));
|
let mut rsc = DefaultRsc::init(Arc::new(Proxy(proxy.clone())));
|
||||||
let state = State::new(default_state, &mut rsc, Proxy(proxy));
|
let state = State::new(default_state, &mut rsc, Proxy(proxy));
|
||||||
let render = UiRenderState::new();
|
let render = UiRenderState::new();
|
||||||
Self { rsc, state, render }
|
Self { rsc, state, render }
|
||||||
@@ -223,9 +221,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
DefaultEvent::User(event) => self.state.event(event, &mut self.rsc, &mut self.render),
|
DefaultEvent::User(event) => self.state.event(event, &mut self.rsc, &mut self.render),
|
||||||
DefaultEvent::Update(update) => update(&mut self.state, &mut self.rsc),
|
DefaultEvent::Update(update) => update(&mut self.state, &mut self.rsc),
|
||||||
}
|
}
|
||||||
// An update is not a reason to draw; whether it made anything dirty
|
self.request_redraw_if_needed();
|
||||||
// is. That is why a task posts here rather than asking for a redraw.
|
|
||||||
self.schedule_redraw();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||||
@@ -309,7 +305,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
state.window_event(event, rsc, render);
|
state.window_event(event, rsc, render);
|
||||||
self.schedule_redraw();
|
self.request_redraw_if_needed();
|
||||||
self.state.default_state_mut().input.end_frame();
|
self.state.default_state_mut().input.end_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,7 +315,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<State: DefaultAppState> DefaultApp<State> {
|
impl<State: DefaultAppState> DefaultApp<State> {
|
||||||
fn schedule_redraw(&mut self) {
|
fn request_redraw_if_needed(&mut self) {
|
||||||
let ui_state = self.state.default_state_mut();
|
let ui_state = self.state.default_state_mut();
|
||||||
if self.render.needs_redraw(&ui_state.root, self.rsc.widgets()) {
|
if self.render.needs_redraw(&ui_state.root, self.rsc.widgets()) {
|
||||||
ui_state.renderer.window().request_redraw();
|
ui_state.renderer.window().request_redraw();
|
||||||
|
|||||||
@@ -22,6 +22,25 @@ impl TaskQueue<DefaultRsc<HarnessState>> for Queue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `assert_eq!` for where a frame put a widget, which `PixelRegion` cannot do
|
||||||
|
/// for itself: it neither compares nor prints.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! assert_corners {
|
||||||
|
($harness:expr, $id:expr, ($x0:expr, $y0:expr), ($x1:expr, $y1:expr)) => {{
|
||||||
|
let region = $harness.region(&$id).expect("widget drew nothing");
|
||||||
|
assert_eq!(
|
||||||
|
(
|
||||||
|
region.top_left.x,
|
||||||
|
region.top_left.y,
|
||||||
|
region.bot_right.x,
|
||||||
|
region.bot_right.y
|
||||||
|
),
|
||||||
|
($x0 as f32, $y0 as f32, $x1 as f32, $y1 as f32)
|
||||||
|
);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
pub use crate::assert_corners;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct HarnessState {
|
pub struct HarnessState {
|
||||||
pub root: Option<StrongWidget>,
|
pub root: Option<StrongWidget>,
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
use iris::harness::Harness;
|
|
||||||
use iris::prelude::*;
|
|
||||||
|
|
||||||
/// `PixelRegion` neither compares nor prints.
|
|
||||||
pub fn corners(h: &Harness, id: &impl IdLike) -> (f32, f32, f32, f32) {
|
|
||||||
let region = h.region(id).expect("widget drew nothing");
|
|
||||||
(
|
|
||||||
region.top_left.x,
|
|
||||||
region.top_left.y,
|
|
||||||
region.bot_right.x,
|
|
||||||
region.bot_right.y,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
+5
-8
@@ -1,9 +1,6 @@
|
|||||||
//! Where a frame puts things, with no window to put them in.
|
//! Where a frame puts things, with no window to put them in.
|
||||||
|
|
||||||
mod common;
|
use iris::harness::{Harness, assert_corners};
|
||||||
|
|
||||||
use common::corners;
|
|
||||||
use iris::harness::Harness;
|
|
||||||
use iris::prelude::*;
|
use iris::prelude::*;
|
||||||
|
|
||||||
/// A fixed 100 wide, and the rest of the 400 to its neighbour.
|
/// A fixed 100 wide, and the rest of the 400 to its neighbour.
|
||||||
@@ -19,8 +16,8 @@ fn a_span_gives_each_child_the_width_it_asked_for() {
|
|||||||
let mut h = Harness::new((400, 200));
|
let mut h = Harness::new((400, 200));
|
||||||
let (left, right) = two_rects(&mut h);
|
let (left, right) = two_rects(&mut h);
|
||||||
|
|
||||||
assert_eq!(corners(&h, &left), (0.0, 0.0, 100.0, 200.0));
|
assert_corners!(h, left, (0, 0), (100, 200));
|
||||||
assert_eq!(corners(&h, &right), (100.0, 0.0, 400.0, 200.0));
|
assert_corners!(h, right, (100, 0), (400, 200));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -32,6 +29,6 @@ fn resizing_relays_out_against_the_new_output() {
|
|||||||
assert!(h.needs_redraw());
|
assert!(h.needs_redraw());
|
||||||
h.frame();
|
h.frame();
|
||||||
|
|
||||||
assert_eq!(corners(&h, &left), (0.0, 0.0, 100.0, 100.0));
|
assert_corners!(h, left, (0, 0), (100, 100));
|
||||||
assert_eq!(corners(&h, &right), (100.0, 0.0, 800.0, 100.0));
|
assert_corners!(h, right, (100, 0), (800, 100));
|
||||||
}
|
}
|
||||||
+4
-7
@@ -1,9 +1,6 @@
|
|||||||
//! Scrolling moves content and stops at its ends.
|
//! Scrolling moves content and stops at its ends.
|
||||||
|
|
||||||
mod common;
|
use iris::harness::{Harness, assert_corners};
|
||||||
|
|
||||||
use common::corners;
|
|
||||||
use iris::harness::Harness;
|
|
||||||
use iris::prelude::*;
|
use iris::prelude::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -16,14 +13,14 @@ fn a_wheel_scrolls_the_content_and_stops_at_its_end() {
|
|||||||
h.move_to((200, 100));
|
h.move_to((200, 100));
|
||||||
|
|
||||||
// `Scroll` starts snapped to the end.
|
// `Scroll` starts snapped to the end.
|
||||||
assert_eq!(corners(&h, &top).1, -200.0);
|
assert_corners!(h, top, (0, -200), (400, 0));
|
||||||
|
|
||||||
// The handler scales a wheel line by 50.
|
// The handler scales a wheel line by 50.
|
||||||
h.scroll((0, 1));
|
h.scroll((0, 1));
|
||||||
h.frame();
|
h.frame();
|
||||||
assert_eq!(corners(&h, &top).1, -150.0);
|
assert_corners!(h, top, (0, -150), (400, 50));
|
||||||
|
|
||||||
h.scroll((0, 10));
|
h.scroll((0, 10));
|
||||||
h.frame();
|
h.frame();
|
||||||
assert_eq!(corners(&h, &top).1, 0.0);
|
assert_corners!(h, top, (0, 0), (400, 200));
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user