Skip to content

Commit

Permalink
Use e-g Text in ProgressBar
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 9, 2023
1 parent f6ad5bb commit 9c9e65d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 6 additions & 0 deletions gui/src/screens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use embedded_graphics::{
MonoTextStyle,
},
pixelcolor::BinaryColor,
text::{Alignment, Baseline, TextStyle, TextStyleBuilder},
};
use embedded_menu::{
builder::MenuBuilder,
Expand Down Expand Up @@ -59,6 +60,11 @@ pub const BOTTOM_CENTERED_TEXTBOX: TextBoxStyle = TextBoxStyleBuilder::new()
pub const NORMAL_TEXT: MonoTextStyle<'static, BinaryColor> =
MonoTextStyle::new(&FONT_6X10, BinaryColor::On);

pub const CENTERED_TEXT: TextStyle = TextStyleBuilder::new()
.alignment(Alignment::Center)
.baseline(Baseline::Middle)
.build();

#[derive(Clone, Copy, PartialEq)]
pub enum ChargingState {
Discharging,
Expand Down
9 changes: 4 additions & 5 deletions gui/src/widgets/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::{DrawTarget, Point, Size},
primitives::{Primitive, PrimitiveStyle, Rectangle},
text::Text,
Drawable,
};
use embedded_text::TextBox;

use crate::{
screens::{CENTERED_TEXTBOX, NORMAL_TEXT},
screens::{CENTERED_TEXT, NORMAL_TEXT},
utils::BinaryColorDrawTargetExt,
};

Expand All @@ -31,6 +31,7 @@ impl Drawable for ProgressBar<'_> {
const FILL_STYLE: PrimitiveStyle<BinaryColor> = PrimitiveStyle::with_fill(BinaryColor::On);

let progress_border = Rectangle::new(Point::new(0, 51), Size::new(128, 13));
let text_center = progress_border.anchor_point(AnchorPoint::Center);
let filler_area = progress_border.offset(-2); // 1px gap between border and fill

let max_progress = self.max_progress.get();
Expand All @@ -51,9 +52,7 @@ impl Drawable for ProgressBar<'_> {
// of the progress bar with one draw call
let mut draw_area = display.invert_area(&progress_filler);

// using embedded-text because I'm lazy to position the label vertically
TextBox::with_textbox_style(self.label, progress_border, NORMAL_TEXT, CENTERED_TEXTBOX)
.set_vertical_offset(1) // Slight adjustment
Text::with_text_style(self.label, text_center, NORMAL_TEXT, CENTERED_TEXT)
.draw(&mut draw_area)?;

Ok(())
Expand Down

0 comments on commit 9c9e65d

Please sign in to comment.