Skip to content

Commit

Permalink
Fixed window title bar incorrect handling spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
varphone committed Feb 7, 2024
1 parent b35a7dd commit 9f8357e
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -480,13 +486,15 @@ impl<'open> Window<'open> {
None
};

let (content_inner, content_response) = collapsing
// 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| {
resize.show(ui, |ui| {
if title_bar.is_some() {
ui.add_space(title_content_spacing);
}
// 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 {
Expand Down Expand Up @@ -523,6 +531,11 @@ impl<'open> Window<'open> {
);
};

// Fix title bar seperator line position

Check warning on line 534 in crates/egui/src/containers/window.rs

View workflow job for this annotation

GitHub Actions / typos

"seperator" should be "separator".
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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9f8357e

Please sign in to comment.