mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 20:56:43 +01:00
refactor: separate Render from Handle
This commit is contained in:
parent
d9b3bd150e
commit
72ead536be
12 changed files with 255 additions and 212 deletions
|
|
@ -1,111 +1,62 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
pub fn draw_box (buffer: &mut Buffer, area: Rect) -> Rect {
|
||||
if area.width < 1 || area.height < 1 {
|
||||
return area
|
||||
}
|
||||
let border = Style::default().gray().dim();
|
||||
let top = format!("╭{}╮", "─".repeat((area.width - 2).into()));
|
||||
let bottom = format!("╰{}╯", "─".repeat((area.width - 2).into()));
|
||||
buffer.set_string(area.x, area.y, top, border);
|
||||
for y in (area.y + 1)..(area.y + area.height - 1) {
|
||||
buffer.set_string(area.x, y, format!("│"), border);
|
||||
buffer.set_string(area.x + area.width - 1, y, format!("│"), border);
|
||||
}
|
||||
buffer.set_string(area.x, area.y + area.height - 1, bottom, border);
|
||||
area
|
||||
}
|
||||
|
||||
pub fn draw_box_styled (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
|
||||
if area.width < 1 || area.height < 1 {
|
||||
return area
|
||||
}
|
||||
let style = style.unwrap_or(Style::default());
|
||||
let top = format!("╭{}╮", "─".repeat((area.width - 2).into()));
|
||||
let bottom = format!("╰{}╯", "─".repeat((area.width - 2).into()));
|
||||
buffer.set_string(area.x, area.y, top, style);
|
||||
for y in (area.y + 1)..(area.y + area.height - 1) {
|
||||
buffer.set_string(area.x, y, format!("│"), style);
|
||||
buffer.set_string(area.x + area.width - 1, y, format!("│"), style);
|
||||
}
|
||||
buffer.set_string(area.x, area.y + area.height - 1, bottom, style);
|
||||
area
|
||||
}
|
||||
|
||||
pub fn draw_box_styled_dotted (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
|
||||
if area.width < 1 || area.height < 1 {
|
||||
return area
|
||||
}
|
||||
let style = style.unwrap_or(Style::default());
|
||||
let top = format!("╭{}╮", "┅".repeat((area.width - 2).into()));
|
||||
let bottom = format!("╰{}╯", "┅".repeat((area.width - 2).into()));
|
||||
buffer.set_string(area.x, area.y, top, style);
|
||||
for y in (area.y + 1)..(area.y + area.height - 1) {
|
||||
buffer.set_string(area.x, y, format!("┇"), style);
|
||||
buffer.set_string(area.x + area.width - 1, y, format!("┇"), style);
|
||||
}
|
||||
buffer.set_string(area.x, area.y + area.height - 1, bottom, style);
|
||||
area
|
||||
}
|
||||
use super::*;
|
||||
|
||||
pub struct Column(pub Vec<Box<dyn Device>>);
|
||||
|
||||
pub struct Row(pub Vec<Box<dyn Device>>);
|
||||
|
||||
impl Column {
|
||||
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
|
||||
Self(items)
|
||||
}
|
||||
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
|
||||
-> Usually<(Rect, Vec<Rect>)>
|
||||
{
|
||||
let mut w = 0u16;
|
||||
let mut h = 0u16;
|
||||
let mut rects = vec![];
|
||||
for (i, device) in items.iter().enumerate() {
|
||||
let y = area.y + h;
|
||||
let rect = Rect { x: area.x, y, width: area.width, height: area.height - h };
|
||||
let result = device.render(buf, rect)?;
|
||||
rects.push(result);
|
||||
w = w.max(result.width);
|
||||
h = ((h + result.height) as i16 + gap).max(0) as u16;
|
||||
}
|
||||
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Row(pub Vec<Box<dyn Device>>);
|
||||
impl Render for Column {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
Ok(Column::draw(buf, area, &self.0, 0)?.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Row {
|
||||
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
|
||||
Self(items)
|
||||
}
|
||||
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
|
||||
-> Usually<(Rect, Vec<Rect>)>
|
||||
{
|
||||
let mut w = 0u16;
|
||||
let mut h = 0u16;
|
||||
let mut rects = vec![];
|
||||
for (i, device) in items.iter().enumerate() {
|
||||
let x = area.x + w;
|
||||
let rect = Rect { x, y: area.y, width: area.width - w, height: area.height };
|
||||
let result = device.render(buf, rect)?;
|
||||
rects.push(result);
|
||||
w = ((w + result.width) as i16 + gap).max(0) as u16;
|
||||
h = h.max(result.height);
|
||||
}
|
||||
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for Column {
|
||||
impl Render for Row {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
Ok(draw_column(&self.0, buf, area, 0)?.0)
|
||||
Ok(Row::draw(buf, area, &self.0, 0)?.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for Row {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
Ok(draw_row(&self.0, buf, area, 0)?.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_column (items: &[Box<dyn Device>], buf: &mut Buffer, area: Rect, space: i16)
|
||||
-> Usually<(Rect, Vec<Rect>)>
|
||||
{
|
||||
let mut w = 0u16;
|
||||
let mut h = 0u16;
|
||||
let mut rects = vec![];
|
||||
for (i, device) in items.iter().enumerate() {
|
||||
let y = ((area.y + h) as i16).saturating_add(space) as u16;
|
||||
let rect = Rect { x: area.x, y, width: area.width, height: area.height - h };
|
||||
let result = device.render(buf, rect)?;
|
||||
rects.push(result);
|
||||
w = w.max(result.width);
|
||||
h = h + result.height;
|
||||
}
|
||||
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
|
||||
}
|
||||
|
||||
pub fn draw_row (items: &[Box<dyn Device>], buf: &mut Buffer, area: Rect, space: i16)
|
||||
-> Usually<(Rect, Vec<Rect>)>
|
||||
{
|
||||
let mut w = 0u16;
|
||||
let mut h = 0u16;
|
||||
let mut rects = vec![];
|
||||
for (i, device) in items.iter().enumerate() {
|
||||
let x = ((area.x + w) as i16).saturating_add(space) as u16;
|
||||
let rect = Rect { x, y: area.y, width: area.width - w, height: area.height };
|
||||
let result = device.render(buf, rect)?;
|
||||
rects.push(result);
|
||||
w = w + result.width;
|
||||
h = h.max(result.height);
|
||||
}
|
||||
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue