wip: layout

This commit is contained in:
🪞👃🪞 2024-06-12 23:21:56 +03:00
parent ac865824cc
commit d627d257ad
13 changed files with 220 additions and 131 deletions

View file

@ -1,47 +1,123 @@
use crate::prelude::*;
pub struct Rows(pub Vec<Box<dyn Device>>);
pub struct Rows {
focused: bool,
focus: usize,
items: Vec<Box<dyn Device>>,
}
pub struct Columns(pub Vec<Box<dyn Device>>);
impl Rows {
pub fn new (focused: bool, items: Vec<Box<dyn Device>>) -> Self {
Self { focused, focus: 0, items }
}
}
impl Device for Rows {
fn handle (&mut self, event: &EngineEvent) -> Usually<()> {
Ok(())
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<(u16, u16)> {
let mut x = 0u16;
let mut y = 0u16;
for device in self.0.iter() {
let (w, h) = device.render(buf, Rect {
x: area.x,
y: area.y + y,
width: area.width,
height: area.height - y
})?;
x = x.max(w);
y = y + h;
if !self.focused {
match event {
EngineEvent::Input(Event::Key(KeyEvent { code: KeyCode::Esc, .. })) => {
self.focused = true;
Ok(())
},
_ => self.items[self.focus].handle(event)
}
} else {
match event {
EngineEvent::Input(event) => match event {
Event::Key(KeyEvent { code: KeyCode::Up, .. }) => {
if self.focus == 0 {
self.focus = self.items.len();
}
self.focus = self.focus - 1;
},
Event::Key(KeyEvent { code: KeyCode::Down, .. }) => {
self.focus = self.focus + 1;
if self.focus >= self.items.len() {
self.focus = 0;
}
},
Event::Key(KeyEvent { code: KeyCode::Enter, .. }) => {
self.focused = false;
},
Event::Key(KeyEvent { code: KeyCode::Esc, .. }) => {
self.focused = true;
},
_ => {
println!("{event:?}");
}
},
_ => {}
}
Ok(())
}
Ok((x, y))
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let mut w = 0u16;
let mut h = 0u16;
for (i, device) in self.items.iter().enumerate() {
let result = device.render(buf, Rect {
x: area.x,
y: area.y + h,
width: area.width,
height: area.height - h
})?;
if self.focused && i == self.focus {
draw_box_styled(buf, result, Some(Style::default().gray().bold().not_dim()))
}
w = w.max(result.width);
h = h + result.height;
}
Ok(Rect {
x: area.x,
y: area.y,
width: w,
height: h
})
}
}
pub struct Columns {
focused: bool,
focus: usize,
items: Vec<Box<dyn Device>>,
}
impl Columns {
pub fn new (focused: bool, items: Vec<Box<dyn Device>>) -> Self {
Self { focused, focus: 0, items }
}
}
impl Device for Columns {
fn handle (&mut self, event: &EngineEvent) -> Result<(), Box<dyn Error>> {
Ok(())
fn handle (&mut self, event: &EngineEvent) -> Usually<()> {
if self.focused {
self.items[self.focus].handle(event)
} else {
Ok(())
}
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<(u16, u16)> {
let mut x = 0u16;
let mut y = 0u16;
for device in self.0.iter() {
let (w, h) = device.render(buf, Rect {
x: area.x + x,
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let mut w = 0u16;
let mut h = 0u16;
for (i, device) in self.items.iter().enumerate() {
let result = device.render(buf, Rect {
x: area.x + w,
y: area.y,
width: area.width - x,
width: area.width - w,
height: area.height
})?;
x = x + w;
y = y.max(h);
if self.focused && i == self.focus {
draw_box_styled(buf, result, Some(Style::default().yellow()))
}
w = w + result.width;
h = h.max(result.height);
}
Ok((x, y))
Ok(Rect {
x: area.x,
y: area.y,
width: w,
height: h
})
}
}