Add /image endpoint

This commit is contained in:
Joscha 2024-03-09 22:47:10 +01:00
parent ef5f0e3af4
commit 74b0d25dfc
4 changed files with 80 additions and 3 deletions

View file

@ -1,6 +1,7 @@
use image::RgbaImage;
use showbits_common::{
color::{BLACK, WHITE},
widgets::{Block, FontStuff, HasFontStuff, Text},
widgets::{Block, FontStuff, HasFontStuff, Image, Text},
Node, Tree, WidgetExt,
};
use taffy::{
@ -16,6 +17,7 @@ pub enum Command {
Rip,
Test,
Text(String),
Image(RgbaImage),
ChatMessage { username: String, content: String },
}
@ -62,6 +64,7 @@ impl Drawer {
Command::Rip => self.printer.rip()?,
Command::Test => self.on_test()?,
Command::Text(text) => self.on_text(text)?,
Command::Image(image) => self.on_image(image)?,
Command::ChatMessage { username, content } => {
self.on_chat_message(username, content)?
}
@ -120,6 +123,24 @@ impl Drawer {
Ok(())
}
fn on_image(&mut self, image: RgbaImage) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
let image = Image::new(image)
.with_dither_palette(&[BLACK, WHITE])
.node()
.register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.with_padding_bottom(length(32.0))
.and_child(image)
.register(&mut tree)?;
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
Ok(())
}
fn on_chat_message(&mut self, username: String, content: String) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);