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

View file

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

View file

@ -69,6 +69,10 @@ impl Label {
self.text_style(TextStyle::Monospace) 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 { pub fn text_color(mut self, text_color: Srgba) -> Self {
self.text_color = Some(text_color); self.text_color = Some(text_color);
self self
@ -159,8 +163,10 @@ impl Into<Label> for String {
/// A clickable hyperlink, e.g. to `"https://github.com/emilk/egui"`. /// A clickable hyperlink, e.g. to `"https://github.com/emilk/egui"`.
pub struct Hyperlink { pub struct Hyperlink {
// TODO: wrap Label
url: String, url: String,
text: String, text: String,
pub(crate) text_style: Option<TextStyle>,
} }
impl Hyperlink { impl Hyperlink {
@ -169,6 +175,7 @@ impl Hyperlink {
Self { Self {
text: url.clone(), text: url.clone(),
url, url,
text_style: None,
} }
} }
@ -177,14 +184,27 @@ impl Hyperlink {
self.text = text.into(); self.text = text.into();
self 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 { impl Widget for Hyperlink {
fn ui(self, ui: &mut Ui) -> Response { 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 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 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());