use crate::drawing::{Drawable, Plan};
use crate::layouts::{HasPlanner, Layout};
use skia_safe::{Canvas, Rect};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum RowVerticalAlignment {
Top,
Center,
Bottom,
}
#[derive(Clone)]
pub struct RowLayoutElement {
pub drawable: Rc<dyn Drawable>,
pub alignment: RowVerticalAlignment,
}
pub struct RowLayout {
w: RefCell<Option<f32>>,
h: RefCell<Option<f32>>,
pub childs: RefCell<BTreeMap<String, RowLayoutElement>>,
pub planner: RefCell<Option<Rc<dyn Layout>>>,
calculated_rects: RefCell<BTreeMap<usize, Rect>>,
}
impl Default for RowLayout {
fn default() -> Self {
Self::new()
}
}
impl RowLayout {
pub fn new() -> Self {
Self {
w: RefCell::new(None),
h: RefCell::new(None),
childs: RefCell::new(BTreeMap::new()),
planner: RefCell::new(None),
calculated_rects: RefCell::new(BTreeMap::new()),
}
}
pub fn set_w(&self, w: Option<f32>) {
*self.w.borrow_mut() = w;
}
pub fn set_h(&self, h: Option<f32>) {
*self.h.borrow_mut() = h;
}
pub fn add_child(self: &Rc<Self>, id: &str, e: RowLayoutElement) {
e.drawable.set_planner(Some(self.clone()));
self.childs.borrow_mut().insert(id.to_string(), e);
}
pub fn remove_child(&self, id: &str) {
if let Some(c) = self.lookup_child(id) {
c.drawable.set_planner(None);
self.childs.borrow_mut().remove(id);
}
}
pub fn lookup_child(&self, id: &str) -> Option<RowLayoutElement> {
self.childs.borrow().get(id).cloned()
}
}
impl HasPlanner for RowLayout {
fn planner(&self) -> &RefCell<Option<Rc<dyn Layout>>> {
&self.planner
}
fn set_planner(&self, p: Option<Rc<dyn Layout>>) {
*self.planner.borrow_mut() = p;
}
}
impl Drawable for RowLayout {
fn draw(&self, canvas: &Canvas) -> anyhow::Result<()> {
let parent_area = self.get_planned_drawing_area(canvas);
let parent_w = parent_area.width();
let parent_h = parent_area.height();
if parent_w <= 0.0 || parent_h <= 0.0 {
return Ok(());
}
let childs_borrow = self.childs.borrow();
let mut total_fit_w = 0.0;
let mut fill_count = 0;
let mut none_count = 0;
for el in childs_borrow.values() {
match el.drawable.get_horizontal_plan() {
Plan::Fit(w) => total_fit_w += w,
Plan::Fill => fill_count += 1,
Plan::None => none_count += 1,
}
}
if total_fit_w > parent_w {
anyhow::bail!(
"RowLayout Error: Total requested width of 'Fit' children ({}) exceeds available container width ({})",
total_fit_w,
parent_w
);
}
let remaining_w = parent_w - total_fit_w;
let fill_allocated_w = if fill_count > 0 {
remaining_w / fill_count as f32
} else {
0.0
};
let none_allocated_w = if fill_count == 0 && none_count > 0 {
remaining_w / none_count as f32
} else {
0.0
};
{
let mut current_x = parent_area.left;
let mut rects = self.calculated_rects.borrow_mut();
rects.clear();
for el in childs_borrow.values() {
let target_ptr = el.drawable.as_ref() as *const _ as *const () as usize;
let child_w = match el.drawable.get_horizontal_plan() {
Plan::Fit(w) => w,
Plan::Fill => fill_allocated_w,
Plan::None => none_allocated_w,
};
let child_h = match el.drawable.get_vertical_plan() {
Plan::Fit(h) => h,
_ => parent_h, };
let child_y = match el.alignment {
RowVerticalAlignment::Top => parent_area.top,
RowVerticalAlignment::Center => parent_area.top + (parent_h - child_h) / 2.0,
RowVerticalAlignment::Bottom => parent_area.bottom - child_h,
};
let child_rect = Rect::from_xywh(current_x, child_y, child_w, child_h);
rects.insert(target_ptr, child_rect);
current_x += child_w;
}
}
for (key, el) in childs_borrow.iter() {
if let Err(e) = el.drawable.draw(canvas) {
println!("RowLayout draw error at child {}: {:?}", key, e);
}
}
Ok(())
}
fn get_horizontal_plan(&self) -> Plan {
match *self.w.borrow() {
Some(w) => Plan::Fit(w),
None => Plan::Fill,
}
}
fn get_vertical_plan(&self) -> Plan {
match *self.h.borrow() {
Some(h) => Plan::Fit(h),
None => Plan::Fill,
}
}
}
impl Layout for RowLayout {
fn measure_child(&self, c: &dyn Drawable, _canvas: &Canvas) -> Rect {
let target_ptr = c as *const _ as *const () as usize;
self.calculated_rects
.borrow()
.get(&target_ptr)
.cloned()
.unwrap_or_else(|| Rect::new(0.0, 0.0, 0.0, 0.0))
}
}