diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 23da6c2..8a3281c 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -20,7 +20,7 @@ use dimensioned::si; use ft_core::{RecordType, TimeDistance, TraxRecord, Weight}; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; -use std::cell::RefCell; +use std::{cell::RefCell, rc::Rc}; pub struct DaySummaryPrivate { date: gtk::Label, @@ -132,6 +132,19 @@ impl DayDetail { let s: Self = Object::builder().build(); s.set_orientation(gtk::Orientation::Vertical); + let click_controller = gtk::GestureClick::new(); + click_controller.connect_released({ + let s = s.clone(); + move |_, _, _, _| { + println!("clicked outside of focusable entity"); + if let Some(widget) = s.focus_child().and_downcast_ref::() { + println!("focused child is the weight view"); + widget.blur(); + } + } + }); + s.add_controller(click_controller); + let weight_record = records.iter().find_map(|record| match record { TraxRecord::Weight(record) => Some(record.clone()), _ => None, @@ -179,6 +192,7 @@ pub struct WeightViewPrivate { view: RefCell, edit: RefCell, current: RefCell, + on_edit_finished: RefCell)>>, } impl Default for WeightViewPrivate { @@ -197,6 +211,7 @@ impl Default for WeightViewPrivate { view: RefCell::new(view), edit: RefCell::new(edit), current: RefCell::new(current.upcast()), + on_edit_finished: RefCell::new(Box::new(|_| {})), } } } @@ -217,12 +232,14 @@ glib::wrapper! { } impl WeightView { - pub fn new(weight: Option, on_blur: OnBlur) -> Self + pub fn new(weight: Option, on_edit_finished: OnEditFinished) -> Self where - OnBlur: Fn(si::Kilogram), + OnEditFinished: Fn(si::Kilogram) + 'static, { let s: Self = Object::builder().build(); + *s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished); + *s.imp().record.borrow_mut() = weight; s.view(); @@ -234,16 +251,7 @@ impl WeightView { } }); - let edit_click_controller = gtk::GestureClick::new(); - edit_click_controller.connect_released({ - let s = s.clone(); - move |_, _, _, _| { - s.view(); - } - }); - s.imp().view.borrow().add_controller(view_click_controller); - s.imp().edit.borrow().add_controller(edit_click_controller); s } @@ -278,6 +286,13 @@ impl WeightView { self.append(&new_view); *current = new_view; } + + fn blur(&self) { + if *self.imp().current.borrow() == *self.imp().edit.borrow() { + self.imp().on_edit_finished.borrow()(0. * si::KG); + self.view(); + } + } } #[derive(Default)]