Dynamic spaced rows

This commit is contained in:
2026-07-15 00:39:35 +02:00
parent b5470a9f05
commit 5820fce49f
4 changed files with 69 additions and 23 deletions
+3 -9
View File
@@ -3,10 +3,7 @@ use crate::unit::{Rect, Size};
#[derive(Debug, Clone)]
pub enum LayoutItem {
Rows { unit: Size, items: Vec<LayoutItem> },
SpacedRows { unit: Size, items: Vec<LayoutItem> },
Columns { unit: Size, items: Vec<LayoutItem> },
SpacedColumns { unit: Size, items: Vec<LayoutItem> },
Frame { padding: u16, item: Box<LayoutItem> },
Widget(Size),
@@ -21,10 +18,7 @@ pub struct Allocation {
impl LayoutItem {
pub fn allocate(&self, alloc: &Rect, full_alloc: &mut Allocation) {
match self {
Self::Rows { items, .. }
| Self::Columns { items, .. }
| Self::SpacedRows { items, .. }
| Self::SpacedColumns { items, .. } => {
Self::Rows { items, .. } | Self::Columns { items, .. } => {
full_alloc.frame.push(*alloc);
let is_row = matches!(self, Self::Rows { .. });
@@ -93,8 +87,8 @@ impl LayoutItem {
pub fn get_size<'a>(&'a self) -> &'a Size {
match self {
Self::Rows { unit, .. } | Self::SpacedRows { unit, .. } => unit,
Self::Columns { unit, .. } | Self::SpacedColumns { unit, .. } => unit,
Self::Rows { unit, .. } => unit,
Self::Columns { unit, .. } => unit,
Self::Widget(unit) => unit,
Self::Frame { item, .. } => item.get_size(),
}
+7
View File
@@ -1,4 +1,5 @@
pub mod outline;
pub mod spaced;
use crate::render::RenderScope;
@@ -30,3 +31,9 @@ display_widget!(u64);
display_widget!(u128);
display_widget!(f32);
display_widget!(f64);
impl Widget for &dyn Widget {
fn render(&self, scope: &mut RenderScope) {
(*self).render(scope);
}
}
+49
View File
@@ -0,0 +1,49 @@
use crate::{layout::LayoutItem, unit::Size::Percent, widget::Widget};
pub struct SpacedRows(pub Vec<(LayoutItem, Box<dyn Widget>)>);
impl Widget for SpacedRows {
fn render(&self, scope: &mut crate::render::RenderScope) {
let mut alloc = scope.rect.allocate(&crate::layout::LayoutItem::Rows {
unit: Percent(100),
items: self.0.iter().map(|v| v.0.clone()).collect(),
});
for i in 0..alloc.widgets.len() {
let item = &self.0[i];
if i > 0 {
let rect = &mut alloc.widgets[i];
scope.draw_text((0, rect.y - 1), "─".repeat(rect.width as usize));
}
alloc.draw(i, &*item.1);
}
}
}
pub struct SpacedColumns(pub Vec<(LayoutItem, Box<dyn Widget>)>);
impl Widget for SpacedColumns {
fn render(&self, scope: &mut crate::render::RenderScope) {
let mut alloc = scope.rect.allocate(&crate::layout::LayoutItem::Columns {
unit: Percent(100),
items: self.0.iter().map(|v| v.0.clone()).collect(),
});
for i in 0..alloc.widgets.len() {
let item = &self.0[i];
if i > 0 {
let rect = &mut alloc.widgets[i];
for i in 0..rect.height {
scope.draw_text((rect.x - 1, i), "│");
}
rect.x += 1;
}
alloc.draw(i, &*item.1);
}
}
}