lucasmerlin / hello_egui

A collection of useful crates for egui
https://lucasmerlin.github.io/hello_egui/
MIT License
272 stars 24 forks source link

egui_dnd: Significant animation jitter #13

Closed trumank closed 1 year ago

trumank commented 1 year ago

376242b3cc6bed14a93880edb5d5aa46f2fc3b9a seems to have introduced some rather significant jitter, espectially when dragging near the top of a scroll area.

https://github.com/lucasmerlin/hello_egui/assets/1144160/efb5001a-9ad7-4cad-8730-bfbd009141bc

Here's the example script using cargo-script-mvs:

#!/usr/bin/env cargo-eval

//! ```cargo
//! [dependencies]
//! eframe = "0.22.0"
//! #egui_dnd = { git = "https://github.com/lucasmerlin/egui_dnd.git", rev = "e9043021e101fb42fc6ce70e508da857cb7ee263" } # good
//! egui_dnd = { git = "https://github.com/lucasmerlin/egui_dnd.git", rev = "376242b3cc6bed14a93880edb5d5aa46f2fc3b9a" } # bad
//!
//! [profile.dev]
//! opt-level = 3
//! ```

use eframe::egui::{Button, CentralPanel, Frame, ScrollArea};
use eframe::NativeOptions;
use std::hash::{Hash, Hasher};

struct ItemType {
    number: u32,
}

impl Hash for ItemType {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.number.hash(state);
    }
}

fn main() -> eframe::Result<()> {
    let mut items: Vec<_> = (0..100).map(|number| ItemType { number }).collect();

    eframe::run_simple_native(
        "dnd scroll demo",
        NativeOptions::default(),
        move |ctx, _| {
            CentralPanel::default().show(ctx, |ui| {
                ui.label("some stuff above");
                ui.label("some stuff above");
                ui.label("some stuff above");
                ui.label("some stuff above");
                ui.with_layout(ui.layout().with_cross_justify(true), |ui| {
                    ScrollArea::vertical().show(ui, |ui| {
                        let res = egui_dnd::dnd(ui, ui.id()).show(
                            items.iter_mut().enumerate(),
                            |ui, (_index, item), handle, state| {
                                let mut frame = Frame::none();
                                if state.dragged {
                                    frame.fill = ui.visuals().extreme_bg_color
                                } else if state.index % 2 == 1 {
                                    frame.fill = ui.visuals().faint_bg_color
                                }
                                frame.show(ui, |ui| {
                                    ui.horizontal(|ui| {
                                        handle.ui(ui, |ui| {
                                            ui.label(" ☰ ");
                                        });

                                        for i in 0..(item.number % 3) + 10 {
                                            ui.add_enabled(
                                                false,
                                                Button::new(format!("button {i}")),
                                            );
                                        }
                                    });
                                });
                            },
                        );

                        if res.final_update().is_some() {
                            res.update_vec(&mut items);
                        }
                    })
                })
            });
        },
    )
}
lucasmerlin commented 1 year ago

Seems like this was caused by the easing function, not sure why. I've reverted back to linear easing and it seems fine now. Can you try the latest commit to see if it fixes it for you too?

trumank commented 1 year ago

Yep, latest commit seems to have fixed it.