Dynamic spaced rows
This commit is contained in:
@@ -3,10 +3,7 @@ use crate::unit::{Rect, Size};
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum LayoutItem {
|
pub enum LayoutItem {
|
||||||
Rows { unit: Size, items: Vec<LayoutItem> },
|
Rows { unit: Size, items: Vec<LayoutItem> },
|
||||||
SpacedRows { unit: Size, items: Vec<LayoutItem> },
|
|
||||||
|
|
||||||
Columns { unit: Size, items: Vec<LayoutItem> },
|
Columns { unit: Size, items: Vec<LayoutItem> },
|
||||||
SpacedColumns { unit: Size, items: Vec<LayoutItem> },
|
|
||||||
|
|
||||||
Frame { padding: u16, item: Box<LayoutItem> },
|
Frame { padding: u16, item: Box<LayoutItem> },
|
||||||
Widget(Size),
|
Widget(Size),
|
||||||
@@ -21,10 +18,7 @@ pub struct Allocation {
|
|||||||
impl LayoutItem {
|
impl LayoutItem {
|
||||||
pub fn allocate(&self, alloc: &Rect, full_alloc: &mut Allocation) {
|
pub fn allocate(&self, alloc: &Rect, full_alloc: &mut Allocation) {
|
||||||
match self {
|
match self {
|
||||||
Self::Rows { items, .. }
|
Self::Rows { items, .. } | Self::Columns { items, .. } => {
|
||||||
| Self::Columns { items, .. }
|
|
||||||
| Self::SpacedRows { items, .. }
|
|
||||||
| Self::SpacedColumns { items, .. } => {
|
|
||||||
full_alloc.frame.push(*alloc);
|
full_alloc.frame.push(*alloc);
|
||||||
|
|
||||||
let is_row = matches!(self, Self::Rows { .. });
|
let is_row = matches!(self, Self::Rows { .. });
|
||||||
@@ -93,8 +87,8 @@ impl LayoutItem {
|
|||||||
|
|
||||||
pub fn get_size<'a>(&'a self) -> &'a Size {
|
pub fn get_size<'a>(&'a self) -> &'a Size {
|
||||||
match self {
|
match self {
|
||||||
Self::Rows { unit, .. } | Self::SpacedRows { unit, .. } => unit,
|
Self::Rows { unit, .. } => unit,
|
||||||
Self::Columns { unit, .. } | Self::SpacedColumns { unit, .. } => unit,
|
Self::Columns { unit, .. } => unit,
|
||||||
Self::Widget(unit) => unit,
|
Self::Widget(unit) => unit,
|
||||||
Self::Frame { item, .. } => item.get_size(),
|
Self::Frame { item, .. } => item.get_size(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod outline;
|
pub mod outline;
|
||||||
|
pub mod spaced;
|
||||||
|
|
||||||
use crate::render::RenderScope;
|
use crate::render::RenderScope;
|
||||||
|
|
||||||
@@ -30,3 +31,9 @@ display_widget!(u64);
|
|||||||
display_widget!(u128);
|
display_widget!(u128);
|
||||||
display_widget!(f32);
|
display_widget!(f32);
|
||||||
display_widget!(f64);
|
display_widget!(f64);
|
||||||
|
|
||||||
|
impl Widget for &dyn Widget {
|
||||||
|
fn render(&self, scope: &mut RenderScope) {
|
||||||
|
(*self).render(scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-14
@@ -5,7 +5,7 @@ use pulse_ui::{
|
|||||||
layout::{LayoutItem, layout},
|
layout::{LayoutItem, layout},
|
||||||
state::{Refresh, State},
|
state::{Refresh, State},
|
||||||
unit::Size,
|
unit::Size,
|
||||||
widget::outline::Outline,
|
widget::spaced::SpacedRows,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct PulseTradeApp {
|
pub struct PulseTradeApp {
|
||||||
@@ -24,22 +24,18 @@ impl App for PulseTradeApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn layout(&self) -> pulse_ui::layout::LayoutItem {
|
async fn layout(&self) -> pulse_ui::layout::LayoutItem {
|
||||||
layout(vec![
|
layout(vec![LayoutItem::Widget(Size::Flex(1))])
|
||||||
LayoutItem::Frame {
|
|
||||||
padding: 1,
|
|
||||||
item: Box::new(LayoutItem::Widget(Size::Flex(1))),
|
|
||||||
},
|
|
||||||
LayoutItem::Frame {
|
|
||||||
padding: 1,
|
|
||||||
item: Box::new(LayoutItem::Widget(Size::Flex(1))),
|
|
||||||
},
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render(&mut self, layout: pulse_ui::layout::Allocation) {
|
async fn render(&mut self, layout: pulse_ui::layout::Allocation) {
|
||||||
layout.draw(1, format!("{}", self.count.display().await).as_str());
|
layout.draw(
|
||||||
layout.draw_frame(1, Outline);
|
0,
|
||||||
layout.draw_frame(2, Outline);
|
SpacedRows(vec![
|
||||||
|
(LayoutItem::Widget(Size::Flex(1)), Box::new("one")),
|
||||||
|
(LayoutItem::Widget(Size::Flex(1)), Box::new("two")),
|
||||||
|
(LayoutItem::Widget(Size::Flex(1)), Box::new("three")),
|
||||||
|
]),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user