Add Resize widget
This commit is contained in:
parent
ba716dd089
commit
b27cb81642
3 changed files with 112 additions and 1 deletions
|
|
@ -1,6 +1,8 @@
|
|||
use async_trait::async_trait;
|
||||
|
||||
use crate::widgets::{Background, Border, Either, Either3, Float, JoinSegment, Layer, Padding};
|
||||
use crate::widgets::{
|
||||
Background, Border, Either, Either3, Float, JoinSegment, Layer, Padding, Resize,
|
||||
};
|
||||
use crate::{Frame, Size};
|
||||
|
||||
// TODO Feature-gate these traits
|
||||
|
|
@ -76,6 +78,10 @@ pub trait WidgetExt: Sized {
|
|||
fn padding(self) -> Padding<Self> {
|
||||
Padding::new(self)
|
||||
}
|
||||
|
||||
fn resize(self) -> Resize<Self> {
|
||||
Resize::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
// It would be nice if this could be restricted to types implementing Widget.
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ mod float;
|
|||
mod join;
|
||||
mod layer;
|
||||
mod padding;
|
||||
mod resize;
|
||||
mod text;
|
||||
|
||||
pub use background::*;
|
||||
|
|
@ -18,4 +19,5 @@ pub use float::*;
|
|||
pub use join::*;
|
||||
pub use layer::*;
|
||||
pub use padding::*;
|
||||
pub use resize::*;
|
||||
pub use text::*;
|
||||
|
|
|
|||
103
src/widgets/resize.rs
Normal file
103
src/widgets/resize.rs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
use async_trait::async_trait;
|
||||
|
||||
use crate::{AsyncWidget, Frame, Size, Widget};
|
||||
|
||||
pub struct Resize<I> {
|
||||
inner: I,
|
||||
min_width: Option<u16>,
|
||||
min_height: Option<u16>,
|
||||
max_width: Option<u16>,
|
||||
max_height: Option<u16>,
|
||||
}
|
||||
|
||||
impl<I> Resize<I> {
|
||||
pub fn new(inner: I) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
min_width: None,
|
||||
min_height: None,
|
||||
max_width: None,
|
||||
max_height: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min_width(mut self, width: u16) -> Self {
|
||||
self.min_width = Some(width);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn min_height(mut self, height: u16) -> Self {
|
||||
self.min_height = Some(height);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn max_width(mut self, width: u16) -> Self {
|
||||
self.max_width = Some(width);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn max_height(mut self, height: u16) -> Self {
|
||||
self.max_height = Some(height);
|
||||
self
|
||||
}
|
||||
|
||||
fn resize(&self, size: Size) -> Size {
|
||||
let mut width = size.width;
|
||||
let mut height = size.height;
|
||||
|
||||
if let Some(min_width) = self.min_width {
|
||||
width = width.max(min_width);
|
||||
}
|
||||
if let Some(min_height) = self.min_height {
|
||||
height = height.max(min_height);
|
||||
}
|
||||
|
||||
if let Some(max_width) = self.max_width {
|
||||
width = width.min(max_width);
|
||||
}
|
||||
if let Some(max_height) = self.max_height {
|
||||
height = height.min(max_height);
|
||||
}
|
||||
|
||||
Size::new(width, height)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, I> Widget<E> for Resize<I>
|
||||
where
|
||||
I: Widget<E>,
|
||||
{
|
||||
fn size(
|
||||
&self,
|
||||
frame: &mut Frame,
|
||||
max_width: Option<u16>,
|
||||
max_height: Option<u16>,
|
||||
) -> Result<Size, E> {
|
||||
let size = self.inner.size(frame, max_width, max_height)?;
|
||||
Ok(self.resize(size))
|
||||
}
|
||||
|
||||
fn draw(self, frame: &mut Frame) -> Result<(), E> {
|
||||
self.inner.draw(frame)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<E, I> AsyncWidget<E> for Resize<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> {
|
||||
let size = self.inner.size(frame, max_width, max_height).await?;
|
||||
Ok(self.resize(size))
|
||||
}
|
||||
|
||||
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
|
||||
self.inner.draw(frame).await
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue