monorepo/fitnesstrax/app/src/components/mod.rs

149 lines
4.9 KiB
Rust
Raw Normal View History

/*
Copyright 2023-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/>.
*/
2024-01-18 03:13:55 +00:00
mod action_group;
pub use action_group::ActionGroup;
mod day;
2024-01-16 04:27:55 +00:00
pub use day::{DayDetail, DayEdit, DaySummary};
mod singleton;
pub use singleton::{Singleton, SingletonImpl};
2024-01-20 20:59:03 +00:00
mod steps;
pub use steps::{steps_editor, Steps};
mod text_entry;
pub use text_entry::TextEntry;
mod time_distance;
pub use time_distance::TimeDistanceView;
mod weight;
pub use weight::{weight_editor, WeightLabel};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, path::PathBuf, rc::Rc};
pub struct FileChooserRowPrivate {
path: RefCell<Option<PathBuf>>,
label: gtk::Label,
}
#[glib::object_subclass]
impl ObjectSubclass for FileChooserRowPrivate {
const NAME: &'static str = "FileChooser";
type Type = FileChooserRow;
type ParentType = gtk::Box;
fn new() -> Self {
Self {
path: RefCell::new(None),
label: gtk::Label::builder().hexpand(true).build(),
}
}
}
impl ObjectImpl for FileChooserRowPrivate {}
impl WidgetImpl for FileChooserRowPrivate {}
impl BoxImpl for FileChooserRowPrivate {}
glib::wrapper! {
pub struct FileChooserRow(ObjectSubclass<FileChooserRowPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl FileChooserRow {
pub fn new<F>(on_selected: F) -> Self
where
F: Fn(PathBuf) + 'static,
{
let s: Self = Object::builder().build();
2023-12-19 15:10:02 +00:00
s.set_css_classes(&["dialog-row", "card"]);
s.set_orientation(gtk::Orientation::Horizontal);
2023-12-19 15:10:02 +00:00
s.set_spacing(8);
// The database selection row should be a box that shows a default database path, along with a
// button that triggers a file chooser dialog. Once the dialog returns, the box should be
// updated to reflect the chosen path.
s.imp().label.set_text("No database selected");
let on_selected = Rc::new(Box::new(on_selected));
2023-12-19 15:10:02 +00:00
let import_button = gtk::Button::builder().label("Import a Database").build();
let handle_file_selection = Rc::new(Box::new({
let s = s.clone();
2023-12-19 15:10:02 +00:00
let on_selected = on_selected.clone();
move |file_id: Result<gio::File, glib::Error>| match file_id {
Ok(file_id) => match file_id.path() {
Some(path) => {
s.imp().label.set_text(path.to_str().unwrap());
on_selected(path.clone());
*s.imp().path.borrow_mut() = Some(path);
}
None => {
*s.imp().path.borrow_mut() = None;
s.imp().label.set_text("No database selected");
}
},
Err(err) => println!("file opening failed: {}", err),
}
}));
import_button.connect_clicked({
let handle_file_selection = handle_file_selection.clone();
move |_| {
let no_window: Option<&gtk::Window> = None;
let not_cancellable: Option<&gio::Cancellable> = None;
let handle_file_selection = handle_file_selection.clone();
gtk::FileDialog::builder().build().open(
no_window,
not_cancellable,
move |file_id| handle_file_selection(file_id),
);
}
});
let new_button = gtk::Button::builder().label("Create Database").build();
new_button.connect_clicked({
let handle_file_selection = handle_file_selection.clone();
move |_| {
let no_window: Option<&gtk::Window> = None;
let not_cancellable: Option<&gio::Cancellable> = None;
2023-12-19 15:10:02 +00:00
let handle_file_selection = handle_file_selection.clone();
2023-12-19 05:37:51 +00:00
gtk::FileDialog::builder().build().save(
no_window,
not_cancellable,
2023-12-19 15:10:02 +00:00
move |file_id| handle_file_selection(file_id),
);
}
});
2023-12-19 15:10:02 +00:00
s.imp().label.set_halign(gtk::Align::Start);
s.append(&s.imp().label);
2023-12-19 15:10:02 +00:00
s.append(&import_button);
s.append(&new_button);
s
}
pub fn path(&self) -> Option<PathBuf> {
self.imp().path.borrow().clone()
}
}