egui icon indicating copy to clipboard operation
egui copied to clipboard

ScrollArea scroll state modified by sibling textedit

Open sphaerophoria opened this issue 2 years ago • 5 comments

Describe the bug If I add a text edit, then a scroll area, typing in the text edit moves the position of the scroll area. E.g. scroll_on_text_entry

To Reproduce Sample program...

use eframe::egui::{self, ScrollArea};

fn main() {
    let native_options = eframe::NativeOptions::default();
    eframe::run_native("My egui App", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))));
}

#[derive(Default)]
struct MyEguiApp {
    input: String,
}

impl MyEguiApp {
    fn new(cc: &eframe::CreationContext<'_>) -> Self {
        // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
        // Restore app state using cc.storage (requires the "persistence" feature).
        // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
        // for e.g. egui::PaintCallback.
        Self::default()
    }
}

impl eframe::App for MyEguiApp {
   fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
       egui::CentralPanel::default().show(ctx, |ui| {
           ui.text_edit_singleline(&mut self.input);
           ScrollArea::vertical()
               .show(ui, |ui| {
                   for i in 0..100 {
                       ui.label(format!("test{}", i));
                   }
               });
       });
   }
}

Expected behavior Scroll area stays fixed when typing in adjacent boxes

Desktop (please complete the following information):

  • OS: Linux, observed on both Ubuntu 20.04 and Arch Linux

Additional context

  • Typing when the text edit is not selected does not move the scroll area
  • I tried a few workarounds thinking that it might be related to internal state from the text edit leaking into the scroll area
    • Adding id sources to the text edit or the scroll area didn't seem to help
    • Wrapping the text edit within a sub-ui with ui.allocate_rect also doesn't resolve the issue

sphaerophoria avatar Apr 24 '22 22:04 sphaerophoria

I'm unsure if this is by design or a mistake.

It looks like the problem is that in the textedit we call ui.scroll_to_rect() when the text changes. This sets the frame state for the whole context which is read back in the scroll area

sphaerophoria avatar Apr 24 '22 23:04 sphaerophoria

This is a bug - it should only scroll the parent scroll area, but there is no logic in place to ensure this. Good catch!

emilk avatar Apr 25 '22 09:04 emilk

I have run into this bug. Is there a fix for it?

[edit] Or perhaps a workaround?

Barugon avatar Jun 28 '22 20:06 Barugon

Looking at this line, it seems to me that a TextEdit would not scroll into view if it was completely outside the clip area. Therefore, this code is really of no use (other than to cause havoc).

Barugon avatar Jun 30 '22 00:06 Barugon

Confirmed. Using the OP's code, I moved the TextEdit into the scroll area, gave it focus, scrolled it out of view, typed some stuff and it did not scroll into view.

[edit] The TextEdit will only scroll into view if it's partially visible. Is this intended behavior?

Barugon avatar Jun 30 '22 04:06 Barugon