pub use rlvgl_core::widget::Color;
use core::ops::BitOr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Part(pub u32);
impl Part {
pub const MAIN: Self = Self(0);
pub const SCROLLBAR: Self = Self(1);
pub const INDICATOR: Self = Self(2);
pub const KNOB: Self = Self(3);
pub const SELECTED: Self = Self(4);
pub const ITEMS: Self = Self(5);
pub const fn custom(id: u32) -> Self {
Self(id)
}
pub const fn bits(self) -> u32 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct State(u32);
impl State {
pub const DEFAULT: Self = Self(0);
pub const PRESSED: Self = Self(1 << 0);
pub const FOCUSED: Self = Self(1 << 1);
pub const CHECKED: Self = Self(1 << 2);
pub const DISABLED: Self = Self(1 << 3);
pub const fn bits(self) -> u32 {
self.0
}
}
impl BitOr for State {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
State(self.0 | rhs.0)
}
}
impl Default for State {
fn default() -> Self {
State::DEFAULT
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style {
pub bg_color: Color,
pub text_color: Color,
pub border_color: Color,
pub border_width: u8,
pub radius: u8,
pub padding: u8,
pub margin: u8,
}
impl Default for Style {
fn default() -> Self {
Self {
bg_color: Color(255, 255, 255, 255),
text_color: Color(0, 0, 0, 255),
border_color: Color(0, 0, 0, 255),
border_width: 0,
radius: 0,
padding: 0,
margin: 0,
}
}
}
#[derive(Debug, Default)]
pub struct StyleBuilder {
style: Style,
}
impl StyleBuilder {
pub fn new() -> Self {
Self {
style: Style::default(),
}
}
pub fn bg(mut self, color: Color) -> Self {
self.style.bg_color = color;
self
}
pub fn text(mut self, color: Color) -> Self {
self.style.text_color = color;
self
}
pub fn border_color(mut self, color: Color) -> Self {
self.style.border_color = color;
self
}
pub fn border_width(mut self, width: u8) -> Self {
self.style.border_width = width;
self
}
pub fn radius(mut self, radius: u8) -> Self {
self.style.radius = radius;
self
}
pub fn padding(mut self, value: u8) -> Self {
self.style.padding = value;
self
}
pub fn margin(mut self, value: u8) -> Self {
self.style.margin = value;
self
}
pub fn build(self) -> Style {
self.style
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_sets_all_fields() {
let style = StyleBuilder::new()
.bg(Color(1, 2, 3, 255))
.text(Color(4, 5, 6, 255))
.border_color(Color(7, 8, 9, 255))
.border_width(2)
.radius(3)
.padding(4)
.margin(5)
.build();
assert_eq!(style.bg_color, Color(1, 2, 3, 255));
assert_eq!(style.text_color, Color(4, 5, 6, 255));
assert_eq!(style.border_color, Color(7, 8, 9, 255));
assert_eq!(style.border_width, 2);
assert_eq!(style.radius, 3);
assert_eq!(style.padding, 4);
assert_eq!(style.margin, 5);
}
}