app window size persists on app close

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-10-18 18:21:56 -04:00
parent d3b4a9efc1
commit 03bfb34172
6 changed files with 144 additions and 4 deletions

View File

@@ -208,7 +208,7 @@ fn get_selected_index(accounts: &[UserAccount], keystore: &KeyStorageType) -> Op
}
KeyStorageResponse::ReceivedResult(Err(e)) => error!("Error getting selected key: {}", e),
_ => (),
KeyStorageResponse::Waiting | KeyStorageResponse::ReceivedResult(Ok(None)) => {}
};
None

View File

@@ -1,6 +1,7 @@
use crate::{
account_manager::AccountManager,
app_creation::setup_cc,
app_size_handler::AppSizeHandler,
app_style::user_requested_visuals_change,
args::Args,
column::Columns,
@@ -60,6 +61,7 @@ pub struct Damus {
pub img_cache: ImageCache,
pub accounts: AccountManager,
pub subscriptions: Subscriptions,
pub app_rect_handler: AppSizeHandler,
frame_history: crate::frame_history::FrameHistory,
@@ -507,6 +509,8 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
error!("error processing event: {}", err);
}
damus.app_rect_handler.try_save_app_size(ctx);
damus.columns.attempt_perform_deletion_request();
}
@@ -746,6 +750,7 @@ impl Damus {
accounts,
frame_history: FrameHistory::default(),
view_state: ViewState::default(),
app_rect_handler: AppSizeHandler::default(),
}
}
@@ -829,6 +834,7 @@ impl Damus {
accounts: AccountManager::new(KeyStorageType::None),
frame_history: FrameHistory::default(),
view_state: ViewState::default(),
app_rect_handler: AppSizeHandler::default(),
}
}

View File

@@ -1,3 +1,4 @@
use crate::app_size_handler::AppSizeHandler;
use crate::app_style::{
create_custom_style, dark_mode, desktop_font_size, light_mode, mobile_font_size,
};
@@ -8,10 +9,16 @@ use eframe::NativeOptions;
pub fn generate_native_options() -> NativeOptions {
generate_native_options_with_builder_modifiers(|builder| {
builder
let builder = builder
.with_fullsize_content_view(true)
.with_titlebar_shown(false)
.with_title_shown(false)
.with_title_shown(false);
if let Some(window_size) = AppSizeHandler::default().get_app_size() {
builder.with_inner_size(window_size)
} else {
builder
}
})
}

100
src/app_size_handler.rs Normal file
View File

@@ -0,0 +1,100 @@
use std::time::{Duration, Instant};
use egui::Context;
use tracing::{error, info};
use crate::{
storage::{write_file, Directory},
DataPaths,
};
pub struct AppSizeHandler {
directory: Option<Directory>,
saved_size: Option<egui::Vec2>,
last_saved: Instant,
}
static FILE_NAME: &str = "app_size.json";
static DELAY: Duration = Duration::from_millis(500);
impl Default for AppSizeHandler {
fn default() -> Self {
let directory = match DataPaths::Setting.get_path() {
Ok(path) => Some(Directory::new(path)),
Err(e) => {
error!("Could not load settings path: {}", e);
None
}
};
Self {
directory,
saved_size: None,
last_saved: Instant::now() - DELAY,
}
}
}
impl AppSizeHandler {
pub fn try_save_app_size(&mut self, ctx: &Context) {
if let Some(interactor) = &self.directory {
// 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(interactor, &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 Some(directory) = &self.directory {
if let Ok(file_contents) = 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 write_file(
&interactor.file_path,
FILE_NAME.to_owned(),
&serialized_rect,
)
.is_ok()
{
info!("wrote size {}", cur_size,);
*maybe_saved_size = Some(cur_size);
}
}
}