Use Pixels for Text::size

This commit is contained in:
Héctor Ramón Jiménez 2023-02-04 16:41:18 +01:00
parent 7b8b01f560
commit 570600ce51
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
16 changed files with 79 additions and 78 deletions

View file

@ -22,7 +22,7 @@ pub struct Backend {
quad_pipeline: quad::Pipeline,
text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline,
default_text_size: u16,
default_text_size: f32,
}
impl Backend {
@ -228,7 +228,7 @@ impl backend::Text for Backend {
const CHECKMARK_ICON: char = font::CHECKMARK_ICON;
const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON;
fn default_size(&self) -> u16 {
fn default_size(&self) -> f32 {
self.default_text_size
}

View file

@ -4,7 +4,7 @@ pub use iced_graphics::Antialiasing;
/// The settings of a [`Backend`].
///
/// [`Backend`]: crate::Backend
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq)]
pub struct Settings {
/// The bytes of the font that will be used by default.
///
@ -13,8 +13,8 @@ pub struct Settings {
/// The default size of text.
///
/// By default, it will be set to 20.
pub default_text_size: u16,
/// By default, it will be set to `20.0`.
pub default_text_size: f32,
/// If enabled, spread text workload in multiple threads when multiple cores
/// are available.
@ -32,7 +32,7 @@ impl Default for Settings {
fn default() -> Settings {
Settings {
default_font: None,
default_text_size: 20,
default_text_size: 20.0,
text_multithreading: false,
antialiasing: None,
}

View file

@ -32,7 +32,7 @@ pub trait Text {
const ARROW_DOWN_ICON: char;
/// Returns the default size of text.
fn default_size(&self) -> u16;
fn default_size(&self) -> f32;
/// Measures the text contents with the given size and font,
/// returning the size of a laid out paragraph that fits in the provided

View file

@ -130,14 +130,14 @@ where
const CHECKMARK_ICON: char = B::CHECKMARK_ICON;
const ARROW_DOWN_ICON: char = B::ARROW_DOWN_ICON;
fn default_size(&self) -> u16 {
fn default_size(&self) -> f32 {
self.backend().default_size()
}
fn measure(
&self,
content: &str,
size: u16,
size: f32,
font: Font,
bounds: Size,
) -> (f32, f32) {

View file

@ -11,8 +11,8 @@ use crate::widget::container::{self, Container};
use crate::widget::scrollable::{self, Scrollable};
use crate::widget::Tree;
use crate::{
Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle,
Shell, Size, Vector, Widget,
Clipboard, Color, Element, Layout, Length, Padding, Pixels, Point,
Rectangle, Shell, Size, Vector, Widget,
};
pub use iced_style::menu::{Appearance, StyleSheet};
@ -30,7 +30,7 @@ where
last_selection: &'a mut Option<T>,
width: f32,
padding: Padding,
text_size: Option<u16>,
text_size: Option<f32>,
font: Renderer::Font,
style: <Renderer::Theme as StyleSheet>::Style,
}
@ -76,8 +76,8 @@ where
}
/// Sets the text size of the [`Menu`].
pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = Some(text_size);
pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
self.text_size = Some(text_size.into().0);
self
}
@ -310,7 +310,7 @@ where
hovered_option: &'a mut Option<usize>,
last_selection: &'a mut Option<T>,
padding: Padding,
text_size: Option<u16>,
text_size: Option<f32>,
font: Renderer::Font,
style: <Renderer::Theme as StyleSheet>::Style,
}
@ -344,8 +344,9 @@ where
let size = {
let intrinsic = Size::new(
0.0,
f32::from(text_size + self.padding.vertical())
* self.options.len() as f32,
text_size
+ f32::from(self.padding.vertical())
* self.options.len() as f32,
);
limits.resolve(intrinsic)
@ -386,7 +387,7 @@ where
*self.hovered_option = Some(
((cursor_position.y - bounds.y)
/ f32::from(text_size + self.padding.vertical()))
/ (text_size + f32::from(self.padding.vertical())))
as usize,
);
}
@ -401,7 +402,7 @@ where
*self.hovered_option = Some(
((cursor_position.y - bounds.y)
/ f32::from(text_size + self.padding.vertical()))
/ (text_size + f32::from(self.padding.vertical())))
as usize,
);
@ -450,7 +451,8 @@ where
let text_size =
self.text_size.unwrap_or_else(|| renderer.default_size());
let option_height = (text_size + self.padding.vertical()) as usize;
let option_height =
(text_size + f32::from(self.padding.vertical())) as usize;
let offset = viewport.y - bounds.y;
let start = (offset / option_height as f32) as usize;
@ -467,7 +469,7 @@ where
x: bounds.x,
y: bounds.y + (option_height * i) as f32,
width: bounds.width,
height: f32::from(text_size + self.padding.vertical()),
height: text_size + f32::from(self.padding.vertical()),
};
if is_selected {

View file

@ -44,14 +44,14 @@ impl text::Renderer for Null {
const CHECKMARK_ICON: char = '0';
const ARROW_DOWN_ICON: char = '0';
fn default_size(&self) -> u16 {
20
fn default_size(&self) -> f32 {
20.0
}
fn measure(
&self,
_content: &str,
_size: u16,
_size: f32,
_font: Font,
_bounds: Size,
) -> (f32, f32) {

View file

@ -73,20 +73,20 @@ pub trait Renderer: crate::Renderer {
const ARROW_DOWN_ICON: char;
/// Returns the default size of [`Text`].
fn default_size(&self) -> u16;
fn default_size(&self) -> f32;
/// Measures the text in the given bounds and returns the minimum boundaries
/// that can fit the contents.
fn measure(
&self,
content: &str,
size: u16,
size: f32,
font: Self::Font,
bounds: Size,
) -> (f32, f32);
/// Measures the width of the text as if it were laid out in a single line.
fn measure_width(&self, content: &str, size: u16, font: Self::Font) -> f32 {
fn measure_width(&self, content: &str, size: f32, font: Self::Font) -> f32 {
let (width, _) = self.measure(content, size, font, Size::INFINITY);
width

View file

@ -54,7 +54,7 @@ where
width: Length,
size: f32,
spacing: u16,
text_size: Option<u16>,
text_size: Option<f32>,
font: Renderer::Font,
icon: Icon<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style,
@ -120,8 +120,8 @@ where
}
/// Sets the text size of the [`Checkbox`].
pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = Some(text_size);
pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
self.text_size = Some(text_size.into().0);
self
}

View file

@ -13,8 +13,8 @@ use crate::widget::container;
use crate::widget::scrollable;
use crate::widget::tree::{self, Tree};
use crate::{
Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size,
Widget,
Clipboard, Element, Layout, Length, Padding, Pixels, Point, Rectangle,
Shell, Size, Widget,
};
use std::borrow::Cow;
@ -34,7 +34,7 @@ where
selected: Option<T>,
width: Length,
padding: Padding,
text_size: Option<u16>,
text_size: Option<f32>,
font: Renderer::Font,
handle: Handle<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style,
@ -95,8 +95,8 @@ where
}
/// Sets the text size of the [`PickList`].
pub fn text_size(mut self, size: u16) -> Self {
self.text_size = Some(size);
pub fn text_size(mut self, size: impl Into<Pixels>) -> Self {
self.text_size = Some(size.into().0);
self
}
@ -297,14 +297,14 @@ impl<T> Default for State<T> {
}
/// The handle to the right side of the [`PickList`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub enum Handle<Font> {
/// Displays an arrow icon (▼).
///
/// This is the default.
Arrow {
/// Font size of the content.
size: Option<u16>,
size: Option<f32>,
},
/// A custom static handle.
Static(Icon<Font>),
@ -326,14 +326,14 @@ impl<Font> Default for Handle<Font> {
}
/// The icon of a [`Handle`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Icon<Font> {
/// Font that will be used to display the `code_point`,
pub font: Font,
/// The unicode code point that will be used as the icon.
pub code_point: char,
/// Font size of the content.
pub size: Option<u16>,
pub size: Option<f32>,
}
/// Computes the layout of a [`PickList`].
@ -342,7 +342,7 @@ pub fn layout<Renderer, T>(
limits: &layout::Limits,
width: Length,
padding: Padding,
text_size: Option<u16>,
text_size: Option<f32>,
font: &Renderer::Font,
placeholder: Option<&str>,
options: &[T],
@ -354,7 +354,6 @@ where
use std::f32;
let limits = limits.width(width).height(Length::Shrink).pad(padding);
let text_size = text_size.unwrap_or_else(|| renderer.default_size());
let max_width = match width {
@ -514,7 +513,7 @@ pub fn overlay<'a, T, Message, Renderer>(
layout: Layout<'_>,
state: &'a mut State<T>,
padding: Padding,
text_size: Option<u16>,
text_size: Option<f32>,
font: Renderer::Font,
options: &'a [T],
style: <Renderer::Theme as StyleSheet>::Style,
@ -561,7 +560,7 @@ pub fn draw<'a, T, Renderer>(
layout: Layout<'_>,
cursor_position: Point,
padding: Padding,
text_size: Option<u16>,
text_size: Option<f32>,
font: &Renderer::Font,
placeholder: Option<&str>,
selected: Option<&T>,

View file

@ -8,8 +8,8 @@ use crate::text;
use crate::touch;
use crate::widget::{self, Row, Text, Tree};
use crate::{
Alignment, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
Shell, Widget,
Alignment, Clipboard, Color, Element, Layout, Length, Pixels, Point,
Rectangle, Shell, Widget,
};
pub use iced_style::radio::{Appearance, StyleSheet};
@ -52,7 +52,7 @@ where
width: Length,
size: f32,
spacing: u16,
text_size: Option<u16>,
text_size: Option<f32>,
font: Renderer::Font,
style: <Renderer::Theme as StyleSheet>::Style,
}
@ -119,8 +119,8 @@ where
}
/// Sets the text size of the [`Radio`] button.
pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = Some(text_size);
pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
self.text_size = Some(text_size.into().0);
self
}

View file

@ -4,7 +4,7 @@ use crate::layout;
use crate::renderer;
use crate::text;
use crate::widget::Tree;
use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget};
use crate::{Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget};
use std::borrow::Cow;
@ -32,7 +32,7 @@ where
Renderer::Theme: StyleSheet,
{
content: Cow<'a, str>,
size: Option<u16>,
size: Option<f32>,
width: Length,
height: Length,
horizontal_alignment: alignment::Horizontal,
@ -61,8 +61,8 @@ where
}
/// Sets the size of the [`Text`].
pub fn size(mut self, size: u16) -> Self {
self.size = Some(size);
pub fn size(mut self, size: impl Into<Pixels>) -> Self {
self.size = Some(size.into().0);
self
}
@ -185,7 +185,7 @@ pub fn draw<Renderer>(
style: &renderer::Style,
layout: Layout<'_>,
content: &str,
size: Option<u16>,
size: Option<f32>,
font: Renderer::Font,
appearance: Appearance,
horizontal_alignment: alignment::Horizontal,
@ -209,7 +209,7 @@ pub fn draw<Renderer>(
renderer.fill_text(crate::text::Text {
content,
size: f32::from(size.unwrap_or_else(|| renderer.default_size())),
size: size.unwrap_or_else(|| renderer.default_size()),
bounds: Rectangle { x, y, ..bounds },
color: appearance.color.unwrap_or(style.text_color),
font,

View file

@ -25,7 +25,7 @@ use crate::widget::operation::{self, Operation};
use crate::widget::tree::{self, Tree};
use crate::window;
use crate::{
Clipboard, Color, Command, Element, Layout, Length, Padding, Point,
Clipboard, Color, Command, Element, Layout, Length, Padding, Pixels, Point,
Rectangle, Shell, Size, Vector, Widget,
};
@ -64,7 +64,7 @@ where
font: Renderer::Font,
width: Length,
padding: Padding,
size: Option<u16>,
size: Option<f32>,
on_change: Box<dyn Fn(String) -> Message + 'a>,
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_submit: Option<Message>,
@ -145,8 +145,8 @@ where
}
/// Sets the text size of the [`TextInput`].
pub fn size(mut self, size: u16) -> Self {
self.size = Some(size);
pub fn size(mut self, size: impl Into<Pixels>) -> Self {
self.size = Some(size.into().0);
self
}
@ -379,7 +379,7 @@ pub fn layout<Renderer>(
limits: &layout::Limits,
width: Length,
padding: Padding,
size: Option<u16>,
size: Option<f32>,
) -> layout::Node
where
Renderer: text::Renderer,
@ -405,7 +405,7 @@ pub fn update<'a, Message, Renderer>(
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
value: &mut Value,
size: Option<u16>,
size: Option<f32>,
font: &Renderer::Font,
is_secure: bool,
on_change: &dyn Fn(String) -> Message,
@ -811,7 +811,7 @@ pub fn draw<Renderer>(
state: &State,
value: &Value,
placeholder: &str,
size: Option<u16>,
size: Option<f32>,
font: &Renderer::Font,
is_secure: bool,
style: &<Renderer::Theme as StyleSheet>::Style,
@ -1124,7 +1124,7 @@ fn offset<Renderer>(
renderer: &Renderer,
text_bounds: Rectangle,
font: Renderer::Font,
size: u16,
size: f32,
value: &Value,
state: &State,
) -> f32
@ -1158,7 +1158,7 @@ fn measure_cursor_and_scroll_offset<Renderer>(
renderer: &Renderer,
text_bounds: Rectangle,
value: &Value,
size: u16,
size: f32,
cursor_index: usize,
font: Renderer::Font,
) -> (f32, f32)
@ -1181,7 +1181,7 @@ fn find_cursor_position<Renderer>(
renderer: &Renderer,
text_bounds: Rectangle,
font: Renderer::Font,
size: Option<u16>,
size: Option<f32>,
value: &Value,
state: &State,
x: f32,

View file

@ -7,8 +7,8 @@ use crate::renderer;
use crate::text;
use crate::widget::{self, Row, Text, Tree};
use crate::{
Alignment, Clipboard, Element, Event, Layout, Length, Point, Rectangle,
Shell, Widget,
Alignment, Clipboard, Element, Event, Layout, Length, Pixels, Point,
Rectangle, Shell, Widget,
};
pub use iced_style::toggler::{Appearance, StyleSheet};
@ -39,7 +39,7 @@ where
label: Option<String>,
width: Length,
size: f32,
text_size: Option<u16>,
text_size: Option<f32>,
text_alignment: alignment::Horizontal,
spacing: u16,
font: Renderer::Font,
@ -97,8 +97,8 @@ where
}
/// Sets the text size o the [`Toggler`].
pub fn text_size(mut self, text_size: u16) -> Self {
self.text_size = Some(text_size);
pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
self.text_size = Some(text_size.into().0);
self
}

View file

@ -28,8 +28,8 @@ pub struct Settings<Flags> {
/// The text size that will be used by default.
///
/// The default value is 20.
pub default_text_size: u16,
/// The default value is `20.0`.
pub default_text_size: f32,
/// If enabled, spread text workload in multiple threads when multiple cores
/// are available.
@ -97,7 +97,7 @@ where
window: Default::default(),
flags: Default::default(),
default_font: Default::default(),
default_text_size: 20,
default_text_size: 20.0,
text_multithreading: false,
antialiasing: false,
exit_on_close_request: true,

View file

@ -29,7 +29,7 @@ pub struct Backend {
#[cfg(any(feature = "image", feature = "svg"))]
image_pipeline: image::Pipeline,
default_text_size: u16,
default_text_size: f32,
}
impl Backend {
@ -265,7 +265,7 @@ impl backend::Text for Backend {
const CHECKMARK_ICON: char = font::CHECKMARK_ICON;
const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON;
fn default_size(&self) -> u16 {
fn default_size(&self) -> f32 {
self.default_text_size
}

View file

@ -4,7 +4,7 @@ pub use crate::Antialiasing;
/// The settings of a [`Backend`].
///
/// [`Backend`]: crate::Backend
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Settings {
/// The present mode of the [`Backend`].
///
@ -21,8 +21,8 @@ pub struct Settings {
/// The default size of text.
///
/// By default, it will be set to 20.
pub default_text_size: u16,
/// By default, it will be set to `16.0`.
pub default_text_size: f32,
/// If enabled, spread text workload in multiple threads when multiple cores
/// are available.
@ -66,7 +66,7 @@ impl Default for Settings {
present_mode: wgpu::PresentMode::AutoVsync,
internal_backend: wgpu::Backends::all(),
default_font: None,
default_text_size: 20,
default_text_size: 20.0,
text_multithreading: false,
antialiasing: None,
}