Skip to content

Commit

Permalink
feat: Persistent and volatile settings are now split for easier versi…
Browse files Browse the repository at this point in the history
…oning of configuration files
  • Loading branch information
woelper committed Sep 2, 2024
1 parent e255560 commit 936c996
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ impl Default for OculanteState {
key_grab: Default::default(),
edit_state: Default::default(),
pointer_over_ui: Default::default(),
persistent_settings: Default::default(),
volatile_settings: Default::default(),
persistent_settings: PersistentSettings::load().unwrap_or_default(),
volatile_settings: VolatileSettings::load().unwrap_or_default(),
always_on_top: Default::default(),
network_mode: Default::default(),
window_size: Default::default(),
Expand Down
44 changes: 22 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,27 @@ fn main() -> Result<(), String> {
let _ = mac::launch();
}

// Unfortunately we need to load the volatile settings here, too - the window settings need
// to be set before window creation
match settings::VolatileSettings::load() {
Ok(volatile_settings) => {
if volatile_settings.window_geometry != Default::default() {
window_config.width = volatile_settings.window_geometry.1 .0 as u32;
window_config.height = volatile_settings.window_geometry.1 .1 as u32;
}
}
Err(e) => error!("Could not load volatile settings: {e}"),
}

// Unfortunately we need to load the persistent settings here, too - the window settings need
// to be set before window creation
match settings::PersistentSettings::load() {
Ok(settings) => {
window_config.vsync = settings.vsync;
window_config.lazy_loop = !settings.force_redraw;
window_config.decorations = !settings.borderless;
if settings.window_geometry != Default::default() {
window_config.width = settings.window_geometry.1 .0 as u32;
window_config.height = settings.window_geometry.1 .1 as u32;
}
debug!("Loaded settings.");

trace!("Loaded settings.");
if settings.zen_mode {
let mut title_string = window_config.title.clone();
title_string.push_str(&format!(
Expand All @@ -139,7 +148,7 @@ fn main() -> Result<(), String> {
window_config.min_size = Some(settings.min_window_size);
}
Err(e) => {
error!("Could not load settings: {e}");
error!("Could not load persistent settings: {e}");
}
}
window_config.always_on_top = true;
Expand Down Expand Up @@ -198,18 +207,6 @@ fn init(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins) -> OculanteSta
..Default::default()
};

match settings::PersistentSettings::load() {
Ok(settings) => {
state.persistent_settings = settings;
info!("Successfully loaded previous settings.")
}
Err(e) => {
warn!("Settings failed to load: {e}. This may happen after application updates. Generating a fresh file.");
state.persistent_settings = Default::default();
state.persistent_settings.save_blocking();
}
}

state.player = Player::new(
state.texture_channel.0.clone(),
state.persistent_settings.max_cache,
Expand Down Expand Up @@ -466,6 +463,7 @@ fn event(app: &mut App, state: &mut OculanteState, evt: Event) {
}
if key_pressed(app, state, Quit) {
state.persistent_settings.save_blocking();
_ = state.volatile_settings.save_blocking();
app.backend.exit();
}
#[cfg(feature = "turbo")]
Expand Down Expand Up @@ -608,6 +606,7 @@ fn event(app: &mut App, state: &mut OculanteState, evt: Event) {
app.window().size(),
);
state.persistent_settings.save_blocking();
_ = state.volatile_settings.save_blocking();
}
Event::MouseWheel { delta_y, .. } => {
trace!("Mouse wheel event");
Expand Down Expand Up @@ -711,6 +710,7 @@ fn update(app: &mut App, state: &mut OculanteState) {
app.window().size(),
);
state.persistent_settings.save_blocking();
_ = state.volatile_settings.save_blocking();
trace!("Save {t}");
}

Expand Down Expand Up @@ -847,12 +847,12 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
| FrameSource::Still
| FrameSource::ImageCollectionMember => {
if let Some(path) = &state.current_path {
if !state.persistent_settings.recent_images.contains(path) {
if !state.volatile_settings.recent_images.contains(path) {
state
.persistent_settings
.volatile_settings
.recent_images
.insert(0, path.clone());
state.persistent_settings.recent_images.truncate(10);
state.volatile_settings.recent_images.truncate(10);
}
}
}
Expand Down Expand Up @@ -1216,7 +1216,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
// Show file browser to select image to load
#[cfg(feature = "file_open")]
fn browse_for_image_path(state: &mut OculanteState) {
let start_directory = state.persistent_settings.last_open_directory.clone();
let start_directory = state.volatile_settings.last_open_directory.clone();
let load_sender = state.load_channel.0.clone();
state.redraw = true;
std::thread::spawn(move || {
Expand Down
19 changes: 11 additions & 8 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{shortcuts::*, utils::ColorChannel};
use anyhow::{anyhow, Result};
use log::{info, trace};
use notan::egui::{Context, Visuals};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -125,16 +126,17 @@ impl PersistentSettings {
}
}

fn save(s: &PersistentSettings) -> Result<()> {
fn save(ps: &PersistentSettings) -> Result<()> {
let local_dir = dirs::config_local_dir()
.ok_or(anyhow!("Can't get local dir"))?
.join("oculante");
if !local_dir.exists() {
_ = create_dir_all(&local_dir);
}

let f = File::create(local_dir.join("config.json"))?;
Ok(serde_json::to_writer_pretty(f, s)?)
_ = serde_json::to_writer_pretty(f, ps)?;
trace!("Saved to {}", local_dir.display());
Ok(())
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
Expand All @@ -148,7 +150,6 @@ pub struct VolatileSettings {

impl VolatileSettings {
pub fn load() -> Result<Self> {
//data_local_dir
let config_path = dirs::config_local_dir()
.ok_or(anyhow!("Can't get config_local dir"))?
.join("oculante")
Expand All @@ -157,9 +158,9 @@ impl VolatileSettings {
// migrate old config
?;

Ok(serde_json::from_reader::<_, VolatileSettings>(File::open(
config_path,
)?)?)
let s = serde_json::from_reader::<_, VolatileSettings>(File::open(config_path)?)?;
info!("Loaded volatile settings.");
Ok(s)
}

pub fn save_blocking(&self) -> Result<()> {
Expand All @@ -171,7 +172,9 @@ impl VolatileSettings {
}

let f = File::create(local_dir.join("config_volatile.json"))?;
Ok(serde_json::to_writer_pretty(f, self)?)
let _res = serde_json::to_writer_pretty(f, self)?;
trace!("Saved volatile settings");
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ pub fn edit_ui(app: &mut App, ctx: &Context, state: &mut OculanteState, gfx: &mu
if state.current_image.is_some() {
if ui.button(format!("{FLOPPY_DISK} Save as...")).clicked() {

let start_directory = state.persistent_settings.last_open_directory.clone();
let start_directory = state.volatile_settings.last_open_directory.clone();

let image_to_save = state.edit_state.result_pixel_op.clone();
let msg_sender = state.message_channel.0.clone();
Expand Down Expand Up @@ -2207,7 +2207,7 @@ pub fn draw_hamburger_menu(ui: &mut Ui, state: &mut OculanteState, app: &mut App
}

ui.menu_button("Recent", |ui| {
for r in &state.persistent_settings.recent_images.clone() {
for r in &state.volatile_settings.recent_images.clone() {
if let Some(filename) = r.file_name() {
if ui.button(filename.to_string_lossy()).clicked() {
load_image_from_path(r, state);
Expand Down

0 comments on commit 936c996

Please sign in to comment.