monorepo/fitnesstrax/app/src/components/weight.rs

67 lines
1.9 KiB
Rust

/*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of FitnessTrax.
FitnessTrax is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{
components::TextEntry,
types::{FormatOption, Weight},
};
use gtk::prelude::*;
pub struct WeightLabel {
label: gtk::Label,
}
impl WeightLabel {
pub fn new(weight: Option<Weight>) -> Self {
let label = gtk::Label::builder()
.css_classes(["card", "weight-view"])
.can_focus(true)
.build();
match weight {
Some(w) => label.set_text(&w.format(FormatOption::Abbreviated)),
None => label.set_text("No weight recorded"),
}
Self { label }
}
pub fn widget(&self) -> gtk::Widget {
self.label.clone().upcast()
}
}
pub fn weight_editor<OnUpdate>(weight: Option<Weight>, on_update: OnUpdate) -> TextEntry<Weight>
where
OnUpdate: Fn(Weight) + 'static,
{
TextEntry::new(
"0 kg",
weight,
|val: &Weight| val.format(FormatOption::Abbreviated),
move |v: &str| {
let new_weight = Weight::parse(v);
match new_weight {
Ok(w) => {
on_update(w);
Ok(w)
}
Err(err) => Err(err),
}
},
)
}