Compare commits
5 Commits
c4befcc6de
...
f6bba16b26
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | f6bba16b26 | |
Savanni D'Gerinel | 1140377aa5 | |
Savanni D'Gerinel | 87a07955a3 | |
Savanni D'Gerinel | 7ee3e1432e | |
Savanni D'Gerinel | e008a97f83 |
|
@ -976,6 +976,7 @@ checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6"
|
|||
name = "fitnesstrax"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"emseries",
|
||||
"ft-core",
|
||||
"gio",
|
||||
"glib",
|
||||
|
|
|
@ -7,6 +7,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] }
|
||||
emseries = { path = "../../emseries" }
|
||||
ft-core = { path = "../core" }
|
||||
gio = { version = "0.18" }
|
||||
glib = { version = "0.18" }
|
||||
|
|
|
@ -1,16 +1,224 @@
|
|||
/*
|
||||
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 gtk::{prelude::*, STYLE_PROVIDER_PRIORITY_USER};
|
||||
use std::env;
|
||||
use glib::Object;
|
||||
use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
|
||||
use std::{
|
||||
env,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
use ui::{welcome_modal, Modal};
|
||||
|
||||
const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev";
|
||||
const APP_ID_PROD: &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)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
let label = gtk::Label::builder()
|
||||
.label("Database is not configured.")
|
||||
.build();
|
||||
s.append(&label);
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// window setup...
|
||||
// main window with all of its layout
|
||||
// modals that overlay atop the main window and capture focus
|
||||
// menus
|
||||
// There is more than one view for the main window. There's the view for data entry, then another
|
||||
// view for showing graphs. Then a third view, or maybe a modal, for editing a day.
|
||||
//
|
||||
// So, the ordinary data view is the history the metrics, and the calendar. Scrollable, and items
|
||||
// within the scrolling area can be clicked upon in order to open the edit.
|
||||
//
|
||||
// I don't need to model the whole thing at once. The graphs will be some time out, and so I can
|
||||
// model just the main view, which consists of metrics, the data, and the calendar. Day entries
|
||||
// should be summaries of the day, expandable to the details.
|
||||
//
|
||||
// Then there is the view which notifies the user that the database has not been configured.
|
||||
|
||||
/// These are the possible states of the main application view.
|
||||
enum MainView {
|
||||
/// The application is not configured yet. This is a basic background widget to take up the
|
||||
/// space when there is no data to be shown.
|
||||
Unconfigured(UnconfiguredView),
|
||||
|
||||
/// The Historical view shows a history of records and whatnot.
|
||||
HistoricalView(HistoricalView),
|
||||
}
|
||||
|
||||
/// The application window, or the main window, is the main user interface for the app.
|
||||
struct AppWindow {
|
||||
app: App,
|
||||
window: adw::ApplicationWindow,
|
||||
overlay: gtk::Overlay,
|
||||
current_view: MainView,
|
||||
}
|
||||
|
||||
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 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 window = adw::ApplicationWindow::builder()
|
||||
.application(adw_app)
|
||||
.width_request(800)
|
||||
.height_request(600)
|
||||
.build();
|
||||
|
||||
let context = window.style_context();
|
||||
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
|
||||
|
||||
window.present();
|
||||
|
||||
// GTK overlays aren't all that well documented. The Overlay object needs to be the
|
||||
// content/child of the window. The main content should then be added to the overlay as
|
||||
// `add_overlay`. The overlays/modals should be added as `set_child`.
|
||||
let overlay = gtk::Overlay::new();
|
||||
window.set_content(Some(&overlay));
|
||||
|
||||
let current_view = if app.database.read().unwrap().is_none() {
|
||||
let view = UnconfiguredView::new();
|
||||
overlay.set_child(Some(&view));
|
||||
|
||||
// I have to access the overlay directly here because I haven't fully constructed Self
|
||||
// yet, and so I don't have access to `open_modal` yet.
|
||||
overlay.add_overlay(&welcome_modal());
|
||||
|
||||
MainView::Unconfigured(view)
|
||||
} else {
|
||||
let view = HistoricalView::new();
|
||||
overlay.set_child(Some(&view));
|
||||
|
||||
MainView::HistoricalView(view)
|
||||
};
|
||||
|
||||
Self {
|
||||
app,
|
||||
window,
|
||||
overlay,
|
||||
current_view,
|
||||
}
|
||||
}
|
||||
|
||||
/// Use [modal] as a modal overlay of the application window.
|
||||
fn open_modal(&self, modal: Modal) {
|
||||
self.overlay.set_child(Some(&modal));
|
||||
}
|
||||
|
||||
/// Close the modal by discarding the component.
|
||||
fn close_modal(&self) {
|
||||
let none: Option<>k::Widget> = None;
|
||||
self.overlay.set_child(none);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -31,11 +239,13 @@ fn main() {
|
|||
|
||||
println!("database path: {}", settings.string("series-path"));
|
||||
|
||||
let app = adw::Application::builder()
|
||||
let adw_app = adw::Application::builder()
|
||||
.application_id(app_id)
|
||||
.resource_base_path(RESOURCE_BASE_PATH)
|
||||
.build();
|
||||
|
||||
let app = App::new();
|
||||
|
||||
/*
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
|
@ -43,32 +253,15 @@ fn main() {
|
|||
.unwrap();
|
||||
*/
|
||||
|
||||
let app = adw::Application::builder()
|
||||
let adw_app = adw::Application::builder()
|
||||
.application_id(app_id)
|
||||
.resource_base_path(RESOURCE_BASE_PATH)
|
||||
.build();
|
||||
|
||||
app.connect_activate(move |app| {
|
||||
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 window = adw::ApplicationWindow::new(app);
|
||||
let context = window.style_context();
|
||||
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
|
||||
|
||||
window.present();
|
||||
adw_app.connect_activate(move |adw_app| {
|
||||
AppWindow::new(adw_app, app.clone());
|
||||
});
|
||||
|
||||
let args: Vec<String> = env::args().collect();
|
||||
ApplicationExtManual::run_with_args(&app, &args);
|
||||
ApplicationExtManual::run_with_args(&adw_app, &args);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
mod modal;
|
||||
pub use modal::{welcome_modal, Modal};
|
|
@ -0,0 +1,127 @@
|
|||
//! 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;
|
||||
|
||||
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").build();
|
||||
let content = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
let actions = gtk::Box::new(gtk::Orientation::Horizontal, 0);
|
||||
let primary_action = gtk::Button::builder().label("Primary").build();
|
||||
|
||||
let footer = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.hexpand(true)
|
||||
.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_margin_start(100);
|
||||
s.set_margin_end(100);
|
||||
s.set_margin_top(100);
|
||||
s.set_margin_bottom(100);
|
||||
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());
|
||||
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() -> Modal {
|
||||
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();
|
||||
|
||||
content.append(>k::Label::new(Some("Welcome to FitnessTrax. The application has not yet been configured, so I will walk you through that. Let's start out by selecting your database.")));
|
||||
|
||||
// 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.
|
||||
let db_row = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.build();
|
||||
db_row.append(
|
||||
>k::Label::builder()
|
||||
.label("No Path Selected")
|
||||
.hexpand(true)
|
||||
.build(),
|
||||
);
|
||||
db_row.append(>k::Button::builder().label("Select Database").build());
|
||||
|
||||
modal.set_content(content.upcast());
|
||||
modal.set_primary_action(gtk::Button::builder().label("Save Settings").build());
|
||||
modal
|
||||
}
|
|
@ -4,3 +4,4 @@ use emseries::DateTimeTz;
|
|||
|
||||
mod legacy;
|
||||
mod types;
|
||||
pub use types::TraxRecord;
|
||||
|
|
Loading…
Reference in New Issue