Skip to content

Commit

Permalink
Round font row heights
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 30, 2024
1 parent b7ea43f commit 6a33dab
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl<'open> Window<'open> {
let (title_bar_height, title_content_spacing) = if with_title_bar {
let style = ctx.style();
let spacing = window_margin.top + window_margin.bottom;
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
let height = ctx.fonts(|f| title.font_height(f, &style)).round() + spacing;
window_frame.rounding.ne = window_frame.rounding.ne.clamp(0.0, height / 2.0);
window_frame.rounding.nw = window_frame.rounding.nw.clamp(0.0, height / 2.0);
(height, spacing)
Expand Down
1 change: 1 addition & 0 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ impl Ui {
/// The height of text of this text style
pub fn text_style_height(&self, style: &TextStyle) -> f32 {
self.fonts(|f| f.row_height(&style.resolve(self.style())))
.round()
}

/// Screen-space rectangle for clipping what we paint in this ui.
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl RichText {
if let Some(family) = &self.family {
font_id.family = family.clone();
}
fonts.row_height(&font_id)
fonts.row_height(&font_id).round()
}

/// Append to an existing [`LayoutJob`]
Expand Down
4 changes: 3 additions & 1 deletion crates/egui/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ impl Widget for Button<'_> {
}

let space_available_for_image = if let Some(text) = &text {
let font_height = ui.fonts(|fonts| text.font_height(fonts, ui.style()));
let font_height = ui
.fonts(|fonts| text.font_height(fonts, ui.style()))
.round();
Vec2::splat(font_height) // Reasonable?
} else {
ui.available_size() - 2.0 * button_padding
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl<'t> TextEdit<'t> {
let prev_text = text.as_str().to_owned();

let font_id = font_selection.resolve(ui.style());
let row_height = ui.fonts(|f| f.row_height(&font_id));
let row_height = ui.fonts(|f| f.row_height(&font_id)).round();
const MIN_WIDTH: f32 = 24.0; // Never make a [`TextEdit`] more narrow than this.
let available_width = (ui.available_width() - margin.sum().x).at_least(MIN_WIDTH);
let desired_width = desired_width.unwrap_or_else(|| ui.spacing().text_edit_width);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/scrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn huge_content_painter(ui: &mut egui::Ui) {
ui.add_space(4.0);

let font_id = TextStyle::Body.resolve(ui.style());
let row_height = ui.fonts(|f| f.row_height(&font_id)) + ui.spacing().item_spacing.y;
let row_height = ui.fonts(|f| f.row_height(&font_id)).round() + ui.spacing().item_spacing.y;
let num_rows = 10_000;

ScrollArea::vertical()
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/easy_mark/easy_mark_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn bullet_point(ui: &mut Ui, width: f32) -> Response {

fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response {
let font_id = TextStyle::Body.resolve(ui.style());
let row_height = ui.fonts(|f| f.row_height(&font_id));
let row_height = ui.fonts(|f| f.row_height(&font_id)).round();
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
let text = format!("{number}.");
let text_color = ui.visuals().strong_text_color();
Expand Down

0 comments on commit 6a33dab

Please sign in to comment.