Skip to content

ScrollAreas can overrun their container's margins #3385

@valadaptive

Description

@valadaptive

Describe the bug

When displaying a ScrollArea inside a container, and it only scrolls along one axis, it will be a few pixels too big and will overflow into that container's margins along that axis.

This is easiest to see when we set the container's margins to 0. Here's an example with a ScrollArea containing the grey rectangle in the central panel, surrounded by 4 panels:

Peek 2023-09-25 01-09

I haven't delved into the code much, but it seems like the scroll area overruns the container margin by clip_rect_margin pixels. Setting clip_rect_margin to 0.0 before rendering the scroll area fixes the issue.

To Reproduce
Steps to reproduce the behavior:

See the following demo code, which renders the UI shown above:

Demo code ```rust use eframe::egui;

fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(480.0, 360.0)),
..Default::default()
};
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| Box::::default()),
)
}

#[derive(Default)]
struct MyApp {}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
let no_margins = egui::Frame::side_top_panel(&ctx.style()).inner_margin(0.0);
egui::TopBottomPanel::top("top").show(ctx, |
| {});
egui::TopBottomPanel::bottom("bottom").show(ctx, || {});
egui::SidePanel::left("left").show(ctx, |
| {});
egui::SidePanel::right("right").show(ctx, |_| {});
egui::CentralPanel::default()
.frame(no_margins)
.show(ctx, |ui| {
egui::ScrollArea::new([true; 2]).show(ui, |ui| {
ui.add(
egui::Image::from_texture((
egui::TextureId::default(),
egui::vec2(360.0, 240.0),
))
.uv(egui::Rect::from_min_max(
egui::pos2(0.0, 0.0),
egui::pos2(0.0, 0.0),
))
.tint(egui::Color32::GRAY),
);
});
});
}
}

</details>

**Expected behavior**
The ScrollArea should properly resize to fit within its container.

**Screenshots**
See GIF above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is broken

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions