layout: Center views relative to each other and the layout bounds

This commit is contained in:
Dorota Czaplejewicz
2020-02-02 17:52:02 +00:00
parent 782d80a007
commit 40b79f6209
4 changed files with 72 additions and 35 deletions

View File

@ -329,11 +329,8 @@ pub mod c {
Point { x: x_widget, y: y_widget }
);
let state = {
let view = layout.get_current_view();
view.find_button_by_position(point)
.map(|place| place.button.state.clone())
};
let state = layout.find_button_by_position(point)
.map(|place| place.button.state.clone());
if let Some(state) = state {
seat::handle_press_key(
@ -374,8 +371,7 @@ pub mod c {
let pressed = layout.pressed_keys.clone();
let button_info = {
let view = layout.get_current_view();
let place = view.find_button_by_position(point);
let place = layout.find_button_by_position(point);
place.map(|place| {(
place.button.state.clone(),
place.button.clone(),
@ -552,14 +548,14 @@ impl View {
})
}
fn get_width(&self) -> f64 {
pub fn get_width(&self) -> f64 {
// No need to call `get_rows()`,
// as the biggest row is the most far reaching in both directions
// because they are all centered.
find_max_double(self.rows.iter(), |(_offset, row)| row.get_width())
}
fn get_height(&self) -> f64 {
pub fn get_height(&self) -> f64 {
self.rows.iter().next_back()
.map(|(y_offset, row)| row.get_height() + y_offset)
.unwrap_or(0.0)
@ -576,6 +572,21 @@ impl View {
row,
)}).collect()
}
/// Returns a size which contains all the views
/// if they are all centered on the same point.
pub fn calculate_super_size(views: Vec<&View>) -> Size {
Size {
height: find_max_double(
views.iter(),
|view| view.get_height(),
),
width: find_max_double(
views.iter(),
|view| view.get_width(),
),
}
}
}
/// The physical characteristic of layout for the purpose of styling
@ -603,7 +614,8 @@ pub struct Layout {
// Views own the actual buttons which have state
// Maybe they should own UI only,
// and keys should be owned by a dedicated non-UI-State?
pub views: HashMap<String, View>,
/// Point is the offset within the layout
pub views: HashMap<String, (c::Point, View)>,
// Non-UI stuff
/// xkb keymap applicable to the contained keys. Unchangeable
@ -622,7 +634,8 @@ pub struct Layout {
/// A builder structure for picking up layout data from storage
pub struct LayoutData {
pub views: HashMap<String, View>,
/// Point is the offset within layout
pub views: HashMap<String, (c::Point, View)>,
pub keymap_str: CString,
pub margins: Margins,
}
@ -653,8 +666,13 @@ impl Layout {
}
}
pub fn get_current_view_position(&self) -> &(c::Point, View) {
&self.views
.get(&self.current_view).expect("Selected nonexistent view")
}
pub fn get_current_view(&self) -> &View {
self.views.get(&self.current_view).expect("Selected nonexistent view")
&self.get_current_view_position().1
}
fn set_view(&mut self, view: String) -> Result<(), NoSuchView> {
@ -668,16 +686,9 @@ impl Layout {
/// Calculates size without margins
fn calculate_inner_size(&self) -> Size {
Size {
height: find_max_double(
self.views.iter(),
|(_name, view)| view.get_height(),
),
width: find_max_double(
self.views.iter(),
|(_name, view)| view.get_width(),
),
}
View::calculate_super_size(
self.views.iter().map(|(_, (_offset, v))| v).collect()
)
}
/// Size including margins
@ -712,6 +723,11 @@ impl Layout {
scale: 1.0,
})
}
fn find_button_by_position(&self, point: c::Point) -> Option<ButtonPlace> {
let (offset, layout) = self.get_current_view_position();
layout.find_button_by_position(point - offset)
}
}
mod procedures {
@ -1068,7 +1084,7 @@ mod test {
bottom: 1.0,
},
views: hashmap! {
String::new() => view,
String::new() => (c::Point { x: 0.0, y: 0.0 }, view),
},
};
assert_eq!(