use tuirealm::command::{Cmd, CmdResult};
use tuirealm::component::Component;
use tuirealm::props::{
AttrValue, Attribute, Borders, Color, PropPayload, PropValue, Props, QueryResult, Style,
TextModifiers, Title,
};
use tuirealm::ratatui::Frame;
use tuirealm::ratatui::layout::Rect;
use tuirealm::ratatui::widgets::Gauge as TuiGauge;
use tuirealm::state::State;
use crate::prop_ext::CommonProps;
#[derive(Default)]
#[must_use]
pub struct Gauge {
common: CommonProps,
props: Props,
}
impl Gauge {
pub fn foreground(mut self, fg: Color) -> Self {
self.attr(Attribute::Foreground, AttrValue::Color(fg));
self
}
pub fn background(mut self, bg: Color) -> Self {
self.attr(Attribute::Background, AttrValue::Color(bg));
self
}
pub fn modifiers(mut self, m: TextModifiers) -> Self {
self.attr(Attribute::TextProps, AttrValue::TextModifiers(m));
self
}
pub fn style(mut self, style: Style) -> Self {
self.attr(Attribute::Style, AttrValue::Style(style));
self
}
pub fn inactive(mut self, s: Style) -> Self {
self.attr(Attribute::UnfocusedBorderStyle, AttrValue::Style(s));
self
}
pub fn borders(mut self, b: Borders) -> Self {
self.attr(Attribute::Borders, AttrValue::Borders(b));
self
}
pub fn title<T: Into<Title>>(mut self, title: T) -> Self {
self.attr(Attribute::Title, AttrValue::Title(title.into()));
self
}
pub fn label<S: Into<String>>(mut self, s: S) -> Self {
self.attr(Attribute::Text, AttrValue::String(s.into()));
self
}
pub fn progress(mut self, p: f64) -> Self {
Self::assert_progress(p);
self.attr(
Attribute::Value,
AttrValue::Payload(PropPayload::Single(PropValue::F64(p))),
);
self
}
fn assert_progress(p: f64) {
assert!(
(0.0..=1.0).contains(&p),
"Progress value must be in range [0.0, 1.0]"
);
}
}
impl Component for Gauge {
fn view(&mut self, render: &mut Frame, area: Rect) {
if !self.common.display {
return;
}
let label = self
.props
.get(Attribute::Text)
.and_then(AttrValue::as_string);
let percentage = self
.props
.get(Attribute::Value)
.and_then(AttrValue::as_payload)
.and_then(PropPayload::as_single)
.and_then(PropValue::as_f64)
.unwrap_or_default();
let mut widget = TuiGauge::default()
.style(self.common.style)
.gauge_style(self.common.style)
.ratio(percentage)
.use_unicode(true);
if let Some(label) = label {
widget = widget.label(label.as_str());
}
if let Some(block) = self.common.get_block() {
widget = widget.block(block);
}
render.render_widget(widget, area);
}
fn query<'a>(&'a self, attr: Attribute) -> Option<QueryResult<'a>> {
if let Some(value) = self.common.get_for_query(attr) {
return Some(value);
}
self.props.get_for_query(attr)
}
fn attr(&mut self, attr: Attribute, value: AttrValue) {
if let Some(value) = self.common.set(attr, value) {
if let Attribute::Value = attr
&& let AttrValue::Payload(p) = value.clone()
{
Self::assert_progress(p.unwrap_single().unwrap_f64());
}
self.props.set(attr, value);
}
}
fn state(&self) -> State {
State::None
}
fn perform(&mut self, cmd: Cmd) -> CmdResult {
CmdResult::Invalid(cmd)
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use tuirealm::props::HorizontalAlignment;
use super::*;
#[test]
fn test_components_progress_bar() {
let component = Gauge::default()
.background(Color::Red)
.foreground(Color::White)
.progress(0.60)
.title(Title::from("Downloading file...").alignment(HorizontalAlignment::Center))
.label("60% - ETA 00:20")
.borders(Borders::default());
assert_eq!(component.state(), State::None);
}
#[test]
#[should_panic = "Progress value must be in range [0.0, 1.0]"]
fn test_components_progress_bar_bad_prog() {
let _ = Gauge::default()
.background(Color::Red)
.foreground(Color::White)
.progress(6.0)
.title(Title::from("Downloading file...").alignment(HorizontalAlignment::Center))
.label("60% - ETA 00:20")
.borders(Borders::default());
}
}