Skip to content

Commit

Permalink
Merge branch 'autosave'
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Jan 9, 2024
2 parents 628b3ef + 5309666 commit b3d7d55
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 30 deletions.
Binary file added assets/festival/gui/state/settings3_new.bin
Binary file not shown.
Binary file added assets/festival/gui/state/settings3_real.bin
Binary file not shown.
5 changes: 4 additions & 1 deletion gui/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub const GUI: &str = "gui";
pub const STATE_VERSION: u8 = 1;

/// Current major version of `GUI`'s `Settings`
pub const SETTINGS_VERSION: u8 = 2;
pub const SETTINGS_VERSION: u8 = 3;

//---------------------------------------------------------------------------------------------------- Resolution
// 700.0 works on some `Album`'s in view tabs
Expand Down 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";

// Auto-save default interval.
pub const AUTO_SAVE_INTERVAL_SECONDS: u8 = 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
2 changes: 2 additions & 0 deletions gui/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod settings0;
pub use settings0::*;
mod settings1;
pub use settings1::*;
mod settings2;
pub use settings2::*;

mod gui;
pub use gui::*;
Expand Down
31 changes: 8 additions & 23 deletions gui/src/data/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::constants::{
ACCENT_COLOR,
SETTINGS_VERSION,
PIXELS_PER_POINT_DEFAULT,
AUTO_SAVE_INTERVAL_SECONDS,
};
use shukusai::{
constants::{
Expand Down Expand Up @@ -75,6 +76,9 @@ pub struct Settings {
/// instead of going to the previous?
pub previous_threshold: u32,

/// Auto-save the audio state to disk every `auto_save` seconds.
pub auto_save: u8,

/// Restore playback on re-open.
pub restore_state: bool,

Expand Down Expand Up @@ -107,6 +111,7 @@ impl Settings {
album_pixel_size: ALBUM_ART_SIZE_DEFAULT,
albums_per_row: ALBUMS_PER_ROW_DEFAULT,
previous_threshold: PREVIOUS_THRESHOLD_DEFAULT,
auto_save: AUTO_SAVE_INTERVAL_SECONDS,
restore_state: true,
empty_autoplay: true,
accent_color: ACCENT_COLOR,
Expand All @@ -131,30 +136,9 @@ mod test {
use disk::Bincode2;

// Empty.
const S1: Lazy<Settings> = Lazy::new(|| Settings::from_path("../assets/festival/gui/state/settings2_new.bin").unwrap());
const S1: Lazy<Settings> = Lazy::new(|| Settings::from_path("../assets/festival/gui/state/settings3_new.bin").unwrap());
// Filled.
const S2: Lazy<Settings> = Lazy::new(|| Settings::from_path("../assets/festival/gui/state/settings2_real.bin").unwrap());

// See `settings0.rs` on why this is commented out.
//
// #[test]
// // Compares `new()`.
// fn cmp() {
// #[cfg(not(target_os = "macos"))]
// assert_eq!(Lazy::force(&S1), &Settings::new());
// #[cfg(target_os = "macos")]
// {
// let mut settings = Settings::new();
// settings.pixels_per_point = 1.5;
// assert_eq!(Lazy::force(&S1), &settings);
// }
//
// assert_ne!(Lazy::force(&S1), Lazy::force(&S2));
//
// let b1 = S1.to_bytes().unwrap();
// let b2 = S2.to_bytes().unwrap();
// assert_ne!(b1, b2);
// }
const S2: Lazy<Settings> = Lazy::new(|| Settings::from_path("../assets/festival/gui/state/settings3_real.bin").unwrap());

#[test]
// Attempts to deserialize the non-empty.
Expand All @@ -169,6 +153,7 @@ mod test {
assert_eq!(S2.album_pixel_size, 227.0);
assert_eq!(S2.albums_per_row, 10);
assert_eq!(S2.previous_threshold, 10);
assert_eq!(S2.auto_save, 30);
assert_eq!(S2.restore_state, false);
assert_eq!(S2.empty_autoplay, false);
assert_eq!(S2.accent_color, egui::Color32::from_rgb(97,101,119));
Expand Down
2 changes: 2 additions & 0 deletions gui/src/data/settings0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::constants::{
ACCENT_COLOR,
SETTINGS_VERSION,
PIXELS_PER_POINT_DEFAULT,
AUTO_SAVE_INTERVAL_SECONDS,
};
use shukusai::{
constants::{
Expand Down Expand Up @@ -181,6 +182,7 @@ impl Into<Settings> for Settings0 {

// New fields.
pixels_per_point: PIXELS_PER_POINT_DEFAULT,
auto_save: AUTO_SAVE_INTERVAL_SECONDS,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions gui/src/data/settings1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::constants::{
ACCENT_COLOR,
SETTINGS_VERSION,
PIXELS_PER_POINT_DEFAULT,
AUTO_SAVE_INTERVAL_SECONDS,
};
use shukusai::{
constants::{
Expand Down Expand Up @@ -160,6 +161,9 @@ impl Into<Settings> for Settings1 {
accent_color,
collection_paths,
pixels_per_point,

// New fields.
auto_save: AUTO_SAVE_INTERVAL_SECONDS,
}
}
}
Expand Down
205 changes: 205 additions & 0 deletions gui/src/data/settings2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
//---------------------------------------------------------------------------------------------------- Use
//use anyhow::{bail,ensure,Error};
//use log::{info,error,warn,trace,debug};
use serde::{Serialize,Deserialize};
use bincode::{Encode,Decode};
use disk::Bincode2;
use std::path::PathBuf;
use crate::constants::{
GUI,
ALBUM_ART_SIZE_DEFAULT,
ALBUMS_PER_ROW_DEFAULT,
ACCENT_COLOR,
SETTINGS_VERSION,
PIXELS_PER_POINT_DEFAULT,
AUTO_SAVE_INTERVAL_SECONDS,
};
use shukusai::{
constants::{
HEADER,
FESTIVAL,
STATE_SUB_DIR,
},
sort::{
ArtistSort,
AlbumSort,
SongSort,
},
audio::PREVIOUS_THRESHOLD_DEFAULT,
search::SearchKind,
};
use crate::data::{
Settings,
AlbumSizing,
SearchSort,
ArtistSubTab,
WindowTitle,
};
use const_format::formatcp;
use std::marker::PhantomData;

//---------------------------------------------------------------------------------------------------- Settings
disk::bincode2!(Settings2, disk::Dir::Data, FESTIVAL, formatcp!("{GUI}/{STATE_SUB_DIR}"), "settings", HEADER, 2);
#[derive(Clone,Debug,PartialEq,Serialize,Deserialize,Encode,Decode)]
/// Version 2 of `GUI`'s settings.
pub struct Settings2 {
/// Collection sorting of artist view.
pub artist_sort: ArtistSort,

/// Collection sorting of album view.
pub album_sort: AlbumSort,

/// Collection sorting of album view.
pub song_sort: SongSort,

/// Which search kind to use for `Kernel`
pub search_kind: SearchKind,

/// To sort by `Song` title or
/// `Artist` name in the search tab?
pub search_sort: SearchSort,

/// Which way to set the window title when changing songs.
pub window_title: WindowTitle,

/// Does the user want a certain amount of
/// `Album`'s per row or a static pixel size?
pub album_sizing: AlbumSizing,
pub album_pixel_size: f32,
pub albums_per_row: u8,

/// How many seconds does a song need to play
/// before the `Previous` button resets the current
/// instead of going to the previous?
pub previous_threshold: u32,

/// Restore playback on re-open.
pub restore_state: bool,

/// Start playback if we added stuff to an empty queue.
pub empty_autoplay: bool,

#[bincode(with_serde)]
/// Our accent color.
pub accent_color: egui::Color32,

/// List of [`PathBuf`]'s to source music
/// data from when making a new [`Collection`].
pub collection_paths: Vec<PathBuf>,

/// What `egui::Context::pixels_per_point` are we set to?
/// Default is `1.0`, this allows the user to scale manually.
pub pixels_per_point: f32,
}

impl Settings2 {
pub fn new() -> Self {
Self {
artist_sort: Default::default(),
album_sort: Default::default(),
song_sort: Default::default(),
search_kind: Default::default(),
search_sort: Default::default(),
window_title: Default::default(),
album_sizing: Default::default(),
album_pixel_size: ALBUM_ART_SIZE_DEFAULT,
albums_per_row: ALBUMS_PER_ROW_DEFAULT,
previous_threshold: PREVIOUS_THRESHOLD_DEFAULT,
restore_state: true,
empty_autoplay: true,
accent_color: ACCENT_COLOR,
collection_paths: vec![],
pixels_per_point: PIXELS_PER_POINT_DEFAULT,
}
}

/// Reads from disk, then calls `.into()` if `Ok`.
pub fn disk_into() -> Result<Settings, anyhow::Error> {
// SAFETY: memmap is used.
unsafe { Self::from_file_memmap().map(Into::into) }
}
}

impl Into<Settings> for Settings2 {
fn into(self) -> Settings {
let Settings2 {
artist_sort,
album_sort,
song_sort,
search_kind,
search_sort,
window_title,
album_sizing,
album_pixel_size,
albums_per_row,
previous_threshold,
restore_state,
empty_autoplay,
accent_color,
collection_paths,
pixels_per_point,
..
} = self;

Settings {
artist_sort,
album_sort,
song_sort,
search_kind,
search_sort,
window_title,
album_sizing,
album_pixel_size,
albums_per_row,
previous_threshold,
restore_state,
empty_autoplay,
accent_color,
collection_paths,
pixels_per_point,

// New fields.
auto_save: AUTO_SAVE_INTERVAL_SECONDS,
}
}
}

impl Default for Settings2 {
fn default() -> Self {
Self::new()
}
}

//---------------------------------------------------------------------------------------------------- TESTS
#[cfg(test)]
mod test {
use super::*;
use once_cell::sync::Lazy;
use std::path::PathBuf;
use disk::Bincode2;

// Empty.
const S1: Lazy<Settings2> = Lazy::new(|| Settings2::from_path("../assets/festival/gui/state/settings2_new.bin").unwrap());
// Filled.
const S2: Lazy<Settings2> = Lazy::new(|| Settings2::from_path("../assets/festival/gui/state/settings2_real.bin").unwrap());

#[test]
// Attempts to deserialize the non-empty.
fn real() {
assert_eq!(S2.artist_sort, ArtistSort::RuntimeRev);
assert_eq!(S2.album_sort, AlbumSort::LexiRevArtistLexi);
assert_eq!(S2.song_sort, SongSort::Runtime);
assert_eq!(S2.search_kind, SearchKind::All);
assert_eq!(S2.search_sort, SearchSort::Album);
assert_eq!(S2.window_title, WindowTitle::Queue);
assert_eq!(S2.album_sizing, AlbumSizing::Row);
assert_eq!(S2.album_pixel_size, 227.0);
assert_eq!(S2.albums_per_row, 10);
assert_eq!(S2.previous_threshold, 10);
assert_eq!(S2.restore_state, false);
assert_eq!(S2.empty_autoplay, false);
assert_eq!(S2.accent_color, egui::Color32::from_rgb(97,101,119));
assert_eq!(S2.collection_paths, [PathBuf::from("/home/main/Music")]);
assert_eq!(S2.pixels_per_point.round(), 2.0);
}
}
3 changes: 3 additions & 0 deletions gui/src/func/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::data::{
State,
State0,
Settings,
Settings2,
Settings1,
Settings0,
DebugInfo,
Expand Down Expand Up @@ -171,6 +172,7 @@ impl crate::data::Gui {
// Read `Settings` from disk.
let settings = Settings::from_versions(&[
(SETTINGS_VERSION, Settings::from_file),
(2, Settings2::disk_into),
(1, Settings1::disk_into),
(0, Settings0::disk_into),
]);
Expand Down Expand Up @@ -254,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
4 changes: 4 additions & 0 deletions gui/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ pub const PREVIOUS_THRESHOLD: &str =
r#"If the current song runtime has passed this number, the [Previous] button will reset the current song instead of skipping backwards.
Setting this to [0] will make the [Previous] button always go to the previous song."#;
pub const AUTO_SAVE: &str =
r#"Auto-save all state, settings, and playlist changes every [x] seconds.
This setting is disabled if set to [0]."#;
pub const RESTORE_STATE: &str = "Restore playback state from the last session when opening Festival";
pub const WINDOW_TITLE: &str = "Set Festival's window title when changing songs";
pub const ACCENT_COLOR: &str = formatcp!(
Expand Down
Loading

0 comments on commit b3d7d55

Please sign in to comment.