Skip to content

Commit

Permalink
Don't use cropped draw targets to draw status bar items
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 15, 2023
1 parent a5d293f commit 73ecb42
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
36 changes: 28 additions & 8 deletions gui/examples/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ use embedded_graphics::{
use embedded_graphics_simulator::{
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use gui::screens::init::StartupScreen;
use gui::{
screens::{init::StartupScreen, screen::Screen, BatteryInfo, ChargingState},
widgets::{
battery_small::{Battery, BatteryStyle},
status_bar::StatusBar,
wifi::{WifiState, WifiStateView},
},
};

fn main() -> Result<(), Infallible> {
let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(128, 64));
Expand All @@ -24,14 +31,27 @@ fn main() -> Result<(), Infallible> {
'running: loop {
display.clear(BinaryColor::Off).unwrap();

StartupScreen {
label: "Release to shutdown",
progress: if progress > 255 {
510 - progress
} else {
progress
Screen {
content: StartupScreen {
label: "Release to shutdown",
progress: if progress > 255 {
510 - progress
} else {
progress
},
},
status_bar: StatusBar {
battery: Battery::with_style(
Some(BatteryInfo {
voltage: 4100,
percentage: 90,
charging_state: ChargingState::Charging,
is_low: false,
}),
BatteryStyle::Percentage,
),
wifi: WifiStateView::enabled(WifiState::Connected),
},
max_progress: 255,
}
.draw(&mut display)
.unwrap();
Expand Down
26 changes: 13 additions & 13 deletions gui/src/widgets/battery_small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use embedded_graphics::{
geometry::AnchorPoint,
image::{Image, ImageRaw},
pixelcolor::BinaryColor,
prelude::{DrawTarget, DrawTargetExt, OriginDimensions, Point, Size},
prelude::{DrawTarget, OriginDimensions, Point, Size},
primitives::{Primitive, PrimitiveStyle, Rectangle},
text::{renderer::TextRenderer, Alignment, Baseline, Text, TextStyleBuilder},
Drawable,
Expand Down Expand Up @@ -92,7 +92,7 @@ pub enum BatteryStyle {
impl BatteryStyle {
#[rustfmt::skip]
#[allow(clippy::unusual_byte_groupings)]
const BATTERY_OUTLINE: ImageRaw<'_, BinaryColor> = ImageRaw::new(
const BATTERY_OUTLINE: ImageRaw<'static, BinaryColor> = ImageRaw::new(
&[
0b00000000, 0b00000_000,
0b11111111, 0b11110_000,
Expand All @@ -109,7 +109,7 @@ impl BatteryStyle {

#[rustfmt::skip]
#[allow(clippy::unusual_byte_groupings)]
const LOW_BATTERY: ImageRaw<'_, BinaryColor> = ImageRaw::new(
const LOW_BATTERY: ImageRaw<'static, BinaryColor> = ImageRaw::new(
&[
0b00000000, 0b00000_000,
0b11111111, 0b11110_000,
Expand Down Expand Up @@ -162,10 +162,12 @@ impl BatteryStyle {
&self,
target: &mut D,
string: &str,
bounds: &Rectangle,
) -> Result<u32, D::Error> {
let top_right = bounds.anchor_point(AnchorPoint::TopRight);
Text::with_text_style(
string,
Point::new(target.bounding_box().size.width as i32 - 1, 0),
top_right,
NORMAL_TEXT,
TextStyleBuilder::new()
.baseline(Baseline::Top)
Expand All @@ -185,6 +187,7 @@ impl BatteryStyle {
&self,
target: &mut D,
data: BatteryInfo,
bounds: &Rectangle,
) -> Result<(), D::Error> {
let battery_data_width = match self {
BatteryStyle::MilliVolts | BatteryStyle::Percentage => {
Expand All @@ -196,11 +199,11 @@ impl BatteryStyle {
_ = uwrite!(&mut string, "{}%", data.percentage);
}

self.draw_text(target, &string)?
self.draw_text(target, &string, bounds)?
}
BatteryStyle::LowIndicator if !data.is_charging() => {
if data.percentage < 25 {
let top_right = target.bounding_box().anchor_point(AnchorPoint::TopRight);
let top_right = bounds.anchor_point(AnchorPoint::TopRight);
let box_top_left = self.draw_low_battery(target, top_right)?;

(top_right.x - box_top_left.x + 1) as u32
Expand All @@ -211,7 +214,7 @@ impl BatteryStyle {
BatteryStyle::Icon | BatteryStyle::LowIndicator => {
let bars = (data.percentage.saturating_sub(1)) / 25;

let top_right = target.bounding_box().anchor_point(AnchorPoint::TopRight);
let top_right = bounds.anchor_point(AnchorPoint::TopRight);
let box_top_left = self.draw_battery_outline(target, top_right)?;

let mut top_left = box_top_left + Point::new(1, 3);
Expand All @@ -229,10 +232,8 @@ impl BatteryStyle {

ChargingIndicator {
state: data.charging_state,
top_right: Point::new(
(target.bounding_box().size.width - battery_data_width) as i32,
0,
),
top_right: bounds.anchor_point(AnchorPoint::TopRight)
- Point::new(battery_data_width as i32, 0),
}
.draw(target)?;

Expand Down Expand Up @@ -289,8 +290,7 @@ impl Drawable for Battery {
D: DrawTarget<Color = Self::Color>,
{
if let Some(data) = self.data {
let mut cropped = target.cropped(&self.bounds());
self.style.draw(&mut cropped, data)?;
self.style.draw(target, data, &self.bounds())?;
}

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions gui/src/widgets/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use embedded_graphics::{
image::{Image, ImageRaw},
pixelcolor::BinaryColor,
prelude::{Dimensions, DrawTarget, DrawTargetExt, Point, Size},
prelude::{Dimensions, DrawTarget, Point, Size},
primitives::Rectangle,
Drawable,
};
Expand Down Expand Up @@ -121,8 +121,7 @@ impl Drawable for WifiStateView {
D: DrawTarget<Color = Self::Color>,
{
if let Some(data) = self.data {
let mut cropped = target.cropped(&self.bounds());
Image::new(&data.image(), Point::zero()).draw(&mut cropped)?;
Image::new(&data.image(), self.top_left).draw(target)?;
}

Ok(())
Expand Down

0 comments on commit 73ecb42

Please sign in to comment.