From fa739a49b2250bae62d36df16e2b565f0f4be3f4 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 7 Sep 2024 12:40:27 +0300 Subject: [PATCH] Rect -> [u16;4] in core --- crates/tek_core/src/engine/layout.rs | 10 ++++ crates/tek_core/src/space.rs | 66 ++++++++------------------ crates/tek_core/src/tui.rs | 67 +++++++++++---------------- crates/tek_core/src/tui/tui_border.rs | 27 +++++------ crates/tek_core/src/tui/tui_buffer.rs | 10 ++-- crates/tek_core/src/tui/tui_colors.rs | 4 +- crates/tek_core/src/tui/tui_layout.rs | 14 +++--- 7 files changed, 86 insertions(+), 112 deletions(-) diff --git a/crates/tek_core/src/engine/layout.rs b/crates/tek_core/src/engine/layout.rs index a6c8b85e..824b138b 100644 --- a/crates/tek_core/src/engine/layout.rs +++ b/crates/tek_core/src/engine/layout.rs @@ -1,5 +1,15 @@ use crate::*; +// TODO: Convert to component +// pub enum Align { Center, NW, N, NE, E, SE, S, SW, W, } +pub fn center_box (area: Rect, w: u16, h: u16) -> Rect { + let width = w.min(area.width * 3 / 5); + let height = h.min(area.width * 3 / 5); + let x = area.x + (area.width - width) / 2; + let y = area.y + (area.height - height) / 2; + Rect { x, y, width, height } +} + /// Trait for structs that compute drawing area before rendering pub trait Layout: Render { fn layout (&self, area: E::Area) -> Perhaps; diff --git a/crates/tek_core/src/space.rs b/crates/tek_core/src/space.rs index 3cb29743..0f9a8809 100644 --- a/crates/tek_core/src/space.rs +++ b/crates/tek_core/src/space.rs @@ -3,30 +3,18 @@ use crate::*; pub trait Point { fn x (&self) -> N; fn y (&self) -> N; - fn w (&self) -> N { - self.x() - } - fn h (&self) -> N { - self.y() - } + fn w (&self) -> N { self.x() } + fn h (&self) -> N { self.y() } } impl Point for (N, N) { - fn x (&self) -> N { - self.0 - } - fn y (&self) -> N { - self.1 - } + fn x (&self) -> N { self.0 } + fn y (&self) -> N { self.1 } } impl Point for [N;2] { - fn x (&self) -> N { - self[0] - } - fn y (&self) -> N { - self[1] - } + fn x (&self) -> N { self[0] } + fn y (&self) -> N { self[1] } } pub trait Area { @@ -34,42 +22,28 @@ pub trait Area { fn y (&self) -> N; fn w (&self) -> N; fn h (&self) -> N; - fn x2 (&self) -> N { - self.x() + self.w() + fn x2 (&self) -> N { self.x() + self.w() } + fn y2 (&self) -> N { self.y() + self.h() } + fn xywh (&self) -> [N;4] { + [self.x(), self.y(), self.w(), self.h()] } - fn y2 (&self) -> N { - self.y() + self.h() + fn lrtb (&self) -> [N;4] { + [self.x(), self.x2(), self.y(), self.y2()] } } impl Area for (N, N, N, N) { - fn x (&self) -> N { - self.0 - } - fn y (&self) -> N { - self.1 - } - fn w (&self) -> N { - self.2 - } - fn h (&self) -> N { - self.3 - } + fn x (&self) -> N { self.0 } + fn y (&self) -> N { self.1 } + fn w (&self) -> N { self.2 } + fn h (&self) -> N { self.3 } } impl Area for [N;4] { - fn x (&self) -> N { - self[0] - } - fn y (&self) -> N { - self[1] - } - fn w (&self) -> N { - self[2] - } - fn h (&self) -> N { - self[3] - } + fn x (&self) -> N { self[0] } + fn y (&self) -> N { self[1] } + fn w (&self) -> N { self[2] } + fn h (&self) -> N { self[3] } } macro_rules! impl_axis_common { ($A:ident $T:ty) => { diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index dde008e7..0bd0c4eb 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -19,16 +19,16 @@ pub struct Tui { buffers: [Buffer;2], backend: CrosstermBackend, event: RwLock>, - area: Rect, + area: [u16;4], } impl Engine for Tui { type Unit = u16; - type Area = Rect; + type Area = [Self::Unit;4]; type HandleInput = Self; type Handled = bool; type RenderInput = Self; - type Rendered = Rect; + type Rendered = Self::Area; fn exited (&self) -> bool { self.exited.fetch_and(true, Ordering::Relaxed) } @@ -54,7 +54,7 @@ impl Engine for Tui { } #[inline] fn with_area (&mut self, x: u16, y: u16, w: u16, h: u16) -> &mut Self { - self.with_rect(Rect { x, y, width: w, height: h }); + self.with_rect([x, y, w, h]); self } } @@ -71,7 +71,7 @@ impl Tui { buffer: 0, buffers: [Buffer::empty(area), Buffer::empty(area)], backend, - area, + area: area.xywh(), }; engine.setup()?; let engine = Arc::new(RwLock::new(engine)); @@ -109,7 +109,7 @@ impl Tui { if engine.exited() { break } - engine.area = engine.backend.size().expect("get size failed"); + engine.area = engine.backend.size().expect("get size failed").xywh(); state.render(&mut engine).expect("render failed"); engine.flip(); } @@ -134,22 +134,22 @@ impl Tui { self.buffers[1 - self.buffer].reset(); self.buffer = 1 - self.buffer; } - pub fn buffer_update (&mut self, area: Rect, callback: &impl Fn(&mut Cell, u16, u16)) { + pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) { buffer_update(self.buffer(), area, callback) } - pub fn fill_bg (&mut self, area: Rect, color: Color) { + pub fn fill_bg (&mut self, area: [u16;4], color: Color) { self.buffer_update(area, &|cell,_,_|{cell.set_bg(color);}) } - pub fn fill_fg (&mut self, area: Rect, color: Color) { + pub fn fill_fg (&mut self, area: [u16;4], color: Color) { self.buffer_update(area, &|cell,_,_|{cell.set_fg(color);}) } - pub fn fill_ul (&mut self, area: Rect, color: Color) { + pub fn fill_ul (&mut self, area: [u16;4], color: Color) { self.buffer_update(area, &|cell,_,_|{ cell.modifier = ratatui::prelude::Modifier::UNDERLINED; cell.underline_color = color; }) } - pub fn fill_char (&mut self, area: Rect, c: char) { + pub fn fill_char (&mut self, area: [u16;4], c: char) { self.buffer_update(area, &|cell,_,_|{cell.set_char(c);}) } pub fn make_dim (&mut self) { @@ -161,25 +161,23 @@ impl Tui { } pub fn blit ( &mut self, text: &impl AsRef, x: u16, y: u16, style: Option