Create a Singleton component and use it to simplify the weight view
This commit is contained in:
parent
2d22397382
commit
2e2ff6b47e
|
@ -699,7 +699,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dashboard"
|
name = "dashboard"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cairo-rs",
|
"cairo-rs",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -20,6 +20,9 @@ pub use day::{DayDetail, DaySummary};
|
||||||
mod edit_view;
|
mod edit_view;
|
||||||
pub use edit_view::EditView;
|
pub use edit_view::EditView;
|
||||||
|
|
||||||
|
mod singleton;
|
||||||
|
pub use singleton::Singleton;
|
||||||
|
|
||||||
mod text_entry;
|
mod text_entry;
|
||||||
pub use text_entry::{ParseError, TextEntry};
|
pub use text_entry::{ParseError, TextEntry};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
Copyright 2024, 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! A Widget container for a single components
|
||||||
|
use glib::Object;
|
||||||
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
|
pub struct SingletonPrivate {
|
||||||
|
widget: RefCell<gtk::Widget>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for SingletonPrivate {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
widget: RefCell::new(gtk::Label::new(None).upcast()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for SingletonPrivate {
|
||||||
|
const NAME: &'static str = "Singleton";
|
||||||
|
type Type = Singleton;
|
||||||
|
type ParentType = gtk::Box;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for SingletonPrivate {}
|
||||||
|
impl WidgetImpl for SingletonPrivate {}
|
||||||
|
impl BoxImpl for SingletonPrivate {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
/// The Singleton component contains exactly one child widget. The swap function makes it easy
|
||||||
|
/// to handle the job of swapping that child out for a different one.
|
||||||
|
pub struct Singleton(ObjectSubclass<SingletonPrivate>) @extends gtk::Box, gtk::Widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Singleton {
|
||||||
|
/// Construct a Singleton object. The Singleton defaults to an invisible child.
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let s: Self = Object::builder().build();
|
||||||
|
|
||||||
|
s.append(&*s.imp().widget.borrow());
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace the currently visible child widget with a new one. The old widget will be
|
||||||
|
/// discarded.
|
||||||
|
pub fn swap(&self, new_widget: >k::Widget) {
|
||||||
|
self.remove(&*self.imp().widget.borrow());
|
||||||
|
self.append(new_widget);
|
||||||
|
*self.imp().widget.borrow_mut() = new_widget.clone();
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,12 +24,12 @@ 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>>,
|
renderer: Rc<Box<dyn Fn(&T) -> String>>,
|
||||||
validator: Rc<Box<dyn Fn(&str) -> Result<T, ParseError>>>,
|
parser: Rc<Box<dyn Fn(&str) -> Result<T, ParseError>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// I do not understand why the data should be 'static.
|
// I do not understand why the data should be 'static.
|
||||||
impl<T: Clone + 'static> TextEntry<T> {
|
impl<T: Clone + 'static> TextEntry<T> {
|
||||||
pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, validator: V) -> Self
|
pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, parser: V) -> Self
|
||||||
where
|
where
|
||||||
R: Fn(&T) -> String + 'static,
|
R: Fn(&T) -> String + 'static,
|
||||||
V: Fn(&str) -> Result<T, ParseError> + 'static,
|
V: Fn(&str) -> Result<T, ParseError> + 'static,
|
||||||
|
@ -44,7 +44,7 @@ impl<T: Clone + 'static> TextEntry<T> {
|
||||||
value: Rc::new(RefCell::new(value)),
|
value: Rc::new(RefCell::new(value)),
|
||||||
widget,
|
widget,
|
||||||
renderer: Rc::new(Box::new(renderer)),
|
renderer: Rc::new(Box::new(renderer)),
|
||||||
validator: Rc::new(Box::new(validator)),
|
parser: Rc::new(Box::new(parser)),
|
||||||
};
|
};
|
||||||
|
|
||||||
s.widget.buffer().connect_text_notify({
|
s.widget.buffer().connect_text_notify({
|
||||||
|
@ -61,7 +61,7 @@ impl<T: Clone + 'static> TextEntry<T> {
|
||||||
self.widget.remove_css_class("error");
|
self.widget.remove_css_class("error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match (self.validator)(buffer.text().as_str()) {
|
match (self.parser)(buffer.text().as_str()) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
*self.value.borrow_mut() = Some(v);
|
*self.value.borrow_mut() = Some(v);
|
||||||
self.widget.remove_css_class("error");
|
self.widget.remove_css_class("error");
|
||||||
|
|
|
@ -14,7 +14,7 @@ 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/>.
|
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::components::{EditView, ParseError, TextEntry};
|
use crate::components::{EditView, ParseError, Singleton, TextEntry};
|
||||||
use chrono::{Local, NaiveDate};
|
use chrono::{Local, NaiveDate};
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
use ft_core::Weight;
|
use ft_core::Weight;
|
||||||
|
@ -27,6 +27,7 @@ pub struct WeightViewPrivate {
|
||||||
record: RefCell<Option<Weight>>,
|
record: RefCell<Option<Weight>>,
|
||||||
|
|
||||||
widget: RefCell<EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>>,
|
widget: RefCell<EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>>,
|
||||||
|
container: Singleton,
|
||||||
|
|
||||||
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
||||||
}
|
}
|
||||||
|
@ -37,6 +38,7 @@ impl Default for WeightViewPrivate {
|
||||||
date: RefCell::new(Local::now().date_naive()),
|
date: RefCell::new(Local::now().date_naive()),
|
||||||
record: RefCell::new(None),
|
record: RefCell::new(None),
|
||||||
widget: RefCell::new(EditView::Unconfigured),
|
widget: RefCell::new(EditView::Unconfigured),
|
||||||
|
container: Singleton::new(),
|
||||||
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,6 +70,8 @@ impl WeightView {
|
||||||
{
|
{
|
||||||
let s: Self = Object::builder().build();
|
let s: Self = Object::builder().build();
|
||||||
|
|
||||||
|
s.append(&s.imp().container);
|
||||||
|
|
||||||
*s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished);
|
*s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished);
|
||||||
*s.imp().date.borrow_mut() = date;
|
*s.imp().date.borrow_mut() = date;
|
||||||
|
|
||||||
|
@ -126,6 +130,12 @@ impl WeightView {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn swap(&self, new_view: EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>) {
|
fn swap(&self, new_view: EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>) {
|
||||||
|
match new_view {
|
||||||
|
EditView::Unconfigured => {}
|
||||||
|
EditView::View(view) => self.imp().container.swap(&view.upcast()),
|
||||||
|
EditView::Edit(editor) => self.imp().container.swap(&editor.widget()),
|
||||||
|
}
|
||||||
|
/*
|
||||||
let mut widget = self.imp().widget.borrow_mut();
|
let mut widget = self.imp().widget.borrow_mut();
|
||||||
match *widget {
|
match *widget {
|
||||||
EditView::Unconfigured => {}
|
EditView::Unconfigured => {}
|
||||||
|
@ -139,6 +149,7 @@ impl WeightView {
|
||||||
EditView::Edit(ref editor) => self.append(&editor.widget()),
|
EditView::Edit(ref editor) => self.append(&editor.widget()),
|
||||||
}
|
}
|
||||||
*widget = new_view;
|
*widget = new_view;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn blur(&self) {
|
pub fn blur(&self) {
|
||||||
|
|
Loading…
Reference in New Issue