Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random sorting randomly gets stuck #98

Open
MauroGuida opened this issue Oct 24, 2024 · 0 comments
Open

Random sorting randomly gets stuck #98

MauroGuida opened this issue Oct 24, 2024 · 0 comments

Comments

@MauroGuida
Copy link

MauroGuida commented Oct 24, 2024

Hi, I've found an issue with random sorting (both grouped and ungrouped). The function update_current_image in daemon/image_picker.rs does not correctly push new random wallpapers to the queue in some scenarios (e.g., when wallpapers from the folder are not picked sequentially, such as 0,1,2,...).

In the following video, this behavior can be observed. The bitrate is awful, and my terminal isn't transparent enough for a clear view of the background, but you can still see the wallpaper change at the top.

In this video wallpapers are randomly picked in order (0: FoggyRoad, 1: Gray, 2: HardDisk) and correctly added to the queue:

https://drive.google.com/file/d/1-HqrxHUSlugL-ywoyAQ4D5UZE-fq_p_9/view?usp=drive_link

Here everything works as expected: 3 wallpapers are randomly picked one after the other and correctly added to the queue. Subsequent calls correctly assign a new random wallpaper, and wpapperd never gets stuck.

In the following video, I wasn't as "lucky". The first wallpaper picked is the one with index 1 (Gray), which is correctly added to the queue. The next randomly chosen wallpaper is the one with index 0 (FoggyRoad), which is correctly displayed but not added to the queue. On the next request, wpapperd gets stuck.

https://drive.google.com/file/d/1vDRT-aOMHPdkDEGFLCBzevZw-D0xum18/view?usp=drive_link

As you can see in the terminal, the transitions from the old wallpaper to the new wallpaper and the current queue elements are displayed. These are printed by update_current_image function in daemon/image_picker.rs. The code is as follows:

    pub fn update_current_image(&mut self, img_path: PathBuf, index: usize) {
        match (self.action.take(), &mut self.sorting) {
            (Some(ImagePickerAction::Next), ImagePickerSorting::Random(queue)) => {
                if queue.has_reached_end() || queue.buffer.get(index).is_none() {
                    queue.push(img_path.clone());
                }
            }
            (None | Some(ImagePickerAction::Previous), ImagePickerSorting::Random { .. }) => {}
            (
                None | Some(ImagePickerAction::Previous),
                ImagePickerSorting::GroupedRandom(group),
            ) => {
                let mut group = group.group.borrow_mut();
                group.loading_image = None;
                group.current_image.clone_from(&img_path);
                group.index = index;
            }
            (
                _,
                ImagePickerSorting::Ascending(current_index)
                | ImagePickerSorting::Descending(current_index),
            ) => *current_index = index,
            (Some(ImagePickerAction::Next), ImagePickerSorting::GroupedRandom(group)) => {
                let mut group = group.group.borrow_mut();
                let queue = &mut group.queue;
                println!("Current: {:?} -- New: {:?}", self.current_img, img_path);
                println!();
                if queue.has_reached_end() || queue.buffer.get(index).is_none() {
                    queue.push(img_path.clone());
                }
                println!("C Index: {} -- Q: {:?}", index, queue.buffer);
                println!();
                println!();
                println!();
                group.loading_image = None;
                group.current_image.clone_from(&img_path);
                group.index = index;
            }
        }

        self.current_img = img_path;
    }

As far as I understand, this issue is caused by this if statement, which is responsible for adding images to the queue and is present for both GroupedRandom and Random sorting:

if queue.has_reached_end() || queue.buffer.get(index).is_none() {
    queue.push(img_path.clone());
}

Without this if statement, this issue does not occur, and random sorting works as expected. The queue does not contain duplicates, thanks to a duplicate prevention check in the push function. However, I don't clearly understand why those checks are there and what their purpose is; maybe I'm missing something, and their removal could cause unintended behaviors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant