Extract the core app loop and the UI from the main file #127
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use emseries::Series;
|
||||||
|
use ft_core::TraxRecord;
|
||||||
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::{Arc, RwLock},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Invocations are how parts of the application, primarily the UI, will send requests to the core.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum AppInvocation {
|
||||||
|
/// Tell the core to try to open a database.
|
||||||
|
OpenDatabase(PathBuf),
|
||||||
|
|
||||||
|
/// Request a set of records from the core.
|
||||||
|
// Note: this will require a time range, but doesn't yet.
|
||||||
|
RequestRecords,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Responses are messages that the core sends to the UI. Though they are called responses, the
|
||||||
|
/// could actually be pre-emptively sent, such as notifications. The UI will need to be able to
|
||||||
|
/// process those any time they arrive.
|
||||||
|
///
|
||||||
|
/// A typical use would be for the UI to send an [AppInvocation::RequestRecords] request and
|
||||||
|
/// receive [AppResponse::Records].
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum AppResponse {
|
||||||
|
/// No database is available. The UI should typically display a placeholder, such as the
|
||||||
|
/// welcome view.
|
||||||
|
NoDatabase,
|
||||||
|
|
||||||
|
/// The database is open and here is a set of records. Typically, the set of records will be
|
||||||
|
/// all of the records within a time frame, but this can actually be any set of records.
|
||||||
|
Records,
|
||||||
|
|
||||||
|
/// The database has been changed. This message is useful for telling the UI that a significant
|
||||||
|
/// change has happened. Further, the UI needs to save PathBuf to settings, because the
|
||||||
|
/// gio::Settings system can't be run in the fully async background.
|
||||||
|
DatabaseChanged(PathBuf),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The real, headless application. This is where all of the logic will reside.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct App {
|
||||||
|
database: Arc<RwLock<Option<Series<TraxRecord>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new(db_path: Option<PathBuf>) -> Self {
|
||||||
|
let database = db_path.map(|path| Series::open(path).unwrap());
|
||||||
|
let s = Self {
|
||||||
|
database: Arc::new(RwLock::new(database)),
|
||||||
|
};
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn process_invocation(&self, invocation: AppInvocation) -> AppResponse {
|
||||||
|
match invocation {
|
||||||
|
AppInvocation::OpenDatabase(db_path) => {
|
||||||
|
self.open_db(&db_path);
|
||||||
|
AppResponse::DatabaseChanged(db_path)
|
||||||
|
}
|
||||||
|
AppInvocation::RequestRecords => {
|
||||||
|
if self.database.read().unwrap().is_none() {
|
||||||
|
AppResponse::NoDatabase
|
||||||
|
} else {
|
||||||
|
AppResponse::Records
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_db(&self, path: &Path) {
|
||||||
|
let db = Series::open(path).unwrap();
|
||||||
|
*self.database.write().unwrap() = Some(db);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,150 @@
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
app::{AppInvocation, AppResponse},
|
||||||
|
views::{HistoricalView, PlaceholderView, View, ViewName, WelcomeView},
|
||||||
|
};
|
||||||
|
use adw::prelude::*;
|
||||||
|
use async_channel::Sender;
|
||||||
|
use gio::resources_lookup_data;
|
||||||
|
use gtk::STYLE_PROVIDER_PRIORITY_USER;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
/// The application window, or the main window, is the main user interface for the app. Almost
|
||||||
|
/// everything occurs here.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct AppWindow {
|
||||||
|
app_tx: Sender<AppInvocation>,
|
||||||
|
layout: gtk::Box,
|
||||||
|
current_view: Rc<RefCell<View>>,
|
||||||
|
settings: gio::Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
pub fn new(
|
||||||
|
app_id: &str,
|
||||||
|
resource_path: &str,
|
||||||
|
adw_app: &adw::Application,
|
||||||
|
app_tx: Sender<AppInvocation>,
|
||||||
|
) -> AppWindow {
|
||||||
|
let window = adw::ApplicationWindow::builder()
|
||||||
|
.application(adw_app)
|
||||||
|
.width_request(800)
|
||||||
|
.height_request(600)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let stylesheet = String::from_utf8(
|
||||||
|
resources_lookup_data(
|
||||||
|
&format!("{}style.css", resource_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);
|
||||||
|
|
||||||
|
#[allow(deprecated)]
|
||||||
|
let context = window.style_context();
|
||||||
|
#[allow(deprecated)]
|
||||||
|
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
|
||||||
|
|
||||||
|
let header = adw::HeaderBar::builder()
|
||||||
|
.title_widget(>k::Label::new(Some("FitnessTrax")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let layout = gtk::Box::builder()
|
||||||
|
.orientation(gtk::Orientation::Vertical)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let initial_view = View::Placeholder(PlaceholderView::new().upcast());
|
||||||
|
|
||||||
|
layout.append(&header);
|
||||||
|
layout.append(initial_view.widget());
|
||||||
|
|
||||||
|
window.set_content(Some(&layout));
|
||||||
|
window.present();
|
||||||
|
|
||||||
|
let s = Self {
|
||||||
|
app_tx,
|
||||||
|
layout,
|
||||||
|
current_view: Rc::new(RefCell::new(initial_view)),
|
||||||
|
settings: gio::Settings::new(app_id),
|
||||||
|
};
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn change_view(&self, view: ViewName) {
|
||||||
|
self.swap_main(self.construct_view(view));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_response(&self, response: AppResponse) {
|
||||||
|
match response {
|
||||||
|
AppResponse::DatabaseChanged(db_path) => {
|
||||||
|
self.settings
|
||||||
|
.set_string("series-path", db_path.to_str().unwrap())
|
||||||
|
.unwrap();
|
||||||
|
self.change_view(ViewName::Historical);
|
||||||
|
}
|
||||||
|
AppResponse::NoDatabase => {
|
||||||
|
self.change_view(ViewName::Welcome);
|
||||||
|
}
|
||||||
|
AppResponse::Records => {
|
||||||
|
self.change_view(ViewName::Historical);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 swap_main(&self, view: View) {
|
||||||
|
let mut current_widget = self.current_view.borrow_mut();
|
||||||
|
self.layout.remove(&*current_widget.widget());
|
||||||
|
*current_widget = view;
|
||||||
|
self.layout.append(&*current_widget.widget());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn construct_view(&self, view: ViewName) -> View {
|
||||||
|
match view {
|
||||||
|
ViewName::Welcome => View::Welcome(
|
||||||
|
WelcomeView::new({
|
||||||
|
let s = self.clone();
|
||||||
|
Box::new(move |path: PathBuf| {
|
||||||
|
s.app_tx
|
||||||
|
.send_blocking(AppInvocation::OpenDatabase(path))
|
||||||
|
.unwrap();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.upcast(),
|
||||||
|
),
|
||||||
|
ViewName::Historical => View::Historical(HistoricalView::new().upcast()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,11 +16,7 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
|
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
use gtk::{prelude::*, subclass::prelude::*};
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
use std::{
|
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
||||||
cell::RefCell,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
rc::Rc,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct FileChooserRowPrivate {
|
pub struct FileChooserRowPrivate {
|
||||||
path: RefCell<Option<PathBuf>>,
|
path: RefCell<Option<PathBuf>>,
|
|
@ -14,388 +14,20 @@ 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/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mod ui;
|
mod app;
|
||||||
|
mod app_window;
|
||||||
|
mod components;
|
||||||
|
mod views;
|
||||||
|
|
||||||
use adw::prelude::*;
|
use adw::prelude::*;
|
||||||
use async_channel::{Receiver, Sender};
|
use app_window::AppWindow;
|
||||||
use emseries::{EmseriesReadError, Series};
|
use std::{env, path::PathBuf};
|
||||||
use ft_core::TraxRecord;
|
|
||||||
use gio::resources_lookup_data;
|
|
||||||
use glib::Object;
|
|
||||||
use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
|
|
||||||
use std::{
|
|
||||||
cell::RefCell,
|
|
||||||
env,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
rc::Rc,
|
|
||||||
sync::{Arc, RwLock},
|
|
||||||
};
|
|
||||||
use ui::FileChooserRow;
|
|
||||||
|
|
||||||
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/";
|
||||||
|
|
||||||
/// Invocations are how parts of the application, primarily the UI, will send requests to the core.
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum AppInvocation {
|
|
||||||
/// Tell the core to try to open a database.
|
|
||||||
OpenDatabase(PathBuf),
|
|
||||||
|
|
||||||
/// Request a set of records from the core.
|
|
||||||
// Note: this will require a time range, but doesn't yet.
|
|
||||||
RequestRecords,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Responses are messages that the core sends to the UI. Though they are called responses, the
|
|
||||||
/// could actually be pre-emptively sent, such as notifications. The UI will need to be able to
|
|
||||||
/// process those any time they arrive.
|
|
||||||
///
|
|
||||||
/// A typical use would be for the UI to send an [AppInvocation::RequestRecords] request and
|
|
||||||
/// receive [AppResponse::Records].
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum AppResponse {
|
|
||||||
/// No database is available. The UI should typically display a placeholder, such as the
|
|
||||||
/// welcome view.
|
|
||||||
NoDatabase,
|
|
||||||
|
|
||||||
/// The database is open and here is a set of records. Typically, the set of records will be
|
|
||||||
/// all of the records within a time frame, but this can actually be any set of records.
|
|
||||||
Records,
|
|
||||||
|
|
||||||
/// The database has been changed. This message is useful for telling the UI that a significant
|
|
||||||
/// change has happened. Further, the UI needs to save PathBuf to settings, because the
|
|
||||||
/// gio::Settings system can't be run in the fully async background.
|
|
||||||
DatabaseChanged(PathBuf),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note that I have not yet figured out the communication channel or how the central dispatcher
|
|
||||||
// should work. There's a dance between the App and the AppWindow that I haven't figured out yet.
|
|
||||||
|
|
||||||
/// 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(db_path: Option<PathBuf>) -> Self {
|
|
||||||
let database = db_path.map(|path| Series::open(path).unwrap());
|
|
||||||
let s = Self {
|
|
||||||
database: Arc::new(RwLock::new(database)),
|
|
||||||
};
|
|
||||||
|
|
||||||
s
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn process_invocation(&self, invocation: AppInvocation) -> AppResponse {
|
|
||||||
match invocation {
|
|
||||||
AppInvocation::OpenDatabase(db_path) => {
|
|
||||||
self.open_db(&db_path);
|
|
||||||
AppResponse::DatabaseChanged(db_path)
|
|
||||||
}
|
|
||||||
AppInvocation::RequestRecords => {
|
|
||||||
if self.database.read().unwrap().is_none() {
|
|
||||||
AppResponse::NoDatabase
|
|
||||||
} else {
|
|
||||||
AppResponse::Records
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn open_db(&self, path: &Path) {
|
|
||||||
let db = Series::open(path).unwrap();
|
|
||||||
*self.database.write().unwrap() = Some(db);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PlaceholderViewPrivate {}
|
|
||||||
|
|
||||||
#[glib::object_subclass]
|
|
||||||
impl ObjectSubclass for PlaceholderViewPrivate {
|
|
||||||
const NAME: &'static str = "PlaceholderView";
|
|
||||||
type Type = PlaceholderView;
|
|
||||||
type ParentType = gtk::Box;
|
|
||||||
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ObjectImpl for PlaceholderViewPrivate {}
|
|
||||||
impl WidgetImpl for PlaceholderViewPrivate {}
|
|
||||||
impl BoxImpl for PlaceholderViewPrivate {}
|
|
||||||
|
|
||||||
glib::wrapper! {
|
|
||||||
pub struct PlaceholderView(ObjectSubclass<PlaceholderViewPrivate>) @extends gtk::Box, gtk::Widget;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PlaceholderView {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let s: Self = Object::builder().build();
|
|
||||||
s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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 WelcomeViewPrivate {}
|
|
||||||
|
|
||||||
#[glib::object_subclass]
|
|
||||||
impl ObjectSubclass for WelcomeViewPrivate {
|
|
||||||
const NAME: &'static str = "WelcomeView";
|
|
||||||
type Type = WelcomeView;
|
|
||||||
type ParentType = gtk::Box;
|
|
||||||
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ObjectImpl for WelcomeViewPrivate {}
|
|
||||||
impl WidgetImpl for WelcomeViewPrivate {}
|
|
||||||
impl BoxImpl for WelcomeViewPrivate {}
|
|
||||||
|
|
||||||
glib::wrapper! {
|
|
||||||
pub struct WelcomeView(ObjectSubclass<WelcomeViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WelcomeView {
|
|
||||||
pub fn new<F>(on_save: Box<F>) -> Self
|
|
||||||
where
|
|
||||||
F: Fn(PathBuf) + 'static,
|
|
||||||
{
|
|
||||||
let s: Self = Object::builder().build();
|
|
||||||
s.set_orientation(gtk::Orientation::Vertical);
|
|
||||||
s.set_css_classes(&["welcome"]);
|
|
||||||
|
|
||||||
// Replace this with the welcome screen that we set up in the fitnesstrax/unconfigured-page
|
|
||||||
// branch.
|
|
||||||
let title = gtk::Label::builder()
|
|
||||||
.label("Welcome to FitnessTrax")
|
|
||||||
.css_classes(["welcome-title"])
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let content = gtk::Box::builder()
|
|
||||||
.css_classes(["model-content"])
|
|
||||||
.orientation(gtk::Orientation::Vertical)
|
|
||||||
.vexpand(true)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let save_button = gtk::Button::builder()
|
|
||||||
.label("Save Settings")
|
|
||||||
.sensitive(false)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// 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 = FileChooserRow::new({
|
|
||||||
let save_button = save_button.clone();
|
|
||||||
move |_| save_button.set_sensitive(true)
|
|
||||||
});
|
|
||||||
|
|
||||||
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.")));
|
|
||||||
content.append(&db_row);
|
|
||||||
|
|
||||||
let on_save = on_save;
|
|
||||||
save_button.connect_clicked({
|
|
||||||
move |_| {
|
|
||||||
if let Some(path) = db_row.path() {
|
|
||||||
on_save(path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
s.append(&title);
|
|
||||||
s.append(&content);
|
|
||||||
s.append(&save_button);
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
|
||||||
enum ViewName {
|
|
||||||
Placeholder,
|
|
||||||
Welcome,
|
|
||||||
Historical,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum View {
|
|
||||||
Placeholder(gtk::Widget),
|
|
||||||
Welcome(gtk::Widget),
|
|
||||||
Historical(gtk::Widget),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl View {
|
|
||||||
fn widget<'a>(&'a self) -> &'a gtk::Widget {
|
|
||||||
match self {
|
|
||||||
View::Placeholder(widget) => widget,
|
|
||||||
View::Welcome(widget) => widget,
|
|
||||||
View::Historical(widget) => widget,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The application window, or the main window, is the main user interface for the app. Almost
|
|
||||||
/// everything occurs here.
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct AppWindow {
|
|
||||||
app_tx: Sender<AppInvocation>,
|
|
||||||
window: adw::ApplicationWindow,
|
|
||||||
layout: gtk::Box,
|
|
||||||
current_view: Rc<RefCell<View>>,
|
|
||||||
settings: gio::Settings,
|
|
||||||
}
|
|
||||||
|
|
||||||
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(app_id: &str, adw_app: &adw::Application, app_tx: Sender<AppInvocation>) -> AppWindow {
|
|
||||||
let window = adw::ApplicationWindow::builder()
|
|
||||||
.application(adw_app)
|
|
||||||
.width_request(800)
|
|
||||||
.height_request(600)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
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(>k::Label::new(Some("FitnessTrax")))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let layout = gtk::Box::builder()
|
|
||||||
.orientation(gtk::Orientation::Vertical)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let initial_view = View::Placeholder(PlaceholderView::new().upcast());
|
|
||||||
|
|
||||||
layout.append(&header);
|
|
||||||
layout.append(initial_view.widget());
|
|
||||||
|
|
||||||
window.set_content(Some(&layout));
|
|
||||||
window.present();
|
|
||||||
|
|
||||||
let s = Self {
|
|
||||||
app_tx,
|
|
||||||
window,
|
|
||||||
layout,
|
|
||||||
current_view: Rc::new(RefCell::new(initial_view)),
|
|
||||||
settings: gio::Settings::new(app_id),
|
|
||||||
};
|
|
||||||
|
|
||||||
s
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn change_view(&self, view: ViewName) {
|
|
||||||
self.swap_main(self.construct_view(view));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn process_response(&self, response: AppResponse) {
|
|
||||||
match response {
|
|
||||||
AppResponse::DatabaseChanged(db_path) => {
|
|
||||||
self.settings
|
|
||||||
.set_string("series-path", db_path.to_str().unwrap())
|
|
||||||
.unwrap();
|
|
||||||
self.change_view(ViewName::Historical);
|
|
||||||
}
|
|
||||||
AppResponse::NoDatabase => {
|
|
||||||
self.change_view(ViewName::Welcome);
|
|
||||||
}
|
|
||||||
AppResponse::Records => {
|
|
||||||
self.change_view(ViewName::Historical);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 swap_main(&self, view: View) {
|
|
||||||
let mut current_widget = self.current_view.borrow_mut();
|
|
||||||
self.layout.remove(&*current_widget.widget());
|
|
||||||
*current_widget = view;
|
|
||||||
self.layout.append(&*current_widget.widget());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn construct_view(&self, view: ViewName) -> View {
|
|
||||||
match view {
|
|
||||||
ViewName::Placeholder => View::Placeholder(PlaceholderView::new().upcast()),
|
|
||||||
ViewName::Welcome => View::Welcome(
|
|
||||||
WelcomeView::new({
|
|
||||||
let s = self.clone();
|
|
||||||
Box::new(move |path: PathBuf| {
|
|
||||||
s.app_tx
|
|
||||||
.send_blocking(AppInvocation::OpenDatabase(path))
|
|
||||||
.unwrap();
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.upcast(),
|
|
||||||
),
|
|
||||||
ViewName::Historical => View::Historical(HistoricalView::new().upcast()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// I still don't fully understand gio resources. resources_register_include! is convenient
|
// I still don't fully understand gio resources. resources_register_include! is convenient
|
||||||
// because I don't have to deal with filesystem locations at runtime. However, I think other
|
// because I don't have to deal with filesystem locations at runtime. However, I think other
|
||||||
|
@ -411,7 +43,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let settings = gio::Settings::new(app_id);
|
let settings = gio::Settings::new(app_id);
|
||||||
let app = App::new({
|
let app = app::App::new({
|
||||||
let path = settings.string("series-path");
|
let path = settings.string("series-path");
|
||||||
if path.is_empty() {
|
if path.is_empty() {
|
||||||
None
|
None
|
||||||
|
@ -433,20 +65,20 @@ fn main() {
|
||||||
adw_app.connect_activate(move |adw_app| {
|
adw_app.connect_activate(move |adw_app| {
|
||||||
// These channels are used to send messages to the UI. Anything that needs to send a
|
// These channels are used to send messages to the UI. Anything that needs to send a
|
||||||
// message to the UI will send it via `ui_tx`. We will have one single process that owns
|
// message to the UI will send it via `ui_tx`. We will have one single process that owns
|
||||||
// `ui_rx`. That process will read messages coming in and send them to AppWindow for proper
|
// `ui_rx`. That process will read messages coming in and send them to [AppWindow] for proper
|
||||||
// processing.
|
// processing.
|
||||||
//
|
//
|
||||||
// The core app will usually only send messages in response to a request, but this channel
|
// The core app will usually only send messages in response to a request, but this channel
|
||||||
// can also be used to tell the UI that something happened in the background, such as
|
// can also be used to tell the UI that something happened in the background, such as
|
||||||
// detecting a watch, detecting new tracks to import, and so forth.
|
// detecting a watch, detecting new tracks to import, and so forth.
|
||||||
let (ui_tx, ui_rx) = async_channel::unbounded::<AppResponse>();
|
let (ui_tx, ui_rx) = async_channel::unbounded::<app::AppResponse>();
|
||||||
|
|
||||||
// These channels are used for communicating with the app. Already I can see that a lot of
|
// These channels are used for communicating with the app. Already I can see that a lot of
|
||||||
// different event handlers will need copies of app_tx in order to send requests into the
|
// different event handlers will need copies of app_tx in order to send requests into the
|
||||||
// UI.
|
// UI.
|
||||||
let (app_tx, app_rx) = async_channel::unbounded::<AppInvocation>();
|
let (app_tx, app_rx) = async_channel::unbounded::<app::AppInvocation>();
|
||||||
|
|
||||||
let window = AppWindow::new(app_id, adw_app, app_tx.clone());
|
let window = AppWindow::new(app_id, RESOURCE_BASE_PATH, adw_app, app_tx.clone());
|
||||||
|
|
||||||
// Spawn a future where the UI will receive messages for the app window. Previously, this
|
// Spawn a future where the UI will receive messages for the app window. Previously, this
|
||||||
// would have been done by creating a glib::MainContext::channel(), but that has been
|
// would have been done by creating a glib::MainContext::channel(), but that has been
|
||||||
|
@ -454,7 +86,7 @@ fn main() {
|
||||||
glib::spawn_future_local(async move {
|
glib::spawn_future_local(async move {
|
||||||
// The app requests data to start with. This kicks everything off. The response from
|
// The app requests data to start with. This kicks everything off. The response from
|
||||||
// the app will cause the window to be updated shortly.
|
// the app will cause the window to be updated shortly.
|
||||||
let _ = app_tx.send(AppInvocation::RequestRecords).await;
|
let _ = app_tx.send(app::AppInvocation::RequestRecords).await;
|
||||||
|
|
||||||
while let Ok(response) = ui_rx.recv().await {
|
while let Ok(response) = ui_rx.recv().await {
|
||||||
window.process_response(response);
|
window.process_response(response);
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use glib::Object;
|
||||||
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
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 historical_view;
|
||||||
|
pub use historical_view::HistoricalView;
|
||||||
|
|
||||||
|
mod placeholder_view;
|
||||||
|
pub use placeholder_view::PlaceholderView;
|
||||||
|
|
||||||
|
mod welcome_view;
|
||||||
|
pub use welcome_view::WelcomeView;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum ViewName {
|
||||||
|
Welcome,
|
||||||
|
Historical,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum View {
|
||||||
|
Placeholder(gtk::Widget),
|
||||||
|
Welcome(gtk::Widget),
|
||||||
|
Historical(gtk::Widget),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl View {
|
||||||
|
pub fn widget<'a>(&'a self) -> &'a gtk::Widget {
|
||||||
|
match self {
|
||||||
|
View::Placeholder(widget) => widget,
|
||||||
|
View::Welcome(widget) => widget,
|
||||||
|
View::Historical(widget) => widget,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use glib::Object;
|
||||||
|
use gtk::subclass::prelude::*;
|
||||||
|
|
||||||
|
pub struct PlaceholderViewPrivate {}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for PlaceholderViewPrivate {
|
||||||
|
const NAME: &'static str = "PlaceholderView";
|
||||||
|
type Type = PlaceholderView;
|
||||||
|
type ParentType = gtk::Box;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for PlaceholderViewPrivate {}
|
||||||
|
impl WidgetImpl for PlaceholderViewPrivate {}
|
||||||
|
impl BoxImpl for PlaceholderViewPrivate {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct PlaceholderView(ObjectSubclass<PlaceholderViewPrivate>) @extends gtk::Box, gtk::Widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PlaceholderView {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let s: Self = Object::builder().build();
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use crate::components::FileChooserRow;
|
||||||
|
use glib::Object;
|
||||||
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
/// 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 WelcomeViewPrivate {}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for WelcomeViewPrivate {
|
||||||
|
const NAME: &'static str = "WelcomeView";
|
||||||
|
type Type = WelcomeView;
|
||||||
|
type ParentType = gtk::Box;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for WelcomeViewPrivate {}
|
||||||
|
impl WidgetImpl for WelcomeViewPrivate {}
|
||||||
|
impl BoxImpl for WelcomeViewPrivate {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct WelcomeView(ObjectSubclass<WelcomeViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WelcomeView {
|
||||||
|
pub fn new<F>(on_save: Box<F>) -> Self
|
||||||
|
where
|
||||||
|
F: Fn(PathBuf) + 'static,
|
||||||
|
{
|
||||||
|
let s: Self = Object::builder().build();
|
||||||
|
s.set_orientation(gtk::Orientation::Vertical);
|
||||||
|
s.set_css_classes(&["welcome"]);
|
||||||
|
|
||||||
|
// Replace this with the welcome screen that we set up in the fitnesstrax/unconfigured-page
|
||||||
|
// branch.
|
||||||
|
let title = gtk::Label::builder()
|
||||||
|
.label("Welcome to FitnessTrax")
|
||||||
|
.css_classes(["welcome-title"])
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let content = gtk::Box::builder()
|
||||||
|
.css_classes(["model-content"])
|
||||||
|
.orientation(gtk::Orientation::Vertical)
|
||||||
|
.vexpand(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let save_button = gtk::Button::builder()
|
||||||
|
.label("Save Settings")
|
||||||
|
.sensitive(false)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 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 = FileChooserRow::new({
|
||||||
|
let save_button = save_button.clone();
|
||||||
|
move |_| save_button.set_sensitive(true)
|
||||||
|
});
|
||||||
|
|
||||||
|
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.")));
|
||||||
|
content.append(&db_row);
|
||||||
|
|
||||||
|
let on_save = on_save;
|
||||||
|
save_button.connect_clicked({
|
||||||
|
move |_| {
|
||||||
|
if let Some(path) = db_row.path() {
|
||||||
|
on_save(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
s.append(&title);
|
||||||
|
s.append(&content);
|
||||||
|
s.append(&save_button);
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue