Setup a layer trait

This commit is contained in:
2026-09-16 21:03:41 -04:00
parent a33a40c27f
commit 1b4b4d39c4
5 changed files with 71 additions and 12 deletions
-1
View File
@@ -1,3 +1,2 @@
# .cargo/config.toml
[env] [env]
CMAKE_POLICY_VERSION_MINIMUM = "3.5" CMAKE_POLICY_VERSION_MINIMUM = "3.5"
+18 -10
View File
@@ -1,20 +1,23 @@
use std::time::Duration; use std::time::Duration;
use eframe::{egui, Frame, Storage, CreationContext}; use eframe::{Frame, Storage, CreationContext};
use eframe::egui::{Context, Ui}; use eframe::egui::{Context, Ui};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Default)]
pub(crate) struct Application { pub(crate) struct Application {
settings: AppSettings, settings: AppSettings,
layers: Vec<Box<dyn crate::layer::Layer>>,
} }
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
struct AppSettings { struct AppSettings {
serialization_test_string: String, use_cookies: bool,
use_firefox_cookies: bool,
use_chrome_cookies: bool,
save_thumbnails: bool,
} }
impl Application { impl Application {
pub fn new(cc: &CreationContext) -> Self { pub fn new(cc: &CreationContext, layers: Vec<Box<dyn crate::layer::Layer>>) -> Self {
let mut settings = AppSettings::default(); let mut settings = AppSettings::default();
if let Some(storage) = cc.storage { if let Some(storage) = cc.storage {
if let Some(stored_str) = storage.get_string("app_settings") { if let Some(stored_str) = storage.get_string("app_settings") {
@@ -24,21 +27,26 @@ impl Application {
} }
} }
Self { settings } Self { settings, layers }
} }
} }
impl eframe::App for Application { impl eframe::App for Application {
fn logic(&mut self, ctx: &Context, frame: &mut Frame) { fn logic(&mut self, ctx: &Context, frame: &mut Frame) {
let mut layers = std::mem::take(&mut self.layers);
for layer in &mut layers {
layer.logic(self, ctx, frame);
}
self.layers = layers;
} }
fn ui(&mut self, ui: &mut Ui, frame: &mut Frame) { fn ui(&mut self, ui: &mut Ui, frame: &mut Frame) {
egui::CentralPanel::default().show(ui, |ui| { let mut layers = std::mem::take(&mut self.layers);
ui.heading("Hello!"); for layer in &mut layers {
ui.text_edit_singleline(&mut self.settings.serialization_test_string); layer.ui(self, ui, frame);
}); }
self.layers = layers;
} }
fn save(&mut self, _storage: &mut dyn Storage) { fn save(&mut self, _storage: &mut dyn Storage) {
+19
View File
@@ -0,0 +1,19 @@
use eframe::{egui, Frame};
use egui::Context;
use egui::Ui;
use crate::app::Application;
#[allow(unused_variables)]
pub trait Layer {
fn logic(&mut self, application: &mut Application, ctx: &Context, frame: &mut Frame) {
}
fn ui(&mut self, application: &mut Application, ui: &mut Ui, frame: &mut Frame) {
}
fn on_exit(&mut self, application: &mut Application) {
}
}
+9 -1
View File
@@ -2,9 +2,13 @@ use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use tracing::{debug, error}; use tracing::{debug, error};
use crate::test_layer::TestLayer;
mod logging; mod logging;
mod args; mod args;
mod app; mod app;
mod layer;
mod test_layer;
fn main() { fn main() {
let args = args::parse_args(); let args = args::parse_args();
@@ -23,12 +27,16 @@ fn main() {
} }
} }
let mut layers: Vec<Box<dyn layer::Layer>> = Vec::new();
layers.push(Box::new(TestLayer::default()));
let mut options = eframe::NativeOptions::default(); let mut options = eframe::NativeOptions::default();
options.persistence_path = Some(args.state.parse().expect("Failed to parse state config file path into path")); options.persistence_path = Some(args.state.parse().expect("Failed to parse state config file path into path"));
eframe::run_native( eframe::run_native(
"yt-offline", "yt-offline",
options, options,
Box::new(|cc| { Box::new(|cc| {
Ok(Box::new(app::Application::new(cc))) Ok(Box::new(app::Application::new(cc, layers)))
})).expect("TODO: panic message"); })).expect("TODO: panic message");
} }
+25
View File
@@ -0,0 +1,25 @@
use eframe::egui::{Context, Ui};
use eframe::{egui, Frame};
use crate::app;
use crate::layer::Layer;
#[derive(Default)]
pub struct TestLayer {
test_string: String,
}
impl Layer for TestLayer {
fn logic(&mut self, application: &mut app::Application, ctx: &Context, frame: &mut Frame) {
}
fn ui(&mut self, application: &mut app::Application, ui: &mut Ui, frame: &mut Frame) {
egui::CentralPanel::default().show(ui, |ui| {
ui.heading("Hello from the test_layer!");
ui.text_edit_singleline(&mut self.test_string);
if ui.button("Print test_string in layer").clicked() {
println!("{}", self.test_string);
}
});
}
}