Turn Calendar into drawing

This commit is contained in:
Joscha 2024-03-30 12:21:38 +01:00
parent a50e0999ca
commit d04a697d51
4 changed files with 26 additions and 16 deletions

View file

@ -16,6 +16,8 @@ use tokio::sync::mpsc;
use crate::printer::Printer; use crate::printer::Printer;
pub use calendar::CalendarDrawing;
#[derive(Default)] #[derive(Default)]
pub struct Context { pub struct Context {
font_stuff: FontStuff, font_stuff: FontStuff,
@ -35,7 +37,6 @@ pub enum Command {
Image { image: RgbaImage, bright: bool }, Image { image: RgbaImage, bright: bool },
Photo { image: RgbaImage, title: String }, Photo { image: RgbaImage, title: String },
ChatMessage { username: String, content: String }, ChatMessage { username: String, content: String },
Calendar { year: i32, month: u8 },
Cells { rule: u8, rows: u32, scale: u32 }, Cells { rule: u8, rows: u32, scale: u32 },
} }
@ -86,7 +87,6 @@ impl Drawer {
Command::ChatMessage { username, content } => { Command::ChatMessage { username, content } => {
self.on_chat_message(username, content)? self.on_chat_message(username, content)?
} }
Command::Calendar { year, month } => self.draw_calendar(year, month)?,
Command::Cells { rule, rows, scale } => self.draw_cells(rule, rows, scale)?, Command::Cells { rule, rows, scale } => self.draw_cells(rule, rows, scale)?,
} }
Ok(()) Ok(())

View file

@ -9,11 +9,18 @@ use taffy::{
}; };
use time::Date; use time::Date;
use super::{Context, Drawer}; use crate::printer::Printer;
impl Drawer { use super::{Context, Drawing};
pub fn draw_calendar(&mut self, year: i32, month: u8) -> anyhow::Result<()> {
let mut date = Date::from_calendar_date(year, month.try_into()?, 1)?; pub struct CalendarDrawing {
pub year: i32,
pub month: u8,
}
impl Drawing for CalendarDrawing {
fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> {
let mut date = Date::from_calendar_date(self.year, self.month.try_into()?, 1)?;
let mut tree = Tree::<Context>::new(WHITE); let mut tree = Tree::<Context>::new(WHITE);
@ -27,7 +34,7 @@ impl Drawer {
let text = Text::new() let text = Text::new()
.with_metrics(Text::default_metrics().scale(2.0)) .with_metrics(Text::default_metrics().scale(2.0))
.and_plain(weekday) .and_plain(weekday)
.widget(&mut self.ctx.font_stuff) .widget(&mut ctx.font_stuff)
.node() .node()
.with_justify_self(Some(AlignItems::Center)) .with_justify_self(Some(AlignItems::Center))
.with_align_self(Some(AlignItems::Center)) .with_align_self(Some(AlignItems::Center))
@ -45,7 +52,7 @@ impl Drawer {
loop { loop {
let day = Text::new() let day = Text::new()
.and_plain(date.day().to_string()) .and_plain(date.day().to_string())
.widget(&mut self.ctx.font_stuff) .widget(&mut ctx.font_stuff)
.node() .node()
.register(&mut tree)?; .register(&mut tree)?;
@ -69,14 +76,16 @@ impl Drawer {
} }
let title = Text::new() let title = Text::new()
.and_plain(format!("Ankreuzkalender {year:04}-{month:02}")) .and_plain(format!(
.widget(&mut self.ctx.font_stuff) "Ankreuzkalender {:04}-{:02}",
self.year, self.month
))
.widget(&mut ctx.font_stuff)
.node() .node()
.register(&mut tree)?; .register(&mut tree)?;
let root = Node::empty() let root = Node::empty()
.with_size_width(percent(1.0)) .with_size_width(percent(1.0))
.with_padding_bottom(length(Self::FEED))
.with_display(Display::Flex) .with_display(Display::Flex)
.with_flex_direction(FlexDirection::Column) .with_flex_direction(FlexDirection::Column)
.with_align_items(Some(AlignItems::Center)) .with_align_items(Some(AlignItems::Center))
@ -84,7 +93,8 @@ impl Drawer {
.and_child(grid.register(&mut tree)?) .and_child(grid.register(&mut tree)?)
.register(&mut tree)?; .register(&mut tree)?;
self.printer.print_tree(&mut tree, &mut self.ctx, root)?; printer.print_tree(&mut tree, ctx, root)?;
printer.feed()?;
Ok(()) Ok(())
} }
} }

View file

@ -56,7 +56,7 @@ impl Printer {
}) })
} }
pub fn rip(&mut self) -> anyhow::Result<()> { pub fn feed(&mut self) -> anyhow::Result<()> {
if let Some(printer) = &mut self.printer { if let Some(printer) = &mut self.printer {
printer.init()?.feeds(4)?.print()?; printer.init()?.feeds(4)?.print()?;
} }

View file

@ -12,7 +12,7 @@ use axum::{
use serde::Deserialize; use serde::Deserialize;
use tokio::{net::TcpListener, sync::mpsc}; use tokio::{net::TcpListener, sync::mpsc};
use crate::drawer::Command; use crate::drawer::{CalendarDrawing, Command};
use self::{r#static::get_static_file, statuscode::status_code}; use self::{r#static::get_static_file, statuscode::status_code};
@ -133,10 +133,10 @@ struct PostCalendarForm {
async fn post_calendar(server: State<Server>, request: Form<PostCalendarForm>) { async fn post_calendar(server: State<Server>, request: Form<PostCalendarForm>) {
let _ = server let _ = server
.tx .tx
.send(Command::Calendar { .send(Command::draw(CalendarDrawing {
year: request.0.year, year: request.0.year,
month: request.0.month, month: request.0.month,
}) }))
.await; .await;
} }