Add checkbox rendering logic for iced

This commit is contained in:
João Capucho 2021-09-10 21:17:06 +01:00
parent 37a6ea7abc
commit 5323bb3ce8
No known key found for this signature in database
GPG Key ID: 1AD5CE7CCAEBDA21
4 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,74 @@
use super::super::super::widget::image;
#[derive(Clone, Copy)]
struct Background {
default: image::Handle,
hover: image::Handle,
press: image::Handle,
}
impl Background {
fn new(image: image::Handle) -> Self {
Self {
default: image,
hover: image,
press: image,
}
}
}
#[derive(Clone, Copy)]
pub struct Style {
background: Option<Background>,
checked: Option<image::Handle>,
}
impl Style {
pub fn new(image: image::Handle, checked: image::Handle) -> Self {
Self {
background: Some(Background::new(image)),
checked: Some(checked),
}
}
pub fn hover_image(mut self, image: image::Handle) -> Self {
self.background = Some(match self.background {
Some(mut background) => {
background.hover = image;
background
},
None => Background::new(image),
});
self
}
pub fn press_image(mut self, image: image::Handle) -> Self {
self.background = Some(match self.background {
Some(mut background) => {
background.press = image;
background
},
None => Background::new(image),
});
self
}
pub fn pressed(&self) -> Option<image::Handle> { self.background.as_ref().map(|b| b.press) }
pub fn checked(&self) -> Option<image::Handle> { self.checked }
pub fn hovered(&self) -> Option<image::Handle> { self.background.as_ref().map(|b| b.hover) }
pub fn background(&self) -> Option<image::Handle> {
self.background.as_ref().map(|b| b.default)
}
}
impl Default for Style {
fn default() -> Self {
Self {
background: None,
checked: None,
}
}
}

View File

@ -1,4 +1,5 @@
pub mod button;
pub mod checkbox;
pub mod container;
pub mod scrollable;
pub mod slider;

View File

@ -0,0 +1,64 @@
use super::super::{super::Rotation, style, IcedRenderer, Primitive};
use iced::{checkbox, mouse, Rectangle};
impl checkbox::Renderer for IcedRenderer {
// TODO: what if this gets large enough to not be copied around?
type Style = style::checkbox::Style;
const DEFAULT_SIZE: u16 = 20;
const DEFAULT_SPACING: u16 = 15;
fn draw(
&mut self,
bounds: Rectangle,
is_checked: bool,
is_mouse_over: bool,
(label, _): Self::Output,
style: &Self::Style,
) -> Self::Output {
let default_rect = || Primitive::Rectangle {
bounds,
linear_color: vek::Rgba::broadcast(1.0),
};
let background_image = match (is_checked, is_mouse_over) {
(true, _) => style.pressed(),
(_, true) => style.hovered(),
_ => style.background(),
};
let background = background_image
.map(|image| Primitive::Image {
handle: (image, Rotation::None),
bounds,
color: vek::Rgba::broadcast(255),
source_rect: None,
})
.unwrap_or_else(default_rect);
(
Primitive::Group {
primitives: if is_checked {
let check = style
.pressed()
.map(|image| Primitive::Image {
handle: (image, Rotation::None),
bounds,
color: vek::Rgba::broadcast(255),
source_rect: None,
})
.unwrap_or_else(default_rect);
vec![background, check, label]
} else {
vec![background, label]
},
},
if is_mouse_over {
mouse::Interaction::Pointer
} else {
mouse::Interaction::default()
},
)
}
}

View File

@ -1,6 +1,7 @@
mod aspect_ratio_container;
mod background_container;
mod button;
mod checkbox;
mod column;
mod compound_graphic;
mod container;