changes
This commit is contained in:
@@ -11,3 +11,17 @@ impl Element for String {
|
|||||||
self.to_owned()
|
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 mod prelude {
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
elements::*,
|
||||||
|
func,
|
||||||
style::{Color, Style},
|
style::{Color, Style},
|
||||||
utils::init,
|
utils::init,
|
||||||
Props, Result, Ui,
|
Props, Result, Ui,
|
||||||
@@ -20,9 +22,13 @@ pub trait Framing {
|
|||||||
|
|
||||||
pub trait Element {
|
pub trait Element {
|
||||||
fn render(&self) -> String;
|
fn render(&self) -> String;
|
||||||
|
fn event(&self, event: crossterm::event::Event) {
|
||||||
|
_ = event
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
pub type Handler<T> = std::sync::Arc<std::sync::Mutex<T>>;
|
||||||
pub type Ui = Arc<dyn Fn(&mut dyn Framing)>;
|
pub type Ui = Arc<dyn Fn(&mut dyn Framing)>;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -42,6 +48,7 @@ pub struct Console<'a> {
|
|||||||
height: u16,
|
height: u16,
|
||||||
handle: Option<std::io::StdoutLock<'a>>,
|
handle: Option<std::io::StdoutLock<'a>>,
|
||||||
pub mouse_position: Option<(u16, u16)>,
|
pub mouse_position: Option<(u16, u16)>,
|
||||||
|
pub event: Option<crossterm::event::Event>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Props {
|
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 (x, y) = (x - style.px, y - style.py);
|
||||||
let px = " ".repeat(style.px as usize);
|
let px = " ".repeat(style.px as usize);
|
||||||
written.push_str(&format!("\n{}", " ".repeat(width as usize)).repeat(style.py as usize));
|
written.push_str(&format!("\n{}", " ".repeat(width as usize)).repeat(style.py as usize));
|
||||||
@@ -217,3 +228,7 @@ impl Console<'_> {
|
|||||||
utils::clear()
|
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)),
|
}) => console.mouse_position = Some((column, row)),
|
||||||
crossterm::event::Event::Resize(_, _) => {}
|
crossterm::event::Event::Resize(_, _) => {}
|
||||||
_ => break,
|
e => console.event = Some(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +26,18 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
pub fn app() -> Ui {
|
pub fn app() -> Ui {
|
||||||
Arc::new(|frame| {
|
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,
|
mouse,
|
||||||
handle: None,
|
handle: None,
|
||||||
mouse_position: None,
|
mouse_position: None,
|
||||||
|
event: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user