View plugins

This commit is contained in:
2026-02-01 17:18:59 +01:00
parent 2ca419bfd4
commit c4addd7f01
5 changed files with 67 additions and 21 deletions
+4 -2
View File
@@ -43,16 +43,18 @@ use crate::render::DrawContext;
pub mod component;
pub mod engine;
pub mod frontend;
pub mod render;
pub mod hooks;
pub mod render;
pub mod view_plugins;
pub mod prelude {
//! Prelude module - Re-exports commonly used items for convenience
pub use crate::component::{context::*, scope::*, *};
pub use crate::engine::*;
pub use crate::frontend::*;
pub use crate::render::*;
pub use crate::hooks::*;
pub use crate::render::*;
pub use crate::view_plugins::*;
pub use crate::{sleep, Error, Result, View, ViewWrapper};
pub use crossterm;
pub use osui_macros::{component, rsx};
+14 -9
View File
@@ -18,7 +18,7 @@ pub enum DrawInstruction {
}
/// Represents the dimensions of a drawable area
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Size {
/// Width in terminal columns
pub width: u16,
@@ -27,7 +27,7 @@ pub struct Size {
}
/// Represents a position in 2D space
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Point {
/// X coordinate (column)
pub x: u16,
@@ -36,7 +36,7 @@ pub struct Point {
}
/// Represents a rectangular area with position and dimensions
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Area {
/// X coordinate (column) of the top-left corner
pub x: u16,
@@ -68,8 +68,8 @@ impl DrawContext {
Self {
area,
allocated: Area {
x: u16::MAX,
y: u16::MAX,
x: 0,
y: 0,
width: 0,
height: 0,
},
@@ -80,10 +80,10 @@ impl DrawContext {
/// Allocates space within the drawable area and returns the allocated area
/// Updates the allocated bounds to include this allocation
pub fn allocate(&mut self, x: u16, y: u16, width: u16, height: u16) -> Area {
self.allocated.x = self.allocated.x.min(x);
self.allocated.y = self.allocated.y.min(y);
self.allocated.width = self.allocated.width.max(width);
self.allocated.height = self.allocated.height.max(height);
self.allocated.x = x;
self.allocated.y = y;
self.allocated.width = width;
self.allocated.height = height;
Area {
x,
@@ -108,4 +108,9 @@ impl DrawContext {
pub fn draw_view(&mut self, area: Area, view: View) {
self.drawing.push(DrawInstruction::View(area, view));
}
/// Clears drawing
pub fn clear(&mut self) {
self.drawing.clear();
}
}
+41
View File
@@ -0,0 +1,41 @@
use crate::{
render::{DrawContext, DrawInstruction},
View,
};
pub fn x_center(ctx: &mut DrawContext, _view: &View) {
ctx.allocated.y = (ctx.area.height - ctx.allocated.height) / 2;
}
pub fn y_center(ctx: &mut DrawContext, _view: &View) {
ctx.allocated.y = (ctx.area.height - ctx.allocated.height) / 2;
}
pub fn center(ctx: &mut DrawContext, _view: &View) {
ctx.allocated.x = (ctx.area.width - ctx.allocated.width) / 2;
ctx.allocated.y = (ctx.area.height - ctx.allocated.height) / 2;
}
pub fn size_auto(ctx: &mut DrawContext, _view: &View) {
for i in &ctx.drawing {
match i {
DrawInstruction::Text(_, text) => {
ctx.allocated.width = text.len() as u16;
ctx.allocated.height = 1;
}
DrawInstruction::View(area, view) => {
let mut c = DrawContext::new(area.clone());
view(&mut c);
size_auto(&mut c, view);
ctx.allocated.width = c.allocated.width;
ctx.allocated.height = c.allocated.height;
}
DrawInstruction::Child(_, _) => {}
}
}
}
pub fn redraw(ctx: &mut DrawContext, view: &View) {
ctx.clear();
ctx.draw_view(ctx.allocated.clone(), view.clone());
}