Skip to content

Commit

Permalink
Remember mouse position
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Oct 12, 2024
1 parent 148e79a commit 24f29d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions crates/egui_kittest/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl HarnessBuilder {
/// Set the size of the window.
#[inline]
pub fn with_size(mut self, size: impl Into<Vec2>) -> Self {
let size = size.into();
self.screen_rect.set_width(size.x);
self.screen_rect.set_height(size.y);
self
Expand Down
75 changes: 39 additions & 36 deletions crates/egui_kittest/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,48 @@ impl EventState {
pub fn kittest_event_to_egui(&mut self, event: kittest::Event) -> Option<egui::Event> {
match event {
kittest::Event::ActionRequest(e) => Some(Event::AccessKitActionRequest(e)),
kittest::Event::Simulated(e) => {
match e {
SimulatedEvent::CursorMoved { position } => Some(Event::PointerMoved(
Pos2::new(position.x as f32, position.y as f32),
)),
SimulatedEvent::MouseInput { state, button } => pointer_button_to_egui(button)
.map(|button| PointerButton {
button,
modifiers: self.modifiers,
pos: self.last_mouse_pos,
pressed: matches!(state, ElementState::Pressed),
}),
SimulatedEvent::Ime(text) => Some(Event::Text(text)),
SimulatedEvent::KeyInput { state, key } => {
match key {
kittest::Key::Alt => {
self.modifiers.alt = matches!(state, ElementState::Pressed);
}
kittest::Key::Command => {
self.modifiers.command = matches!(state, ElementState::Pressed);
}
kittest::Key::Control => {
self.modifiers.ctrl = matches!(state, ElementState::Pressed);
}
kittest::Key::Shift => {
self.modifiers.shift = matches!(state, ElementState::Pressed);
}
_ => {}
kittest::Event::Simulated(e) => match e {
SimulatedEvent::CursorMoved { position } => {
self.last_mouse_pos = Pos2::new(position.x as f32, position.y as f32);
Some(Event::PointerMoved(Pos2::new(
position.x as f32,
position.y as f32,
)))
}
SimulatedEvent::MouseInput { state, button } => {
pointer_button_to_egui(button).map(|button| PointerButton {
button,
modifiers: self.modifiers,
pos: self.last_mouse_pos,
pressed: matches!(state, ElementState::Pressed),
})
}
SimulatedEvent::Ime(text) => Some(Event::Text(text)),
SimulatedEvent::KeyInput { state, key } => {
match key {
kittest::Key::Alt => {
self.modifiers.alt = matches!(state, ElementState::Pressed);
}
kittest::Key::Command => {
self.modifiers.command = matches!(state, ElementState::Pressed);
}
kittest::Key::Control => {
self.modifiers.ctrl = matches!(state, ElementState::Pressed);
}
kittest::Key::Shift => {
self.modifiers.shift = matches!(state, ElementState::Pressed);
}
kittest_key_to_egui(key).map(|key| Event::Key {
key,
modifiers: self.modifiers,
pressed: matches!(state, ElementState::Pressed),
repeat: false,
physical_key: None,
})
_ => {}
}
kittest_key_to_egui(key).map(|key| Event::Key {
key,
modifiers: self.modifiers,
pressed: matches!(state, ElementState::Pressed),
repeat: false,
physical_key: None,
})
}
}
},
}
}
}
Expand Down

0 comments on commit 24f29d6

Please sign in to comment.