Develop a pattern to detect clicking outside of a focused child

This commit is contained in:
Savanni D'Gerinel 2023-12-26 10:45:50 -05:00
parent 9e0cdb50ea
commit 3e681dbffd
1 changed files with 27 additions and 12 deletions

View File

@ -20,7 +20,7 @@ use dimensioned::si;
use ft_core::{RecordType, TimeDistance, TraxRecord, Weight}; use ft_core::{RecordType, TimeDistance, TraxRecord, Weight};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell; use std::{cell::RefCell, rc::Rc};
pub struct DaySummaryPrivate { pub struct DaySummaryPrivate {
date: gtk::Label, date: gtk::Label,
@ -132,6 +132,19 @@ impl DayDetail {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical); 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::<WeightView>() {
println!("focused child is the weight view");
widget.blur();
}
}
});
s.add_controller(click_controller);
let weight_record = records.iter().find_map(|record| match record { let weight_record = records.iter().find_map(|record| match record {
TraxRecord::Weight(record) => Some(record.clone()), TraxRecord::Weight(record) => Some(record.clone()),
_ => None, _ => None,
@ -179,6 +192,7 @@ pub struct WeightViewPrivate {
view: RefCell<gtk::Label>, view: RefCell<gtk::Label>,
edit: RefCell<gtk::Entry>, edit: RefCell<gtk::Entry>,
current: RefCell<gtk::Widget>, current: RefCell<gtk::Widget>,
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
} }
impl Default for WeightViewPrivate { impl Default for WeightViewPrivate {
@ -197,6 +211,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()),
on_edit_finished: RefCell::new(Box::new(|_| {})),
} }
} }
} }
@ -217,12 +232,14 @@ glib::wrapper! {
} }
impl WeightView { impl WeightView {
pub fn new<OnBlur>(weight: Option<Weight>, on_blur: OnBlur) -> Self pub fn new<OnEditFinished>(weight: Option<Weight>, on_edit_finished: OnEditFinished) -> Self
where where
OnBlur: Fn(si::Kilogram<f64>), OnEditFinished: Fn(si::Kilogram<f64>) + 'static,
{ {
let s: Self = Object::builder().build(); 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.imp().record.borrow_mut() = weight;
s.view(); 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().view.borrow().add_controller(view_click_controller);
s.imp().edit.borrow().add_controller(edit_click_controller);
s s
} }
@ -278,6 +286,13 @@ impl WeightView {
self.append(&new_view); self.append(&new_view);
*current = 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)] #[derive(Default)]