Skip to content

Commit 8dc2ba2

Browse files
committed
Refactor: add TitleBar::new in window.rs
1 parent dfbc7f0 commit 8dc2ba2

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

crates/egui/src/containers/window.rs

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'open> Window<'open> {
528528
frame.content_ui.spacing_mut().item_spacing.y = title_content_spacing;
529529

530530
let title_bar = if with_title_bar {
531-
let title_bar = show_title_bar(
531+
let title_bar = TitleBar::new(
532532
&mut frame.content_ui,
533533
title,
534534
show_close_button,
@@ -1040,60 +1040,60 @@ struct TitleBar {
10401040
rect: Rect,
10411041
}
10421042

1043-
fn show_title_bar(
1044-
ui: &mut Ui,
1045-
title: WidgetText,
1046-
show_close_button: bool,
1047-
collapsing: &mut CollapsingState,
1048-
collapsible: bool,
1049-
) -> TitleBar {
1050-
let inner_response = ui.horizontal(|ui| {
1051-
let height = ui
1052-
.fonts(|fonts| title.font_height(fonts, ui.style()))
1053-
.max(ui.spacing().interact_size.y);
1054-
ui.set_min_height(height);
1055-
1056-
let item_spacing = ui.spacing().item_spacing;
1057-
let button_size = Vec2::splat(ui.spacing().icon_width);
1043+
impl TitleBar {
1044+
fn new(
1045+
ui: &mut Ui,
1046+
title: WidgetText,
1047+
show_close_button: bool,
1048+
collapsing: &mut CollapsingState,
1049+
collapsible: bool,
1050+
) -> Self {
1051+
let inner_response = ui.horizontal(|ui| {
1052+
let height = ui
1053+
.fonts(|fonts| title.font_height(fonts, ui.style()))
1054+
.max(ui.spacing().interact_size.y);
1055+
ui.set_min_height(height);
10581056

1059-
let pad = (height - button_size.y) / 2.0; // calculated so that the icon is on the diagonal (if window padding is symmetrical)
1057+
let item_spacing = ui.spacing().item_spacing;
1058+
let button_size = Vec2::splat(ui.spacing().icon_width);
10601059

1061-
if collapsible {
1062-
ui.add_space(pad);
1063-
collapsing.show_default_button_with_size(ui, button_size);
1064-
}
1060+
let pad = (height - button_size.y) / 2.0; // calculated so that the icon is on the diagonal (if window padding is symmetrical)
10651061

1066-
let title_galley = title.into_galley(
1067-
ui,
1068-
Some(crate::TextWrapMode::Extend),
1069-
f32::INFINITY,
1070-
TextStyle::Heading,
1071-
);
1062+
if collapsible {
1063+
ui.add_space(pad);
1064+
collapsing.show_default_button_with_size(ui, button_size);
1065+
}
10721066

1073-
let minimum_width = if collapsible || show_close_button {
1074-
// If at least one button is shown we make room for both buttons (since title is centered):
1075-
2.0 * (pad + button_size.x + item_spacing.x) + title_galley.size().x
1076-
} else {
1077-
pad + title_galley.size().x + pad
1078-
};
1079-
let min_rect = Rect::from_min_size(ui.min_rect().min, vec2(minimum_width, height));
1080-
let id = ui.advance_cursor_after_rect(min_rect);
1067+
let title_galley = title.into_galley(
1068+
ui,
1069+
Some(crate::TextWrapMode::Extend),
1070+
f32::INFINITY,
1071+
TextStyle::Heading,
1072+
);
10811073

1082-
TitleBar {
1083-
id,
1084-
title_galley,
1085-
min_rect,
1086-
rect: Rect::NAN, // Will be filled in later
1087-
}
1088-
});
1074+
let minimum_width = if collapsible || show_close_button {
1075+
// If at least one button is shown we make room for both buttons (since title is centered):
1076+
2.0 * (pad + button_size.x + item_spacing.x) + title_galley.size().x
1077+
} else {
1078+
pad + title_galley.size().x + pad
1079+
};
1080+
let min_rect = Rect::from_min_size(ui.min_rect().min, vec2(minimum_width, height));
1081+
let id = ui.advance_cursor_after_rect(min_rect);
1082+
1083+
Self {
1084+
id,
1085+
title_galley,
1086+
min_rect,
1087+
rect: Rect::NAN, // Will be filled in later
1088+
}
1089+
});
10891090

1090-
let title_bar = inner_response.inner;
1091-
let rect = inner_response.response.rect;
1091+
let title_bar = inner_response.inner;
1092+
let rect = inner_response.response.rect;
10921093

1093-
TitleBar { rect, ..title_bar }
1094-
}
1094+
Self { rect, ..title_bar }
1095+
}
10951096

1096-
impl TitleBar {
10971097
/// Finishes painting of the title bar when the window content size already known.
10981098
///
10991099
/// # Parameters

0 commit comments

Comments
 (0)