mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
Allow setting default/override text align
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use std::{collections::BTreeMap, ops::RangeInclusive, sync::Arc};
|
use std::{collections::BTreeMap, ops::RangeInclusive, sync::Arc};
|
||||||
|
|
||||||
|
use emath::Align;
|
||||||
use epaint::{text::FontTweak, Rounding, Shadow, Stroke};
|
use epaint::{text::FontTweak, Rounding, Shadow, Stroke};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -196,6 +197,11 @@ pub struct Style {
|
|||||||
/// which will take precedence over this.
|
/// which will take precedence over this.
|
||||||
pub override_font_id: Option<FontId>,
|
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 [`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`].
|
/// The most convenient way to look something up in this is to use [`TextStyle::resolve`].
|
||||||
@@ -1201,6 +1207,7 @@ impl Default for Style {
|
|||||||
Self {
|
Self {
|
||||||
override_font_id: None,
|
override_font_id: None,
|
||||||
override_text_style: None,
|
override_text_style: None,
|
||||||
|
override_text_valign: None,
|
||||||
text_styles: default_text_styles(),
|
text_styles: default_text_styles(),
|
||||||
drag_value_text_style: TextStyle::Button,
|
drag_value_text_style: TextStyle::Button,
|
||||||
number_formatter: NumberFormatter(Arc::new(emath::format_with_decimals_in_range)),
|
number_formatter: NumberFormatter(Arc::new(emath::format_with_decimals_in_range)),
|
||||||
@@ -1501,6 +1508,7 @@ impl Style {
|
|||||||
let Self {
|
let Self {
|
||||||
override_font_id,
|
override_font_id,
|
||||||
override_text_style,
|
override_text_style,
|
||||||
|
override_text_valign,
|
||||||
text_styles,
|
text_styles,
|
||||||
drag_value_text_style,
|
drag_value_text_style,
|
||||||
number_formatter: _, // can't change callbacks in the UI
|
number_formatter: _, // can't change callbacks in the UI
|
||||||
@@ -1534,7 +1542,7 @@ impl Style {
|
|||||||
ui.end_row();
|
ui.end_row();
|
||||||
|
|
||||||
ui.label("Override text style");
|
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 {
|
.selected_text(match override_text_style {
|
||||||
None => "None".to_owned(),
|
None => "None".to_owned(),
|
||||||
Some(override_text_style) => override_text_style.to_string(),
|
Some(override_text_style) => override_text_style.to_string(),
|
||||||
@@ -1550,6 +1558,28 @@ impl Style {
|
|||||||
});
|
});
|
||||||
ui.end_row();
|
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");
|
ui.label("Text style of DragValue");
|
||||||
crate::ComboBox::from_id_salt("drag_value_text_style")
|
crate::ComboBox::from_id_salt("drag_value_text_style")
|
||||||
.selected_text(drag_value_text_style.to_string())
|
.selected_text(drag_value_text_style.to_string())
|
||||||
|
|||||||
@@ -620,8 +620,9 @@ impl Ui {
|
|||||||
/// How to vertically align text
|
/// How to vertically align text
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn text_valign(&self) -> Align {
|
pub fn text_valign(&self) -> Align {
|
||||||
#![allow(clippy::unused_self)]
|
self.style()
|
||||||
Align::BOTTOM
|
.override_text_valign
|
||||||
|
.unwrap_or_else(|| self.layout().vertical_align())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a painter for a sub-region of this Ui.
|
/// Create a painter for a sub-region of this Ui.
|
||||||
|
|||||||
Reference in New Issue
Block a user