1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 07:03:14 -04:00

Allow setting default/override text align

This commit is contained in:
Emil Ernerfeldt
2024-09-19 10:04:44 +02:00
parent 59e1b833e4
commit 3c705c010e
2 changed files with 34 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
use std::{collections::BTreeMap, ops::RangeInclusive, sync::Arc};
use emath::Align;
use epaint::{text::FontTweak, Rounding, Shadow, Stroke};
use crate::{
@@ -196,6 +197,11 @@ pub struct Style {
/// which will take precedence over this.
pub override_font_id: Option<FontId>,
/// How to vertically align text.
///
/// Default depends on layout.
pub override_text_valign: Option<Align>,
/// The [`FontFamily`] and size you want to use for a specific [`TextStyle`].
///
/// The most convenient way to look something up in this is to use [`TextStyle::resolve`].
@@ -1201,6 +1207,7 @@ impl Default for Style {
Self {
override_font_id: None,
override_text_style: None,
override_text_valign: None,
text_styles: default_text_styles(),
drag_value_text_style: TextStyle::Button,
number_formatter: NumberFormatter(Arc::new(emath::format_with_decimals_in_range)),
@@ -1501,6 +1508,7 @@ impl Style {
let Self {
override_font_id,
override_text_style,
override_text_valign,
text_styles,
drag_value_text_style,
number_formatter: _, // can't change callbacks in the UI
@@ -1534,7 +1542,7 @@ impl Style {
ui.end_row();
ui.label("Override text style");
crate::ComboBox::from_id_salt("Override text style")
crate::ComboBox::from_id_salt("override_text_style")
.selected_text(match override_text_style {
None => "None".to_owned(),
Some(override_text_style) => override_text_style.to_string(),
@@ -1550,6 +1558,28 @@ impl Style {
});
ui.end_row();
fn valign_name(valign: Align) -> &'static str {
match valign {
Align::TOP => "Top",
Align::Center => "Center",
Align::BOTTOM => "Bottom",
}
}
ui.label("Override text valign");
crate::ComboBox::from_id_salt("override_text_valign")
.selected_text(match override_text_valign {
None => "None",
Some(override_text_valign) => valign_name(*override_text_valign),
})
.show_ui(ui, |ui| {
ui.selectable_value(override_text_valign, None, "None");
for align in [Align::TOP, Align::Center, Align::BOTTOM] {
ui.selectable_value(override_text_valign, Some(align), valign_name(align));
}
});
ui.end_row();
ui.label("Text style of DragValue");
crate::ComboBox::from_id_salt("drag_value_text_style")
.selected_text(drag_value_text_style.to_string())

View File

@@ -620,8 +620,9 @@ impl Ui {
/// How to vertically align text
#[inline]
pub fn text_valign(&self) -> Align {
#![allow(clippy::unused_self)]
Align::BOTTOM
self.style()
.override_text_valign
.unwrap_or_else(|| self.layout().vertical_align())
}
/// Create a painter for a sub-region of this Ui.