Clean up the parameters to TextEntry and populate the field
This commit is contained in:
parent
a25b76d230
commit
2e3d5fc5a4
|
@ -248,9 +248,12 @@ impl Default for WeightViewPrivate {
|
||||||
.halign(gtk::Align::Start)
|
.halign(gtk::Align::Start)
|
||||||
.can_focus(true)
|
.can_focus(true)
|
||||||
.build();
|
.build();
|
||||||
let edit = TextEntry::<si::Kilogram<f64>>::new("weight", None, &|w: &str| {
|
let edit = TextEntry::<si::Kilogram<f64>>::new(
|
||||||
w.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError)
|
"weight",
|
||||||
});
|
None,
|
||||||
|
|w: &si::Kilogram<f64>| w.to_string(),
|
||||||
|
|w: &str| w.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError),
|
||||||
|
);
|
||||||
|
|
||||||
let current = view.clone();
|
let current = view.clone();
|
||||||
|
|
||||||
|
|
|
@ -23,43 +23,65 @@ pub struct ParseError;
|
||||||
pub struct TextEntry<T: Clone> {
|
pub struct TextEntry<T: Clone> {
|
||||||
value: Rc<RefCell<Option<T>>>,
|
value: Rc<RefCell<Option<T>>>,
|
||||||
widget: gtk::Entry,
|
widget: gtk::Entry,
|
||||||
|
renderer: Rc<Box<dyn Fn(&T) -> String>>,
|
||||||
|
validator: Rc<Box<dyn Fn(&str) -> Result<T, ParseError>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> TextEntry<T> {
|
// I do not understand why the data should be 'static.
|
||||||
pub fn new(
|
impl<T: Clone + 'static> TextEntry<T> {
|
||||||
placeholder: &str,
|
pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, validator: V) -> Self
|
||||||
value: Option<T>,
|
where
|
||||||
validator: &'static dyn Fn(&str) -> Result<T, ParseError>,
|
R: Fn(&T) -> String + 'static,
|
||||||
) -> Self {
|
V: Fn(&str) -> Result<T, ParseError> + 'static,
|
||||||
|
{
|
||||||
let widget = gtk::Entry::builder().placeholder_text(placeholder).build();
|
let widget = gtk::Entry::builder().placeholder_text(placeholder).build();
|
||||||
|
match value {
|
||||||
|
Some(ref v) => widget.set_text(&renderer(&v)),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
|
||||||
let s = Self {
|
let s = Self {
|
||||||
value: Rc::new(RefCell::new(value)),
|
value: Rc::new(RefCell::new(value)),
|
||||||
widget,
|
widget,
|
||||||
|
renderer: Rc::new(Box::new(renderer)),
|
||||||
|
validator: Rc::new(Box::new(validator)),
|
||||||
};
|
};
|
||||||
|
|
||||||
s.widget.buffer().connect_text_notify({
|
s.widget.buffer().connect_text_notify({
|
||||||
let s = s.clone();
|
let s = s.clone();
|
||||||
move |buffer| {
|
move |buffer| s.handle_text_change(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
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_text_change(&self, buffer: >k::EntryBuffer) {
|
||||||
|
if buffer.text().is_empty() {
|
||||||
|
*self.value.borrow_mut() = None;
|
||||||
|
self.widget.remove_css_class("error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match (self.validator)(buffer.text().as_str()) {
|
||||||
|
Ok(v) => {
|
||||||
|
*self.value.borrow_mut() = Some(v);
|
||||||
|
self.widget.remove_css_class("error");
|
||||||
|
}
|
||||||
|
// need to change the border to provide a visual indicator of an error
|
||||||
|
Err(_) => {
|
||||||
|
self.widget.add_css_class("error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> Option<T> {
|
pub fn value(&self) -> Option<T> {
|
||||||
self.value.borrow().clone()
|
self.value.borrow().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_value(&self, value: Option<T>) {
|
pub fn set_value(&self, value: Option<T>) {
|
||||||
|
match value {
|
||||||
|
Some(ref v) => self.widget.set_text(&(self.renderer)(&v)),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
*self.value.borrow_mut() = value;
|
*self.value.borrow_mut() = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue