Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
noxware committed Jun 20, 2024
1 parent ad6d644 commit e596461
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
15 changes: 7 additions & 8 deletions hframe-core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl<P: Platform> Context<P> {
_ => {}
}
}
todo!()
}
}

Expand All @@ -46,17 +45,14 @@ mod tests {
composed_area::{ComposedAreaKind, ComposedAreaState, ComposedHtml},
geo::{Pos, Rect, Size},
id::Id,
platform,
};

use super::*;

#[test]
fn it_works() {
let platform = TestPlatform::new();

let mut ctx = Context {
platform: platform.clone(),
platform: TestPlatform::new(),
world: World::new(Rect::from((0.0, 0.0, 100.0, 100.0))),
pointer_pos: Pos::new(0.0, 0.0),
};
Expand Down Expand Up @@ -91,17 +87,20 @@ mod tests {
assert_eq!(data.value.id, Id::root());
});

platform.set_mouse_pos(Pos::new(15.0, 15.0));
ctx.platform.move_pointer_to(Pos::new(15.0, 15.0));
ctx.sync();
ctx.get_hovered_area().unwrap().read(|data| {
assert_eq!(data.value.id, Id::from("grandchild"));
});

platform.set_mouse_pos(Pos::new(30.0, 30.0));
ctx.platform.move_pointer_to(Pos::new(30.0, 30.0));
ctx.sync();
ctx.get_hovered_area().unwrap().read(|data| {
assert_eq!(data.value.id, Id::from("grandchild"));
});

platform.set_mouse_pos(Pos::new(31.0, 31.0));
ctx.platform.move_pointer_to(Pos::new(31.0, 31.0));
ctx.sync();
ctx.get_hovered_area().unwrap().read(|data| {
assert_eq!(data.value.id, Id::from("child"));
});
Expand Down
2 changes: 1 addition & 1 deletion hframe-core/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub(crate) enum PlatformEvent {
}

pub(crate) trait Platform {
fn events(&self) -> impl Iterator<Item = impl AsRef<PlatformEvent>>;
fn events(&self) -> impl Iterator<Item = &PlatformEvent>;
fn clear_events(&mut self);
}
30 changes: 8 additions & 22 deletions hframe-core/src/test_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,26 @@ use crate::{
platform::{Platform, PlatformEvent},
};

pub(crate) struct TestPlatformInner {
pub(crate) events: Vec<PlatformEvent>,
}

pub(crate) struct TestPlatform(Rc<RefCell<TestPlatformInner>>);

impl Clone for TestPlatform {
fn clone(&self) -> Self {
TestPlatform(Rc::clone(&self.0))
}
pub(crate) struct TestPlatform {
events: Vec<PlatformEvent>,
}

impl Platform for TestPlatform {
fn clear_events(&mut self) {
self.0.borrow_mut().events.clear();
self.events.clear();
}

fn events(&self) -> impl Iterator<Item = impl AsRef<PlatformEvent>> {
self.0
.borrow()
.events
.iter()
.map(|e| e as &dyn AsRef<PlatformEvent>)
fn events(&self) -> impl Iterator<Item = &PlatformEvent> {
self.events.iter()
}
}

impl TestPlatform {
pub(crate) fn new() -> Self {
TestPlatform(Rc::new(RefCell::new(TestPlatformInner {
events: Vec::new(),
})))
Self { events: Vec::new() }
}

pub(crate) fn set_mouse_pos(&self, mouse_pos: Pos) {
self.0.borrow_mut().mouse_pos = mouse_pos;
pub(crate) fn move_pointer_to(&mut self, pos: Pos) {
self.events.push(PlatformEvent::PointerMove(pos));
}
}

0 comments on commit e596461

Please sign in to comment.