From 1b4b4d39c4b9583006514321b390f6616a1d96eb Mon Sep 17 00:00:00 2001 From: Lyra Date: Wed, 16 Sep 2026 21:03:41 -0400 Subject: [PATCH] Setup a layer trait --- .cargo/cargo.toml | 1 - src/app.rs | 28 ++++++++++++++++++---------- src/layer.rs | 19 +++++++++++++++++++ src/main.rs | 10 +++++++++- src/test_layer.rs | 25 +++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/layer.rs create mode 100644 src/test_layer.rs diff --git a/.cargo/cargo.toml b/.cargo/cargo.toml index 557031b..4504ade 100644 --- a/.cargo/cargo.toml +++ b/.cargo/cargo.toml @@ -1,3 +1,2 @@ -# .cargo/config.toml [env] CMAKE_POLICY_VERSION_MINIMUM = "3.5" \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index f766bfd..5b8320c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,20 +1,23 @@ use std::time::Duration; -use eframe::{egui, Frame, Storage, CreationContext}; +use eframe::{Frame, Storage, CreationContext}; use eframe::egui::{Context, Ui}; use serde::{Deserialize, Serialize}; -#[derive(Default)] pub(crate) struct Application { settings: AppSettings, + layers: Vec>, } #[derive(Serialize, Deserialize, Default)] struct AppSettings { - serialization_test_string: String, + use_cookies: bool, + use_firefox_cookies: bool, + use_chrome_cookies: bool, + save_thumbnails: bool, } impl Application { - pub fn new(cc: &CreationContext) -> Self { + pub fn new(cc: &CreationContext, layers: Vec>) -> Self { let mut settings = AppSettings::default(); if let Some(storage) = cc.storage { 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 { 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) { - egui::CentralPanel::default().show(ui, |ui| { - ui.heading("Hello!"); - ui.text_edit_singleline(&mut self.settings.serialization_test_string); - }); + let mut layers = std::mem::take(&mut self.layers); + for layer in &mut layers { + layer.ui(self, ui, frame); + } + self.layers = layers; } fn save(&mut self, _storage: &mut dyn Storage) { diff --git a/src/layer.rs b/src/layer.rs new file mode 100644 index 0000000..d311006 --- /dev/null +++ b/src/layer.rs @@ -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) { + + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5d265b2..b720c7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,13 @@ use std::fs; use std::path::PathBuf; use std::process::exit; use tracing::{debug, error}; +use crate::test_layer::TestLayer; + mod logging; mod args; mod app; +mod layer; +mod test_layer; fn main() { let args = args::parse_args(); @@ -23,12 +27,16 @@ fn main() { } } + let mut layers: Vec> = Vec::new(); + layers.push(Box::new(TestLayer::default())); + + let mut options = eframe::NativeOptions::default(); options.persistence_path = Some(args.state.parse().expect("Failed to parse state config file path into path")); eframe::run_native( "yt-offline", options, Box::new(|cc| { - Ok(Box::new(app::Application::new(cc))) + Ok(Box::new(app::Application::new(cc, layers))) })).expect("TODO: panic message"); } \ No newline at end of file diff --git a/src/test_layer.rs b/src/test_layer.rs new file mode 100644 index 0000000..a91058b --- /dev/null +++ b/src/test_layer.rs @@ -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); + } + }); + } +} \ No newline at end of file