Try to draw simple "Hello world!"

This commit is contained in:
Joscha 2024-03-08 17:01:54 +01:00
parent 13ed6ef3e3
commit 6ab43a8d5e
6 changed files with 47 additions and 8 deletions

1
Cargo.lock generated
View file

@ -1173,6 +1173,7 @@ dependencies = [
"anyhow",
"axum",
"clap",
"cosmic-text",
"escpos",
"image",
"palette",

View file

@ -8,6 +8,7 @@ edition = "2021"
[workspace.dependencies]
anyhow = "1.0.80"
cosmic-text = "0.11.2"
image = "0.24.9"
palette = "0.7.5"
showbits-common.path = "./showbits-common"

View file

@ -5,7 +5,7 @@ edition.workspace = true
[dependencies]
anyhow.workspace = true
cosmic-text = "0.11.2"
cosmic-text.workspace = true
image.workspace = true
palette.workspace = true
taffy.workspace = true

View file

@ -143,6 +143,6 @@ impl<C: HasFontStuff> Widget<C> for Text {
view.rect(area, color);
});
todo!()
Ok(())
}
}

View file

@ -7,6 +7,7 @@ edition.workspace = true
anyhow.workspace = true
axum = "0.7.4"
clap = { version = "4.5.1", features = ["derive", "deprecated"] }
cosmic-text.workspace = true
escpos = { version = "0.7.2", features = ["full"] }
image.workspace = true
palette.workspace = true

View file

@ -1,3 +1,10 @@
use cosmic_text::{Attrs, Metrics};
use palette::Srgb;
use showbits_common::{
widgets::{FontStuff, HasFontStuff, Text},
Tree, WidgetExt,
};
use taffy::style_helpers::length;
use tokio::sync::mpsc;
use crate::printer::Printer;
@ -10,14 +17,30 @@ pub enum Command {
ChatMessage { username: String, content: String },
}
#[derive(Default)]
struct Context {
font_stuff: FontStuff,
}
impl HasFontStuff for Context {
fn font_stuff(&mut self) -> &mut FontStuff {
&mut self.font_stuff
}
}
pub struct Drawer {
rx: mpsc::Receiver<Command>,
printer: Printer,
ctx: Context,
}
impl Drawer {
pub fn new(rx: mpsc::Receiver<Command>, printer: Printer) -> Self {
Self { rx, printer }
Self {
rx,
printer,
ctx: Context::default(),
}
}
pub fn run(&mut self) -> anyhow::Result<()> {
@ -35,17 +58,30 @@ impl Drawer {
match command {
Command::Stop => {} // Already handled one level above
Command::Rip => self.printer.rip()?,
Command::Test => todo!(),
Command::Test => self.on_test()?,
Command::Text(_) => todo!(),
Command::ChatMessage { username, content } => todo!(),
}
Ok(())
}
// fn on_rip(&mut self) -> anyhow::Result<()> {
// self.printer.init()?.feeds(6)?.print()?;
// Ok(())
// }
fn on_test(&mut self) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new();
let root = Text::simple(
&mut self.ctx.font_stuff,
Metrics::new(16.0, 32.0),
Attrs::new(),
"Hello world!",
)
.color(Srgb::new(1.0, 1.0, 1.0))
.node()
.margin_all(length(10.0))
.register(&mut tree)?;
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
Ok(())
}
// fn on_text(&mut self, text: String) -> anyhow::Result<()> {
// let text = util::sanitize(&text);