[style] you can override the default body text style
This commit is contained in:
parent
2c96cbfa9c
commit
8b93135fe4
7 changed files with 45 additions and 18 deletions
|
@ -223,7 +223,7 @@ impl CollapsingHeader {
|
||||||
painter.galley(
|
painter.galley(
|
||||||
text_pos,
|
text_pos,
|
||||||
galley,
|
galley,
|
||||||
label.text_style,
|
label.text_style_or_default(ui.style()),
|
||||||
ui.style().interact(&response).stroke.color,
|
ui.style().interact(&response).stroke.color,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -201,8 +201,8 @@ impl<'open> Window<'open> {
|
||||||
// First interact (move etc) to avoid frame delay:
|
// First interact (move etc) to avoid frame delay:
|
||||||
let last_frame_outer_rect = area.state().rect();
|
let last_frame_outer_rect = area.state().rect();
|
||||||
let interaction = if possible.movable || possible.resizable {
|
let interaction = if possible.movable || possible.resizable {
|
||||||
let title_bar_height =
|
let title_bar_height = title_label.font_height(ctx.fonts(), &ctx.style())
|
||||||
title_label.font_height(ctx.fonts()) + 1.0 * ctx.style().spacing.item_spacing.y; // this could be better
|
+ 1.0 * ctx.style().spacing.item_spacing.y; // this could be better
|
||||||
let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height);
|
let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height);
|
||||||
|
|
||||||
window_interaction(
|
window_interaction(
|
||||||
|
@ -596,7 +596,7 @@ fn show_title_bar(
|
||||||
collapsible: bool,
|
collapsible: bool,
|
||||||
) -> TitleBar {
|
) -> TitleBar {
|
||||||
let title_bar_and_rect = ui.horizontal_centered(|ui| {
|
let title_bar_and_rect = ui.horizontal_centered(|ui| {
|
||||||
ui.set_desired_height(title_label.font_height(ui.fonts()));
|
ui.set_desired_height(title_label.font_height(ui.fonts(), ui.style()));
|
||||||
|
|
||||||
let item_spacing = ui.style().spacing.item_spacing;
|
let item_spacing = ui.style().spacing.item_spacing;
|
||||||
let button_size = ui.style().spacing.icon_width;
|
let button_size = ui.style().spacing.icon_width;
|
||||||
|
|
|
@ -527,6 +527,7 @@ impl Context {
|
||||||
|
|
||||||
pub fn inspection_ui(&self, ui: &mut Ui) {
|
pub fn inspection_ui(&self, ui: &mut Ui) {
|
||||||
use crate::containers::*;
|
use crate::containers::*;
|
||||||
|
ui.style_mut().body_text_style = TextStyle::Monospace;
|
||||||
|
|
||||||
CollapsingHeader::new("Input")
|
CollapsingHeader::new("Input")
|
||||||
.default_open(true)
|
.default_open(true)
|
||||||
|
|
|
@ -13,7 +13,8 @@ use super::{
|
||||||
|
|
||||||
// TODO: rename
|
// TODO: rename
|
||||||
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
// #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
|
||||||
pub enum TextStyle {
|
pub enum TextStyle {
|
||||||
Body,
|
Body,
|
||||||
Button,
|
Button,
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
#![allow(clippy::if_same_then_else)]
|
#![allow(clippy::if_same_then_else)]
|
||||||
|
|
||||||
use crate::{color::*, math::*, paint::Stroke, types::*};
|
use crate::{
|
||||||
|
color::*,
|
||||||
|
math::*,
|
||||||
|
paint::{Stroke, TextStyle},
|
||||||
|
types::*,
|
||||||
|
};
|
||||||
|
|
||||||
/// Specifies the look and feel of a `Ui`.
|
/// Specifies the look and feel of a `Ui`.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||||
pub struct Style {
|
pub struct Style {
|
||||||
|
/// Default `TextStyle` for normal text (i.e. for `Label` and `TextEdit`).
|
||||||
|
pub body_text_style: TextStyle,
|
||||||
|
|
||||||
pub spacing: Spacing,
|
pub spacing: Spacing,
|
||||||
pub interaction: Interaction,
|
pub interaction: Interaction,
|
||||||
pub visuals: Visuals,
|
pub visuals: Visuals,
|
||||||
|
@ -166,6 +174,7 @@ pub struct WidgetVisuals {
|
||||||
impl Default for Style {
|
impl Default for Style {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
body_text_style: TextStyle::Body,
|
||||||
spacing: Spacing::default(),
|
spacing: Spacing::default(),
|
||||||
interaction: Interaction::default(),
|
interaction: Interaction::default(),
|
||||||
visuals: Visuals::default(),
|
visuals: Visuals::default(),
|
||||||
|
@ -265,11 +274,18 @@ impl Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
let Self {
|
let Self {
|
||||||
|
body_text_style,
|
||||||
spacing,
|
spacing,
|
||||||
interaction,
|
interaction,
|
||||||
visuals,
|
visuals,
|
||||||
animation_time,
|
animation_time,
|
||||||
} = self;
|
} = self;
|
||||||
|
ui.horizontal_centered(|ui| {
|
||||||
|
ui.label("Default text style:");
|
||||||
|
for &value in &[TextStyle::Body, TextStyle::Monospace] {
|
||||||
|
ui.radio_value(format!("{:?}", value), body_text_style, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
ui.collapsing("Spacing", |ui| spacing.ui(ui));
|
ui.collapsing("Spacing", |ui| spacing.ui(ui));
|
||||||
ui.collapsing("Interaction", |ui| interaction.ui(ui));
|
ui.collapsing("Interaction", |ui| interaction.ui(ui));
|
||||||
ui.collapsing("Visuals", |ui| visuals.ui(ui));
|
ui.collapsing("Visuals", |ui| visuals.ui(ui));
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub struct Label {
|
||||||
pub(crate) text: String,
|
pub(crate) text: String,
|
||||||
pub(crate) multiline: bool,
|
pub(crate) multiline: bool,
|
||||||
auto_shrink: bool,
|
auto_shrink: bool,
|
||||||
pub(crate) text_style: TextStyle, // TODO: Option<TextStyle>, where None means "use the default for the ui"
|
pub(crate) text_style: Option<TextStyle>,
|
||||||
pub(crate) text_color: Option<Srgba>,
|
pub(crate) text_color: Option<Srgba>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ impl Label {
|
||||||
text: text.into(),
|
text: text.into(),
|
||||||
multiline: true,
|
multiline: true,
|
||||||
auto_shrink: false,
|
auto_shrink: false,
|
||||||
text_style: TextStyle::Body,
|
text_style: None,
|
||||||
text_color: None,
|
text_color: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,8 +62,9 @@ impl Label {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If you do not set a `TextStyle`, the default `style.text_style`.
|
||||||
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
||||||
self.text_style = text_style;
|
self.text_style = Some(text_style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +87,8 @@ impl Label {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn layout_width(&self, ui: &Ui, max_width: f32) -> font::Galley {
|
pub fn layout_width(&self, ui: &Ui, max_width: f32) -> font::Galley {
|
||||||
let font = &ui.fonts()[self.text_style];
|
let text_style = self.text_style_or_default(ui.style());
|
||||||
|
let font = &ui.fonts()[text_style];
|
||||||
if self.multiline {
|
if self.multiline {
|
||||||
font.layout_multiline(self.text.clone(), max_width) // TODO: avoid clone
|
font.layout_multiline(self.text.clone(), max_width) // TODO: avoid clone
|
||||||
} else {
|
} else {
|
||||||
|
@ -94,8 +96,9 @@ impl Label {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn font_height(&self, fonts: &Fonts) -> f32 {
|
pub fn font_height(&self, fonts: &Fonts, style: &Style) -> f32 {
|
||||||
fonts[self.text_style].height()
|
let text_style = self.text_style_or_default(style);
|
||||||
|
fonts[text_style].height()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this should return a LabelLayout which has a paint method.
|
// TODO: this should return a LabelLayout which has a paint method.
|
||||||
|
@ -107,11 +110,16 @@ impl Label {
|
||||||
// This should be the easiest method of putting text anywhere.
|
// This should be the easiest method of putting text anywhere.
|
||||||
|
|
||||||
pub fn paint_galley(&self, ui: &mut Ui, pos: Pos2, galley: font::Galley) {
|
pub fn paint_galley(&self, ui: &mut Ui, pos: Pos2, galley: font::Galley) {
|
||||||
|
let text_style = self.text_style_or_default(ui.style());
|
||||||
let text_color = self
|
let text_color = self
|
||||||
.text_color
|
.text_color
|
||||||
.unwrap_or_else(|| ui.style().visuals.text_color);
|
.unwrap_or_else(|| ui.style().visuals.text_color);
|
||||||
ui.painter()
|
ui.painter().galley(pos, galley, text_style, text_color);
|
||||||
.galley(pos, galley, self.text_style, text_color);
|
}
|
||||||
|
|
||||||
|
/// Read the text style, or get the default for the current style
|
||||||
|
pub fn text_style_or_default(&self, style: &Style) -> TextStyle {
|
||||||
|
self.text_style.unwrap_or_else(|| style.body_text_style)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +188,7 @@ impl Widget for Hyperlink {
|
||||||
let Hyperlink { url, text } = self;
|
let Hyperlink { url, text } = self;
|
||||||
|
|
||||||
let color = color::LIGHT_BLUE;
|
let color = color::LIGHT_BLUE;
|
||||||
let text_style = TextStyle::Body;
|
let text_style = ui.style().body_text_style;
|
||||||
let id = ui.make_child_id(&url);
|
let id = ui.make_child_id(&url);
|
||||||
let font = &ui.fonts()[text_style];
|
let font = &ui.fonts()[text_style];
|
||||||
let galley = font.layout_multiline(text, ui.available().width());
|
let galley = font.layout_multiline(text, ui.available().width());
|
||||||
|
|
|
@ -14,7 +14,7 @@ pub struct TextEdit<'t> {
|
||||||
text: &'t mut String,
|
text: &'t mut String,
|
||||||
id: Option<Id>,
|
id: Option<Id>,
|
||||||
id_source: Option<Id>,
|
id_source: Option<Id>,
|
||||||
text_style: TextStyle, // TODO: Option<TextStyle>, where None means "use the default for the current Ui"
|
text_style: Option<TextStyle>,
|
||||||
text_color: Option<Srgba>,
|
text_color: Option<Srgba>,
|
||||||
multiline: bool,
|
multiline: bool,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
|
@ -27,7 +27,7 @@ impl<'t> TextEdit<'t> {
|
||||||
text,
|
text,
|
||||||
id: None,
|
id: None,
|
||||||
id_source: None,
|
id_source: None,
|
||||||
text_style: TextStyle::Body,
|
text_style: None,
|
||||||
text_color: None,
|
text_color: None,
|
||||||
multiline: true,
|
multiline: true,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
@ -46,7 +46,7 @@ impl<'t> TextEdit<'t> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
||||||
self.text_style = text_style;
|
self.text_style = Some(text_style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +90,7 @@ impl<'t> Widget for TextEdit<'t> {
|
||||||
|
|
||||||
let mut state = ui.memory().text_edit.get(&id).cloned().unwrap_or_default();
|
let mut state = ui.memory().text_edit.get(&id).cloned().unwrap_or_default();
|
||||||
|
|
||||||
|
let text_style = text_style.unwrap_or_else(|| ui.style().body_text_style);
|
||||||
let font = &ui.fonts()[text_style];
|
let font = &ui.fonts()[text_style];
|
||||||
let line_spacing = font.line_spacing();
|
let line_spacing = font.line_spacing();
|
||||||
let available_width = ui.available().width();
|
let available_width = ui.available().width();
|
||||||
|
|
Loading…
Reference in a new issue