Skip to content

Commit 8dd97b6

Browse files
authored
fix: enable recursive scanning for custom wallpaper directories
1 parent b1ca4c1 commit 8dd97b6

3 files changed

Lines changed: 84 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ features = ["calloop"]
3434

3535
[profile.release]
3636
opt-level = 3
37+
38+
[dev-dependencies]
39+
tempfile = "3"

src/wallpaper.rs

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,15 @@ impl Wallpaper {
236236
{
237237
image_queue.push_front(img_path.path().into());
238238
}
239-
} else if let Ok(dir) = source.read_dir() {
240-
for entry in dir.filter_map(Result::ok) {
241-
let Ok(path) = entry.path().canonicalize() else {
242-
continue;
243-
};
244-
245-
if path.is_file() {
246-
image_queue.push_front(path);
247-
}
239+
} else {
240+
// Recursively find images in custom directory
241+
for img_path in WalkDir::new(source)
242+
.follow_links(true)
243+
.into_iter()
244+
.filter_map(Result::ok)
245+
.filter(|p| p.path().is_file())
246+
{
247+
image_queue.push_front(img_path.path().into());
248248
}
249249
}
250250
} else if source.is_file() {
@@ -386,3 +386,74 @@ fn current_image(output: &str) -> Option<Source> {
386386

387387
wallpaper.map(|(_name, path)| path)
388388
}
389+
390+
#[cfg(test)]
391+
mod tests {
392+
use super::*;
393+
use std::fs::File;
394+
use std::path::PathBuf;
395+
use tempfile::tempdir;
396+
397+
#[test]
398+
fn test_custom_dir_loading() {
399+
// Create a temp directory structure
400+
// root/
401+
// img1.png
402+
// subdir/
403+
// img2.png
404+
405+
let dir = tempdir().unwrap();
406+
let root = dir.path();
407+
let subdir = root.join("subdir");
408+
fs::create_dir(&subdir).unwrap();
409+
410+
File::create(root.join("img1.png")).unwrap();
411+
File::create(subdir.join("img2.png")).unwrap();
412+
413+
// Create a Wallpaper instance with Source pointing to root
414+
// We need to mock dependencies or use minimal construction if possible.
415+
// Wallpaper::new requires QueueHandle and LoopHandle which are hard to mock here.
416+
// Instead, we can verify the logic by extracting the loading logic or just replicating it here to confirm behavior.
417+
418+
// Let's replicate the logic from load_images for custom directories check
419+
let source = root.to_path_buf();
420+
let mut image_queue = VecDeque::new();
421+
422+
// Assume XDG_DATA_DIRS does NOT contain this temp dir (which is true)
423+
let xdg_data_dirs: Vec<String> = Vec::new();
424+
425+
if let Ok(source) = source.canonicalize() {
426+
if source.is_dir() {
427+
if xdg_data_dirs
428+
.iter()
429+
.any(|xdg_data_dir| source.starts_with(xdg_data_dir))
430+
{
431+
// This block should NOT be hit
432+
panic!("Test setup error: temp dir shouldn't be in XDG_DATA_DIRS");
433+
} else {
434+
for img_path in WalkDir::new(source)
435+
.follow_links(true)
436+
.into_iter()
437+
.filter_map(Result::ok)
438+
.filter(|p| p.path().is_file())
439+
{
440+
image_queue.push_front(img_path.path().into());
441+
}
442+
}
443+
}
444+
}
445+
446+
// With WalkDir, we expect to find 2 images (recursive)
447+
assert_eq!(image_queue.len(), 2, "Should find 2 images recursively");
448+
assert!(
449+
image_queue
450+
.iter()
451+
.any(|p: &PathBuf| p.ends_with("img1.png"))
452+
);
453+
assert!(
454+
image_queue
455+
.iter()
456+
.any(|p: &PathBuf| p.ends_with("img2.png"))
457+
);
458+
}
459+
}

0 commit comments

Comments
 (0)