changes
This commit is contained in:
@@ -11,3 +11,17 @@ impl Element for String {
|
||||
self.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Button {
|
||||
pub on_click: crate::Handler<dyn FnMut()>,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl Element for Button {
|
||||
fn render(&self) -> String {
|
||||
self.text.to_owned()
|
||||
}
|
||||
fn event(&self, _: crossterm::event::Event) {
|
||||
(self.on_click.clone().lock().unwrap())()
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -7,6 +7,8 @@ use std::{io::Write, sync::Arc};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::{
|
||||
elements::*,
|
||||
func,
|
||||
style::{Color, Style},
|
||||
utils::init,
|
||||
Props, Result, Ui,
|
||||
@@ -20,9 +22,13 @@ pub trait Framing {
|
||||
|
||||
pub trait Element {
|
||||
fn render(&self) -> String;
|
||||
fn event(&self, event: crossterm::event::Event) {
|
||||
_ = event
|
||||
}
|
||||
}
|
||||
|
||||
// Types
|
||||
pub type Handler<T> = std::sync::Arc<std::sync::Mutex<T>>;
|
||||
pub type Ui = Arc<dyn Fn(&mut dyn Framing)>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -42,6 +48,7 @@ pub struct Console<'a> {
|
||||
height: u16,
|
||||
handle: Option<std::io::StdoutLock<'a>>,
|
||||
pub mouse_position: Option<(u16, u16)>,
|
||||
pub event: Option<crossterm::event::Event>,
|
||||
}
|
||||
|
||||
impl Props {
|
||||
@@ -170,6 +177,10 @@ impl Framing for Console<'_> {
|
||||
)
|
||||
};
|
||||
|
||||
if let Some(event) = &self.event {
|
||||
element.event(event.clone());
|
||||
}
|
||||
|
||||
let (x, y) = (x - style.px, y - style.py);
|
||||
let px = " ".repeat(style.px as usize);
|
||||
written.push_str(&format!("\n{}", " ".repeat(width as usize)).repeat(style.py as usize));
|
||||
@@ -217,3 +228,7 @@ impl Console<'_> {
|
||||
utils::clear()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn func<T>(t: T) -> Handler<T> {
|
||||
std::sync::Arc::new(std::sync::Mutex::new(t))
|
||||
}
|
||||
|
||||
+14
-2
@@ -17,7 +17,7 @@ fn main() -> Result<()> {
|
||||
..
|
||||
}) => console.mouse_position = Some((column, row)),
|
||||
crossterm::event::Event::Resize(_, _) => {}
|
||||
_ => break,
|
||||
e => console.event = Some(e),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,18 @@ fn main() -> Result<()> {
|
||||
|
||||
pub fn app() -> Ui {
|
||||
Arc::new(|frame| {
|
||||
frame.draw(&"this is centered!", &Props::center());
|
||||
frame.draw(
|
||||
&Button {
|
||||
on_click: func(|| {}),
|
||||
text: String::from("Testing"),
|
||||
},
|
||||
&Props::center().state(
|
||||
"hover",
|
||||
Style {
|
||||
color: Color::Red,
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -114,5 +114,6 @@ pub fn init<'a>(mouse: bool) -> crate::Result<crate::Console<'a>> {
|
||||
mouse,
|
||||
handle: None,
|
||||
mouse_position: None,
|
||||
event: None,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user