notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 205d627f99731d3ca244007c4a1fb865afec5a1f
parent a24a089e87bf86b7a9efd36726bf92172b8af2e9
Author: kernelkind <kernelkind@gmail.com>
Date:   Mon, 13 Jan 2025 18:07:57 -0500

use TimedSerializer in AppSizeHandler

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Mcrates/notedeck_chrome/src/app_size.rs | 78++++++++++++------------------------------------------------------------------
Mcrates/notedeck_chrome/src/lib.rs | 1+
2 files changed, 13 insertions(+), 66 deletions(-)

diff --git a/crates/notedeck_chrome/src/app_size.rs b/crates/notedeck_chrome/src/app_size.rs @@ -1,85 +1,31 @@ -use std::time::{Duration, Instant}; +use std::time::Duration; use egui::Context; -use tracing::info; -use notedeck::{storage, DataPath, DataPathType, Directory}; +use notedeck::{DataPath, DataPathType}; + +use crate::timed_serializer::TimedSerializer; pub struct AppSizeHandler { - directory: Directory, - saved_size: Option<egui::Vec2>, - last_saved: Instant, + serializer: TimedSerializer<egui::Vec2>, } -static FILE_NAME: &str = "app_size.json"; -static DELAY: Duration = Duration::from_millis(500); - impl AppSizeHandler { pub fn new(path: &DataPath) -> Self { - let directory = Directory::new(path.path(DataPathType::Setting)); + let serializer = + TimedSerializer::new(path, DataPathType::Setting, "app_size.json".to_owned()) + .with_delay(Duration::from_millis(500)); - Self { - directory, - saved_size: None, - last_saved: Instant::now() - DELAY, - } + Self { serializer } } pub fn try_save_app_size(&mut self, ctx: &Context) { // There doesn't seem to be a way to check if user is resizing window, so if the rect is different than last saved, we'll wait DELAY before saving again to avoid spamming io - if self.last_saved.elapsed() >= DELAY { - internal_try_save_app_size(&self.directory, &mut self.saved_size, ctx); - self.last_saved = Instant::now(); - } + let cur_size = ctx.input(|i| i.screen_rect.size()); + self.serializer.try_save(cur_size); } pub fn get_app_size(&self) -> Option<egui::Vec2> { - if self.saved_size.is_some() { - return self.saved_size; - } - - if let Ok(file_contents) = self.directory.get_file(FILE_NAME.to_owned()) { - if let Ok(rect) = serde_json::from_str::<egui::Vec2>(&file_contents) { - return Some(rect); - } - } else { - info!("Could not find {}", FILE_NAME); - } - - None - } -} - -fn internal_try_save_app_size( - interactor: &Directory, - maybe_saved_size: &mut Option<egui::Vec2>, - ctx: &Context, -) { - let cur_size = ctx.input(|i| i.screen_rect.size()); - if let Some(saved_size) = maybe_saved_size { - if cur_size != *saved_size { - try_save_size(interactor, cur_size, maybe_saved_size); - } - } else { - try_save_size(interactor, cur_size, maybe_saved_size); - } -} - -fn try_save_size( - interactor: &Directory, - cur_size: egui::Vec2, - maybe_saved_size: &mut Option<egui::Vec2>, -) { - if let Ok(serialized_rect) = serde_json::to_string(&cur_size) { - if storage::write_file( - &interactor.file_path, - FILE_NAME.to_owned(), - &serialized_rect, - ) - .is_ok() - { - info!("wrote size {}", cur_size,); - *maybe_saved_size = Some(cur_size); - } + self.serializer.get_item() } } diff --git a/crates/notedeck_chrome/src/lib.rs b/crates/notedeck_chrome/src/lib.rs @@ -2,6 +2,7 @@ pub mod app_size; pub mod fonts; pub mod setup; pub mod theme; +pub mod timed_serializer; mod app;