Add initial Slider widget

This commit is contained in:
mars 2022-11-21 20:25:29 -07:00
parent 415d9e7845
commit 0fe8139e02
2 changed files with 69 additions and 0 deletions

View File

@ -10,6 +10,7 @@ pub mod flex;
pub mod menu;
pub mod palette;
pub mod scroll;
pub mod slider;
pub mod shell;
pub mod text;

View File

@ -0,0 +1,68 @@
use super::prelude::*;
pub struct SliderStyle {
pub bg_color: Color,
pub bg_padding: f32,
pub bg_rounding: f32,
pub fg_color: Color,
}
impl Default for SliderStyle {
fn default() -> Self {
Self {
bg_color: THEME.palette.overlay,
bg_padding: 2.5,
bg_rounding: 2.5,
fg_color: THEME.palette.blue,
}
}
}
pub struct Slider {
style: SliderStyle,
bg_rect: Rect,
fg_rect: Rect,
position: f32,
dirty: bool,
}
impl Slider {
pub fn new(style: SliderStyle, rect: Rect) -> Self {
Self {
style,
bg_rect: rect,
fg_rect: rect,
position: 0.5,
dirty: true,
}
}
pub fn set_position(&mut self, position: f32) {
self.position = position;
self.dirty = true;
}
pub fn set_rect(&mut self, rect: Rect) {
self.bg_rect = rect;
self.dirty = true;
}
fn undirty(&mut self) {
if !self.dirty {
return;
}
let mut fg_space = self.bg_rect.inset(self.style.bg_padding);
fg_space.br.x = (fg_space.width() * self.position) + fg_space.tl.x;
self.fg_rect = fg_space;
self.dirty = false;
}
}
impl Widget for Slider {
fn draw(&mut self, ctx: &DrawContext) {
self.undirty();
ctx.draw_rounded_rect(self.bg_rect, self.style.bg_rounding, self.style.bg_color);
ctx.draw_rect(self.fg_rect, self.style.fg_color);
}
}