Develop a pattern to detect clicking outside of a focused child
This commit is contained in:
parent
e84c49c343
commit
64c4e971f8
|
@ -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::<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 {
|
||||
TraxRecord::Weight(record) => Some(record.clone()),
|
||||
_ => None,
|
||||
|
@ -179,6 +192,7 @@ pub struct WeightViewPrivate {
|
|||
view: RefCell<gtk::Label>,
|
||||
edit: RefCell<gtk::Entry>,
|
||||
current: RefCell<gtk::Widget>,
|
||||
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
||||
}
|
||||
|
||||
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<OnBlur>(weight: Option<Weight>, on_blur: OnBlur) -> Self
|
||||
pub fn new<OnEditFinished>(weight: Option<Weight>, on_edit_finished: OnEditFinished) -> Self
|
||||
where
|
||||
OnBlur: Fn(si::Kilogram<f64>),
|
||||
OnEditFinished: Fn(si::Kilogram<f64>) + '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)]
|
||||
|
|
Loading…
Reference in New Issue