Skip to content

Commit

Permalink
[Rust - libgui] Model a left-click-ended event
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Mar 27, 2024
1 parent 5441ec5 commit d5d04fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions rust_programs/libgui/src/ui_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub trait UIElement: Drawable + NestedLayerSlice {

fn handle_left_click(&self, _mouse_point: Point) {}

fn handle_left_click_up(&self, _mouse_point: Point) {}

fn handle_key_pressed(&self, _key: KeyCode) {}
fn handle_key_released(&self, _key: KeyCode) {}

Expand Down
25 changes: 21 additions & 4 deletions rust_programs/libgui/src/window_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl LikeLayerSlice for PixelLayerSlice {
match fill_mode {
FillMode::Filled => {
for line in
scanline_compute_fill_lines_from_edges(&polygon_stack.lines()).into_iter()
scanline_compute_fill_lines_from_edges(&polygon_stack.lines()).into_iter()
{
// Horizontal line?
if line.p1.y.round() == line.p2.y.round() {
Expand Down Expand Up @@ -340,9 +340,9 @@ impl PixelLayer {
size.height.try_into().unwrap(),
surface_texture,
)
.surface_texture_format(TextureFormat::Bgra8Unorm)
.build()
.unwrap()
.surface_texture_format(TextureFormat::Bgra8Unorm)
.build()
.unwrap()
};
pixel_buffer.render().unwrap();
Self {
Expand Down Expand Up @@ -519,6 +519,17 @@ impl AwmWindow {
}
}

fn left_click_up(&self, mouse_point: Point) {
// Note that we send this event to all elements, regardless of whether they bound the mouse
// when it's released. This helps to implement drag states where the element stays dragged
// even if the mouse leaves the element's frame.
for elem in self.ui_elements.borrow().iter() {
// Translate the mouse position to the element's coordinate system
let elem_pos = mouse_point - elem.frame().origin;
elem.handle_left_click_up(elem_pos);
}
}

fn mouse_exited(&self) {
let elems_containing_mouse = &mut *self.elements_containing_mouse.borrow_mut();
for elem in elems_containing_mouse.drain(..) {
Expand Down Expand Up @@ -569,6 +580,12 @@ impl AwmWindow {
}
_ => (),
},
ElementState::Released => match button {
MouseButton::Left => {
self_clone.left_click_up(last_cursor_pos);
}
_ => (),
}
_ => (),
}
}
Expand Down

0 comments on commit d5d04fd

Please sign in to comment.