Skip to content

Commit 377f86e

Browse files
authored
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)
1 parent b35a7dd commit 377f86e

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

crates/egui/src/containers/window.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,15 @@ impl<'open> Window<'open> {
416416
let on_top = Some(area_layer_id) == ctx.top_layer_id();
417417
let mut area = area.begin(ctx);
418418

419-
let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y;
420-
421419
// Calculate roughly how much larger the window size is compared to the inner rect
422-
let title_bar_height = if with_title_bar {
420+
let (title_bar_height, title_content_spacing) = if with_title_bar {
423421
let style = ctx.style();
424-
ctx.fonts(|f| title.font_height(f, &style)) + title_content_spacing * 2.0
422+
let window_margin = style.spacing.window_margin;
423+
let spacing = window_margin.top + window_margin.bottom;
424+
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
425+
(height, spacing)
425426
} else {
426-
0.0
427+
(0.0, 0.0)
427428
};
428429

429430
// First interact (move etc) to avoid frame delay:
@@ -466,6 +467,11 @@ impl<'open> Window<'open> {
466467

467468
let where_to_put_header_background = &area_content_ui.painter().add(Shape::Noop);
468469

470+
// Backup item spacing before the title bar
471+
let item_spacing = frame.content_ui.spacing().item_spacing;
472+
// Use title bar spacing as the item spacing before the content
473+
frame.content_ui.spacing_mut().item_spacing.y = title_content_spacing;
474+
469475
let title_bar = if with_title_bar {
470476
let title_bar = show_title_bar(
471477
&mut frame.content_ui,
@@ -480,13 +486,15 @@ impl<'open> Window<'open> {
480486
None
481487
};
482488

483-
let (content_inner, content_response) = collapsing
489+
// Remove item spacing after the title bar
490+
frame.content_ui.spacing_mut().item_spacing.y = 0.0;
491+
492+
let (content_inner, mut content_response) = collapsing
484493
.show_body_unindented(&mut frame.content_ui, |ui| {
485-
resize.show(ui, |ui| {
486-
if title_bar.is_some() {
487-
ui.add_space(title_content_spacing);
488-
}
494+
// Restore item spacing for the content
495+
ui.spacing_mut().item_spacing.y = item_spacing.y;
489496

497+
resize.show(ui, |ui| {
490498
if scroll.is_any_scroll_enabled() {
491499
scroll.show(ui, add_contents).inner
492500
} else {
@@ -523,6 +531,11 @@ impl<'open> Window<'open> {
523531
);
524532
};
525533

534+
// Fix title bar separator line position
535+
if let Some(response) = &mut content_response {
536+
response.rect.min.y = outer_rect.min.y + title_bar_height;
537+
}
538+
526539
title_bar.ui(
527540
&mut area_content_ui,
528541
outer_rect,
@@ -1026,7 +1039,7 @@ impl TitleBar {
10261039

10271040
if let Some(content_response) = &content_response {
10281041
// paint separator between title and content:
1029-
let y = content_response.rect.top() + ui.spacing().item_spacing.y * 0.5;
1042+
let y = content_response.rect.top();
10301043
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
10311044
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
10321045
ui.painter().hline(outer_rect.x_range(), y, stroke);

0 commit comments

Comments
 (0)