Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7dc7614ae6 | ||
|
|
e865467a3f | ||
|
|
c8ac669f95 |
No files matched your search
@@ -421,7 +421,7 @@ impl Display for UiRegion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct PixelRegion {
|
pub struct PixelRegion {
|
||||||
pub top_left: Vec2,
|
pub top_left: Vec2,
|
||||||
pub bot_right: Vec2,
|
pub bot_right: Vec2,
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ impl UiRenderState {
|
|||||||
self.resized = true;
|
self.resized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn output_size(&self) -> Vec2 {
|
||||||
|
self.output_size
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update<'a>(&mut self, root: impl Into<Option<&'a StrongWidget>>, rsc: &mut dyn UiRsc) {
|
pub fn update<'a>(&mut self, root: impl Into<Option<&'a StrongWidget>>, rsc: &mut dyn UiRsc) {
|
||||||
// safety mechanism for memory leaks; might wanna return a result instead so user can
|
// safety mechanism for memory leaks; might wanna return a result instead so user can
|
||||||
// decide whether to panic or not
|
// decide whether to panic or not
|
||||||
|
|||||||
+1
-5
@@ -10,11 +10,7 @@ struct State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultAppState for State {
|
impl DefaultAppState for State {
|
||||||
fn new(
|
fn new(mut ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, _: Proxy<Self>) -> Self {
|
||||||
mut ui_state: DefaultUiState,
|
|
||||||
rsc: &mut DefaultRsc<Self>,
|
|
||||||
_: Proxy<Self::Event>,
|
|
||||||
) -> Self {
|
|
||||||
rect(Color::RED).set_root(rsc, &mut ui_state);
|
rect(Color::RED).set_root(rsc, &mut ui_state);
|
||||||
Self { ui_state }
|
Self { ui_state }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,7 @@ pub struct Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultAppState for Client {
|
impl DefaultAppState for Client {
|
||||||
fn new(
|
fn new(mut ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, _: Proxy<Self>) -> Self {
|
||||||
mut ui_state: DefaultUiState,
|
|
||||||
rsc: &mut DefaultRsc<Self>,
|
|
||||||
_: Proxy<Self::Event>,
|
|
||||||
) -> Self {
|
|
||||||
let rrect = rect(Color::WHITE).radius(20);
|
let rrect = rect(Color::WHITE).radius(20);
|
||||||
let pad_test = (
|
let pad_test = (
|
||||||
rrect.color(Color::BLUE),
|
rrect.color(Color::BLUE),
|
||||||
|
|||||||
+1
-5
@@ -11,11 +11,7 @@ struct State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultAppState for State {
|
impl DefaultAppState for State {
|
||||||
fn new(
|
fn new(mut ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, _: Proxy<Self>) -> Self {
|
||||||
mut ui_state: DefaultUiState,
|
|
||||||
rsc: &mut DefaultRsc<Self>,
|
|
||||||
_: Proxy<Self::Event>,
|
|
||||||
) -> Self {
|
|
||||||
let rect = rect(Color::RED).add(rsc);
|
let rect = rect(Color::RED).add(rsc);
|
||||||
rect.task_on(CursorSense::click(), async move |mut ctx| {
|
rect.task_on(CursorSense::click(), async move |mut ctx| {
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
|||||||
+1
-5
@@ -36,11 +36,7 @@ impl Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultAppState for State {
|
impl DefaultAppState for State {
|
||||||
fn new(
|
fn new(mut ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, _: Proxy<Self>) -> Self {
|
||||||
mut ui_state: DefaultUiState,
|
|
||||||
rsc: &mut DefaultRsc<Self>,
|
|
||||||
_: Proxy<Self::Event>,
|
|
||||||
) -> Self {
|
|
||||||
let test = Test::new(rsc);
|
let test = Test::new(rsc);
|
||||||
|
|
||||||
test.on(CursorSense::click(), move |_, rsc| {
|
test.on(CursorSense::click(), move |_, rsc| {
|
||||||
|
|||||||
+56
-39
@@ -25,7 +25,35 @@ pub use sense::*;
|
|||||||
pub use state::*;
|
pub use state::*;
|
||||||
pub use task::*;
|
pub use task::*;
|
||||||
|
|
||||||
pub type Proxy<Event> = EventLoopProxy<Event>;
|
/// Sends an application's own events to its event loop. It wraps the proxy
|
||||||
|
/// rather than being one because task updates travel the same way: what an
|
||||||
|
/// application sends is its `Event`, not the loop's whole message type.
|
||||||
|
pub struct Proxy<State: DefaultAppState>(EventLoopProxy<DefaultEvent<State>>);
|
||||||
|
|
||||||
|
impl<State: DefaultAppState> Clone for Proxy<State> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self(self.0.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<State: DefaultAppState> Proxy<State> {
|
||||||
|
pub fn send_event(&self, event: State::Event) {
|
||||||
|
let _ = self.0.send_event(DefaultEvent::User(event));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What the event loop carries: the application's own events, and the
|
||||||
|
/// updates tasks send back to the ui thread.
|
||||||
|
pub enum DefaultEvent<State: DefaultAppState> {
|
||||||
|
User(State::Event),
|
||||||
|
Update(Box<dyn TaskUpdate<DefaultRsc<State>>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<State: DefaultAppState> TaskQueue<DefaultRsc<State>> for Proxy<State> {
|
||||||
|
fn send(&self, update: Box<dyn TaskUpdate<DefaultRsc<State>>>) {
|
||||||
|
let _ = self.0.send_event(DefaultEvent::Update(update));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct DefaultUiState {
|
pub struct DefaultUiState {
|
||||||
pub root: Option<StrongWidget>,
|
pub root: Option<StrongWidget>,
|
||||||
@@ -66,9 +94,8 @@ pub trait HasDefaultUiState: Sized + 'static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait DefaultAppState: HasDefaultUiState {
|
pub trait DefaultAppState: HasDefaultUiState {
|
||||||
type Event = ();
|
type Event: Send = ();
|
||||||
fn new(ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, proxy: Proxy<Self::Event>)
|
fn new(ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, proxy: Proxy<Self>) -> Self;
|
||||||
-> Self;
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn event(
|
fn event(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -101,18 +128,14 @@ pub struct DefaultRsc<State: 'static> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<State> DefaultRsc<State> {
|
impl<State> DefaultRsc<State> {
|
||||||
fn init(window: Arc<Window>) -> (Self, TaskMsgReceiver<Self>) {
|
pub fn init(queue: Arc<dyn TaskQueue<Self>>) -> Self {
|
||||||
let (tasks, recv) = Tasks::init(window);
|
|
||||||
(
|
|
||||||
Self {
|
Self {
|
||||||
ui: Default::default(),
|
ui: Default::default(),
|
||||||
events: Default::default(),
|
events: Default::default(),
|
||||||
tasks,
|
tasks: Tasks::init(queue),
|
||||||
state: Default::default(),
|
state: Default::default(),
|
||||||
_state: Default::default(),
|
_state: Default::default(),
|
||||||
},
|
}
|
||||||
recv,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_state<T: 'static>(&mut self, id: impl IdLike, data: T) -> WeakState<T> {
|
pub fn create_state<T: 'static>(&mut self, id: impl IdLike, data: T) -> WeakState<T> {
|
||||||
@@ -177,43 +200,32 @@ pub struct DefaultApp<State: DefaultAppState> {
|
|||||||
rsc: DefaultRsc<State>,
|
rsc: DefaultRsc<State>,
|
||||||
render: UiRenderState,
|
render: UiRenderState,
|
||||||
state: State,
|
state: State,
|
||||||
task_recv: TaskMsgReceiver<DefaultRsc<State>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
||||||
type Event = State::Event;
|
type Event = DefaultEvent<State>;
|
||||||
|
|
||||||
fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<Self::Event>) -> Self {
|
fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<Self::Event>) -> Self {
|
||||||
let window = event_loop
|
let window = event_loop
|
||||||
.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, task_recv) = DefaultRsc::init(default_state.window.clone());
|
let mut rsc = DefaultRsc::init(Arc::new(Proxy(proxy.clone())));
|
||||||
let state = State::new(default_state, &mut rsc, proxy);
|
let state = State::new(default_state, &mut rsc, Proxy(proxy));
|
||||||
let render = UiRenderState::new();
|
let render = UiRenderState::new();
|
||||||
Self {
|
Self { rsc, state, render }
|
||||||
rsc,
|
|
||||||
state,
|
|
||||||
render,
|
|
||||||
task_recv,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event(&mut self, event: Self::Event, _: &ActiveEventLoop) {
|
fn event(&mut self, event: Self::Event, _: &ActiveEventLoop) {
|
||||||
self.state.event(event, &mut self.rsc, &mut self.render);
|
match event {
|
||||||
|
DefaultEvent::User(event) => self.state.event(event, &mut self.rsc, &mut self.render),
|
||||||
|
DefaultEvent::Update(update) => update(&mut self.state, &mut self.rsc),
|
||||||
|
}
|
||||||
|
self.request_redraw_if_needed();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||||
let Self {
|
let Self { rsc, render, state } = self;
|
||||||
rsc,
|
|
||||||
render,
|
|
||||||
state,
|
|
||||||
task_recv,
|
|
||||||
} = self;
|
|
||||||
|
|
||||||
for update in task_recv.try_iter() {
|
|
||||||
update(state, rsc);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ui_state = state.default_state_mut();
|
let ui_state = state.default_state_mut();
|
||||||
let input_changed = ui_state.input.event(&event);
|
let input_changed = ui_state.input.event(&event);
|
||||||
@@ -223,8 +235,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
ui_state.focus = None;
|
ui_state.focus = None;
|
||||||
}
|
}
|
||||||
if input_changed {
|
if input_changed {
|
||||||
let window_size = ui_state.window_size();
|
render.run_sensors(rsc, state, cursor_state);
|
||||||
render.run_sensors(rsc, state, cursor_state, window_size);
|
|
||||||
}
|
}
|
||||||
let ui_state = state.default_state_mut();
|
let ui_state = state.default_state_mut();
|
||||||
if old != ui_state.focus
|
if old != ui_state.focus
|
||||||
@@ -293,11 +304,8 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
state.window_event(event, rsc, render);
|
state.window_event(event, rsc, render);
|
||||||
let ui_state = self.state.default_state_mut();
|
self.request_redraw_if_needed();
|
||||||
if render.needs_redraw(&ui_state.root, rsc.widgets()) {
|
self.state.default_state_mut().input.end_frame();
|
||||||
ui_state.renderer.window().request_redraw();
|
|
||||||
}
|
|
||||||
ui_state.input.end_frame();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit(&mut self) {
|
fn exit(&mut self) {
|
||||||
@@ -305,6 +313,15 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<State: DefaultAppState> DefaultApp<State> {
|
||||||
|
fn request_redraw_if_needed(&mut self) {
|
||||||
|
let ui_state = self.state.default_state_mut();
|
||||||
|
if self.render.needs_redraw(&ui_state.root, self.rsc.widgets()) {
|
||||||
|
ui_state.renderer.window().request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait RscIdx<Rsc> {
|
pub trait RscIdx<Rsc> {
|
||||||
type Output;
|
type Output;
|
||||||
fn get(self, rsc: &Rsc) -> &Self::Output;
|
fn get(self, rsc: &Rsc) -> &Self::Output;
|
||||||
|
|||||||
+22
-14
@@ -27,10 +27,7 @@ pub struct CursorSenses(Vec<CursorSense>);
|
|||||||
|
|
||||||
impl Event for CursorSenses {
|
impl Event for CursorSenses {
|
||||||
type Data<'a> = CursorData<'a>;
|
type Data<'a> = CursorData<'a>;
|
||||||
/// Who the cursor was inside on the last input, which is what says whose
|
type Global = Hovered;
|
||||||
/// hover has ended -- including a widget a higher layer has since covered,
|
|
||||||
/// which this walk never reaches.
|
|
||||||
type Global = Vec<WidgetId>;
|
|
||||||
fn should_run<'a>(&self, data: &Self::Data<'a>) -> Option<Self::Data<'a>> {
|
fn should_run<'a>(&self, data: &Self::Data<'a>) -> Option<Self::Data<'a>> {
|
||||||
if let Some(sense) = should_run(self, &data.cursor, data.hover) {
|
if let Some(sense) = should_run(self, &data.cursor, data.hover) {
|
||||||
let mut data = data.clone();
|
let mut data = data.clone();
|
||||||
@@ -48,6 +45,18 @@ impl Event for CursorSenses {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Who the cursor was inside, before and after an input. The difference is
|
||||||
|
/// whose hover has ended -- including a widget a higher layer has covered,
|
||||||
|
/// which the walk stops before reaching.
|
||||||
|
///
|
||||||
|
/// Two buffers that swap rather than one rebuilt, so an input allocates
|
||||||
|
/// nothing once they have grown.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Hovered {
|
||||||
|
was: Vec<WidgetId>,
|
||||||
|
now: Vec<WidgetId>,
|
||||||
|
}
|
||||||
|
|
||||||
impl CursorSense {
|
impl CursorSense {
|
||||||
pub fn click() -> Self {
|
pub fn click() -> Self {
|
||||||
Self::PressStart(CursorButton::Left)
|
Self::PressStart(CursorButton::Left)
|
||||||
@@ -163,7 +172,6 @@ pub trait SensorUi {
|
|||||||
rsc: &mut Rsc,
|
rsc: &mut Rsc,
|
||||||
state: &mut Rsc::State,
|
state: &mut Rsc::State,
|
||||||
cursor: CursorState,
|
cursor: CursorState,
|
||||||
window_size: Vec2,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,17 +181,16 @@ impl SensorUi for UiRenderState {
|
|||||||
rsc: &mut Rsc,
|
rsc: &mut Rsc,
|
||||||
state: &mut Rsc::State,
|
state: &mut Rsc::State,
|
||||||
cursor: CursorState,
|
cursor: CursorState,
|
||||||
window_size: Vec2,
|
|
||||||
) {
|
) {
|
||||||
// in order to remove this take, need to store active list in UiRenderState somehow
|
// in order to remove this take, need to store active list in UiRenderState somehow
|
||||||
// this would probably be done through a generic parameter that adds yet another rsc /
|
// this would probably be done through a generic parameter that adds yet another rsc /
|
||||||
// state like thing, but local to render state, and is passed to UiRsc events so you can
|
// state like thing, but local to render state, and is passed to UiRsc events so you can
|
||||||
// update it there?
|
// update it there?
|
||||||
let active = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().active);
|
let active = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().active);
|
||||||
let was = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().global);
|
let mut hovered = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().global);
|
||||||
|
hovered.now.clear();
|
||||||
let position_only = cursor.position_only();
|
let position_only = cursor.position_only();
|
||||||
let mut now: Vec<WidgetId> = Vec::new();
|
let region_of = |id| self.window_region(&id);
|
||||||
let region_of = |id| Some(self.active.get(&id)?.region.to_px(window_size));
|
|
||||||
|
|
||||||
for layer in self.layers.indices().rev() {
|
for layer in self.layers.indices().rev() {
|
||||||
let mut consumed = false;
|
let mut consumed = false;
|
||||||
@@ -194,8 +201,8 @@ impl SensorUi for UiRenderState {
|
|||||||
if !cursor.exists || !region.contains(cursor.pos) {
|
if !cursor.exists || !region.contains(cursor.pos) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
now.push(id);
|
hovered.now.push(id);
|
||||||
let hover = match was.contains(&id) {
|
let hover = match hovered.was.contains(&id) {
|
||||||
true => ActivationState::On,
|
true => ActivationState::On,
|
||||||
false => ActivationState::Start,
|
false => ActivationState::Start,
|
||||||
};
|
};
|
||||||
@@ -216,17 +223,18 @@ impl SensorUi for UiRenderState {
|
|||||||
// Whatever the cursor was inside and is not now, whether it left or a
|
// Whatever the cursor was inside and is not now, whether it left or a
|
||||||
// layer above took the input before the walk reached it. A widget that
|
// layer above took the input before the walk reached it. A widget that
|
||||||
// stopped being drawn has no region to report and is simply dropped.
|
// stopped being drawn has no region to report and is simply dropped.
|
||||||
for id in was {
|
for &id in &hovered.was {
|
||||||
if !now.contains(&id)
|
if !hovered.now.contains(&id)
|
||||||
&& let Some(region) = region_of(id)
|
&& let Some(region) = region_of(id)
|
||||||
{
|
{
|
||||||
deliver(self, rsc, state, id, ActivationState::End, &cursor, region);
|
deliver(self, rsc, state, id, ActivationState::End, &cursor, region);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
std::mem::swap(&mut hovered.was, &mut hovered.now);
|
||||||
|
|
||||||
let senses = rsc.events_mut().get_type::<CursorSense>();
|
let senses = rsc.events_mut().get_type::<CursorSense>();
|
||||||
senses.active = active;
|
senses.active = active;
|
||||||
senses.global = now;
|
senses.global = hovered;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-34
@@ -1,11 +1,5 @@
|
|||||||
use iris_core::HasState;
|
use iris_core::HasState;
|
||||||
use std::{
|
use std::{pin::Pin, sync::Arc};
|
||||||
pin::Pin,
|
|
||||||
sync::{
|
|
||||||
Arc,
|
|
||||||
mpsc::{Receiver as SyncReceiver, Sender as SyncSender, channel as sync_channel},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
runtime::Runtime,
|
runtime::Runtime,
|
||||||
sync::mpsc::{
|
sync::mpsc::{
|
||||||
@@ -13,64 +7,52 @@ use tokio::{
|
|||||||
unbounded_channel as async_channel,
|
unbounded_channel as async_channel,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use winit::window::Window;
|
|
||||||
|
|
||||||
pub type TaskMsgSender<Rsc> = SyncSender<Box<dyn TaskUpdate<Rsc>>>;
|
|
||||||
pub type TaskMsgReceiver<Rsc> = SyncReceiver<Box<dyn TaskUpdate<Rsc>>>;
|
|
||||||
|
|
||||||
pub trait TaskUpdate<Rsc: HasState>: FnOnce(&mut Rsc::State, &mut Rsc) + Send {}
|
pub trait TaskUpdate<Rsc: HasState>: FnOnce(&mut Rsc::State, &mut Rsc) + Send {}
|
||||||
impl<F: FnOnce(&mut Rsc::State, &mut Rsc) + Send, Rsc: HasState> TaskUpdate<Rsc> for F {}
|
impl<F: FnOnce(&mut Rsc::State, &mut Rsc) + Send, Rsc: HasState> TaskUpdate<Rsc> for F {}
|
||||||
|
|
||||||
|
/// Hands an update from a task to the thread that owns the ui. Delivery and
|
||||||
|
/// waking are one act: a host posts the update as a message its loop already
|
||||||
|
/// carries, so nothing has to wake the loop separately, or claim a redraw to
|
||||||
|
/// be looked at.
|
||||||
|
pub trait TaskQueue<Rsc: HasState>: Send + Sync + 'static {
|
||||||
|
fn send(&self, update: Box<dyn TaskUpdate<Rsc>>);
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Tasks<Rsc: HasState> {
|
pub struct Tasks<Rsc: HasState> {
|
||||||
start: AsyncSender<BoxTask>,
|
start: AsyncSender<BoxTask>,
|
||||||
window: Arc<Window>,
|
queue: Arc<dyn TaskQueue<Rsc>>,
|
||||||
msg_send: SyncSender<Box<dyn TaskUpdate<Rsc>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TaskCtx<Rsc: HasState> {
|
pub struct TaskCtx<Rsc: HasState> {
|
||||||
send: TaskMsgSender<Rsc>,
|
queue: Arc<dyn TaskQueue<Rsc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Rsc: HasState> TaskCtx<Rsc> {
|
impl<Rsc: HasState> TaskCtx<Rsc> {
|
||||||
pub fn update(&mut self, f: impl TaskUpdate<Rsc> + 'static) {
|
pub fn update(&mut self, f: impl TaskUpdate<Rsc> + 'static) {
|
||||||
let _ = self.send.send(Box::new(f));
|
self.queue.send(Box::new(f));
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<Rsc: HasState + 'static> TaskCtx<Rsc> {
|
|
||||||
fn new(send: TaskMsgSender<Rsc>) -> Self {
|
|
||||||
Self { send }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type BoxTask = Pin<Box<dyn Future<Output = ()> + Send>>;
|
type BoxTask = Pin<Box<dyn Future<Output = ()> + Send>>;
|
||||||
|
|
||||||
impl<Rsc: HasState> Tasks<Rsc> {
|
impl<Rsc: HasState> Tasks<Rsc> {
|
||||||
pub fn init(window: Arc<Window>) -> (Self, TaskMsgReceiver<Rsc>) {
|
pub fn init(queue: Arc<dyn TaskQueue<Rsc>>) -> Self {
|
||||||
let (start, start_recv) = async_channel();
|
let (start, start_recv) = async_channel();
|
||||||
let (msgs, msgs_recv) = sync_channel();
|
|
||||||
std::thread::spawn(|| {
|
std::thread::spawn(|| {
|
||||||
let rt = Runtime::new().unwrap();
|
let rt = Runtime::new().unwrap();
|
||||||
rt.block_on(listen(start_recv))
|
rt.block_on(listen(start_recv))
|
||||||
});
|
});
|
||||||
(
|
Self { start, queue }
|
||||||
Self {
|
|
||||||
start,
|
|
||||||
msg_send: msgs,
|
|
||||||
window,
|
|
||||||
},
|
|
||||||
msgs_recv,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawn<F: AsyncFnOnce(TaskCtx<Rsc>) + 'static + std::marker::Send>(&mut self, task: F)
|
pub fn spawn<F: AsyncFnOnce(TaskCtx<Rsc>) + 'static + std::marker::Send>(&mut self, task: F)
|
||||||
where
|
where
|
||||||
F::CallOnceFuture: Send,
|
F::CallOnceFuture: Send,
|
||||||
{
|
{
|
||||||
let send = self.msg_send.clone();
|
let queue = self.queue.clone();
|
||||||
let window = self.window.clone();
|
|
||||||
let _ = self.start.send(Box::pin(async move {
|
let _ = self.start.send(Box::pin(async move {
|
||||||
task(TaskCtx::new(send)).await;
|
task(TaskCtx { queue }).await;
|
||||||
window.request_redraw();
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+180
@@ -0,0 +1,180 @@
|
|||||||
|
//! A ui with no window: build a tree, run frames, move a pointer, and read
|
||||||
|
//! back where widgets landed.
|
||||||
|
//!
|
||||||
|
//! It does not draw. A claim about pixels still needs a real surface.
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
use std::{
|
||||||
|
sync::{
|
||||||
|
Arc,
|
||||||
|
mpsc::{Receiver, SyncSender, sync_channel},
|
||||||
|
},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// There is no loop here to post to, so updates queue until the test asks
|
||||||
|
/// for them.
|
||||||
|
struct Queue(SyncSender<Box<dyn TaskUpdate<DefaultRsc<HarnessState>>>>);
|
||||||
|
|
||||||
|
impl TaskQueue<DefaultRsc<HarnessState>> for Queue {
|
||||||
|
fn send(&self, update: Box<dyn TaskUpdate<DefaultRsc<HarnessState>>>) {
|
||||||
|
let _ = self.0.send(update);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `assert_eq!` for where a frame put a widget, written as its two corners.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! assert_corners {
|
||||||
|
($harness:expr, $id:expr, ($x0:expr, $y0:expr), ($x1:expr, $y1:expr)) => {
|
||||||
|
assert_eq!(
|
||||||
|
$harness.region(&$id).expect("widget drew nothing"),
|
||||||
|
$crate::core::PixelRegion {
|
||||||
|
top_left: $crate::core::util::Vec2::new($x0 as f32, $y0 as f32),
|
||||||
|
bot_right: $crate::core::util::Vec2::new($x1 as f32, $y1 as f32),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub use crate::assert_corners;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct HarnessState {
|
||||||
|
pub root: Option<StrongWidget>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasRoot for HarnessState {
|
||||||
|
fn set_root(&mut self, root: StrongWidget) {
|
||||||
|
self.root = Some(root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Harness {
|
||||||
|
pub rsc: DefaultRsc<HarnessState>,
|
||||||
|
pub render: UiRenderState,
|
||||||
|
pub state: HarnessState,
|
||||||
|
updates: Receiver<Box<dyn TaskUpdate<DefaultRsc<HarnessState>>>>,
|
||||||
|
cursor: CursorState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Harness {
|
||||||
|
/// `size` is the output in physical pixels.
|
||||||
|
pub fn new(size: impl Into<Vec2>) -> Self {
|
||||||
|
// A `TaskQueue` must be `Sync`, which `mpsc::Sender` is not; the
|
||||||
|
// bound that comes with `SyncSender` is far past anything a test
|
||||||
|
// leaves unread.
|
||||||
|
let (send, updates) = sync_channel(1024);
|
||||||
|
let rsc = DefaultRsc::init(Arc::new(Queue(send)));
|
||||||
|
let mut render = UiRenderState::new();
|
||||||
|
render.resize(size);
|
||||||
|
Self {
|
||||||
|
rsc,
|
||||||
|
render,
|
||||||
|
state: HarnessState::default(),
|
||||||
|
updates,
|
||||||
|
cursor: CursorState::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> Vec2 {
|
||||||
|
self.render.output_size()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||||
|
self.render.resize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the root and lays it out, so a pointer event has something to hit.
|
||||||
|
pub fn set_root<T>(&mut self, widget: impl WidgetLike<DefaultRsc<HarnessState>, T>) {
|
||||||
|
widget.set_root(&mut self.rsc, &mut self.state);
|
||||||
|
self.frame();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn needs_redraw(&self) -> bool {
|
||||||
|
self.render
|
||||||
|
.needs_redraw(&self.state.root, self.rsc.widgets())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn apply_updates(&mut self) -> usize {
|
||||||
|
let mut applied = 0;
|
||||||
|
while let Ok(update) = self.updates.try_recv() {
|
||||||
|
update(&mut self.state, &mut self.rsc);
|
||||||
|
applied += 1;
|
||||||
|
}
|
||||||
|
applied
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Waits for a task's first update, then applies everything waiting.
|
||||||
|
/// False if none arrived in time.
|
||||||
|
#[must_use]
|
||||||
|
pub fn await_update(&mut self, timeout: Duration) -> bool {
|
||||||
|
let Ok(update) = self.updates.recv_timeout(timeout) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
update(&mut self.state, &mut self.rsc);
|
||||||
|
self.apply_updates();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Lays the tree out and builds its primitives.
|
||||||
|
pub fn frame(&mut self) {
|
||||||
|
self.apply_updates();
|
||||||
|
self.render.update(&self.state.root, &mut self.rsc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the last frame put a widget, or `None` if it drew nothing.
|
||||||
|
pub fn region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||||
|
self.render.window_region(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_to(&mut self, pos: impl Into<Vec2>) {
|
||||||
|
self.cursor.pos = pos.into();
|
||||||
|
self.cursor.exists = true;
|
||||||
|
self.sense();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn leave(&mut self) {
|
||||||
|
self.cursor.exists = false;
|
||||||
|
self.sense();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn press(&mut self, button: CursorButton) {
|
||||||
|
self.button(button).update(true);
|
||||||
|
self.sense();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(&mut self, button: CursorButton) {
|
||||||
|
self.button(button).update(false);
|
||||||
|
self.sense();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wheel carries no position, so this goes wherever the cursor was last
|
||||||
|
/// moved to -- nowhere, until it has been moved.
|
||||||
|
pub fn scroll(&mut self, delta: impl Into<Vec2>) {
|
||||||
|
self.cursor.scroll_delta = delta.into();
|
||||||
|
self.sense();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn click(&mut self, pos: impl Into<Vec2>) {
|
||||||
|
self.move_to(pos);
|
||||||
|
self.press(CursorButton::Left);
|
||||||
|
self.release(CursorButton::Left);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn button(&mut self, button: CursorButton) -> &mut ActivationState {
|
||||||
|
let buttons = &mut self.cursor.buttons;
|
||||||
|
match button {
|
||||||
|
CursorButton::Left => &mut buttons.left,
|
||||||
|
CursorButton::Middle => &mut buttons.middle,
|
||||||
|
CursorButton::Right => &mut buttons.right,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dispatches against the layout of the last frame, which is what a
|
||||||
|
/// window delivers input against too.
|
||||||
|
fn sense(&mut self) {
|
||||||
|
let cursor = self.cursor.clone();
|
||||||
|
self.render
|
||||||
|
.run_sensors(&mut self.rsc, &mut self.state, cursor);
|
||||||
|
self.cursor.end_frame();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
pub mod default;
|
pub mod default;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
|
pub mod harness;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
|
|
||||||
pub use iris_core as core;
|
pub use iris_core as core;
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
//! Where a frame puts things, with no window to put them in.
|
||||||
|
|
||||||
|
use iris::harness::{Harness, assert_corners};
|
||||||
|
use iris::prelude::*;
|
||||||
|
|
||||||
|
/// A fixed 100 wide, and the rest of the 400 to its neighbour.
|
||||||
|
fn two_rects(h: &mut Harness) -> (WidgetId, WidgetId) {
|
||||||
|
let left = rect(Color::RED).width(100).add(&mut h.rsc);
|
||||||
|
let right = rect(Color::BLUE).add(&mut h.rsc);
|
||||||
|
h.set_root((left, right).span(Dir::RIGHT));
|
||||||
|
(left.id(), right.id())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_span_gives_each_child_the_width_it_asked_for() {
|
||||||
|
let mut h = Harness::new((400, 200));
|
||||||
|
let (left, right) = two_rects(&mut h);
|
||||||
|
|
||||||
|
assert_corners!(h, left, (0, 0), (100, 200));
|
||||||
|
assert_corners!(h, right, (100, 0), (400, 200));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn resizing_relays_out_against_the_new_output() {
|
||||||
|
let mut h = Harness::new((400, 200));
|
||||||
|
let (left, right) = two_rects(&mut h);
|
||||||
|
|
||||||
|
h.resize((800, 100));
|
||||||
|
assert!(h.needs_redraw());
|
||||||
|
h.frame();
|
||||||
|
|
||||||
|
assert_corners!(h, left, (0, 0), (100, 100));
|
||||||
|
assert_corners!(h, right, (100, 0), (800, 100));
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
//! Which widget an input reaches.
|
||||||
|
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
use iris::harness::Harness;
|
||||||
|
use iris::prelude::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_press_reaches_only_the_widget_under_the_cursor() {
|
||||||
|
let mut h = Harness::new((400, 200));
|
||||||
|
let clicks = Rc::new(RefCell::new(Vec::new()));
|
||||||
|
|
||||||
|
let (on_left, on_right) = (clicks.clone(), clicks.clone());
|
||||||
|
let left = rect(Color::RED)
|
||||||
|
.width(100)
|
||||||
|
.on(CursorSense::click(), move |_, _| {
|
||||||
|
on_left.borrow_mut().push("left")
|
||||||
|
})
|
||||||
|
.add(&mut h.rsc);
|
||||||
|
let right = rect(Color::BLUE)
|
||||||
|
.on(CursorSense::click(), move |_, _| {
|
||||||
|
on_right.borrow_mut().push("right")
|
||||||
|
})
|
||||||
|
.add(&mut h.rsc);
|
||||||
|
h.set_root((left, right).span(Dir::RIGHT));
|
||||||
|
|
||||||
|
h.click((50, 100));
|
||||||
|
assert_eq!(*clicks.borrow(), ["left"]);
|
||||||
|
|
||||||
|
h.click((300, 100));
|
||||||
|
assert_eq!(*clicks.borrow(), ["left", "right"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hover_ends_when_the_cursor_leaves_the_window() {
|
||||||
|
let mut h = Harness::new((400, 200));
|
||||||
|
let hovered = Rc::new(RefCell::new(0));
|
||||||
|
let ended = Rc::new(RefCell::new(0));
|
||||||
|
|
||||||
|
let (h_count, e_count) = (hovered.clone(), ended.clone());
|
||||||
|
let widget = rect(Color::RED)
|
||||||
|
.on(CursorSense::HoverStart, move |_, _| {
|
||||||
|
*h_count.borrow_mut() += 1
|
||||||
|
})
|
||||||
|
.on(CursorSense::HoverEnd, move |_, _| {
|
||||||
|
*e_count.borrow_mut() += 1
|
||||||
|
})
|
||||||
|
.add(&mut h.rsc);
|
||||||
|
h.set_root(widget);
|
||||||
|
|
||||||
|
h.move_to((200, 100));
|
||||||
|
assert_eq!((*hovered.borrow(), *ended.borrow()), (1, 0));
|
||||||
|
|
||||||
|
// A second sample inside the same widget is not a second hover.
|
||||||
|
h.move_to((210, 100));
|
||||||
|
assert_eq!((*hovered.borrow(), *ended.borrow()), (1, 0));
|
||||||
|
|
||||||
|
h.leave();
|
||||||
|
assert_eq!((*hovered.borrow(), *ended.borrow()), (1, 1));
|
||||||
|
}
|
||||||
+92
-234
@@ -1,45 +1,10 @@
|
|||||||
//! Input across layers: what stops at a layer, what passes through it, and
|
//! Input across layers: what stops at a layer, what passes through it, and
|
||||||
//! where hovering stops. These drive `run_sensors` directly, which needs no
|
//! where hovering stops.
|
||||||
//! GPU and no window.
|
|
||||||
|
|
||||||
use iris::prelude::*;
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
struct SenseRsc {
|
use iris::harness::Harness;
|
||||||
ui: UiData,
|
use iris::prelude::*;
|
||||||
events: EventManager<SenseRsc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UiRsc for SenseRsc {
|
|
||||||
fn ui(&self) -> &UiData {
|
|
||||||
&self.ui
|
|
||||||
}
|
|
||||||
fn ui_mut(&mut self) -> &mut UiData {
|
|
||||||
&mut self.ui
|
|
||||||
}
|
|
||||||
fn on_draw(&mut self, active: &ActiveData) {
|
|
||||||
self.events.draw(active);
|
|
||||||
}
|
|
||||||
fn on_undraw(&mut self, active: &ActiveData) {
|
|
||||||
self.events.undraw(active);
|
|
||||||
}
|
|
||||||
fn on_remove(&mut self, id: WidgetId) {
|
|
||||||
self.events.remove(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasState for SenseRsc {
|
|
||||||
type State = ();
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasEvents for SenseRsc {
|
|
||||||
fn events(&self) -> &EventManager<Self> {
|
|
||||||
&self.events
|
|
||||||
}
|
|
||||||
fn events_mut(&mut self) -> &mut EventManager<Self> {
|
|
||||||
&mut self.events
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const WINDOW: f32 = 100.0;
|
const WINDOW: f32 = 100.0;
|
||||||
|
|
||||||
@@ -53,88 +18,36 @@ impl Fired {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Ui {
|
/// A widget filling whatever it is given, recording the senses it is sent.
|
||||||
rsc: SenseRsc,
|
fn listener(h: &mut Harness, senses: impl Into<CursorSenses>) -> (WeakWidget<Rect>, Fired) {
|
||||||
render: UiRenderState,
|
|
||||||
state: (),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ui {
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
rsc: SenseRsc {
|
|
||||||
ui: UiData::default(),
|
|
||||||
events: EventManager::default(),
|
|
||||||
},
|
|
||||||
render: UiRenderState::new(),
|
|
||||||
state: (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn listen<W: Widget + ?core::marker::Sized + 'static>(
|
|
||||||
&mut self,
|
|
||||||
widget: &StrongWidget<W>,
|
|
||||||
senses: impl Into<CursorSenses>,
|
|
||||||
) -> Fired {
|
|
||||||
let fired = Fired::default();
|
let fired = Fired::default();
|
||||||
let sink = fired.clone();
|
let record = fired.clone();
|
||||||
self.rsc
|
let id = rect(Color::WHITE)
|
||||||
.register_event(widget.weak(), senses.into(), move |ctx, _rsc| {
|
.on(senses.into(), move |ctx, _| {
|
||||||
sink.0.borrow_mut().push(ctx.data.sense)
|
record.0.borrow_mut().push(ctx.data.sense)
|
||||||
});
|
|
||||||
fired
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stacks the widgets bottom first, each on its own layer, and lays them
|
|
||||||
/// out in a square window.
|
|
||||||
fn stack(&mut self, children: Vec<StrongWidget>) {
|
|
||||||
let root = self
|
|
||||||
.rsc
|
|
||||||
.ui
|
|
||||||
.widgets
|
|
||||||
.add_strong(Stack {
|
|
||||||
children,
|
|
||||||
size: StackSize::default(),
|
|
||||||
})
|
})
|
||||||
.any();
|
.add(&mut h.rsc);
|
||||||
self.render.resize((WINDOW, WINDOW));
|
(id, fired)
|
||||||
self.render.update(&root, &mut self.rsc);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cursor(&mut self, at: (f32, f32)) -> CursorState {
|
|
||||||
CursorState {
|
|
||||||
pos: at.into(),
|
|
||||||
exists: true,
|
|
||||||
buttons: Default::default(),
|
|
||||||
scroll_delta: Vec2::ZERO,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(&mut self, cursor: CursorState) {
|
|
||||||
self.render.run_sensors(
|
|
||||||
&mut self.rsc,
|
|
||||||
&mut self.state,
|
|
||||||
cursor,
|
|
||||||
(WINDOW, WINDOW).into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full(ui: &mut Ui) -> StrongWidget<Rect> {
|
/// A widget with no senses of its own, to leave a gap beside one that has.
|
||||||
rect(UiColor::WHITE).add_strong(&mut ui.rsc)
|
fn blank(h: &mut Harness) -> WeakWidget<Rect> {
|
||||||
|
rect(Color::WHITE).add(&mut h.rsc)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn harness() -> Harness {
|
||||||
|
Harness::new((WINDOW, WINDOW))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hover_stops_at_the_topmost_widget() {
|
fn hover_stops_at_the_topmost_widget() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let (bottom, middle, top) = (full(ui), full(ui), full(ui));
|
let (bottom, bottom_hover) = listener(&mut h, CursorSense::HoverStart);
|
||||||
let bottom_hover = ui.listen(&bottom, CursorSense::HoverStart);
|
let (middle, middle_hover) = listener(&mut h, CursorSense::HoverStart);
|
||||||
let middle_hover = ui.listen(&middle, CursorSense::HoverStart);
|
let (top, top_hover) = listener(&mut h, CursorSense::HoverStart);
|
||||||
let top_hover = ui.listen(&top, CursorSense::HoverStart);
|
h.set_root((bottom, middle, top).stack());
|
||||||
ui.stack(vec![bottom.any(), middle.any(), top.any()]);
|
|
||||||
|
|
||||||
let cursor = ui.cursor((50.0, 50.0));
|
h.move_to((50, 50));
|
||||||
ui.run(cursor);
|
|
||||||
|
|
||||||
assert_eq!(top_hover.take(), [CursorSense::HoverStart]);
|
assert_eq!(top_hover.take(), [CursorSense::HoverStart]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -147,16 +60,14 @@ fn hover_stops_at_the_topmost_widget() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_scroll_passes_through_every_widget_that_does_not_want_it() {
|
fn a_scroll_passes_through_every_widget_that_does_not_want_it() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let (list, button, overlay) = (full(ui), full(ui), full(ui));
|
let (list, scrolled) = listener(&mut h, CursorSense::Scroll);
|
||||||
let scrolled = ui.listen(&list, CursorSense::Scroll);
|
let (button, clicked) = listener(&mut h, CursorSense::click());
|
||||||
let clicked = ui.listen(&button, CursorSense::click());
|
let (overlay, overlay_clicked) = listener(&mut h, CursorSense::click());
|
||||||
let overlay_clicked = ui.listen(&overlay, CursorSense::click());
|
h.set_root((list, button, overlay).stack());
|
||||||
ui.stack(vec![list.any(), button.any(), overlay.any()]);
|
|
||||||
|
|
||||||
let mut cursor = ui.cursor((50.0, 50.0));
|
h.move_to((50, 50));
|
||||||
cursor.scroll_delta = (0.0, 10.0).into();
|
h.scroll((0, 10));
|
||||||
ui.run(cursor);
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
scrolled.take(),
|
scrolled.take(),
|
||||||
@@ -168,16 +79,32 @@ fn a_scroll_passes_through_every_widget_that_does_not_want_it() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn only_the_topmost_listener_takes_a_press() {
|
fn hovering_a_button_above_does_not_stop_a_later_scroll() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let (below, above) = (full(ui), full(ui));
|
let (list, scrolled) = listener(&mut h, CursorSense::Scroll);
|
||||||
let below_clicked = ui.listen(&below, CursorSense::click());
|
let (button, _clicked) = listener(&mut h, CursorSense::click());
|
||||||
let above_clicked = ui.listen(&above, CursorSense::click());
|
h.set_root((list, button).stack());
|
||||||
ui.stack(vec![below.any(), above.any()]);
|
|
||||||
|
|
||||||
let mut cursor = ui.cursor((50.0, 50.0));
|
// The hover arrives in its own frame, as a window delivers it.
|
||||||
cursor.buttons.left = ActivationState::Start;
|
h.move_to((50, 50));
|
||||||
ui.run(cursor);
|
assert_eq!(scrolled.take(), []);
|
||||||
|
|
||||||
|
h.scroll((0, 10));
|
||||||
|
assert_eq!(
|
||||||
|
scrolled.take(),
|
||||||
|
[CursorSense::Scroll],
|
||||||
|
"a hover already resting on the button must not consume the wheel"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn only_the_topmost_listener_takes_a_press() {
|
||||||
|
let mut h = harness();
|
||||||
|
let (below, below_clicked) = listener(&mut h, CursorSense::click());
|
||||||
|
let (above, above_clicked) = listener(&mut h, CursorSense::click());
|
||||||
|
h.set_root((below, above).stack());
|
||||||
|
|
||||||
|
h.click((50, 50));
|
||||||
|
|
||||||
assert_eq!(above_clicked.take(), [CursorSense::click()]);
|
assert_eq!(above_clicked.take(), [CursorSense::click()]);
|
||||||
assert_eq!(below_clicked.take(), [], "one press goes to one widget");
|
assert_eq!(below_clicked.take(), [], "one press goes to one widget");
|
||||||
@@ -185,29 +112,18 @@ fn only_the_topmost_listener_takes_a_press() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_press_beside_the_button_reaches_the_layer_below() {
|
fn a_press_beside_the_button_reaches_the_layer_below() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let list = full(ui);
|
let (list, list_clicked) = listener(&mut h, CursorSense::click());
|
||||||
// The row above the list covers it, but only its left half is the button.
|
// The row above the list covers it, but only its left half is the button.
|
||||||
let button = full(ui);
|
let (button, button_clicked) = listener(&mut h, CursorSense::click());
|
||||||
let gap = full(ui);
|
let row = (button, blank(&mut h)).span(Dir::RIGHT).add(&mut h.rsc);
|
||||||
let list_clicked = ui.listen(&list, CursorSense::click());
|
h.set_root((list, row).stack());
|
||||||
let button_clicked = ui.listen(&button, CursorSense::click());
|
|
||||||
let row = ui.rsc.ui.widgets.add_strong(Span {
|
|
||||||
children: vec![button.any(), gap.any()],
|
|
||||||
dir: Dir::RIGHT,
|
|
||||||
gap: 0.0,
|
|
||||||
});
|
|
||||||
ui.stack(vec![list.any(), row.any()]);
|
|
||||||
|
|
||||||
let mut on_button = ui.cursor((20.0, 50.0));
|
h.click((20, 50));
|
||||||
on_button.buttons.left = ActivationState::Start;
|
|
||||||
ui.run(on_button);
|
|
||||||
assert_eq!(button_clicked.take(), [CursorSense::click()]);
|
assert_eq!(button_clicked.take(), [CursorSense::click()]);
|
||||||
assert_eq!(list_clicked.take(), []);
|
assert_eq!(list_clicked.take(), []);
|
||||||
|
|
||||||
let mut beside_it = ui.cursor((80.0, 50.0));
|
h.click((80, 50));
|
||||||
beside_it.buttons.left = ActivationState::Start;
|
|
||||||
ui.run(beside_it);
|
|
||||||
assert_eq!(button_clicked.take(), [], "the cursor is not on the button");
|
assert_eq!(button_clicked.take(), [], "the cursor is not on the button");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
list_clicked.take(),
|
list_clicked.take(),
|
||||||
@@ -218,44 +134,32 @@ fn a_press_beside_the_button_reaches_the_layer_below() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn leaving_a_widget_still_ends_its_hover() {
|
fn leaving_a_widget_still_ends_its_hover() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let widget = full(ui);
|
let (widget, hover) = listener(&mut h, CursorSense::HoverStart | CursorSense::HoverEnd);
|
||||||
let hover = ui.listen(&widget, CursorSense::HoverStart | CursorSense::HoverEnd);
|
h.set_root(widget);
|
||||||
ui.stack(vec![widget.any()]);
|
|
||||||
|
|
||||||
let cursor = ui.cursor((50.0, 50.0));
|
h.move_to((50, 50));
|
||||||
ui.run(cursor);
|
|
||||||
assert_eq!(hover.take(), [CursorSense::HoverStart]);
|
assert_eq!(hover.take(), [CursorSense::HoverStart]);
|
||||||
|
|
||||||
let mut gone = ui.cursor((50.0, 50.0));
|
h.leave();
|
||||||
gone.exists = false;
|
|
||||||
ui.run(gone);
|
|
||||||
assert_eq!(hover.take(), [CursorSense::HoverEnd]);
|
assert_eq!(hover.take(), [CursorSense::HoverEnd]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn leaving_a_widget_does_not_block_the_layer_below() {
|
fn leaving_a_widget_does_not_block_the_layer_below() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let below = full(ui);
|
let (below, below_hover) = listener(&mut h, CursorSense::HoverStart);
|
||||||
// Only the left half of the layer above is a widget, so the cursor can
|
// Only the left half of the layer above is a widget, so the cursor can
|
||||||
// leave it without leaving the one underneath.
|
// leave it without leaving the one underneath.
|
||||||
let (above, gap) = (full(ui), full(ui));
|
let (above, above_hover) = listener(&mut h, CursorSense::HoverStart | CursorSense::HoverEnd);
|
||||||
let below_hover = ui.listen(&below, CursorSense::HoverStart);
|
let row = (above, blank(&mut h)).span(Dir::RIGHT).add(&mut h.rsc);
|
||||||
let above_hover = ui.listen(&above, CursorSense::HoverStart | CursorSense::HoverEnd);
|
h.set_root((below, row).stack());
|
||||||
let row = ui.rsc.ui.widgets.add_strong(Span {
|
|
||||||
children: vec![above.any(), gap.any()],
|
|
||||||
dir: Dir::RIGHT,
|
|
||||||
gap: 0.0,
|
|
||||||
});
|
|
||||||
ui.stack(vec![below.any(), row.any()]);
|
|
||||||
|
|
||||||
let on_above = ui.cursor((20.0, 50.0));
|
h.move_to((20, 50));
|
||||||
ui.run(on_above);
|
|
||||||
assert_eq!(above_hover.take(), [CursorSense::HoverStart]);
|
assert_eq!(above_hover.take(), [CursorSense::HoverStart]);
|
||||||
assert_eq!(below_hover.take(), [], "the layer above is over it");
|
assert_eq!(below_hover.take(), [], "the layer above is over it");
|
||||||
|
|
||||||
let beside_it = ui.cursor((80.0, 50.0));
|
h.move_to((80, 50));
|
||||||
ui.run(beside_it);
|
|
||||||
assert_eq!(above_hover.take(), [CursorSense::HoverEnd]);
|
assert_eq!(above_hover.take(), [CursorSense::HoverEnd]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
below_hover.take(),
|
below_hover.take(),
|
||||||
@@ -264,53 +168,18 @@ fn leaving_a_widget_does_not_block_the_layer_below() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn hovering_a_button_above_does_not_stop_a_later_scroll() {
|
|
||||||
let ui = &mut Ui::new();
|
|
||||||
let (list, button) = (full(ui), full(ui));
|
|
||||||
let scrolled = ui.listen(&list, CursorSense::Scroll);
|
|
||||||
let clicked = ui.listen(&button, CursorSense::click());
|
|
||||||
ui.stack(vec![list.any(), button.any()]);
|
|
||||||
|
|
||||||
// The hover arrives in its own frame, as a window delivers it.
|
|
||||||
let hover = ui.cursor((50.0, 50.0));
|
|
||||||
ui.run(hover);
|
|
||||||
assert_eq!(scrolled.take(), []);
|
|
||||||
|
|
||||||
let mut wheel = ui.cursor((50.0, 50.0));
|
|
||||||
wheel.scroll_delta = (0.0, 10.0).into();
|
|
||||||
ui.run(wheel);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
scrolled.take(),
|
|
||||||
[CursorSense::Scroll],
|
|
||||||
"a hover already resting on the button must not consume the wheel"
|
|
||||||
);
|
|
||||||
assert_eq!(clicked.take(), []);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn covering_a_widget_ends_its_hover() {
|
fn covering_a_widget_ends_its_hover() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
let below = full(ui);
|
let (below, below_hover) = listener(&mut h, CursorSense::HoverStart | CursorSense::HoverEnd);
|
||||||
// Only the left half of the layer above is a widget, so the cursor can
|
let (above, above_hover) = listener(&mut h, CursorSense::HoverStart);
|
||||||
// start beside it and then move onto it.
|
let row = (above, blank(&mut h)).span(Dir::RIGHT).add(&mut h.rsc);
|
||||||
let (above, gap) = (full(ui), full(ui));
|
h.set_root((below, row).stack());
|
||||||
let below_hover = ui.listen(&below, CursorSense::HoverStart | CursorSense::HoverEnd);
|
|
||||||
let above_hover = ui.listen(&above, CursorSense::HoverStart);
|
|
||||||
let row = ui.rsc.ui.widgets.add_strong(Span {
|
|
||||||
children: vec![above.any(), gap.any()],
|
|
||||||
dir: Dir::RIGHT,
|
|
||||||
gap: 0.0,
|
|
||||||
});
|
|
||||||
ui.stack(vec![below.any(), row.any()]);
|
|
||||||
|
|
||||||
let beside_it = ui.cursor((80.0, 50.0));
|
h.move_to((80, 50));
|
||||||
ui.run(beside_it);
|
|
||||||
assert_eq!(below_hover.take(), [CursorSense::HoverStart]);
|
assert_eq!(below_hover.take(), [CursorSense::HoverStart]);
|
||||||
|
|
||||||
let onto_above = ui.cursor((20.0, 50.0));
|
h.move_to((20, 50));
|
||||||
ui.run(onto_above);
|
|
||||||
assert_eq!(above_hover.take(), [CursorSense::HoverStart]);
|
assert_eq!(above_hover.take(), [CursorSense::HoverStart]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
below_hover.take(),
|
below_hover.take(),
|
||||||
@@ -318,8 +187,7 @@ fn covering_a_widget_ends_its_hover() {
|
|||||||
"a widget covered by one that took the input is no longer hovered"
|
"a widget covered by one that took the input is no longer hovered"
|
||||||
);
|
);
|
||||||
|
|
||||||
let off_again = ui.cursor((80.0, 50.0));
|
h.move_to((80, 50));
|
||||||
ui.run(off_again);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
below_hover.take(),
|
below_hover.take(),
|
||||||
[CursorSense::HoverStart],
|
[CursorSense::HoverStart],
|
||||||
@@ -329,36 +197,26 @@ fn covering_a_widget_ends_its_hover() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hover_starts_and_ends_once_each() {
|
fn hover_starts_and_ends_once_each() {
|
||||||
let ui = &mut Ui::new();
|
let mut h = harness();
|
||||||
// Only the left half is the widget, so the cursor can leave it without
|
// Only the left half is the widget, so the cursor can leave it without
|
||||||
// leaving the window.
|
// leaving the window.
|
||||||
let (widget, gap) = (full(ui), full(ui));
|
let (widget, hover) = listener(&mut h, CursorSense::HoverStart | CursorSense::HoverEnd);
|
||||||
let hover = ui.listen(&widget, CursorSense::HoverStart | CursorSense::HoverEnd);
|
let row = (widget, blank(&mut h)).span(Dir::RIGHT).add(&mut h.rsc);
|
||||||
let row = ui.rsc.ui.widgets.add_strong(Span {
|
h.set_root(row);
|
||||||
children: vec![widget.any(), gap.any()],
|
|
||||||
dir: Dir::RIGHT,
|
|
||||||
gap: 0.0,
|
|
||||||
});
|
|
||||||
ui.stack(vec![row.any()]);
|
|
||||||
|
|
||||||
let inside = ui.cursor((20.0, 50.0));
|
h.move_to((20, 50));
|
||||||
ui.run(inside);
|
|
||||||
assert_eq!(hover.take(), [CursorSense::HoverStart]);
|
assert_eq!(hover.take(), [CursorSense::HoverStart]);
|
||||||
|
|
||||||
let further_in = ui.cursor((30.0, 50.0));
|
h.move_to((30, 50));
|
||||||
ui.run(further_in);
|
|
||||||
assert_eq!(hover.take(), [], "staying inside is not a second start");
|
assert_eq!(hover.take(), [], "staying inside is not a second start");
|
||||||
|
|
||||||
let outside = ui.cursor((80.0, 50.0));
|
h.move_to((80, 50));
|
||||||
ui.run(outside);
|
|
||||||
assert_eq!(hover.take(), [CursorSense::HoverEnd]);
|
assert_eq!(hover.take(), [CursorSense::HoverEnd]);
|
||||||
|
|
||||||
let further_out = ui.cursor((90.0, 50.0));
|
h.move_to((90, 50));
|
||||||
ui.run(further_out);
|
|
||||||
assert_eq!(hover.take(), [], "an ended hover does not end again");
|
assert_eq!(hover.take(), [], "an ended hover does not end again");
|
||||||
|
|
||||||
let back_inside = ui.cursor((20.0, 50.0));
|
h.move_to((20, 50));
|
||||||
ui.run(back_inside);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
hover.take(),
|
hover.take(),
|
||||||
[CursorSense::HoverStart],
|
[CursorSense::HoverStart],
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
//! Scrolling moves content and stops at its ends.
|
||||||
|
|
||||||
|
use iris::harness::{Harness, assert_corners};
|
||||||
|
use iris::prelude::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_wheel_scrolls_the_content_and_stops_at_its_end() {
|
||||||
|
let mut h = Harness::new((400, 200));
|
||||||
|
// Twice the window's height, so there is 200 to scroll.
|
||||||
|
let top = rect(Color::RED).height(200).add(&mut h.rsc);
|
||||||
|
let bottom = rect(Color::BLUE).height(200).add(&mut h.rsc);
|
||||||
|
h.set_root((top, bottom).span(Dir::DOWN).scrollable());
|
||||||
|
h.move_to((200, 100));
|
||||||
|
|
||||||
|
// `Scroll` starts snapped to the end.
|
||||||
|
assert_corners!(h, top, (0, -200), (400, 0));
|
||||||
|
|
||||||
|
// The handler scales a wheel line by 50.
|
||||||
|
h.scroll((0, 1));
|
||||||
|
h.frame();
|
||||||
|
assert_corners!(h, top, (0, -150), (400, 50));
|
||||||
|
|
||||||
|
h.scroll((0, 10));
|
||||||
|
h.frame();
|
||||||
|
assert_corners!(h, top, (0, 0), (400, 200));
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
//! What a background task can change, and how it gets back to the ui.
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use iris::harness::Harness;
|
||||||
|
use iris::prelude::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_task_update_reaches_the_tree() {
|
||||||
|
let mut h = Harness::new((400, 200));
|
||||||
|
let widget = rect(Color::RED).add(&mut h.rsc);
|
||||||
|
h.set_root(widget.task_on(CursorSense::click(), async move |mut ctx| {
|
||||||
|
ctx.update(move |_, rsc| widget(rsc).color = Color::BLUE);
|
||||||
|
}));
|
||||||
|
|
||||||
|
h.click((200, 100));
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
h.await_update(Duration::from_secs(5)),
|
||||||
|
"the task sent no update"
|
||||||
|
);
|
||||||
|
assert_eq!(h.rsc[widget].color, Color::BLUE);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user