Create a swappable UI component #160
|
@ -699,7 +699,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "dashboard"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"cairo-rs",
|
||||
"chrono",
|
||||
|
|
|
@ -20,6 +20,9 @@ pub use day::{DayDetail, DaySummary};
|
|||
mod edit_view;
|
||||
pub use edit_view::EditView;
|
||||
|
||||
mod singleton;
|
||||
pub use singleton::Singleton;
|
||||
|
||||
mod text_entry;
|
||||
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>>>,
|
||||
widget: gtk::Entry,
|
||||
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.
|
||||
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
|
||||
R: Fn(&T) -> String + 'static,
|
||||
V: Fn(&str) -> Result<T, ParseError> + 'static,
|
||||
|
@ -44,7 +44,7 @@ impl<T: Clone + 'static> TextEntry<T> {
|
|||
value: Rc::new(RefCell::new(value)),
|
||||
widget,
|
||||
renderer: Rc::new(Box::new(renderer)),
|
||||
validator: Rc::new(Box::new(validator)),
|
||||
parser: Rc::new(Box::new(parser)),
|
||||
};
|
||||
|
||||
s.widget.buffer().connect_text_notify({
|
||||
|
@ -61,7 +61,7 @@ impl<T: Clone + 'static> TextEntry<T> {
|
|||
self.widget.remove_css_class("error");
|
||||
return;
|
||||
}
|
||||
match (self.validator)(buffer.text().as_str()) {
|
||||
match (self.parser)(buffer.text().as_str()) {
|
||||
Ok(v) => {
|
||||
*self.value.borrow_mut() = Some(v);
|
||||
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/>.
|
||||
*/
|
||||
|
||||
use crate::components::{EditView, ParseError, TextEntry};
|
||||
use crate::components::{EditView, ParseError, Singleton, TextEntry};
|
||||
use chrono::{Local, NaiveDate};
|
||||
use dimensioned::si;
|
||||
use ft_core::Weight;
|
||||
|
@ -27,6 +27,7 @@ pub struct WeightViewPrivate {
|
|||
record: RefCell<Option<Weight>>,
|
||||
|
||||
widget: RefCell<EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>>,
|
||||
container: Singleton,
|
||||
|
||||
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()),
|
||||
record: RefCell::new(None),
|
||||
widget: RefCell::new(EditView::Unconfigured),
|
||||
container: Singleton::new(),
|
||||
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +70,8 @@ impl WeightView {
|
|||
{
|
||||
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().date.borrow_mut() = date;
|
||||
|
||||
|
@ -126,6 +130,12 @@ impl WeightView {
|
|||
}
|
||||
|
||||
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();
|
||||
match *widget {
|
||||
EditView::Unconfigured => {}
|
||||
|
@ -139,6 +149,7 @@ impl WeightView {
|
|||
EditView::Edit(ref editor) => self.append(&editor.widget()),
|
||||
}
|
||||
*widget = new_view;
|
||||
*/
|
||||
}
|
||||
|
||||
pub fn blur(&self) {
|
||||
|
|
Loading…
Reference in New Issue