diff --git a/src/calendar.rs b/src/calendar.rs index 8a702ef..4cde76d 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -14,6 +14,8 @@ use embedded_graphics::{ use embedded_layout::prelude::*; use num_traits::cast::FromPrimitive; +use crate::Component; + fn font(height: u32) -> MonoFont<'static> { if height < 8 { return FONT_4X6; @@ -315,7 +317,30 @@ pub struct ScrollingCalendar { pub size: Size, pub style: ComponentStyle, pub day: chrono::NaiveDate, - pub weeks: u32, + pub prev_weeks: u32, + pub next_weeks: u32, +} + +impl ScrollingCalendar { + pub fn new(style: ComponentStyle, prev_weeks: u32, next_weeks: u32) -> Self { + Self { + top_left: Point::new(0, 0), + size: Size::new(0, 0), + style, + day: chrono::Local::now().naive_local().date(), + prev_weeks, + next_weeks, + } + } +} + +impl Component for ScrollingCalendar { + fn set_size(&mut self, size: Size) { + self.size = size; + } + fn set_top_left(&mut self, top_left: Point) { + self.top_left = top_left; + } } impl Drawable for ScrollingCalendar @@ -329,15 +354,16 @@ where where D: DrawTarget, { - let height = self.size.height / (1 + self.weeks * 2); + let total_weeks = self.prev_weeks + 1 + self.next_weeks; + let height = self.size.height / total_weeks; let mut week = ScrollingWeek::from_date( self .day - .checked_sub_signed(chrono::Duration::days(self.weeks as i64 * 7)) + .checked_sub_signed(chrono::Duration::days(self.prev_weeks as i64 * 7)) .unwrap(), ); - for i in 0..(1 + self.weeks * 2) { + for i in 0..total_weeks { week = week.succ(); let calendar_week = CalendarWeek { diff --git a/src/frame.rs b/src/frame.rs new file mode 100644 index 0000000..34e0307 --- /dev/null +++ b/src/frame.rs @@ -0,0 +1,82 @@ +use embedded_graphics::{pixelcolor::BinaryColor, prelude::*}; +use embedded_layout::View; + +pub trait Component { + fn set_top_left(&mut self, top_left: Point); + fn set_size(&mut self, size: Size); +} + +pub struct Frame { + //drawable: Component, + pub weight: f32, +} + +pub struct VSplit { + pub top_left: Point, + pub size: Size, + pub left: T, + pub right: U, + pub ratio: f32, +} + +impl VSplit { + pub fn new(left: T, right: U) -> Self { + VSplit { + top_left: Point::new(0, 0), + size: Size::new(0, 0), + left, + right, + ratio: 0.5, + } + } +} + +impl Component for VSplit +where + C: PixelColor + From, + T: Drawable, + U: Drawable, +{ + fn set_top_left(&mut self, top_left: Point) { + self.top_left = top_left; + } + fn set_size(&mut self, size: Size) { + self.size = size; + } +} + +impl Drawable for VSplit +where + C: PixelColor + From, + T: Drawable + Component + Clone, + U: Drawable + Component + Clone, +{ + type Color = C; + type Output = (); + + fn draw(&self, target: &mut D) -> Result + where + D: DrawTarget, + { + let left_width = (self.size.width as f32 * self.ratio) as u32; + let mut left = self.left.clone(); + left.set_top_left(self.top_left); + left.set_size(Size::new(self.size.height, left_width)); + + let mut right = self.right.clone(); + right.set_top_left(Point::new(left_width as i32, 0)); + right.set_size(Size::new(self.size.height, self.size.width - left_width)); + + left.draw(target)?; + right.draw(target)?; + + Ok(()) + } +} + +pub struct HSplit { + pub top_left: Point, + pub size: Size, + pub top: T, + pub bottom: T, +} diff --git a/src/lib.rs b/src/lib.rs index e08a390..dad8d84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,13 @@ mod bullet_counter; mod calendar; +mod frame; mod gauge; mod list_item; mod style; pub use bullet_counter::BulletCounter; pub use calendar::{Calendar, ScrollingCalendar}; +pub use frame::{Component, HSplit, VSplit}; pub use gauge::Gauge; pub use list_item::{List, ListItem, ListItemData}; pub use style::ComponentStyle; diff --git a/src/list_item.rs b/src/list_item.rs index 8c796d2..2d5d73b 100644 --- a/src/list_item.rs +++ b/src/list_item.rs @@ -11,6 +11,8 @@ use embedded_graphics::{ }; use embedded_layout::prelude::*; +use crate::Component; + pub struct ListItemData { pub text: String, pub icon: String, @@ -39,6 +41,27 @@ pub struct List { pub list_items: Vec, } +impl List { + pub fn new(style: ComponentStyle, list_items: Vec) -> Self { + Self { + style, + top_left: Point::default(), + size: Size::default(), + items_shown: list_items.len(), + list_items, + } + } +} + +impl Component for List { + fn set_size(&mut self, size: Size) { + self.size = size; + } + fn set_top_left(&mut self, top_left: Point) { + self.top_left = top_left; + } +} + impl Drawable for List where C: PixelColor + From,