mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-09 05:06:43 +01:00
Rect -> [u16;4] in core
This commit is contained in:
parent
4b92465073
commit
fa739a49b2
7 changed files with 86 additions and 112 deletions
|
|
@ -19,16 +19,16 @@ pub struct Tui {
|
|||
buffers: [Buffer;2],
|
||||
backend: CrosstermBackend<Stdout>,
|
||||
event: RwLock<Option<TuiEvent>>,
|
||||
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<str>, x: u16, y: u16, style: Option<Style>
|
||||
) -> Perhaps<Rect> {
|
||||
) -> Perhaps<[u16;4]> {
|
||||
let text = text.as_ref();
|
||||
let buf = self.buffer();
|
||||
if x < buf.area.width && y < buf.area.height {
|
||||
buf.set_string(x, y, text, style.unwrap_or(Style::default()));
|
||||
}
|
||||
Ok(Some(Rect { x, y, width: text.len() as u16, height: 1 }))
|
||||
Ok(Some([x, y, text.len() as u16, 1]))
|
||||
}
|
||||
#[inline]
|
||||
pub fn alter_area (
|
||||
&mut self, alter: impl Fn(u16, u16, u16, u16)->(u16, u16, u16, u16)
|
||||
&mut self, alter: impl Fn([u16;4])->[u16;4]
|
||||
) -> &mut Self {
|
||||
let (x, y, width, height) = alter(
|
||||
self.area.x, self.area.y, self.area.width, self.area.height
|
||||
);
|
||||
self.with_area(x, y, width, height)
|
||||
let [x, y, w, h] = alter(self.area.xywh());
|
||||
self.with_area(x, y, w, h)
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_rect (&mut self, area: Rect) -> &mut Self {
|
||||
pub fn with_rect (&mut self, area: [u16;4]) -> &mut Self {
|
||||
self.area = area;
|
||||
self
|
||||
}
|
||||
|
|
@ -200,27 +198,10 @@ pub enum TuiEvent {
|
|||
// Jack(JackEvent)
|
||||
}
|
||||
|
||||
/// Rendering unit struct to Ratatui returns zero-sized [Rect] at render coordinates.
|
||||
/// Rendering unit struct to Ratatui returns zero-sized [Area] at render coordinates.
|
||||
impl Render<Tui> for () {
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
|
||||
Ok(Some(Rect { x: to.area.x, y: to.area.y, width: 0, height: 0 }))
|
||||
}
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
pub fn half_block (lower: bool, upper: bool) -> Option<char> {
|
||||
match (lower, upper) {
|
||||
(true, true) => Some('█'),
|
||||
(true, false) => Some('▄'),
|
||||
(false, true) => Some('▀'),
|
||||
_ => None
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
|
||||
Ok(Some([to.area.x(), to.area.y(), 0, 0]))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -231,3 +212,11 @@ impl Area<u16> for Rect {
|
|||
fn h (&self) -> u16 { self.height }
|
||||
}
|
||||
|
||||
pub fn half_block (lower: bool, upper: bool) -> Option<char> {
|
||||
match (lower, upper) {
|
||||
(true, true) => Some('█'),
|
||||
(true, false) => Some('▄'),
|
||||
(false, true) => Some('▀'),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue