Files
yt-offline/src/app.rs
T
firewire 0009db0ea9
/ Build Release Binary (ubuntu-latest) (push) Successful in 1m42s
/ Build Release Binary (windows-latest) (push) Successful in 5m14s
Merge video_quality to types + prevent downloader from throwing warnings while im working in it + add todos + tweak the about and settings windows
2026-09-17 03:55:27 -04:00

107 lines
3.0 KiB
Rust

use crate::args::ArgsConfig;
use crate::types::{Playlist, Video, VideoDownloadQuality};
use eframe::egui::{Context, Ui};
use eframe::{CreationContext, Frame, Storage};
use serde::{Deserialize, Serialize};
use std::time::Duration;
pub(crate) struct Application {
pub(crate) settings: AppSettings,
layers: Vec<Box<dyn crate::layer::Layer>>,
pub(crate) data: Data,
pub(crate) data_dir: String,
}
#[derive(Serialize, Deserialize, Default)]
pub struct AppSettings {
pub(crate) use_firefox_cookies: bool,
pub(crate) use_chrome_cookies: bool,
pub(crate) save_thumbnails: bool,
pub(crate) reencode_videos: bool,
pub(crate) video_download_quality: VideoDownloadQuality,
}
#[derive(Serialize, Deserialize, Default)]
pub(crate) struct Data {
pub(crate) playlists: Vec<Playlist>,
pub(crate) videos: Vec<Video>,
pub(crate) root_path: String,
}
impl Application {
pub fn new(
cc: &CreationContext,
layers: Vec<Box<dyn crate::layer::Layer>>,
args_config: &ArgsConfig,
) -> Self {
let mut settings = AppSettings::default();
if let Some(storage) = cc.storage {
if let Some(stored_str) = storage.get_string("app_settings") {
if let Ok(parsed) = serde_json::from_str(&stored_str) {
settings = parsed;
}
}
}
let mut data = Data::default();
if let Some(storage) = cc.storage {
if let Some(stored_str) = storage.get_string("app_data") {
if let Ok(parsed) = serde_json::from_str(&stored_str) {
data = parsed;
}
}
}
if data.root_path.is_empty() {
data.root_path = args_config.data_dir.clone() + "/data";
}
Self {
settings,
layers,
data,
data_dir: args_config.data_dir.clone(),
}
}
}
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) {
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) {
if let Ok(serialized) = serde_json::to_string(&self.settings) {
_storage.set_string("app_settings", serialized);
}
if let Ok(serialized) = serde_json::to_string(&self.data) {
_storage.set_string("app_data", serialized);
}
}
fn on_exit(&mut self) {
let mut layers = std::mem::take(&mut self.layers);
for layer in &mut layers {
layer.on_exit(self);
}
self.layers = layers;
}
fn auto_save_interval(&self) -> Duration {
Duration::from_secs(5)
}
}