Files
notedeck/crates/notedeck_chrome/src/app_size.rs
William Casarin ec755493d9 Introducing Damus Notedeck: a nostr browser
This splits notedeck into:

- notedeck
- notedeck_chrome
- notedeck_columns

The `notedeck` crate is the library that `notedeck_chrome` and
`notedeck_columns`, use. It contains common functionality related to
notedeck apps such as the NoteCache, ImageCache, etc.

The `notedeck_chrome` crate is the binary and ui chrome. It is
responsible for managing themes, user accounts, signing, data paths,
nostrdb, image caches etc. It will eventually have its own ui which has
yet to be determined.  For now it just manages the browser data, which
is passed to apps via a new struct called `AppContext`.

`notedeck_columns` is our columns app, with less responsibility now that
more things are handled by `notedeck_chrome`

There is still much work left to do before this is a proper browser:

- process isolation
- sandboxing
- etc

This is the beginning of a new era! We're just getting started.

Signed-off-by: William Casarin <jb55@jb55.com>
2024-12-12 20:08:55 -08:00

86 lines
2.4 KiB
Rust

use std::time::{Duration, Instant};
use egui::Context;
use tracing::info;
use notedeck::{storage, DataPath, DataPathType, Directory};
pub struct AppSizeHandler {
directory: Directory,
saved_size: Option<egui::Vec2>,
last_saved: Instant,
}
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));
Self {
directory,
saved_size: None,
last_saved: Instant::now() - DELAY,
}
}
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();
}
}
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);
}
}
}