egui: Fixed window title bar incorrect handling spacing (#3995)

Currently, the window title bar does not correctly handle the
`item_spacing` and `window_margin`.

* Video snapshot on changing the `item_spacing` and `window_margin`
(master)


[egui-demo-app-incorrect.webm](https://github.com/emilk/egui/assets/1274171/a4e32d43-69b5-44be-a4c3-97b0533147ca)

* Video snapshot on changing the `item_spacing` and `window_margin`
(feature/fix-window-title-bar)


[egui-demo-app-fixed.webm](https://github.com/emilk/egui/assets/1274171/42bbc210-a1f3-4e0a-ab71-d068e58e0e35)
This commit is contained in:
Varphone Wong 2024-02-07 15:52:46 +08:00 committed by GitHub
parent b35a7dd7de
commit 377f86efb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 12 deletions

View File

@ -416,14 +416,15 @@ impl<'open> Window<'open> {
let on_top = Some(area_layer_id) == ctx.top_layer_id();
let mut area = area.begin(ctx);
let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y;
// Calculate roughly how much larger the window size is compared to the inner rect
let title_bar_height = if with_title_bar {
let (title_bar_height, title_content_spacing) = if with_title_bar {
let style = ctx.style();
ctx.fonts(|f| title.font_height(f, &style)) + title_content_spacing * 2.0
let window_margin = style.spacing.window_margin;
let spacing = window_margin.top + window_margin.bottom;
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
(height, spacing)
} else {
0.0
(0.0, 0.0)
};
// First interact (move etc) to avoid frame delay:
@ -466,6 +467,11 @@ impl<'open> Window<'open> {
let where_to_put_header_background = &area_content_ui.painter().add(Shape::Noop);
// Backup item spacing before the title bar
let item_spacing = frame.content_ui.spacing().item_spacing;
// Use title bar spacing as the item spacing before the content
frame.content_ui.spacing_mut().item_spacing.y = title_content_spacing;
let title_bar = if with_title_bar {
let title_bar = show_title_bar(
&mut frame.content_ui,
@ -480,13 +486,15 @@ impl<'open> Window<'open> {
None
};
let (content_inner, content_response) = collapsing
.show_body_unindented(&mut frame.content_ui, |ui| {
resize.show(ui, |ui| {
if title_bar.is_some() {
ui.add_space(title_content_spacing);
}
// Remove item spacing after the title bar
frame.content_ui.spacing_mut().item_spacing.y = 0.0;
let (content_inner, mut content_response) = collapsing
.show_body_unindented(&mut frame.content_ui, |ui| {
// Restore item spacing for the content
ui.spacing_mut().item_spacing.y = item_spacing.y;
resize.show(ui, |ui| {
if scroll.is_any_scroll_enabled() {
scroll.show(ui, add_contents).inner
} else {
@ -523,6 +531,11 @@ impl<'open> Window<'open> {
);
};
// Fix title bar separator line position
if let Some(response) = &mut content_response {
response.rect.min.y = outer_rect.min.y + title_bar_height;
}
title_bar.ui(
&mut area_content_ui,
outer_rect,
@ -1026,7 +1039,7 @@ impl TitleBar {
if let Some(content_response) = &content_response {
// paint separator between title and content:
let y = content_response.rect.top() + ui.spacing().item_spacing.y * 0.5;
let y = content_response.rect.top();
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
ui.painter().hline(outer_rect.x_range(), y, stroke);