Add Small TextStyle

This commit is contained in:
Emil Ernerfeldt 2020-10-25 08:56:48 +01:00
parent dbae893977
commit 8b2bcb29a0
3 changed files with 30 additions and 3 deletions

View file

@ -16,6 +16,7 @@ use super::{
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum TextStyle {
Small,
Body,
Button,
Heading,
@ -46,6 +47,7 @@ impl Default for FontDefinitions {
impl FontDefinitions {
pub fn with_pixels_per_point(pixels_per_point: f32) -> Self {
let mut fonts = BTreeMap::new();
fonts.insert(TextStyle::Small, (FontFamily::VariableWidth, 10.0));
fonts.insert(TextStyle::Body, (FontFamily::VariableWidth, 14.0));
fonts.insert(TextStyle::Button, (FontFamily::VariableWidth, 16.0));
fonts.insert(TextStyle::Heading, (FontFamily::VariableWidth, 24.0));

View file

@ -531,6 +531,11 @@ impl Ui {
self.add(label.into().monospace())
}
/// Shortcut for `add(Label::new(text).small())`
pub fn small(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().small())
}
/// Shortcut for `add(Hyperlink::new(url))`
pub fn hyperlink(&mut self, url: impl Into<String>) -> Response {
self.add(Hyperlink::new(url))

View file

@ -69,6 +69,10 @@ impl Label {
self.text_style(TextStyle::Monospace)
}
pub fn small(self) -> Self {
self.text_style(TextStyle::Small)
}
pub fn text_color(mut self, text_color: Srgba) -> Self {
self.text_color = Some(text_color);
self
@ -159,8 +163,10 @@ impl Into<Label> for String {
/// A clickable hyperlink, e.g. to `"https://github.com/emilk/egui"`.
pub struct Hyperlink {
// TODO: wrap Label
url: String,
text: String,
pub(crate) text_style: Option<TextStyle>,
}
impl Hyperlink {
@ -169,6 +175,7 @@ impl Hyperlink {
Self {
text: url.clone(),
url,
text_style: None,
}
}
@ -177,14 +184,27 @@ impl Hyperlink {
self.text = text.into();
self
}
/// If you do not set a `TextStyle`, the default `style.text_style`.
pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = Some(text_style);
self
}
pub fn small(self) -> Self {
self.text_style(TextStyle::Small)
}
}
impl Widget for Hyperlink {
fn ui(self, ui: &mut Ui) -> Response {
let Hyperlink { url, text } = self;
let Hyperlink {
url,
text,
text_style,
} = self;
let color = color::LIGHT_BLUE;
let text_style = ui.style().body_text_style;
let text_style = text_style.unwrap_or_else(|| ui.style().body_text_style);
let id = ui.make_child_id(&url);
let font = &ui.fonts()[text_style];
let galley = font.layout_multiline(text, ui.available().width());