Make default text size configurable in Settings

This commit is contained in:
Héctor Ramón Jiménez 2020-06-19 00:08:28 +02:00
parent d19c02035f
commit b3c192a2e4
12 changed files with 74 additions and 19 deletions

View File

@ -18,6 +18,7 @@ pub struct Backend {
quad_pipeline: quad::Pipeline, quad_pipeline: quad::Pipeline,
text_pipeline: text::Pipeline, text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline, triangle_pipeline: triangle::Pipeline,
default_text_size: u16,
} }
impl Backend { impl Backend {
@ -33,6 +34,7 @@ impl Backend {
quad_pipeline, quad_pipeline,
text_pipeline, text_pipeline,
triangle_pipeline, triangle_pipeline,
default_text_size: settings.default_text_size,
} }
} }
@ -192,6 +194,10 @@ impl backend::Text for Backend {
const ICON_FONT: Font = font::ICONS; const ICON_FONT: Font = font::ICONS;
const CHECKMARK_ICON: char = font::CHECKMARK_ICON; const CHECKMARK_ICON: char = font::CHECKMARK_ICON;
fn default_size(&self) -> u16 {
self.default_text_size
}
fn measure( fn measure(
&self, &self,
contents: &str, contents: &str,

View File

@ -11,6 +11,11 @@ pub struct Settings {
/// If `None` is provided, a default system font will be chosen. /// If `None` is provided, a default system font will be chosen.
pub default_font: Option<&'static [u8]>, pub default_font: Option<&'static [u8]>,
/// The default size of text.
///
/// By default, it will be set to 20.
pub default_text_size: u16,
/// The antialiasing strategy that will be used for triangle primitives. /// The antialiasing strategy that will be used for triangle primitives.
pub antialiasing: Option<Antialiasing>, pub antialiasing: Option<Antialiasing>,
} }
@ -19,6 +24,7 @@ impl Default for Settings {
fn default() -> Settings { fn default() -> Settings {
Settings { Settings {
default_font: None, default_font: None,
default_text_size: 20,
antialiasing: None, antialiasing: None,
} }
} }

View File

@ -25,6 +25,9 @@ pub trait Text {
/// [`ICON_FONT`]: #associatedconst.ICON_FONt /// [`ICON_FONT`]: #associatedconst.ICON_FONt
const CHECKMARK_ICON: char; const CHECKMARK_ICON: char;
/// Returns the default size of text.
fn default_size(&self) -> u16;
/// Measures the text contents with the given size and font, /// Measures the text contents with the given size and font,
/// returning the size of a laid out paragraph that fits in the provided /// returning the size of a laid out paragraph that fits in the provided
/// bounds. /// bounds.

View File

@ -20,7 +20,9 @@ where
{ {
type Font = Font; type Font = Font;
const DEFAULT_SIZE: u16 = 20; fn default_size(&self) -> u16 {
self.backend().default_size()
}
fn measure( fn measure(
&self, &self,

View File

@ -49,7 +49,9 @@ impl row::Renderer for Null {
impl text::Renderer for Null { impl text::Renderer for Null {
type Font = Font; type Font = Font;
const DEFAULT_SIZE: u16 = 20; fn default_size(&self) -> u16 {
20
}
fn measure( fn measure(
&self, &self,

View File

@ -32,7 +32,7 @@ pub struct Checkbox<Message, Renderer: self::Renderer + text::Renderer> {
width: Length, width: Length,
size: u16, size: u16,
spacing: u16, spacing: u16,
text_size: u16, text_size: Option<u16>,
style: Renderer::Style, style: Renderer::Style,
} }
@ -60,7 +60,7 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
width: Length::Shrink, width: Length::Shrink,
size: <Renderer as self::Renderer>::DEFAULT_SIZE, size: <Renderer as self::Renderer>::DEFAULT_SIZE,
spacing: Renderer::DEFAULT_SPACING, spacing: Renderer::DEFAULT_SPACING,
text_size: <Renderer as text::Renderer>::DEFAULT_SIZE, text_size: None,
style: Renderer::Style::default(), style: Renderer::Style::default(),
} }
} }
@ -93,7 +93,7 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
/// ///
/// [`Checkbox`]: struct.Checkbox.html /// [`Checkbox`]: struct.Checkbox.html
pub fn text_size(mut self, text_size: u16) -> Self { pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = text_size; self.text_size = Some(text_size);
self self
} }
@ -136,7 +136,7 @@ where
.push( .push(
Text::new(&self.label) Text::new(&self.label)
.width(self.width) .width(self.width)
.size(self.text_size), .size(self.text_size.unwrap_or(renderer.default_size())),
) )
.layout(renderer, limits) .layout(renderer, limits)
} }
@ -181,7 +181,7 @@ where
defaults, defaults,
label_layout.bounds(), label_layout.bounds(),
&self.label, &self.label,
self.text_size, self.text_size.unwrap_or(renderer.default_size()),
Default::default(), Default::default(),
None, None,
HorizontalAlignment::Left, HorizontalAlignment::Left,

View File

@ -41,7 +41,7 @@ pub struct Radio<Message, Renderer: self::Renderer + text::Renderer> {
width: Length, width: Length,
size: u16, size: u16,
spacing: u16, spacing: u16,
text_size: u16, text_size: Option<u16>,
style: Renderer::Style, style: Renderer::Style,
} }
@ -75,7 +75,7 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
width: Length::Shrink, width: Length::Shrink,
size: <Renderer as self::Renderer>::DEFAULT_SIZE, size: <Renderer as self::Renderer>::DEFAULT_SIZE,
spacing: Renderer::DEFAULT_SPACING, //15 spacing: Renderer::DEFAULT_SPACING, //15
text_size: <Renderer as text::Renderer>::DEFAULT_SIZE, text_size: None,
style: Renderer::Style::default(), style: Renderer::Style::default(),
} }
} }
@ -108,7 +108,7 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
/// ///
/// [`Radio`]: struct.Radio.html /// [`Radio`]: struct.Radio.html
pub fn text_size(mut self, text_size: u16) -> Self { pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = text_size; self.text_size = Some(text_size);
self self
} }
@ -151,7 +151,7 @@ where
.push( .push(
Text::new(&self.label) Text::new(&self.label)
.width(self.width) .width(self.width)
.size(self.text_size), .size(self.text_size.unwrap_or(renderer.default_size())),
) )
.layout(renderer, limits) .layout(renderer, limits)
} }
@ -194,7 +194,7 @@ where
defaults, defaults,
label_layout.bounds(), label_layout.bounds(),
&self.label, &self.label,
self.text_size, self.text_size.unwrap_or(renderer.default_size()),
Default::default(), Default::default(),
None, None,
HorizontalAlignment::Left, HorizontalAlignment::Left,

View File

@ -131,7 +131,7 @@ where
) -> layout::Node { ) -> layout::Node {
let limits = limits.width(self.width).height(self.height); let limits = limits.width(self.width).height(self.height);
let size = self.size.unwrap_or(Renderer::DEFAULT_SIZE); let size = self.size.unwrap_or(renderer.default_size());
let bounds = limits.max(); let bounds = limits.max();
@ -154,7 +154,7 @@ where
defaults, defaults,
layout.bounds(), layout.bounds(),
&self.content, &self.content,
self.size.unwrap_or(Renderer::DEFAULT_SIZE), self.size.unwrap_or(renderer.default_size()),
self.font, self.font,
self.color, self.color,
self.horizontal_alignment, self.horizontal_alignment,
@ -187,10 +187,10 @@ pub trait Renderer: crate::Renderer {
/// [`Text`]: struct.Text.html /// [`Text`]: struct.Text.html
type Font: Default + Copy; type Font: Default + Copy;
/// The default size of [`Text`]. /// Returns the default size of [`Text`].
/// ///
/// [`Text`]: struct.Text.html /// [`Text`]: struct.Text.html
const DEFAULT_SIZE: u16; fn default_size(&self) -> u16;
/// Measures the [`Text`] in the given bounds and returns the minimum /// Measures the [`Text`] in the given bounds and returns the minimum
/// boundaries that can fit the contents. /// boundaries that can fit the contents.

View File

@ -202,6 +202,7 @@ pub trait Application: Sized {
{ {
let renderer_settings = crate::renderer::Settings { let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font, default_font: settings.default_font,
default_text_size: settings.default_text_size,
antialiasing: if settings.antialiasing { antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4) Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else { } else {

View File

@ -2,7 +2,7 @@
use crate::window; use crate::window;
/// The settings of an application. /// The settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Settings<Flags> { pub struct Settings<Flags> {
/// The window settings. /// The window settings.
/// ///
@ -22,6 +22,11 @@ pub struct Settings<Flags> {
// TODO: Add `name` for web compatibility // TODO: Add `name` for web compatibility
pub default_font: Option<&'static [u8]>, pub default_font: Option<&'static [u8]>,
/// The text size that will be used by default.
///
/// The default value is 20.
pub default_text_size: u16,
/// If set to true, the renderer will try to perform antialiasing for some /// If set to true, the renderer will try to perform antialiasing for some
/// primitives. /// primitives.
/// ///
@ -39,12 +44,28 @@ impl<Flags> Settings<Flags> {
/// ///
/// [`Application`]: ../trait.Application.html /// [`Application`]: ../trait.Application.html
pub fn with_flags(flags: Flags) -> Self { pub fn with_flags(flags: Flags) -> Self {
let default_settings = Settings::<()>::default();
Self { Self {
flags, flags,
// not using ..Default::default() struct update syntax since it is more permissive to antialiasing: default_settings.antialiasing,
// allow initializing with flags without trait bound on Default default_font: default_settings.default_font,
default_text_size: default_settings.default_text_size,
window: default_settings.window,
}
}
}
impl<Flags> Default for Settings<Flags>
where
Flags: Default,
{
fn default() -> Self {
Self {
flags: Default::default(),
antialiasing: Default::default(), antialiasing: Default::default(),
default_font: Default::default(), default_font: Default::default(),
default_text_size: 20,
window: Default::default(), window: Default::default(),
} }
} }

View File

@ -24,6 +24,8 @@ pub struct Backend {
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
image_pipeline: image::Pipeline, image_pipeline: image::Pipeline,
default_text_size: u16,
} }
impl Backend { impl Backend {
@ -50,6 +52,8 @@ impl Backend {
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
image_pipeline, image_pipeline,
default_text_size: settings.default_text_size,
} }
} }
@ -245,6 +249,10 @@ impl backend::Text for Backend {
const ICON_FONT: Font = font::ICONS; const ICON_FONT: Font = font::ICONS;
const CHECKMARK_ICON: char = font::CHECKMARK_ICON; const CHECKMARK_ICON: char = font::CHECKMARK_ICON;
fn default_size(&self) -> u16 {
self.default_text_size
}
fn measure( fn measure(
&self, &self,
contents: &str, contents: &str,

View File

@ -16,6 +16,11 @@ pub struct Settings {
/// If `None` is provided, a default system font will be chosen. /// If `None` is provided, a default system font will be chosen.
pub default_font: Option<&'static [u8]>, pub default_font: Option<&'static [u8]>,
/// The default size of text.
///
/// By default, it will be set to 20.
pub default_text_size: u16,
/// The antialiasing strategy that will be used for triangle primitives. /// The antialiasing strategy that will be used for triangle primitives.
pub antialiasing: Option<Antialiasing>, pub antialiasing: Option<Antialiasing>,
} }
@ -25,6 +30,7 @@ impl Default for Settings {
Settings { Settings {
format: wgpu::TextureFormat::Bgra8UnormSrgb, format: wgpu::TextureFormat::Bgra8UnormSrgb,
default_font: None, default_font: None,
default_text_size: 20,
antialiasing: None, antialiasing: None,
} }
} }