Merge branch 'spacing' into 'master'
Unhardcode spacing See merge request Librem5/squeekboard!176
This commit is contained in:
		@ -1,4 +1,7 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
 | 
					row_spacing: 11.33
 | 
				
			||||||
 | 
					button_spacing: 4.67
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds: { x: 0, y: 6.33, width: 426, height: 250 }
 | 
					bounds: { x: 0, y: 6.33, width: 426, height: 250 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
outlines:
 | 
					outlines:
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,7 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
 | 
					row_spacing: 11.33
 | 
				
			||||||
 | 
					button_spacing: 4.67
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds: { x: 0, y: 6.33, width: 410, height: 250 }
 | 
					bounds: { x: 0, y: 6.33, width: 410, height: 250 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
outlines:
 | 
					outlines:
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,7 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
 | 
					row_spacing: 11.33
 | 
				
			||||||
 | 
					button_spacing: 4.67
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds: { x: 0, y: 1, width: 360, height: 198 }
 | 
					bounds: { x: 0, y: 1, width: 360, height: 198 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
outlines:
 | 
					outlines:
 | 
				
			||||||
 | 
				
			|||||||
@ -170,6 +170,8 @@ fn load_layout_with_fallback(
 | 
				
			|||||||
#[derive(Debug, Deserialize, PartialEq)]
 | 
					#[derive(Debug, Deserialize, PartialEq)]
 | 
				
			||||||
#[serde(deny_unknown_fields)]
 | 
					#[serde(deny_unknown_fields)]
 | 
				
			||||||
pub struct Layout {
 | 
					pub struct Layout {
 | 
				
			||||||
 | 
					    row_spacing: f64,
 | 
				
			||||||
 | 
					    button_spacing: f64,
 | 
				
			||||||
    bounds: Bounds,
 | 
					    bounds: Bounds,
 | 
				
			||||||
    views: HashMap<String, Vec<ButtonIds>>,
 | 
					    views: HashMap<String, Vec<ButtonIds>>,
 | 
				
			||||||
    #[serde(default)] 
 | 
					    #[serde(default)] 
 | 
				
			||||||
@ -302,6 +304,10 @@ impl Layout {
 | 
				
			|||||||
                        width: self.bounds.width,
 | 
					                        width: self.bounds.width,
 | 
				
			||||||
                        height: self.bounds.height,
 | 
					                        height: self.bounds.height,
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
 | 
					                    spacing: ::layout::Spacing {
 | 
				
			||||||
 | 
					                        row: self.row_spacing,
 | 
				
			||||||
 | 
					                        button: self.button_spacing,
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
                    rows: view.iter().map(|row| {
 | 
					                    rows: view.iter().map(|row| {
 | 
				
			||||||
                        Box::new(::layout::Row {
 | 
					                        Box::new(::layout::Row {
 | 
				
			||||||
                            angle: 0,
 | 
					                            angle: 0,
 | 
				
			||||||
@ -499,6 +505,8 @@ mod tests {
 | 
				
			|||||||
                PathBuf::from("tests/layout.yaml")
 | 
					                PathBuf::from("tests/layout.yaml")
 | 
				
			||||||
            ).unwrap(),
 | 
					            ).unwrap(),
 | 
				
			||||||
            Layout {
 | 
					            Layout {
 | 
				
			||||||
 | 
					                row_spacing: 0f64,
 | 
				
			||||||
 | 
					                button_spacing: 0f64,
 | 
				
			||||||
                bounds: Bounds { x: 0f64, y: 0f64, width: 0f64, height: 0f64 },
 | 
					                bounds: Bounds { x: 0f64, y: 0f64, width: 0f64, height: 0f64 },
 | 
				
			||||||
                views: hashmap!(
 | 
					                views: hashmap!(
 | 
				
			||||||
                    "base".into() => vec!("test".into()),
 | 
					                    "base".into() => vec!("test".into()),
 | 
				
			||||||
 | 
				
			|||||||
@ -328,8 +328,8 @@ pub mod c {
 | 
				
			|||||||
                        .map(|button| button.bounds.clone())
 | 
					                        .map(|button| button.bounds.clone())
 | 
				
			||||||
                        .collect()
 | 
					                        .collect()
 | 
				
			||||||
                }).collect();
 | 
					                }).collect();
 | 
				
			||||||
 | 
					                let spacing = view.spacing.clone();
 | 
				
			||||||
                view.place_buttons_with_sizes(sizes);
 | 
					                view.place_buttons_with_sizes(sizes, spacing);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -440,6 +440,10 @@ pub mod c {
 | 
				
			|||||||
                        x: 0f64, y: 0f64,
 | 
					                        x: 0f64, y: 0f64,
 | 
				
			||||||
                        width: 0f64, height: 0f64
 | 
					                        width: 0f64, height: 0f64
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
 | 
					                    spacing: Spacing {
 | 
				
			||||||
 | 
					                        button: 0f64,
 | 
				
			||||||
 | 
					                        row: 0f64,
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
                    rows: vec!(row),
 | 
					                    rows: vec!(row),
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -459,6 +463,10 @@ pub mod c {
 | 
				
			|||||||
                        x: 0f64, y: 0f64,
 | 
					                        x: 0f64, y: 0f64,
 | 
				
			||||||
                        width: 0f64, height: 0f64
 | 
					                        width: 0f64, height: 0f64
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
 | 
					                    spacing: Spacing {
 | 
				
			||||||
 | 
					                        button: 0f64,
 | 
				
			||||||
 | 
					                        row: 0f64,
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
                    rows: Vec::new(),
 | 
					                    rows: Vec::new(),
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
                assert_eq!(
 | 
					                assert_eq!(
 | 
				
			||||||
@ -567,10 +575,6 @@ pub struct Button {
 | 
				
			|||||||
    pub state: Rc<RefCell<KeyState>>,
 | 
					    pub state: Rc<RefCell<KeyState>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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
 | 
					/// The graphical representation of a row of buttons
 | 
				
			||||||
pub struct Row {
 | 
					pub struct Row {
 | 
				
			||||||
    pub buttons: Vec<Box<Button>>,
 | 
					    pub buttons: Vec<Box<Button>>,
 | 
				
			||||||
@ -597,7 +601,7 @@ impl Row {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    fn calculate_button_positions(outlines: Vec<c::Bounds>)
 | 
					    fn calculate_button_positions(outlines: Vec<c::Bounds>, button_spacing: f64)
 | 
				
			||||||
        -> Vec<c::Bounds>
 | 
					        -> Vec<c::Bounds>
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        let mut x_offset = 0f64;
 | 
					        let mut x_offset = 0f64;
 | 
				
			||||||
@ -607,7 +611,7 @@ impl Row {
 | 
				
			|||||||
                x: x_offset,
 | 
					                x: x_offset,
 | 
				
			||||||
                ..outline.clone()
 | 
					                ..outline.clone()
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            x_offset += outline.width + BUTTON_SPACING;
 | 
					            x_offset += outline.width + button_spacing;
 | 
				
			||||||
            position
 | 
					            position
 | 
				
			||||||
        }).collect()
 | 
					        }).collect()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -646,9 +650,16 @@ impl Row {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Clone, Debug)]
 | 
				
			||||||
 | 
					pub struct Spacing {
 | 
				
			||||||
 | 
					    pub row: f64,
 | 
				
			||||||
 | 
					    pub button: f64,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct View {
 | 
					pub struct View {
 | 
				
			||||||
    /// Position relative to keyboard origin
 | 
					    /// Position relative to keyboard origin
 | 
				
			||||||
    pub bounds: c::Bounds,
 | 
					    pub bounds: c::Bounds,
 | 
				
			||||||
 | 
					    pub spacing: Spacing,
 | 
				
			||||||
    pub rows: Vec<Box<Row>>,
 | 
					    pub rows: Vec<Box<Row>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -660,7 +671,9 @@ impl View {
 | 
				
			|||||||
    /// and derive a scaling factor that lets contents fit into view)
 | 
					    /// and derive a scaling factor that lets contents fit into view)
 | 
				
			||||||
    /// (or TODO: blow up view bounds to match contents
 | 
					    /// (or TODO: blow up view bounds to match contents
 | 
				
			||||||
    /// and then scale the entire thing)
 | 
					    /// and then scale the entire thing)
 | 
				
			||||||
    fn calculate_row_positions(&self, sizes: Vec<Size>) -> Vec<c::Bounds> {
 | 
					    fn calculate_row_positions(&self, sizes: Vec<Size>, row_spacing: f64)
 | 
				
			||||||
 | 
					        -> Vec<c::Bounds>
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
        let mut y_offset = self.bounds.y;
 | 
					        let mut y_offset = self.bounds.y;
 | 
				
			||||||
        sizes.into_iter().map(|size| {
 | 
					        sizes.into_iter().map(|size| {
 | 
				
			||||||
            let position = c::Bounds {
 | 
					            let position = c::Bounds {
 | 
				
			||||||
@ -669,7 +682,7 @@ impl View {
 | 
				
			|||||||
                width: size.width,
 | 
					                width: size.width,
 | 
				
			||||||
                height: size.height,
 | 
					                height: size.height,
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            y_offset += size.height + ROW_SPACING;
 | 
					            y_offset += size.height + row_spacing;
 | 
				
			||||||
            position
 | 
					            position
 | 
				
			||||||
        }).collect()
 | 
					        }).collect()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -678,19 +691,23 @@ impl View {
 | 
				
			|||||||
    /// The view itself will not be affected by the sizes
 | 
					    /// The view itself will not be affected by the sizes
 | 
				
			||||||
    fn place_buttons_with_sizes(
 | 
					    fn place_buttons_with_sizes(
 | 
				
			||||||
        &mut self,
 | 
					        &mut self,
 | 
				
			||||||
        button_outlines: Vec<Vec<c::Bounds>>
 | 
					        button_outlines: Vec<Vec<c::Bounds>>,
 | 
				
			||||||
 | 
					        spacing: Spacing,
 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
        // Determine all positions
 | 
					        // Determine all positions
 | 
				
			||||||
        let button_positions: Vec<_>
 | 
					        let button_positions: Vec<_>
 | 
				
			||||||
            = button_outlines.into_iter()
 | 
					            = button_outlines.into_iter()
 | 
				
			||||||
                .map(Row::calculate_button_positions)
 | 
					                .map(|outlines| {
 | 
				
			||||||
 | 
					                    Row::calculate_button_positions(outlines, spacing.button)
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
                .collect();
 | 
					                .collect();
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        let row_sizes = button_positions.iter()
 | 
					        let row_sizes = button_positions.iter()
 | 
				
			||||||
            .map(Row::calculate_row_size)
 | 
					            .map(Row::calculate_row_size)
 | 
				
			||||||
            .collect();
 | 
					            .collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let row_positions = self.calculate_row_positions(row_sizes);
 | 
					        let row_positions
 | 
				
			||||||
 | 
					            = self.calculate_row_positions(row_sizes, spacing.row);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Apply all positions
 | 
					        // Apply all positions
 | 
				
			||||||
        for ((mut row, row_position), button_positions)
 | 
					        for ((mut row, row_position), button_positions)
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,7 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
 | 
					row_spacing: 0
 | 
				
			||||||
 | 
					button_spacing: 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds:
 | 
					bounds:
 | 
				
			||||||
    x: 0
 | 
					    x: 0
 | 
				
			||||||
    y: 0
 | 
					    y: 0
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,8 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
# missing views
 | 
					# missing views
 | 
				
			||||||
 | 
					row_spacing: 0
 | 
				
			||||||
 | 
					button_spacing: 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds:
 | 
					bounds:
 | 
				
			||||||
    x: 0
 | 
					    x: 0
 | 
				
			||||||
    y: 0
 | 
					    y: 0
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,8 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
# extra field
 | 
					# extra field
 | 
				
			||||||
 | 
					row_spacing: 0
 | 
				
			||||||
 | 
					button_spacing: 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds:
 | 
					bounds:
 | 
				
			||||||
    x: 0
 | 
					    x: 0
 | 
				
			||||||
    y: 0
 | 
					    y: 0
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,8 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
# punctuation
 | 
					# punctuation
 | 
				
			||||||
 | 
					row_spacing: 0
 | 
				
			||||||
 | 
					button_spacing: 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds:
 | 
					bounds:
 | 
				
			||||||
    x: 0
 | 
					    x: 0
 | 
				
			||||||
    y: 0
 | 
					    y: 0
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,8 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
# punctuation
 | 
					# punctuation
 | 
				
			||||||
 | 
					row_spacing: 0
 | 
				
			||||||
 | 
					button_spacing: 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bounds:
 | 
					bounds:
 | 
				
			||||||
    x: 0
 | 
					    x: 0
 | 
				
			||||||
    y: 0
 | 
					    y: 0
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user