2024-01-02 04:58:55 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2024-01-20 19:35:10 +00:00
|
|
|
use crate::components::{ParseError, TextEntry};
|
2024-01-02 04:58:55 +00:00
|
|
|
use dimensioned::si;
|
2024-01-20 19:35:10 +00:00
|
|
|
use gtk::prelude::*;
|
2024-01-02 04:58:55 +00:00
|
|
|
|
2024-01-15 20:53:01 +00:00
|
|
|
pub struct Weight {
|
|
|
|
label: gtk::Label,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Weight {
|
2024-01-18 14:00:08 +00:00
|
|
|
pub fn new(weight: Option<si::Kilogram<f64>>) -> Self {
|
2024-01-15 20:53:01 +00:00
|
|
|
let label = gtk::Label::builder()
|
|
|
|
.css_classes(["card", "weight-view"])
|
|
|
|
.can_focus(true)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
match weight {
|
2024-01-18 14:00:08 +00:00
|
|
|
Some(w) => label.set_text(&format!("{:?}", w)),
|
2024-01-15 20:53:01 +00:00
|
|
|
None => label.set_text("No weight recorded"),
|
|
|
|
}
|
|
|
|
|
|
|
|
Self { label }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn widget(&self) -> gtk::Widget {
|
|
|
|
self.label.clone().upcast()
|
|
|
|
}
|
2024-01-02 04:58:55 +00:00
|
|
|
}
|
2024-01-16 04:27:55 +00:00
|
|
|
|
2024-01-20 21:05:33 +00:00
|
|
|
pub fn weight_editor<OnUpdate>(
|
|
|
|
weight: Option<si::Kilogram<f64>>,
|
|
|
|
on_update: OnUpdate,
|
|
|
|
) -> TextEntry<si::Kilogram<f64>>
|
|
|
|
where
|
|
|
|
OnUpdate: Fn(si::Kilogram<f64>) + 'static,
|
|
|
|
{
|
|
|
|
TextEntry::new(
|
|
|
|
"0 kg",
|
|
|
|
weight,
|
|
|
|
|val: &si::Kilogram<f64>| val.to_string(),
|
|
|
|
move |v: &str| {
|
|
|
|
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
|
|
|
|
match new_weight {
|
|
|
|
Ok(w) => {
|
|
|
|
on_update(w);
|
|
|
|
Ok(w)
|
|
|
|
}
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2024-01-16 04:27:55 +00:00
|
|
|
}
|