From 4c0f23c5c10b12b1cad0e52b12c43098f6d685a2 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Wed, 25 Sep 2019 18:43:59 +0000 Subject: [PATCH 1/2] layout: Unhardcode button and row spacing values They are specified by each layout now --- data/keyboards/nb.yaml | 3 +++ data/keyboards/number.yaml | 3 +++ data/keyboards/us.yaml | 3 +++ src/data.rs | 8 ++++++++ src/layout.rs | 41 +++++++++++++++++++++++++++----------- tests/layout.yaml | 3 +++ tests/layout2.yaml | 3 +++ tests/layout3.yaml | 3 +++ tests/layout_key1.yaml | 3 +++ tests/layout_key2.yaml | 3 +++ 10 files changed, 61 insertions(+), 12 deletions(-) diff --git a/data/keyboards/nb.yaml b/data/keyboards/nb.yaml index 20627d48..1b27e645 100644 --- a/data/keyboards/nb.yaml +++ b/data/keyboards/nb.yaml @@ -1,4 +1,7 @@ --- +row_spacing: 11.33 +button_spacing: 4.67 + bounds: { x: 0, y: 6.33, width: 426, height: 250 } outlines: diff --git a/data/keyboards/number.yaml b/data/keyboards/number.yaml index 0530975f..19b62600 100644 --- a/data/keyboards/number.yaml +++ b/data/keyboards/number.yaml @@ -1,4 +1,7 @@ --- +row_spacing: 11.33 +button_spacing: 4.67 + bounds: { x: 0, y: 6.33, width: 410, height: 250 } outlines: diff --git a/data/keyboards/us.yaml b/data/keyboards/us.yaml index 13d8bafe..6988fc62 100644 --- a/data/keyboards/us.yaml +++ b/data/keyboards/us.yaml @@ -1,4 +1,7 @@ --- +row_spacing: 11.33 +button_spacing: 4.67 + bounds: { x: 0, y: 1, width: 360, height: 198 } outlines: diff --git a/src/data.rs b/src/data.rs index f72fa4e4..0fb1f280 100644 --- a/src/data.rs +++ b/src/data.rs @@ -170,6 +170,8 @@ fn load_layout_with_fallback( #[derive(Debug, Deserialize, PartialEq)] #[serde(deny_unknown_fields)] pub struct Layout { + row_spacing: f64, + button_spacing: f64, bounds: Bounds, views: HashMap>, #[serde(default)] @@ -302,6 +304,10 @@ impl Layout { width: self.bounds.width, height: self.bounds.height, }, + spacing: ::layout::Spacing { + row: self.row_spacing, + button: self.button_spacing, + }, rows: view.iter().map(|row| { Box::new(::layout::Row { angle: 0, @@ -499,6 +505,8 @@ mod tests { PathBuf::from("tests/layout.yaml") ).unwrap(), Layout { + row_spacing: 0f64, + button_spacing: 0f64, bounds: Bounds { x: 0f64, y: 0f64, width: 0f64, height: 0f64 }, views: hashmap!( "base".into() => vec!("test".into()), diff --git a/src/layout.rs b/src/layout.rs index 05074d5a..242d0d8f 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -329,7 +329,7 @@ pub mod c { .collect() }).collect(); - view.place_buttons_with_sizes(sizes); + view.place_buttons_with_sizes(sizes, view.spacing.clone()); } } @@ -440,6 +440,10 @@ pub mod c { x: 0f64, y: 0f64, width: 0f64, height: 0f64 }, + spacing: Spacing { + button: 0f64, + row: 0f64, + }, rows: vec!(row), }; @@ -459,6 +463,10 @@ pub mod c { x: 0f64, y: 0f64, width: 0f64, height: 0f64 }, + spacing: Spacing { + button: 0f64, + row: 0f64, + }, rows: Vec::new(), }; assert_eq!( @@ -567,10 +575,6 @@ pub struct Button { pub state: Rc>, } -// FIXME: derive from the style/margin/padding -const BUTTON_SPACING: f64 = 4.67; -const ROW_SPACING: f64 = 11.33; - /// The graphical representation of a row of buttons pub struct Row { pub buttons: Vec>, @@ -597,7 +601,7 @@ impl Row { } } - fn calculate_button_positions(outlines: Vec) + fn calculate_button_positions(outlines: Vec, button_spacing: f64) -> Vec { let mut x_offset = 0f64; @@ -607,7 +611,7 @@ impl Row { x: x_offset, ..outline.clone() }; - x_offset += outline.width + BUTTON_SPACING; + x_offset += outline.width + button_spacing; position }).collect() } @@ -646,9 +650,16 @@ impl Row { } } +#[derive(Clone, Debug)] +pub struct Spacing { + pub row: f64, + pub button: f64, +} + pub struct View { /// Position relative to keyboard origin pub bounds: c::Bounds, + pub spacing: Spacing, pub rows: Vec>, } @@ -660,7 +671,9 @@ impl View { /// and derive a scaling factor that lets contents fit into view) /// (or TODO: blow up view bounds to match contents /// and then scale the entire thing) - fn calculate_row_positions(&self, sizes: Vec) -> Vec { + fn calculate_row_positions(&self, sizes: Vec, row_spacing: f64) + -> Vec + { let mut y_offset = self.bounds.y; sizes.into_iter().map(|size| { let position = c::Bounds { @@ -669,7 +682,7 @@ impl View { width: size.width, height: size.height, }; - y_offset += size.height + ROW_SPACING; + y_offset += size.height + row_spacing; position }).collect() } @@ -678,19 +691,23 @@ impl View { /// The view itself will not be affected by the sizes fn place_buttons_with_sizes( &mut self, - button_outlines: Vec> + button_outlines: Vec>, + spacing: Spacing, ) { // Determine all positions let button_positions: Vec<_> = button_outlines.into_iter() - .map(Row::calculate_button_positions) + .map(|outlines| { + Row::calculate_button_positions(outlines, spacing.button) + }) .collect(); let row_sizes = button_positions.iter() .map(Row::calculate_row_size) .collect(); - let row_positions = self.calculate_row_positions(row_sizes); + let row_positions + = self.calculate_row_positions(row_sizes, spacing.row); // Apply all positions for ((mut row, row_position), button_positions) diff --git a/tests/layout.yaml b/tests/layout.yaml index d7ccc7b9..be25dc96 100644 --- a/tests/layout.yaml +++ b/tests/layout.yaml @@ -1,4 +1,7 @@ --- +row_spacing: 0 +button_spacing: 0 + bounds: x: 0 y: 0 diff --git a/tests/layout2.yaml b/tests/layout2.yaml index f8610ddb..ff37e73d 100644 --- a/tests/layout2.yaml +++ b/tests/layout2.yaml @@ -1,5 +1,8 @@ --- # missing views +row_spacing: 0 +button_spacing: 0 + bounds: x: 0 y: 0 diff --git a/tests/layout3.yaml b/tests/layout3.yaml index 8065d4d0..48d01740 100644 --- a/tests/layout3.yaml +++ b/tests/layout3.yaml @@ -1,5 +1,8 @@ --- # extra field +row_spacing: 0 +button_spacing: 0 + bounds: x: 0 y: 0 diff --git a/tests/layout_key1.yaml b/tests/layout_key1.yaml index 89de08c6..e33e0728 100644 --- a/tests/layout_key1.yaml +++ b/tests/layout_key1.yaml @@ -1,5 +1,8 @@ --- # punctuation +row_spacing: 0 +button_spacing: 0 + bounds: x: 0 y: 0 diff --git a/tests/layout_key2.yaml b/tests/layout_key2.yaml index a8372fc8..d07c2853 100644 --- a/tests/layout_key2.yaml +++ b/tests/layout_key2.yaml @@ -1,5 +1,8 @@ --- # punctuation +row_spacing: 0 +button_spacing: 0 + bounds: x: 0 y: 0 From 3bea256ca56179c0e7fb8a57481aad00e5c31cf9 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 26 Sep 2019 07:02:06 +0000 Subject: [PATCH 2/2] Appease Debian's Rust version's borrow checker --- src/layout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index 242d0d8f..f47dee3f 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -328,8 +328,8 @@ pub mod c { .map(|button| button.bounds.clone()) .collect() }).collect(); - - view.place_buttons_with_sizes(sizes, view.spacing.clone()); + let spacing = view.spacing.clone(); + view.place_buttons_with_sizes(sizes, spacing); } }