Set up the main application window #122

Merged
savanni merged 3 commits from fitnesstrax/main-window into main 2023-12-19 00:13:08 +00:00
1 changed files with 24 additions and 21 deletions
Showing only changes of commit acdf9ec150 - Show all commits

View File

@ -18,7 +18,7 @@ use emseries::Series;
use ft_core::TraxRecord;
use gio::resources_lookup_data;
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use std::{
cell::RefCell,
env,
@ -117,22 +117,13 @@ impl HistoricalView {
}
}
/// 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.
struct AppWindow {
app: App,
window: adw::ApplicationWindow,
current_view: RefCell<MainView>,
layout: gtk::Box,
current_view: RefCell<gtk::Widget>,
}
impl AppWindow {
@ -150,9 +141,9 @@ impl AppWindow {
.build();
let current_view = if app.database.read().unwrap().is_none() {
MainView::Unconfigured(UnconfiguredView::new())
UnconfiguredView::new().upcast()
} else {
MainView::Historical(HistoricalView::new())
HistoricalView::new().upcast()
};
let stylesheet = String::from_utf8(
@ -171,26 +162,38 @@ impl AppWindow {
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.redraw();
s
}
// Switch views.
//
// This function only replaces the old view with the one which matches the current view state.
// If there is any other setup/teardown to do, it will handle that as well.
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)),
}
// 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);
}
}