Implement explicit text caching in the widget state tree

This commit is contained in:
Héctor Ramón Jiménez 2023-08-30 04:31:21 +02:00
parent c9bd48704d
commit ed3454301e
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
79 changed files with 1910 additions and 1705 deletions

View file

@ -6,12 +6,11 @@ use crate::core::mouse;
use crate::core::renderer;
use crate::core::text;
use crate::core::touch;
use crate::core::widget::Tree;
use crate::core::widget;
use crate::core::widget::tree::{self, Tree};
use crate::core::{
Alignment, Clipboard, Element, Layout, Length, Pixels, Rectangle, Shell,
Widget,
Clipboard, Element, Layout, Length, Pixels, Rectangle, Shell, Size, Widget,
};
use crate::{Row, Text};
pub use iced_style::checkbox::{Appearance, StyleSheet};
@ -45,7 +44,7 @@ where
width: Length,
size: f32,
spacing: f32,
text_size: Option<f32>,
text_size: Option<Pixels>,
text_line_height: text::LineHeight,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
@ -118,7 +117,7 @@ where
/// Sets the text size of the [`Checkbox`].
pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self {
self.text_size = Some(text_size.into().0);
self.text_size = Some(text_size.into());
self
}
@ -167,6 +166,14 @@ where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet + crate::text::StyleSheet,
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<widget::text::State<Renderer::Paragraph>>()
}
fn state(&self) -> tree::State {
tree::State::new(widget::text::State::<Renderer::Paragraph>::default())
}
fn width(&self) -> Length {
self.width
}
@ -177,26 +184,35 @@ where
fn layout(
&self,
tree: &Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
Row::<(), Renderer>::new()
.width(self.width)
.spacing(self.spacing)
.align_items(Alignment::Center)
.push(Row::new().width(self.size).height(self.size))
.push(
Text::new(&self.label)
.font(self.font.unwrap_or_else(|| renderer.default_font()))
.width(self.width)
.size(
self.text_size
.unwrap_or_else(|| renderer.default_size()),
)
.line_height(self.text_line_height)
.shaping(self.text_shaping),
)
.layout(renderer, limits)
layout::next_to_each_other(
&limits.width(self.width),
self.spacing,
|_| layout::Node::new(Size::new(self.size, self.size)),
|limits| {
let state = tree
.state
.downcast_ref::<widget::text::State<Renderer::Paragraph>>();
widget::text::layout(
state,
renderer,
limits,
self.width,
Length::Shrink,
&self.label,
self.text_line_height,
self.text_size,
self.font,
alignment::Horizontal::Left,
alignment::Vertical::Top,
self.text_shaping,
)
},
)
}
fn on_event(
@ -244,7 +260,7 @@ where
fn draw(
&self,
_tree: &Tree,
tree: &Tree,
renderer: &mut Renderer,
theme: &Renderer::Theme,
style: &renderer::Style,
@ -283,24 +299,23 @@ where
line_height,
shaping,
} = &self.icon;
let size = size.unwrap_or(bounds.height * 0.7);
let size = size.unwrap_or(Pixels(bounds.height * 0.7));
if self.is_checked {
renderer.fill_text(text::Text {
content: &code_point.to_string(),
font: *font,
size,
line_height: *line_height,
bounds: Rectangle {
x: bounds.center_x(),
y: bounds.center_y(),
..bounds
renderer.fill_text(
text::Text {
content: &code_point.to_string(),
font: *font,
size,
line_height: *line_height,
bounds: bounds.size(),
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
shaping: *shaping,
},
color: custom_style.icon_color,
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
shaping: *shaping,
});
bounds.center(),
custom_style.icon_color,
);
}
}
@ -311,16 +326,10 @@ where
renderer,
style,
label_layout,
&self.label,
self.text_size,
self.text_line_height,
self.font,
tree.state.downcast_ref(),
crate::text::Appearance {
color: custom_style.text_color,
},
alignment::Horizontal::Left,
alignment::Vertical::Center,
self.text_shaping,
);
}
}
@ -348,7 +357,7 @@ pub struct Icon<Font> {
/// The unicode code point that will be used as the icon.
pub code_point: char,
/// Font size of the content.
pub size: Option<f32>,
pub size: Option<Pixels>,
/// The line height of the icon.
pub line_height: text::LineHeight,
/// The shaping strategy of the icon.