From 998e07d8654a02ced822a600079643c17c90a596 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 12 Jun 2021 14:30:42 +0200 Subject: [PATCH] Make sure the scroll bar is never outside the screen rectangle (#475) * Make sure the scroll bar is never outside the screen rectangle This is an alternative attempt to fix the bug mentioned in https://github.com/emilk/egui/pull/392 egui expects that the container can always be made wider, which is true for all egui Ui:s, but not true for the outer frame/chrome that egui ultimately needs to sit within. * Clamp scroll to screen rect rather than available rect * Fix scrollbar spacing when shrinking too small * Update changelog --- CHANGELOG.md | 3 +++ egui/src/containers/scroll_area.rs | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e35ef26d..27ccf4c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [ * `TopPanel::top` is now `TopBottomPanel::top`. * `SidePanel::left` no longet takes the default width by argument, but by a builder call. +### Fixed 🐛 +* Fix invisible scroll bar when native window is too narrow for egui. + ## 0.12.0 - 2021-05-10 - Multitouch, user memory, window pivots, and improved plots diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index 4cdc2db2..fc53cd16 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -267,14 +267,27 @@ impl Prepared { state.offset.y = offset_y + spacing; } - let width = if inner_rect.width().is_finite() { - inner_rect.width().max(content_size.x) // Expand width to fit content - } else { - // ScrollArea is in an infinitely wide parent - content_size.x - }; + let inner_rect = { + let width = if inner_rect.width().is_finite() { + inner_rect.width().max(content_size.x) // Expand width to fit content + } else { + // ScrollArea is in an infinitely wide parent + content_size.x + }; - let inner_rect = Rect::from_min_size(inner_rect.min, vec2(width, inner_rect.height())); + let mut inner_rect = + Rect::from_min_size(inner_rect.min, vec2(width, inner_rect.height())); + + // The window that egui sits in can't be expanded by egui, so we need to respect it: + let max_x = ui.input().screen_rect().right() + - current_scroll_bar_width + - ui.spacing().item_spacing.x; + inner_rect.max.x = inner_rect.max.x.at_most(max_x); + // TODO: when we support it, we should maybe auto-enable + // horizontal scrolling if this limit is reached + + inner_rect + }; let outer_rect = Rect::from_min_size( inner_rect.min,