Skip to content

Commit db008f8

Browse files
committed
ui: screens: iobus/power: make the button legend responsive to output status
For these two screens it is rather easy to make the legend dynamic. For other screens it is a bit more complicated, so only do these two first. Signed-off-by: Leonard Göhrs <[email protected]>
1 parent cba87c2 commit db008f8

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/ui/screens/iobus.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl ActivatableScreen for IoBusScreen {
5656

5757
display.with_lock(|target| {
5858
draw_border(target, "IOBus", SCREEN_TYPE);
59-
draw_button_legend(target, "Toggle", "Screen");
6059

6160
Text::new("CAN Status:", row_anchor(0), ui_text_style)
6261
.draw(target)
@@ -134,6 +133,21 @@ impl ActivatableScreen for IoBusScreen {
134133
)
135134
});
136135

136+
widgets.push(|display| {
137+
DynamicWidget::button_legend(
138+
ui.res.regulators.iobus_pwr_en.clone(),
139+
display,
140+
|state: &bool| {
141+
let lower = match *state {
142+
false => "Turn On",
143+
true => "Turn Off",
144+
};
145+
146+
(lower.into(), "Screen".into())
147+
},
148+
)
149+
});
150+
137151
let iobus_pwr_en = ui.res.regulators.iobus_pwr_en.clone();
138152

139153
let active = Active {

src/ui/screens/power.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ impl ActivatableScreen for PowerScreen {
5656
}
5757

5858
fn activate(&mut self, ui: &Ui, display: Display) -> Box<dyn ActiveScreen> {
59-
display.with_lock(|target| {
60-
draw_border(target, "DUT Power", SCREEN_TYPE);
61-
draw_button_legend(target, "Toggle", "Screen");
62-
});
59+
display.with_lock(|target| draw_border(target, "DUT Power", SCREEN_TYPE));
6360

6461
let mut widgets = WidgetContainer::new(display);
6562

@@ -135,6 +132,21 @@ impl ActivatableScreen for PowerScreen {
135132
)
136133
});
137134

135+
widgets.push(|display| {
136+
DynamicWidget::button_legend(
137+
ui.res.dut_pwr.state.clone(),
138+
display,
139+
|state: &OutputState| {
140+
let lower = match state {
141+
OutputState::On => "Turn Off",
142+
_ => "Turn On",
143+
};
144+
145+
(lower.into(), "Screen".into())
146+
},
147+
)
148+
});
149+
138150
let power_state = ui.res.dut_pwr.state.clone();
139151
let power_request = ui.res.dut_pwr.request.clone();
140152

src/ui/widgets.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,27 @@ impl<T: Serialize + DeserializeOwned + Send + Sync + Clone + 'static> DynamicWid
318318
)
319319
}
320320

321+
/// Draw a dynamic button legend
322+
///
323+
/// Sometimes you want to draw different button legend text based on
324+
/// external input. For example change from "Turn On" to "Turn Off" when
325+
/// the status of the controlled value changes.
326+
pub fn button_legend(
327+
topic: Arc<Topic<T>>,
328+
display: Arc<Display>,
329+
format_fn: fn(&T) -> (String, String),
330+
) -> Self {
331+
Self::new(
332+
topic,
333+
display,
334+
Box::new(move |msg, target| {
335+
let (lower, upper) = format_fn(msg);
336+
337+
Some(draw_button_legend(target, &lower, &upper))
338+
}),
339+
)
340+
}
341+
321342
/// Draw self-updating text with configurable alignment
322343
pub fn text_aligned(
323344
topic: Arc<Topic<T>>,

0 commit comments

Comments
 (0)