update rectangle

This commit is contained in:
Michael Zhang 2021-02-27 01:10:23 -06:00
parent 2074b95778
commit 30b481ee64
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 28 additions and 16 deletions

View file

@ -31,7 +31,19 @@ pub type Screen = Stdout;
/// X Y W H
#[derive(Copy, Clone)]
pub struct Rect(u16, u16, u16, u16);
pub struct Rect {
x: u16,
y: u16,
w: u16,
h: u16
}
impl Rect {
/// Construct a new rectangle from (x, y) and (w, h)
pub fn new(x: u16, y: u16, w: u16, h: u16) -> Self {
Rect { x, y, w, h }
}
}
/// UI entrypoint.
pub async fn run_ui(mut w: Stdout, exit: ExitSender) -> Result<()> {
@ -58,7 +70,7 @@ pub async fn run_ui(mut w: Stdout, exit: ExitSender) -> Result<()> {
println!("time {}", now);
let (term_width, term_height) = terminal::size()?;
let bounds = Rect(5, 5, term_width - 10, term_height - 10);
let bounds = Rect::new(5, 5, term_width - 10, term_height - 10);
// table.draw(&mut w, bounds)?;
tabs.draw(&mut w, bounds)?;
w.flush()?;

View file

@ -59,7 +59,7 @@ impl Widget for Table {
}
for (i, row) in self.rows.iter().enumerate() {
queue!(w, cursor::MoveTo(rect.0, rect.1 + i as u16))?;
queue!(w, cursor::MoveTo(rect.x, rect.y + i as u16))?;
if let Some(v) = self.selected_row {
if v == i as u16 {
queue!(
@ -75,33 +75,33 @@ impl Widget for Table {
)?;
}
}
let mut s = String::with_capacity(rect.2 as usize);
let mut s = String::with_capacity(rect.w as usize);
for (j, cell) in row.iter().enumerate() {
s += &cell;
for _ in 0..columns[j] + 1 {
s += " ";
}
}
for _ in 0..(rect.2 - s.len() as u16) {
for _ in 0..(rect.w - s.len() as u16) {
s += " ";
}
println!("{}", s);
}
let d = "\u{b7}".repeat(rect.2 as usize);
let d = "\u{b7}".repeat(rect.w as usize);
queue!(
w,
style::SetBackgroundColor(Color::Black),
style::SetForegroundColor(Color::White)
)?;
for j in self.rows.len() as u16..rect.3 {
queue!(w, cursor::MoveTo(rect.0, rect.1 + j))?;
for j in self.rows.len() as u16..rect.h {
queue!(w, cursor::MoveTo(rect.x, rect.y + j))?;
println!("{}", d);
}
} else {
let msg = "Nothing in this table!";
let x = rect.0 + (rect.2 - msg.len() as u16) / 2;
let y = rect.1 + rect.3 / 2;
let x = rect.x + (rect.w - msg.len() as u16) / 2;
let y = rect.y + rect.h / 2;
queue!(w, cursor::MoveTo(x, y))?;
println!("{}", msg);
}

View file

@ -7,7 +7,7 @@ use crossterm::{cursor::MoveTo, event::Event};
use super::{Rect, Screen, Widget};
pub struct Tabs {
id: usize,
id_incr: usize,
active_id: usize,
names: Vec<(usize, String)>,
contents: HashMap<usize, Box<dyn Widget>>,
@ -16,7 +16,7 @@ pub struct Tabs {
impl Tabs {
pub fn new() -> Self {
Tabs {
id: 0,
id_incr: 0,
active_id: 0,
names: Vec::new(),
contents: HashMap::new(),
@ -24,8 +24,8 @@ impl Tabs {
}
pub fn add_tab(&mut self, name: impl AsRef<str>, drawable: impl Widget + 'static) {
let id = self.id;
self.id += 1;
let id = self.id_incr;
self.id_incr += 1;
self.names.push((id, name.as_ref().to_owned()));
self.contents.insert(id, Box::new(drawable));
@ -36,12 +36,12 @@ impl Widget for Tabs {
fn update(&mut self, event: Option<Event>) {}
fn draw(&self, w: &mut Screen, rect: Rect) -> Result<()> {
queue!(w, MoveTo(rect.0, rect.1))?;
queue!(w, MoveTo(rect.x, rect.y))?;
for (id, name) in self.names.iter() {
println!(" {} ", name);
}
let new_rect = Rect(rect.0, rect.1 + 1, rect.2, rect.3 - 1);
let new_rect = Rect::new(rect.x, rect.y + 1, rect.w, rect.h - 1);
if let Some(widget) = self.contents.get(&self.active_id) {
widget.draw(w, new_rect)?;
}