a few improvements

This commit is contained in:
2025-02-03 13:41:13 +01:00
parent 52eb3cc490
commit def18fed65
3 changed files with 45 additions and 66 deletions
+5 -5
View File
@@ -1,13 +1,13 @@
use crate::Element;
impl Element for &str {
fn render(&self, frame: &mut dyn crate::Framing) {
frame.draw_str(self);
fn render(&self, frame: &mut String) {
*frame += self;
}
}
impl Element for crate::Ui {
fn render(&self, frame: &mut dyn crate::Framing) {
self(frame)
impl Element for String {
fn render(&self, frame: &mut String) {
*frame += self;
}
}
+33 -40
View File
@@ -12,7 +12,7 @@ pub mod prelude {
state::State,
style::{Color, Style},
utils::init,
Framing, Props, Result, Ui,
Props, Result, Ui,
};
#[cfg(feature = "state")]
@@ -21,16 +21,8 @@ pub mod prelude {
pub use crossterm::event::read;
}
// Traits
pub trait Framing {
fn draw(&mut self, element: &dyn Element, props: &Props);
fn draw_str(&mut self, s: &str) {
_ = s
}
}
pub trait Element {
fn render(&self, frame: &mut dyn Framing);
fn render(&self, frame: &mut String);
fn event(&self, event: crossterm::event::Event) {
_ = event
}
@@ -38,7 +30,10 @@ pub trait Element {
// Types
pub type Handler<T> = std::sync::Arc<std::sync::Mutex<T>>;
pub type Ui = Box<dyn Fn(&mut dyn Framing)>;
pub type Ui = Box<dyn Fn(&mut Frame)>;
// Structs
pub struct Frame(Vec<(Box<dyn Element>, Props)>);
#[derive(Debug, Clone)]
pub struct Props {
@@ -57,6 +52,12 @@ pub struct Console {
pub mouse_position: Option<(u16, u16)>,
}
impl Frame {
pub fn draw(&mut self, element: Box<dyn Element>, props: Props) {
self.0.push((element, props));
}
}
impl Props {
pub fn new(x: u16, y: u16) -> Self {
Self {
@@ -138,7 +139,27 @@ impl Props {
}
}
impl Framing for Console {
impl Console {
pub fn render(&mut self, ui: &Ui) -> Result<()> {
(self.width, self.height) = crossterm::terminal::size()?;
utils::clear()?;
let mut frame = Frame(Vec::new());
ui(&mut frame);
for (e, _) in frame.0 {
self.draw(e.as_ref(), &Props::auto());
}
Ok(())
}
pub fn close(self) -> Result<()> {
if self.mouse {
crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture)?;
}
crossterm::terminal::disable_raw_mode()?;
utils::show_cursor()?;
utils::clear()
}
fn draw(&mut self, element: &dyn Element, props: &Props) {
let mut written = String::new();
element.render(&mut written);
@@ -185,34 +206,6 @@ impl Framing for Console {
}
}
impl Console {
pub fn render(&mut self, ui: &Ui) -> Result<()> {
(self.width, self.height) = crossterm::terminal::size()?;
utils::clear()?;
ui(self);
Ok(())
}
pub fn close(self) -> Result<()> {
if self.mouse {
crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture)?;
}
crossterm::terminal::disable_raw_mode()?;
utils::show_cursor()?;
utils::clear()
}
}
impl Framing for String {
fn draw(&mut self, element: &dyn Element, _props: &Props) {
element.render(self);
}
fn draw_str(&mut self, s: &str) {
*self += s;
}
}
pub fn func<T>(t: T) -> Handler<T> {
std::sync::Arc::new(std::sync::Mutex::new(t))
}
+7 -21
View File
@@ -3,7 +3,9 @@ use osmium_engine::prelude::*;
fn main() -> Result<()> {
let mut console = init(true)?;
let (mut count, ui) = app();
let mut count = use_state(0);
let ui = app(count);
loop {
console.render(&ui)?;
@@ -14,24 +16,8 @@ fn main() -> Result<()> {
console.close()
}
pub fn app() -> (State<i32>, Ui) {
let (count, count_ui) = counter();
(
count,
Box::new(move |frame| {
frame.draw(&count_ui, &Props::center());
}),
)
}
pub fn counter() -> (State<i32>, Ui) {
let count = use_state(0);
(
count,
Box::new(move |frame| {
frame.draw(&format!("Events: {count}").as_str(), &Props::center());
}),
)
pub fn app(count: State<i32>) -> Ui {
Box::new(move |frame| {
frame.draw(Box::new(format!("Events: {count}")), Props::center());
})
}