Compare commits

...

8 Commits

7 changed files with 443 additions and 29 deletions

1
Cargo.lock generated
View File

@ -976,6 +976,7 @@ checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6"
name = "fitnesstrax" name = "fitnesstrax"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"emseries",
"ft-core", "ft-core",
"gio", "gio",
"glib", "glib",

View File

@ -7,10 +7,11 @@ edition = "2021"
[dependencies] [dependencies]
adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] } adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] }
emseries = { path = "../../emseries" }
ft-core = { path = "../core" } ft-core = { path = "../core" }
gio = { version = "0.18" } gio = { version = "0.18" }
glib = { version = "0.18" } glib = { version = "0.18" }
gtk = { version = "0.7", package = "gtk4", features = [ "v4_8" ] } gtk = { version = "0.7", package = "gtk4", features = [ "v4_10" ] }
tokio = { version = "1.34", features = [ "full" ] } tokio = { version = "1.34", features = [ "full" ] }
[build-dependencies] [build-dependencies]

View File

@ -0,0 +1,21 @@
.modal {
margin: 64px;
background-color: @view_bg_color;
border: 1px solid grey;
border-radius: 10px
}
.modal-title {
font-size: larger;
padding: 8px;
background-color: @headerbar_bg_color;
border-bottom: 1px solid @headerbar_border_color;
border-radius: 10px 10px 0px 0px;
}
.modal-content {
padding: 8px;
}
.modal-footer {
}

View File

@ -1,16 +1,204 @@
/*
Copyright 2023, 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/>.
*/
mod ui;
use adw::prelude::*;
use emseries::Series;
use ft_core::TraxRecord;
use gio::resources_lookup_data; use gio::resources_lookup_data;
use gtk::{prelude::*, STYLE_PROVIDER_PRIORITY_USER}; use glib::Object;
use std::env; use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use std::{
cell::RefCell,
env,
sync::{Arc, RwLock},
};
use ui::welcome_modal;
const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev"; const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev";
const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax"; const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax";
const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/fitnesstrax/"; const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/fitnesstrax/";
struct AppState {} /// The real, headless application. This is where all of the logic will reside.
#[derive(Clone)]
struct App {
database: Arc<RwLock<Option<Series<TraxRecord>>>>,
}
impl App {
pub fn new() -> Self {
Self {
database: Arc::new(RwLock::new(None)),
}
}
}
/// This is the view to show if the application has not yet been configured. It will walk the user
/// through the most critical setup steps so that we can move on to the other views in the app.
pub struct UnconfiguredViewPrivate {}
#[glib::object_subclass]
impl ObjectSubclass for UnconfiguredViewPrivate {
const NAME: &'static str = "UnconfiguredView";
type Type = UnconfiguredView;
type ParentType = gtk::Box;
fn new() -> Self {
Self {}
}
}
impl ObjectImpl for UnconfiguredViewPrivate {}
impl WidgetImpl for UnconfiguredViewPrivate {}
impl BoxImpl for UnconfiguredViewPrivate {}
glib::wrapper! {
pub struct UnconfiguredView(ObjectSubclass<UnconfiguredViewPrivate>) @extends gtk::Box, gtk::Widget;
}
impl UnconfiguredView {
pub fn new() -> Self {
let s: Self = Object::builder().build();
// Replace this with the welcome screen that we set up in the fitnesstrax/unconfigured-page
// branch.
let label = gtk::Label::builder()
.label("Welcome to FitnessTrax")
.build();
s.append(&label);
s
}
}
/// The historical view will show a window into the main database. It will show some version of
/// daily summaries, daily details, and will provide all functions the user may need for editing
/// records.
pub struct HistoricalViewPrivate {}
#[glib::object_subclass]
impl ObjectSubclass for HistoricalViewPrivate {
const NAME: &'static str = "HistoricalView";
type Type = HistoricalView;
type ParentType = gtk::Box;
fn new() -> Self {
Self {}
}
}
impl ObjectImpl for HistoricalViewPrivate {}
impl WidgetImpl for HistoricalViewPrivate {}
impl BoxImpl for HistoricalViewPrivate {}
glib::wrapper! {
pub struct HistoricalView(ObjectSubclass<HistoricalViewPrivate>) @extends gtk::Box, gtk::Widget;
}
impl HistoricalView {
pub fn new() -> Self {
let s: Self = Object::builder().build();
let label = gtk::Label::builder()
.label("Database has been configured and now it is time to show data")
.build();
s.append(&label);
s
}
}
/// The application window, or the main window, is the main user interface for the app. Almost
/// everything occurs here.
struct AppWindow { struct AppWindow {
app: App,
window: adw::ApplicationWindow, window: adw::ApplicationWindow,
layout: gtk::Box,
current_view: RefCell<gtk::Widget>,
}
impl AppWindow {
/// Construct a new App Window.
///
/// adw_app is an Adwaita application. Application windows need to have access to this, but
/// otherwise we don't use this.
///
/// app is a core [App] object which encapsulates all of the basic logic.
fn new(adw_app: &adw::Application, app: App) -> AppWindow {
let window = adw::ApplicationWindow::builder()
.application(adw_app)
.width_request(800)
.height_request(600)
.build();
let current_view = if app.database.read().unwrap().is_none() {
UnconfiguredView::new().upcast()
} else {
HistoricalView::new().upcast()
};
let stylesheet = String::from_utf8(
resources_lookup_data(
&format!("{}style.css", RESOURCE_BASE_PATH),
gio::ResourceLookupFlags::NONE,
)
.expect("stylesheet must be available in the resources")
.to_vec(),
)
.expect("to parse stylesheet");
let provider = gtk::CssProvider::new();
provider.load_from_data(&stylesheet);
let context = window.style_context();
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
let header = adw::HeaderBar::builder()
.title_widget(&gtk::Label::new(Some("FitnessTrax")))
.build();
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
layout.append(&header);
layout.append(&current_view);
window.set_content(Some(&layout));
window.present();
let s = Self {
app,
window,
layout,
current_view: RefCell::new(current_view),
};
s
}
// Switch views.
//
// This function only replaces the old view with the one which matches the current view state.
// It is responsible for ensuring that the new view goes into the layout in the correct
// position.
fn change_view(&self, view: gtk::Widget) {
let mut current_view = self.current_view.borrow_mut();
self.layout.remove(&*current_view);
*current_view = view;
self.layout.append(&*current_view);
}
} }
fn main() { fn main() {
@ -31,10 +219,7 @@ fn main() {
println!("database path: {}", settings.string("series-path")); println!("database path: {}", settings.string("series-path"));
let app = adw::Application::builder() let app = App::new();
.application_id(app_id)
.resource_base_path(RESOURCE_BASE_PATH)
.build();
/* /*
let runtime = tokio::runtime::Builder::new_multi_thread() let runtime = tokio::runtime::Builder::new_multi_thread()
@ -43,32 +228,15 @@ fn main() {
.unwrap(); .unwrap();
*/ */
let app = adw::Application::builder() let adw_app = adw::Application::builder()
.application_id(app_id) .application_id(app_id)
.resource_base_path(RESOURCE_BASE_PATH) .resource_base_path(RESOURCE_BASE_PATH)
.build(); .build();
app.connect_activate(move |app| { adw_app.connect_activate(move |adw_app| {
let stylesheet = String::from_utf8( AppWindow::new(adw_app, app.clone());
resources_lookup_data(
&format!("{}style.css", RESOURCE_BASE_PATH),
gio::ResourceLookupFlags::NONE,
)
.expect("stylesheet must be available in the resources")
.to_vec(),
)
.expect("to parse stylesheet");
let provider = gtk::CssProvider::new();
provider.load_from_data(&stylesheet);
let window = adw::ApplicationWindow::new(app);
let context = window.style_context();
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
window.present();
}); });
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
ApplicationExtManual::run_with_args(&app, &args); ApplicationExtManual::run_with_args(&adw_app, &args);
} }

View File

@ -0,0 +1,2 @@
mod modal;
pub use modal::{welcome_modal, Modal};

View File

@ -0,0 +1,220 @@
//! The Modal is a reusable component with a title, arbitrary content, and up to three action
//! buttons. It does not itself enforce being a modal, but is meant to become a child of an Overlay
//! component.
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::{
cell::RefCell,
path::{Path, PathBuf},
rc::Rc,
};
pub struct ModalPrivate {
title: gtk::Label,
content: RefCell<gtk::Widget>,
primary_action: RefCell<gtk::Button>,
secondary_action: RefCell<Option<gtk::Button>>,
tertiary_action: RefCell<Option<gtk::Button>>,
footer: gtk::Box,
}
#[glib::object_subclass]
impl ObjectSubclass for ModalPrivate {
const NAME: &'static str = "Modal";
type Type = Modal;
type ParentType = gtk::Box;
fn new() -> Self {
let title = gtk::Label::builder()
.label("Modal")
.css_classes(["modal-title"])
.build();
let content = gtk::Box::new(gtk::Orientation::Vertical, 0);
let primary_action = gtk::Button::builder().label("Primary").build();
let footer = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.hexpand(true)
.css_classes(["modal-footer"])
.build();
footer.append(&primary_action);
Self {
title,
content: RefCell::new(content.upcast()),
primary_action: RefCell::new(primary_action),
secondary_action: RefCell::new(None),
tertiary_action: RefCell::new(None),
footer,
}
}
}
impl ObjectImpl for ModalPrivate {}
impl WidgetImpl for ModalPrivate {}
impl BoxImpl for ModalPrivate {}
glib::wrapper! {
pub struct Modal(ObjectSubclass<ModalPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl Modal {
pub fn new() -> Self {
let s: Self = Object::builder().build();
s.set_css_classes(&["modal"]);
s.set_orientation(gtk::Orientation::Vertical);
s.append(&s.imp().title);
s.append(&*s.imp().content.borrow());
s.append(&s.imp().footer);
s
}
pub fn set_title(&self, text: &str) {
self.imp().title.set_text(text);
}
pub fn set_content(&self, content: gtk::Widget) {
self.remove(&*self.imp().content.borrow());
content.add_css_class("modal-content");
content.set_hexpand(true);
content.set_vexpand(true);
self.insert_child_after(&content, Some(&self.imp().title));
*self.imp().content.borrow_mut() = content;
}
pub fn set_primary_action(&self, action: gtk::Button) {
self.imp()
.footer
.remove(&*self.imp().primary_action.borrow());
*self.imp().primary_action.borrow_mut() = action;
self.imp()
.footer
.append(&*self.imp().primary_action.borrow());
}
}
/// The welcome modal is the first thing the user will see when FitnessTrax starts up if the
/// database has not been configured yet.
///
/// This is a [Modal] component with all of the welcome content.
pub fn welcome_modal<F>(database_selected: F) -> Modal
where
F: Fn(&Path) + 'static,
{
let modal = Modal::new();
modal.set_title("Welcome to FitnessTrax");
// The content should be a friendly dialog that explains to the user that they're going to set
// up the database.
let content = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
let instructions = gtk::Label::builder()
.wrap(true)
.label("The application has not yet been configured, so I will walk you through that. Fortunately, it is very easy, and we only need to select the location for your database.").build();
content.append(&instructions);
let db_row = DatabaseFileChooserRow::new(database_selected);
content.append(&db_row);
modal.set_content(content.upcast());
modal.set_primary_action(gtk::Button::builder().label("Save Settings").build());
modal
}
pub struct DatabaseFileChooserRowPrivate {
path: RefCell<Option<PathBuf>>,
label: gtk::Label,
on_selected_: RefCell<Box<dyn Fn(&Path)>>,
}
#[glib::object_subclass]
impl ObjectSubclass for DatabaseFileChooserRowPrivate {
const NAME: &'static str = "DatabaseFileChooser";
type Type = DatabaseFileChooserRow;
type ParentType = gtk::Box;
fn new() -> Self {
Self {
path: RefCell::new(None),
label: gtk::Label::builder().hexpand(true).build(),
on_selected_: RefCell::new(Box::new(|_| {})),
}
}
}
impl ObjectImpl for DatabaseFileChooserRowPrivate {}
impl WidgetImpl for DatabaseFileChooserRowPrivate {}
impl BoxImpl for DatabaseFileChooserRowPrivate {}
glib::wrapper! {
pub struct DatabaseFileChooserRow(ObjectSubclass<DatabaseFileChooserRowPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl DatabaseFileChooserRow {
pub fn new<F>(database_selected: F) -> Self
where
F: Fn(&Path) + 'static,
{
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Horizontal);
// 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");
*s.imp().on_selected_.borrow_mut() = Box::new(database_selected);
let db_file_chooser_button = gtk::Button::builder().label("Select Database").build();
db_file_chooser_button.connect_clicked({
let s = s.clone();
move |_| {
let no_window: Option<&gtk::Window> = None;
let not_cancellable: Option<&gio::Cancellable> = None;
let s = s.clone();
gtk::FileDialog::builder().build().open(
no_window,
not_cancellable,
move |file_id| s.on_selected(file_id),
);
}
});
s.append(&s.imp().label);
s.append(&db_file_chooser_button);
s
}
fn on_selected(&self, m_file_id: Result<gio::File, glib::Error>) {
match m_file_id {
Ok(file_id) => {
println!("The user selected {:?}", file_id.path());
*self.imp().path.borrow_mut() = file_id.path();
match *self.imp().path.borrow() {
Some(ref path) => {
(*self.imp().on_selected_.borrow())(path);
self.redraw();
}
None => {}
}
}
Err(err) => println!("file opening failed: {}", err),
}
}
fn redraw(&self) {
match *self.imp().path.borrow() {
Some(ref path) => self.imp().label.set_text(path.to_str().unwrap()),
None => self.imp().label.set_text("No database selected"),
}
}
}

View File

@ -4,3 +4,4 @@ use emseries::DateTimeTz;
mod legacy; mod legacy;
mod types; mod types;
pub use types::TraxRecord;