refactor and clean up app library

This commit is contained in:
Ashley Wulber 2022-01-14 17:53:11 -05:00
parent 4ce3b3cc21
commit 28151177d0
7 changed files with 670 additions and 498 deletions

View file

@ -1,71 +1,44 @@
use std::fs::File;
use glib::signal::Inhibit;
use gtk4::prelude::*;
use gtk4::glib;
use gtk4::subclass::prelude::*;
use gtk4::ScrolledWindow;
use gtk4::{gio, glib};
use gtk4::{GridView, SearchEntry};
use gtk4::SearchEntry;
use once_cell::sync::OnceCell;
use crate::app_group::AppGroup;
use crate::utils::data_path;
use crate::app_grid::AppGrid;
use crate::group_grid::GroupGrid;
// Object holding the state
#[derive(Default)]
pub struct Window {
pub struct AppLibraryWindow {
pub entry: OnceCell<SearchEntry>,
pub app_grid_view: OnceCell<GridView>,
pub app_model: OnceCell<gio::ListStore>,
pub group_grid_view: OnceCell<GridView>,
pub group_scroll_window: OnceCell<ScrolledWindow>,
pub group_model: OnceCell<gio::ListStore>,
pub app_grid: OnceCell<AppGrid>,
pub group_grid: OnceCell<GroupGrid>,
}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for Window {
impl ObjectSubclass for AppLibraryWindow {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "LauncherWindow";
type Type = super::Window;
const NAME: &'static str = "AppLibraryWindow";
type Type = super::AppLibraryWindow;
type ParentType = gtk4::ApplicationWindow;
}
// Trait shared by all GObjects
impl ObjectImpl for Window {}
impl ObjectImpl for AppLibraryWindow {}
// Trait shared by all widgets
impl WidgetImpl for Window {}
impl WidgetImpl for AppLibraryWindow {}
// Trait shared by all windows
impl WindowImpl for Window {
impl WindowImpl for AppLibraryWindow {
fn close_request(&self, window: &Self::Type) -> Inhibit {
// Store todo data in vector
let mut backup_data = Vec::new();
let mut position = 3;
while let Some(item) = window.group_model().item(position) {
if position == window.group_model().n_items() - 1 {
break;
}
// Get `AppGroup` from `glib::Object`
let group_data = item
.downcast_ref::<AppGroup>()
.expect("The object needs to be of type `AppGroupData`.")
.group_data();
// Add todo data to vector and increase position
backup_data.push(group_data);
position += 1;
}
// Save state in file
let file = File::create(data_path()).expect("Could not create json file.");
serde_json::to_writer_pretty(file, &backup_data)
.expect("Could not write data to json file");
let imp = AppLibraryWindow::from_instance(window);
imp.group_grid.get().unwrap().store_data();
// Pass close request on to the parent
self.parent_close_request(window)
}
}
// Trait shared by all application
impl ApplicationWindowImpl for Window {}
impl ApplicationWindowImpl for AppLibraryWindow {}