Create a validated text entry widget
I move the weight edit view into the validated text entry widget, and I work on some of the unfortunate logic in the weight blur function. I've left behind a lot of breadcrumbs for things that still need to be done.
This commit is contained in:
parent
9970161c30
commit
a25b76d230
|
@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
|
|
||||||
// use chrono::NaiveDate;
|
// use chrono::NaiveDate;
|
||||||
// use ft_core::TraxRecord;
|
// use ft_core::TraxRecord;
|
||||||
|
use crate::components::{ParseError, TextEntry};
|
||||||
use chrono::{Local, NaiveDate};
|
use chrono::{Local, NaiveDate};
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
use emseries::Record;
|
use emseries::Record;
|
||||||
|
@ -220,12 +221,23 @@ impl DayDetail {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
enum WeightViewMode {
|
||||||
|
View,
|
||||||
|
Edit,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct WeightViewPrivate {
|
pub struct WeightViewPrivate {
|
||||||
date: RefCell<NaiveDate>,
|
date: RefCell<NaiveDate>,
|
||||||
record: RefCell<Option<Weight>>,
|
record: RefCell<Option<Weight>>,
|
||||||
view: RefCell<gtk::Label>,
|
view: RefCell<gtk::Label>,
|
||||||
edit: RefCell<gtk::Entry>,
|
edit: RefCell<TextEntry<si::Kilogram<f64>>>,
|
||||||
current: RefCell<gtk::Widget>,
|
current: RefCell<gtk::Widget>,
|
||||||
|
mode: RefCell<WeightViewMode>,
|
||||||
|
|
||||||
|
// If I create a swappable component and don't make it a true GTK component, the way
|
||||||
|
// TextEntry<T> ended up not a true GTK component, on_edit_finished will not need to be a
|
||||||
|
// RefCell.
|
||||||
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,7 +248,9 @@ impl Default for WeightViewPrivate {
|
||||||
.halign(gtk::Align::Start)
|
.halign(gtk::Align::Start)
|
||||||
.can_focus(true)
|
.can_focus(true)
|
||||||
.build();
|
.build();
|
||||||
let edit = gtk::Entry::builder().halign(gtk::Align::Start).build();
|
let edit = TextEntry::<si::Kilogram<f64>>::new("weight", None, &|w: &str| {
|
||||||
|
w.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError)
|
||||||
|
});
|
||||||
|
|
||||||
let current = view.clone();
|
let current = view.clone();
|
||||||
|
|
||||||
|
@ -246,6 +260,7 @@ impl Default for WeightViewPrivate {
|
||||||
view: RefCell::new(view),
|
view: RefCell::new(view),
|
||||||
edit: RefCell::new(edit),
|
edit: RefCell::new(edit),
|
||||||
current: RefCell::new(current.upcast()),
|
current: RefCell::new(current.upcast()),
|
||||||
|
mode: RefCell::new(WeightViewMode::View),
|
||||||
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,16 +322,18 @@ impl WeightView {
|
||||||
view.set_label("No weight recorded");
|
view.set_label("No weight recorded");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*self.imp().mode.borrow_mut() = WeightViewMode::View;
|
||||||
self.swap(view.clone().upcast());
|
self.swap(view.clone().upcast());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn edit(&self) {
|
fn edit(&self) {
|
||||||
let edit = self.imp().edit.borrow();
|
let edit = self.imp().edit.borrow();
|
||||||
match *self.imp().record.borrow() {
|
match *self.imp().record.borrow() {
|
||||||
Some(ref record) => edit.buffer().set_text(&format!("{:?}", record.weight)),
|
Some(ref record) => edit.set_value(Some(record.weight)),
|
||||||
None => edit.buffer().set_text(""),
|
None => edit.set_value(None),
|
||||||
}
|
}
|
||||||
self.swap(edit.clone().upcast());
|
*self.imp().mode.borrow_mut() = WeightViewMode::Edit;
|
||||||
|
self.swap(edit.widget());
|
||||||
edit.grab_focus();
|
edit.grab_focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,20 +345,44 @@ impl WeightView {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blur(&self) {
|
fn blur(&self) {
|
||||||
let edit = self.imp().edit.borrow();
|
if *self.imp().mode.borrow() == WeightViewMode::Edit {
|
||||||
if *self.imp().current.borrow() == *edit {
|
println!("on_blur");
|
||||||
let w = edit.buffer().text().parse::<f64>().unwrap();
|
let weight = self.imp().edit.borrow().value();
|
||||||
self.imp().on_edit_finished.borrow()(w * si::KG);
|
|
||||||
|
|
||||||
let mut record = self.imp().record.borrow_mut();
|
println!("new weight: {:?}", weight);
|
||||||
match *record {
|
|
||||||
Some(ref mut record) => record.weight = w * si::KG,
|
// This has really turned into rubbish
|
||||||
None => {
|
// on_edit_finished needs to accept a full record now.
|
||||||
*record = Some(Weight {
|
// needs to be possible to delete a record if the value is None
|
||||||
|
// it's hard to be sure whether I need the full record object or if I need to update
|
||||||
|
// it. I probably don't. I think I need to borrow it and call on_edit_finished with an
|
||||||
|
// updated version of it.
|
||||||
|
// on_edit_finished still doesn't have a way to support a delete operation
|
||||||
|
let record = match (self.imp().record.borrow().clone(), weight) {
|
||||||
|
// update an existing record
|
||||||
|
(Some(record), Some(weight)) => Some(Weight {
|
||||||
|
date: record.date,
|
||||||
|
weight,
|
||||||
|
}),
|
||||||
|
|
||||||
|
// create a new record
|
||||||
|
(None, Some(weight)) => Some(Weight {
|
||||||
date: self.imp().date.borrow().clone(),
|
date: self.imp().date.borrow().clone(),
|
||||||
weight: w * si::KG,
|
weight,
|
||||||
})
|
}),
|
||||||
|
|
||||||
|
// do nothing or delete an existing record
|
||||||
|
(_, None) => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("updated record: {:?}", record);
|
||||||
|
|
||||||
|
match record {
|
||||||
|
Some(record) => {
|
||||||
|
self.imp().on_edit_finished.borrow()(record.weight);
|
||||||
|
*self.imp().record.borrow_mut() = Some(record);
|
||||||
}
|
}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.view();
|
self.view();
|
||||||
|
|
|
@ -17,6 +17,9 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
mod day;
|
mod day;
|
||||||
pub use day::{DayDetail, DaySummary};
|
pub use day::{DayDetail, DaySummary};
|
||||||
|
|
||||||
|
mod text_entry;
|
||||||
|
pub use text_entry::{ParseError, TextEntry};
|
||||||
|
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
use gtk::{prelude::*, subclass::prelude::*};
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
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 gtk::prelude::*;
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
pub struct ParseError;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TextEntry<T: Clone> {
|
||||||
|
value: Rc<RefCell<Option<T>>>,
|
||||||
|
widget: gtk::Entry,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone> TextEntry<T> {
|
||||||
|
pub fn new(
|
||||||
|
placeholder: &str,
|
||||||
|
value: Option<T>,
|
||||||
|
validator: &'static dyn Fn(&str) -> Result<T, ParseError>,
|
||||||
|
) -> Self {
|
||||||
|
let widget = gtk::Entry::builder().placeholder_text(placeholder).build();
|
||||||
|
|
||||||
|
let s = Self {
|
||||||
|
value: Rc::new(RefCell::new(value)),
|
||||||
|
widget,
|
||||||
|
};
|
||||||
|
|
||||||
|
s.widget.buffer().connect_text_notify({
|
||||||
|
let s = s.clone();
|
||||||
|
move |buffer| {
|
||||||
|
if buffer.text().is_empty() {
|
||||||
|
*s.value.borrow_mut() = None;
|
||||||
|
}
|
||||||
|
match validator(buffer.text().as_str()) {
|
||||||
|
Ok(v) => *s.value.borrow_mut() = Some(v),
|
||||||
|
// need to change the border to provide a visual indicator of an error
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn value(&self) -> Option<T> {
|
||||||
|
self.value.borrow().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_value(&self, value: Option<T>) {
|
||||||
|
*self.value.borrow_mut() = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn grab_focus(&self) {
|
||||||
|
self.widget.grab_focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn widget(&self) -> gtk::Widget {
|
||||||
|
self.widget.clone().upcast::<gtk::Widget>()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue