Provide settings to customize photo path and display duration

This commit is contained in:
Jean-Loup Beaussart 2024-12-10 23:40:06 +01:00
parent cc0038c1cd
commit f1fbba405b
No known key found for this signature in database
GPG Key ID: 582B3C35EAFE46F8
5 changed files with 34 additions and 7 deletions

View File

@ -43,6 +43,7 @@ lto = "thin"
[dependencies]
anyhow = "1.0.93"
config = "0.14.1"
image = "0.25.4"
macroquad = { version = "0.4.13" }
walkdir = "2.5.0"

2
settings.yml Normal file
View File

@ -0,0 +1,2 @@
photo_path: "/mnt/c/Users/beaus/Downloads"
duration: 30

View File

@ -1,18 +1,18 @@
use crate::dimensions::{calculate_resize_dimensions, Dimensions};
use image::ImageReader;
use macroquad::texture::Texture2D;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
pub struct ImageProvider {
path: PathBuf,
pub struct ImageProvider<'a> {
path: &'a Path,
directory_it: Box<dyn Iterator<Item = Result<walkdir::DirEntry, walkdir::Error>>>,
texture: Option<Texture2D>,
screen_dimension: Dimensions,
}
impl ImageProvider {
pub fn new(screen_dimension: Dimensions, path: PathBuf) -> Self {
impl<'a> ImageProvider<'a> {
pub fn new(screen_dimension: Dimensions, path: &'a Path) -> Self {
Self {
directory_it: Box::new(WalkDir::new(&path).max_open(8).into_iter()),
texture: None,

View File

@ -8,6 +8,7 @@ use std::{
mod dimensions;
mod image_provider;
mod settings;
fn window_conf() -> Conf {
Conf {
@ -32,9 +33,10 @@ fn display_image_centered(texture: &Texture2D) {
#[macroquad::main(window_conf)]
async fn main() -> anyhow::Result<()> {
let settings = settings::Settings::new("settings.yml".into())?;
show_mouse(false);
let screen_dim = Dimensions::new(screen_width() as u32, screen_height() as u32);
let mut provider = ImageProvider::new(screen_dim, "/mnt/c/Users/beaus/Downloads".into());
let mut provider = ImageProvider::new(screen_dim, &settings.photo_path);
provider.load_next_image()?;
let mut time = Instant::now();
@ -45,7 +47,7 @@ async fn main() -> anyhow::Result<()> {
display_frame(&provider)?;
if time.elapsed().as_secs() > 5 {
if time.elapsed() > settings.duration {
provider.load_next_image()?;
time = Instant::now();
}

22
src/settings.rs Normal file
View File

@ -0,0 +1,22 @@
use config::{Config, File};
use std::{path::PathBuf, time::Duration};
pub struct Settings {
pub photo_path: PathBuf,
pub duration: Duration,
}
impl Settings {
pub fn new(settings_file: PathBuf) -> anyhow::Result<Self> {
let settings = Config::builder()
.add_source(File::from(settings_file))
.build()?;
let photo_path = settings.get_string("photo_path")?.try_into()?;
let duration = settings.get_int("duration")?;
Ok(Self {
photo_path,
duration: Duration::from_secs(duration as u64),
})
}
}