Add Cursor widget

This commit is contained in:
Joscha 2023-02-17 12:13:56 +01:00
parent 5a15838989
commit 2dee39c03c
2 changed files with 69 additions and 0 deletions

View file

@ -1,5 +1,6 @@
mod background; mod background;
mod border; mod border;
mod cursor;
mod empty; mod empty;
mod float; mod float;
mod padding; mod padding;
@ -7,6 +8,7 @@ mod text;
pub use background::*; pub use background::*;
pub use border::*; pub use border::*;
pub use cursor::*;
pub use empty::*; pub use empty::*;
pub use float::*; pub use float::*;
pub use padding::*; pub use padding::*;

67
src/widgets/cursor.rs Normal file
View file

@ -0,0 +1,67 @@
use async_trait::async_trait;
use crate::{AsyncWidget, Frame, Pos, Size, Widget};
pub struct Cursor<I> {
inner: I,
at: Pos,
}
impl<I> Cursor<I> {
pub fn new(inner: I) -> Self {
Self {
inner,
at: Pos::ZERO,
}
}
pub fn at(mut self, pos: Pos) -> Self {
self.at = pos;
self
}
pub fn at_xy(self, x: i32, y: i32) -> Self {
self.at(Pos::new(x, y))
}
}
impl<E, I> Widget<E> for Cursor<I>
where
I: Widget<E>,
{
fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.inner.size(frame, max_width, max_height)
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.inner.draw(frame)?;
frame.show_cursor(self.at);
Ok(())
}
}
#[async_trait]
impl<E, I> AsyncWidget<E> for Cursor<I>
where
I: AsyncWidget<E> + Send + Sync,
{
async fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.inner.size(frame, max_width, max_height).await
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.inner.draw(frame).await?;
frame.show_cursor(self.at);
Ok(())
}
}