From 74479ff2266f12b3c8e2f9149af0dd1ee0f44718 Mon Sep 17 00:00:00 2001 From: Benjamin Schaaf Date: Sat, 26 Sep 2020 01:37:23 +1000 Subject: [PATCH] Expand key press detection to the edges of the view's bounding box If you have a keyboard layout like the following: A B C D E F G H I J K The E and G keys here should be pressed when clicking in the empty space next to them. This is achieved by not checking the bounding boxes of each key and instead just using the button and row offset to extend buttons/rows to the edges of the view. Caching for the size and position of rows is introduced to simplify implementation and possibly improve performance. Fixes #191 --- src/data.rs | 18 ++-- src/layout.rs | 243 ++++++++++++++++++++++++++++++++------------------ src/tests.rs | 4 +- 3 files changed, 165 insertions(+), 100 deletions(-) diff --git a/src/data.rs b/src/data.rs index c3cf5345..2c3fd353 100644 --- a/src/data.rs +++ b/src/data.rs @@ -459,14 +459,14 @@ impl Layout { &mut warning_handler, )) }); - layout::Row { - buttons: add_offsets( + layout::Row::new( + add_offsets( buttons, |button| button.size.width, ).collect() - } + ) }); - let rows = add_offsets(rows, |row| row.get_height()) + let rows = add_offsets(rows, |row| row.get_size().height) .collect(); ( name.clone(), @@ -484,8 +484,8 @@ impl Layout { name, ( layout::c::Point { - x: (total_size.width - view.get_width()) / 2.0, - y: (total_size.height - view.get_height()) / 2.0, + x: (total_size.width - view.get_size().width) / 2.0, + y: (total_size.height - view.get_size().height) / 2.0, }, view, ), @@ -824,7 +824,7 @@ mod tests { assert_eq!( out.views["base"].1 .get_rows()[0].1 - .buttons[0].1 + .get_buttons()[0].1 .label, ::layout::Label::Text(CString::new("test").unwrap()) ); @@ -839,7 +839,7 @@ mod tests { assert_eq!( out.views["base"].1 .get_rows()[0].1 - .buttons[0].1 + .get_buttons()[0].1 .label, ::layout::Label::Text(CString::new("test").unwrap()) ); @@ -855,7 +855,7 @@ mod tests { assert_eq!( out.views["base"].1 .get_rows()[0].1 - .buttons[0].1 + .get_buttons()[0].1 .state.borrow() .keycodes.len(), 2 diff --git a/src/layout.rs b/src/layout.rs index b1cae8ee..2fa09112 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -493,38 +493,63 @@ pub struct Button { } /// The graphical representation of a row of buttons +#[derive(Clone, Debug)] pub struct Row { - /// Buttons together with their offset from the left - pub buttons: Vec<(f64, Box