use rlvgl_core::{
event::Event,
renderer::Renderer,
widget::{Color, Rect, Widget},
};
use rlvgl_widgets::bar::Bar as BaseBar;
use crate::theme::{ColorScheme, ComponentSize, Theme, Variant};
pub use rlvgl_widgets::bar::{BarMode, BarOrientation};
pub struct Bar {
inner: BaseBar,
}
impl Bar {
pub fn new(bounds: Rect, min: i32, max: i32) -> Self {
Self {
inner: BaseBar::new(bounds, min, max),
}
}
pub fn with_value(mut self, value: i32) -> Self {
self.inner.set_value(value);
self
}
pub fn value(&self) -> i32 {
self.inner.value()
}
pub fn set_value(&mut self, value: i32) {
self.inner.set_value(value);
}
pub fn with_range(mut self, min: i32, max: i32) -> Self {
self.inner.set_range(min, max);
self
}
pub fn set_range(&mut self, min: i32, max: i32) {
self.inner.set_range(min, max);
}
pub fn with_start_value(mut self, value: i32) -> Self {
self.inner.set_start_value(value);
self
}
pub fn start_value(&self) -> i32 {
self.inner.start_value()
}
pub fn set_start_value(&mut self, value: i32) {
self.inner.set_start_value(value);
}
pub fn with_mode(mut self, mode: BarMode) -> Self {
self.inner.set_mode(mode);
self
}
pub fn mode(&self) -> BarMode {
self.inner.mode()
}
pub fn set_mode(&mut self, mode: BarMode) {
self.inner.set_mode(mode);
}
pub fn with_orientation(mut self, orientation: BarOrientation) -> Self {
self.inner.set_orientation(orientation);
self
}
pub fn orientation(&self) -> BarOrientation {
self.inner.orientation()
}
pub fn set_orientation(&mut self, orientation: BarOrientation) {
self.inner.set_orientation(orientation);
}
pub fn indicator_color(mut self, color: Color) -> Self {
self.inner.indicator_color = color;
self
}
pub fn indicator_color_value(&self) -> Color {
self.inner.indicator_color
}
pub fn set_indicator_color(&mut self, color: Color) {
self.inner.indicator_color = color;
}
pub fn themed(
mut self,
theme: &Theme,
scheme: ColorScheme,
variant: Variant,
size: ComponentSize,
) -> Self {
let resolved = theme.component_style(scheme, variant, size);
self.inner.style = resolved.style;
self.inner.indicator_color = resolved.accent_color;
self
}
pub fn style(&self) -> &rlvgl_core::style::Style {
&self.inner.style
}
pub fn style_mut(&mut self) -> &mut rlvgl_core::style::Style {
&mut self.inner.style
}
}
impl Widget for Bar {
fn bounds(&self) -> Rect {
self.inner.bounds()
}
fn set_bounds(&mut self, bounds: Rect) {
self.inner.set_bounds(bounds);
}
fn draw(&self, renderer: &mut dyn Renderer) {
self.inner.draw(renderer);
}
fn handle_event(&mut self, event: &Event) -> bool {
self.inner.handle_event(event)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bar_builder_sets_range_mode_and_indicator() {
let bar = Bar::new(rect(0, 0, 100, 8), 0, 100)
.with_value(75)
.with_start_value(25)
.with_mode(BarMode::Range)
.with_orientation(BarOrientation::Horizontal)
.indicator_color(Color(10, 20, 30, 255));
assert_eq!(bar.value(), 75);
assert_eq!(bar.start_value(), 25);
assert_eq!(bar.mode(), BarMode::Range);
assert_eq!(bar.orientation(), BarOrientation::Horizontal);
assert_eq!(bar.indicator_color_value(), Color(10, 20, 30, 255));
}
#[test]
fn bar_themed_sets_style_and_indicator() {
let theme = Theme::material_light();
let bar = Bar::new(rect(0, 0, 100, 8), 0, 100).themed(
&theme,
ColorScheme::Info,
Variant::Outline,
ComponentSize::Lg,
);
assert_eq!(
bar.indicator_color_value(),
theme.scheme(ColorScheme::Info).solid
);
assert_eq!(bar.style().border_width, 2);
}
fn rect(x: i32, y: i32, width: i32, height: i32) -> Rect {
Rect {
x,
y,
width,
height,
}
}
}