use std::collections::HashMap;
use crate::geometry::Rect;
use crate::layout::{CellId, Layout};
use super::Slot;
pub struct CompositionLayout {
pub(super) layout: Layout,
pub(super) regions: HashMap<String, HashMap<Box<str>, CellId>>,
}
impl CompositionLayout {
pub fn get(&self, patch_id: &str, region: impl Region) -> Option<Rect> {
let id = self.regions.get(patch_id)?.get(region.name())?;
self.layout.rect(*id)
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &str, Rect)> + '_ {
let layout = &self.layout;
self.regions.iter().flat_map(move |(id, by_region)| {
by_region.iter().filter_map(move |(region, cell_id)| {
layout.rect(*cell_id).map(|r| (id.as_str(), &**region, r))
})
})
}
pub fn layout(&self) -> &Layout {
&self.layout
}
pub fn translate(&mut self, dx: f64, dy: f64) {
self.layout.translate(dx, dy);
}
}
pub trait Region {
fn name(&self) -> &str;
}
impl Region for Slot {
fn name(&self) -> &str {
Slot::name(*self)
}
}
impl Region for &str {
fn name(&self) -> &str {
self
}
}
impl Region for String {
fn name(&self) -> &str {
self.as_str()
}
}
impl Region for &String {
fn name(&self) -> &str {
self.as_str()
}
}