Add /calendar
This commit is contained in:
parent
d20c5a49a4
commit
853895df79
5 changed files with 152 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ rust-embed = "8.3.0"
|
|||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
showbits-common.workspace = true
|
||||
taffy.workspace = true
|
||||
time = "0.3.34"
|
||||
tokio = { version = "1.36.0", features = ["full"] }
|
||||
|
||||
[lints]
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
mod calendar;
|
||||
|
||||
use image::RgbaImage;
|
||||
use showbits_common::{
|
||||
color::{BLACK, WHITE},
|
||||
|
|
@ -20,6 +22,7 @@ pub enum Command {
|
|||
Image(RgbaImage),
|
||||
Photo { image: RgbaImage, title: String },
|
||||
ChatMessage { username: String, content: String },
|
||||
Calendar { year: i32, month: u8 },
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
@ -72,6 +75,7 @@ impl Drawer {
|
|||
Command::ChatMessage { username, content } => {
|
||||
self.on_chat_message(username, content)?
|
||||
}
|
||||
Command::Calendar { year, month } => self.draw_calendar(year, month)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
89
showbits-thermal-printer/src/drawer/calendar.rs
Normal file
89
showbits-thermal-printer/src/drawer/calendar.rs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
use showbits_common::{
|
||||
color::{BLACK, WHITE},
|
||||
widgets::{Block, Text},
|
||||
Node, Tree, WidgetExt,
|
||||
};
|
||||
use taffy::{
|
||||
style_helpers::{length, percent, repeat}, AlignContent, AlignItems, Display, FlexDirection
|
||||
};
|
||||
use time::Date;
|
||||
|
||||
use super::{Context, Drawer};
|
||||
|
||||
impl Drawer {
|
||||
pub fn draw_calendar(&mut self, year: i32, month: u8) -> anyhow::Result<()> {
|
||||
let mut date = Date::from_calendar_date(year, month.try_into()?, 1)?;
|
||||
|
||||
let mut tree = Tree::<Context>::new(WHITE);
|
||||
|
||||
let mut grid = Node::empty()
|
||||
.with_display(Display::Grid)
|
||||
.with_grid_template_columns(vec![repeat(7, vec![length(50.0)])])
|
||||
.with_grid_auto_rows(vec![length(50.0)])
|
||||
.with_gap(length(2.0));
|
||||
|
||||
for weekday in ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"] {
|
||||
let text = Text::new()
|
||||
.with_metrics(Text::default_metrics().scale(2.0))
|
||||
.and_plain(weekday)
|
||||
.widget(&mut self.ctx.font_stuff)
|
||||
.node()
|
||||
.with_justify_self(Some(AlignItems::Center))
|
||||
.with_align_self(Some(AlignItems::Center))
|
||||
.register(&mut tree)?;
|
||||
|
||||
grid = grid.and_child(text);
|
||||
}
|
||||
|
||||
let placeholders = date.weekday().number_days_from_monday();
|
||||
for _ in 0..placeholders {
|
||||
let empty = Node::empty().register(&mut tree)?;
|
||||
grid = grid.and_child(empty);
|
||||
}
|
||||
|
||||
loop {
|
||||
let day = Text::new()
|
||||
.and_plain(date.day().to_string())
|
||||
.widget(&mut self.ctx.font_stuff)
|
||||
.node()
|
||||
.register(&mut tree)?;
|
||||
|
||||
let block = Block::new()
|
||||
.with_border(BLACK)
|
||||
.node()
|
||||
.with_border(length(2.0))
|
||||
.with_display(Display::Flex)
|
||||
.with_justify_content(Some(AlignContent::Center))
|
||||
.with_align_items(Some(AlignItems::Center))
|
||||
.and_child(day)
|
||||
.register(&mut tree)?;
|
||||
|
||||
grid = grid.and_child(block);
|
||||
|
||||
let next_day = date.next_day().unwrap();
|
||||
if date.month() != next_day.month() {
|
||||
break;
|
||||
}
|
||||
date = next_day;
|
||||
}
|
||||
|
||||
let title = Text::new()
|
||||
.and_plain(format!("Ankreuzkalender {year:04}-{month:02}"))
|
||||
.widget(&mut self.ctx.font_stuff)
|
||||
.node()
|
||||
.register(&mut tree)?;
|
||||
|
||||
let root = Node::empty()
|
||||
.with_size_width(percent(1.0))
|
||||
.with_padding_bottom(length(Self::FEED))
|
||||
.with_display(Display::Flex)
|
||||
.with_flex_direction(FlexDirection::Column)
|
||||
.with_align_items(Some(AlignItems::Center))
|
||||
.and_child(title)
|
||||
.and_child(grid.register(&mut tree)?)
|
||||
.register(&mut tree)?;
|
||||
|
||||
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
|
|||
.route("/image", post(post_image).fallback(get_static_file))
|
||||
.route("/photo", post(post_photo).fallback(get_static_file))
|
||||
.route("/chat_message", post(post_chat_message))
|
||||
.route("/calendar", post(post_calendar))
|
||||
.fallback(get(get_static_file))
|
||||
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
|
||||
.with_state(Server { tx });
|
||||
|
|
@ -124,3 +125,19 @@ async fn post_chat_message(server: State<Server>, request: Form<PostChatMessageF
|
|||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PostCalendarForm {
|
||||
year: i32,
|
||||
month: u8,
|
||||
}
|
||||
|
||||
async fn post_calendar(server: State<Server>, request: Form<PostCalendarForm>) {
|
||||
let _ = server
|
||||
.tx
|
||||
.send(Command::Calendar {
|
||||
year: request.0.year,
|
||||
month: request.0.month,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue