Add the window header bar

This commit is contained in:
Savanni D'Gerinel 2023-12-18 19:08:32 -05:00
parent 0ebdcd7c2a
commit acdf9ec150
1 changed files with 24 additions and 21 deletions

View File

@ -18,7 +18,7 @@ use emseries::Series;
use ft_core::TraxRecord; use ft_core::TraxRecord;
use gio::resources_lookup_data; use gio::resources_lookup_data;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER}; use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use std::{ use std::{
cell::RefCell, cell::RefCell,
env, 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 /// The application window, or the main window, is the main user interface for the app. Almost
/// everything occurs here. /// everything occurs here.
struct AppWindow { struct AppWindow {
app: App, app: App,
window: adw::ApplicationWindow, window: adw::ApplicationWindow,
current_view: RefCell<MainView>, layout: gtk::Box,
current_view: RefCell<gtk::Widget>,
} }
impl AppWindow { impl AppWindow {
@ -150,9 +141,9 @@ impl AppWindow {
.build(); .build();
let current_view = if app.database.read().unwrap().is_none() { let current_view = if app.database.read().unwrap().is_none() {
MainView::Unconfigured(UnconfiguredView::new()) UnconfiguredView::new().upcast()
} else { } else {
MainView::Historical(HistoricalView::new()) HistoricalView::new().upcast()
}; };
let stylesheet = String::from_utf8( let stylesheet = String::from_utf8(
@ -171,26 +162,38 @@ impl AppWindow {
let context = window.style_context(); let context = window.style_context();
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER); 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(); window.present();
let s = Self { let s = Self {
app, app,
window, window,
layout,
current_view: RefCell::new(current_view), current_view: RefCell::new(current_view),
}; };
s.redraw();
s s
} }
// Switch views. // Switch views.
// //
// This function only replaces the old view with the one which matches the current view state. // 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. // It is responsible for ensuring that the new view goes into the layout in the correct
fn redraw(&self) { // position.
match *self.current_view.borrow() { fn change_view(&self, view: gtk::Widget) {
MainView::Unconfigured(ref view) => self.window.set_content(Some(view)), let mut current_view = self.current_view.borrow_mut();
MainView::Historical(ref view) => self.window.set_content(Some(view)), self.layout.remove(&*current_view);
} *current_view = view;
self.layout.append(&*current_view);
} }
} }