2023-12-18 23:30:41 +00:00
|
|
|
use adw::prelude::*;
|
|
|
|
use emseries::Series;
|
|
|
|
use ft_core::TraxRecord;
|
2023-12-18 16:59:56 +00:00
|
|
|
use gio::resources_lookup_data;
|
2023-12-18 23:30:41 +00:00
|
|
|
use glib::Object;
|
|
|
|
use gtk::{prelude::*, subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
|
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
env,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2023-11-20 03:47:36 +00:00
|
|
|
|
2023-12-07 14:45:56 +00:00
|
|
|
const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev";
|
2023-12-07 14:56:10 +00:00
|
|
|
const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax";
|
2023-12-07 14:45:56 +00:00
|
|
|
|
2023-12-18 16:59:56 +00:00
|
|
|
const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/fitnesstrax/";
|
2023-12-07 14:45:56 +00:00
|
|
|
|
2023-12-18 23:30:41 +00:00
|
|
|
/// 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 {}
|
|
|
|
}
|
|
|
|
}
|
2023-11-20 03:47:36 +00:00
|
|
|
|
2023-12-18 23:30:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
Historical(HistoricalView),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The application window, or the main window, is the main user interface for the app. Almost
|
|
|
|
/// everything occurs here.
|
2023-11-20 03:47:36 +00:00
|
|
|
struct AppWindow {
|
2023-12-18 23:30:41 +00:00
|
|
|
app: App,
|
2023-11-20 03:47:36 +00:00
|
|
|
window: adw::ApplicationWindow,
|
2023-12-18 23:30:41 +00:00
|
|
|
current_view: RefCell<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 window = adw::ApplicationWindow::builder()
|
|
|
|
.application(adw_app)
|
|
|
|
.width_request(800)
|
|
|
|
.height_request(600)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let current_view = if app.database.read().unwrap().is_none() {
|
|
|
|
MainView::Unconfigured(UnconfiguredView::new())
|
|
|
|
} else {
|
|
|
|
MainView::Historical(HistoricalView::new())
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
window.present();
|
|
|
|
|
|
|
|
let s = Self {
|
|
|
|
app,
|
|
|
|
window,
|
|
|
|
current_view: RefCell::new(current_view),
|
|
|
|
};
|
|
|
|
s.redraw();
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn redraw(&self) {
|
|
|
|
match *self.current_view.borrow() {
|
|
|
|
MainView::Unconfigured(ref view) => self.window.set_content(Some(view)),
|
|
|
|
MainView::Historical(ref view) => self.window.set_content(Some(view)),
|
|
|
|
}
|
|
|
|
}
|
2023-11-20 03:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 13:52:10 +00:00
|
|
|
fn main() {
|
2023-12-18 16:59:56 +00:00
|
|
|
// 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
|
|
|
|
// GTK applications do that rather than compiling the resources directly into the app. So, I'm
|
|
|
|
// unclear as to how I want to handle this.
|
|
|
|
gio::resources_register_include!("com.luminescent-dreams.fitnesstrax.gresource")
|
|
|
|
.expect("to register resources");
|
|
|
|
|
|
|
|
let app_id = if std::env::var_os("ENV") == Some("dev".into()) {
|
|
|
|
APP_ID_DEV
|
2023-12-07 14:56:10 +00:00
|
|
|
} else {
|
2023-12-18 16:59:56 +00:00
|
|
|
APP_ID_PROD
|
2023-12-07 14:56:10 +00:00
|
|
|
};
|
2023-12-07 14:45:56 +00:00
|
|
|
|
|
|
|
let settings = gio::Settings::new(app_id);
|
|
|
|
|
|
|
|
println!("database path: {}", settings.string("series-path"));
|
|
|
|
|
2023-12-18 23:30:41 +00:00
|
|
|
let app = App::new();
|
2023-11-20 03:47:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
let runtime = tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
*/
|
|
|
|
|
2023-12-18 23:30:41 +00:00
|
|
|
let adw_app = adw::Application::builder()
|
2023-12-18 16:59:56 +00:00
|
|
|
.application_id(app_id)
|
|
|
|
.resource_base_path(RESOURCE_BASE_PATH)
|
2023-11-20 03:47:36 +00:00
|
|
|
.build();
|
|
|
|
|
2023-12-18 23:30:41 +00:00
|
|
|
adw_app.connect_activate(move |adw_app| {
|
|
|
|
AppWindow::new(adw_app, app.clone());
|
2023-11-20 03:47:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2023-12-18 23:30:41 +00:00
|
|
|
ApplicationExtManual::run_with_args(&adw_app, &args);
|
2023-11-13 13:52:10 +00:00
|
|
|
}
|