Skip to content

Commit

Permalink
gui: ui - impl auto_save
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Jan 9, 2024
1 parent 58dde78 commit ac6aafc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions gui/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ pub const PIXELS_PER_POINT_UNIT_STR: &str = "0.1";
pub const PIXELS_PER_POINT_MIN_STR: &str = "0.1";
pub const PIXELS_PER_POINT_MAX_STR: &str = "3.0";

// How many seconds in-between auto-saving?
pub const SETTINGS_AUTO_SAVE_INTERVAL_SECS: u64 = 30;

//---------------------------------------------------------------------------------------------------- Fonts
pub const FONT_SOURCECODE_PRO: &[u8] = include_bytes!("../../assets/fonts/SourceCodePro-Regular.otf");
pub const FONT_SOURCECODE_CN: &[u8] = include_bytes!("../../assets/fonts/SourceHanSansCN-Regular.otf");
Expand Down
2 changes: 2 additions & 0 deletions gui/src/data/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub struct Gui {
/// The pixel size needed for the `Runtime` in the bottom UI bar.
/// It depends on the length of the `Runtime` string.
pub runtime_width: f32,
/// AudioState auto-save interval.
pub auto_save: Instant,

/// Reset State.
///
Expand Down
1 change: 1 addition & 0 deletions gui/src/func/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl crate::data::Gui {
audio_leeway: now!(),
last_song: None,
runtime_width: RUNTIME_WIDTH,
auto_save: now!(),

reset_state: ResetState::new(),

Expand Down
38 changes: 32 additions & 6 deletions gui/src/ui/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ use benri::{
flip,
debug_panic,
};
use std::time::{
Instant,
};
use std::time::Instant;
use crate::constants::{
SLIDER_CIRCLE_INACTIVE,
SLIDER_CIRCLE_HOVERED,
Expand All @@ -55,6 +53,7 @@ use crate::constants::{
BONE,BLACK,
YELLOW,GREEN,MEDIUM_GRAY,
RUNTIME_WIDTH,
SETTINGS_AUTO_SAVE_INTERVAL_SECS,
};
use crate::text::{
HELP,MOD,DRAG_AND_DROP,
Expand Down Expand Up @@ -122,11 +121,12 @@ impl eframe::App for Gui {
}

// Acquire a local copy of the `AUDIO_STATE`.
let queue_diff = {
let (queue_diff, audio_state_diff) = {
let lock = AUDIO_STATE.read();
let diff = lock.queue == self.audio_state.queue;
let audio_state_diff = *lock != self.audio_state;
let queue_diff = audio_state_diff && (lock.queue != self.audio_state.queue);
lock.if_copy(&mut self.audio_state);
diff
(queue_diff, audio_state_diff)
};
// If the queue is different from ours,
// recalculate the total `queue_time`.
Expand All @@ -139,6 +139,32 @@ impl eframe::App for Gui {
.sum::<u64>()
.into();
}
// Auto-save state.
if (self.kernel_returned && !self.resetting_collection) &&
self.settings.auto_save && secs!(self.auto_save) > SETTINGS_AUTO_SAVE_INTERVAL_SECS
{
self.auto_save = now!();

let settings = self.diff_settings();
let state = self.diff_state();
let playlists = self.diff_playlists();

if !audio_state_diff && !settings && !state && !playlists {
debug!("GUI - auto-save: no diffs, not saving");
} else {
use disk::Bincode2;
if audio_state_diff {
match self.audio_state.save_atomic() {
Ok(_) => ok!("GUI - Auto-saved AudioState"),
Err(e) => fail!("GUI - AudioState auto-save: {e}"),
}
}

if settings { self.save_settings(); }
if state { self.save_state(); }
if playlists { self.save_playlists(); }
}
}

// HACK:
// Unconditionally copy this.
Expand Down

0 comments on commit ac6aafc

Please sign in to comment.