Skip to content

Commit 1997584

Browse files
committed
renderer: Remove the str slice from text_size()
Instead, hide access to the string behind a RenderString trait. The objective in the future is to cache the layout, which means that we need to query the text and other properties only if the cache is dirty.
1 parent e6f7496 commit 1997584

File tree

9 files changed

+60
-56
lines changed

9 files changed

+60
-56
lines changed

internal/backends/qt/qt_window.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,13 +2091,12 @@ impl WindowAdapterInternal for QtWindow {
20912091
impl i_slint_core::renderer::RendererSealed for QtWindow {
20922092
fn text_size(
20932093
&self,
2094-
text_item: Pin<&dyn i_slint_core::item_rendering::HasFont>,
2094+
text_item: Pin<&dyn i_slint_core::item_rendering::RenderString>,
20952095
item_rc: &ItemRc,
2096-
text: &str,
20972096
max_width: Option<LogicalLength>,
20982097
text_wrap: TextWrap,
20992098
) -> LogicalSize {
2100-
sharedparley::text_size(self, text_item, item_rc, text, max_width, text_wrap)
2099+
sharedparley::text_size(self, text_item, item_rc, max_width, text_wrap)
21012100
}
21022101

21032102
fn char_size(

internal/backends/testing/testing_backend.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,12 @@ impl WindowAdapter for TestingWindow {
163163
impl RendererSealed for TestingWindow {
164164
fn text_size(
165165
&self,
166-
_text_item: Pin<&dyn i_slint_core::item_rendering::HasFont>,
166+
text_item: Pin<&dyn i_slint_core::item_rendering::RenderString>,
167167
_item_rc: &i_slint_core::item_tree::ItemRc,
168-
text: &str,
169168
_max_width: Option<LogicalLength>,
170169
_text_wrap: TextWrap,
171170
) -> LogicalSize {
172-
LogicalSize::new(text.len() as f32 * 10., 10.)
171+
LogicalSize::new(text_item.text().len() as f32 * 10., 10.)
173172
}
174173

175174
fn char_size(

internal/core/item_rendering.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,16 @@ pub trait HasFont {
292292
fn font_request(self: Pin<&Self>, self_rc: &crate::items::ItemRc) -> FontRequest;
293293
}
294294

295+
/// Trait for an item that represents an string towards the renderer
296+
#[allow(missing_docs)]
297+
pub trait RenderString: HasFont {
298+
fn text(self: Pin<&Self>) -> SharedString;
299+
}
300+
295301
/// Trait for an item that represents an Text towards the renderer
296302
#[allow(missing_docs)]
297-
pub trait RenderText: HasFont {
303+
pub trait RenderText: RenderString {
298304
fn target_size(self: Pin<&Self>) -> LogicalSize;
299-
fn text(self: Pin<&Self>) -> SharedString;
300305
fn color(self: Pin<&Self>) -> Brush;
301306
fn alignment(self: Pin<&Self>) -> (TextHorizontalAlignment, TextVerticalAlignment);
302307
fn wrap(self: Pin<&Self>) -> TextWrap;
@@ -319,15 +324,17 @@ impl HasFont for (SharedString, Brush) {
319324
}
320325
}
321326

327+
impl RenderString for (SharedString, Brush) {
328+
fn text(self: Pin<&Self>) -> SharedString {
329+
self.0.clone()
330+
}
331+
}
332+
322333
impl RenderText for (SharedString, Brush) {
323334
fn target_size(self: Pin<&Self>) -> LogicalSize {
324335
LogicalSize::default()
325336
}
326337

327-
fn text(self: Pin<&Self>) -> SharedString {
328-
self.0.clone()
329-
}
330-
331338
fn color(self: Pin<&Self>) -> Brush {
332339
self.1.clone()
333340
}

internal/core/items/text.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::input::{
1818
key_codes, FocusEvent, FocusEventResult, FocusReason, InputEventFilterResult, InputEventResult,
1919
KeyEvent, KeyboardModifiers, MouseEvent, StandardShortcut, TextShortcut,
2020
};
21-
use crate::item_rendering::{CachedRenderingData, HasFont, ItemRenderer, RenderText};
21+
use crate::item_rendering::{CachedRenderingData, HasFont, ItemRenderer, RenderString, RenderText};
2222
use crate::layout::{LayoutInfo, Orientation};
2323
use crate::lengths::{LogicalLength, LogicalPoint, LogicalRect, LogicalSize};
2424
use crate::platform::Clipboard;
@@ -168,15 +168,17 @@ impl HasFont for ComplexText {
168168
}
169169
}
170170

171+
impl RenderString for ComplexText {
172+
fn text(self: Pin<&Self>) -> SharedString {
173+
self.text()
174+
}
175+
}
176+
171177
impl RenderText for ComplexText {
172178
fn target_size(self: Pin<&Self>) -> LogicalSize {
173179
LogicalSize::from_lengths(self.width(), self.height())
174180
}
175181

176-
fn text(self: Pin<&Self>) -> SharedString {
177-
self.text()
178-
}
179-
180182
fn color(self: Pin<&Self>) -> Brush {
181183
self.color()
182184
}
@@ -386,15 +388,17 @@ impl HasFont for MarkdownText {
386388
}
387389
}
388390

391+
impl RenderString for MarkdownText {
392+
fn text(self: Pin<&Self>) -> SharedString {
393+
self.text()
394+
}
395+
}
396+
389397
impl RenderText for MarkdownText {
390398
fn target_size(self: Pin<&Self>) -> LogicalSize {
391399
LogicalSize::from_lengths(self.width(), self.height())
392400
}
393401

394-
fn text(self: Pin<&Self>) -> SharedString {
395-
self.text()
396-
}
397-
398402
fn color(self: Pin<&Self>) -> Brush {
399403
self.color()
400404
}
@@ -561,15 +565,17 @@ impl HasFont for SimpleText {
561565
}
562566
}
563567

568+
impl RenderString for SimpleText {
569+
fn text(self: Pin<&Self>) -> SharedString {
570+
self.text()
571+
}
572+
}
573+
564574
impl RenderText for SimpleText {
565575
fn target_size(self: Pin<&Self>) -> LogicalSize {
566576
LogicalSize::from_lengths(self.width(), self.height())
567577
}
568578

569-
fn text(self: Pin<&Self>) -> SharedString {
570-
self.text()
571-
}
572-
573579
fn color(self: Pin<&Self>) -> Brush {
574580
self.color()
575581
}
@@ -618,15 +624,8 @@ fn text_layout_info(
618624
orientation: Orientation,
619625
width: Pin<&Property<LogicalLength>>,
620626
) -> LayoutInfo {
621-
let text_string = text.text();
622627
let implicit_size = |max_width, text_wrap| {
623-
window_adapter.renderer().text_size(
624-
text,
625-
self_rc,
626-
text_string.as_str(),
627-
max_width,
628-
text_wrap,
629-
)
628+
window_adapter.renderer().text_size(text, self_rc, max_width, text_wrap)
630629
};
631630

632631
// Stretch uses `round_layout` to explicitly align the top left and bottom right of layout nodes
@@ -758,12 +757,8 @@ impl Item for TextInput {
758757
window_adapter: &Rc<dyn WindowAdapter>,
759758
self_rc: &ItemRc,
760759
) -> LayoutInfo {
761-
let text = self.text();
762760
let implicit_size = |max_width, text_wrap| {
763-
if text.is_empty() {
764-
return window_adapter.renderer().char_size(self, self_rc, '*');
765-
}
766-
window_adapter.renderer().text_size(self, self_rc, text.as_str(), max_width, text_wrap)
761+
window_adapter.renderer().text_size(self, self_rc, max_width, text_wrap)
767762
};
768763

769764
// Stretch uses `round_layout` to explicitly align the top left and bottom right of layout nodes
@@ -1272,6 +1267,12 @@ impl HasFont for TextInput {
12721267
}
12731268
}
12741269

1270+
impl RenderString for TextInput {
1271+
fn text(self: Pin<&Self>) -> SharedString {
1272+
self.as_ref().text()
1273+
}
1274+
}
1275+
12751276
pub enum TextCursorDirection {
12761277
Forward,
12771278
Backward,

internal/core/renderer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pub trait RendererSealed {
3030
/// using the wrapping type passed by `text_wrap`.
3131
fn text_size(
3232
&self,
33-
text_item: Pin<&dyn crate::item_rendering::HasFont>,
33+
text_item: Pin<&dyn crate::item_rendering::RenderString>,
3434
item_rc: &crate::item_tree::ItemRc,
35-
text: &str,
3635
max_width: Option<LogicalLength>,
3736
text_wrap: TextWrap,
3837
) -> LogicalSize;

internal/core/software_renderer.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,8 @@ impl SoftwareRenderer {
723723
impl RendererSealed for SoftwareRenderer {
724724
fn text_size(
725725
&self,
726-
text_item: Pin<&dyn crate::item_rendering::HasFont>,
726+
text_item: Pin<&dyn crate::item_rendering::RenderString>,
727727
item_rc: &crate::item_tree::ItemRc,
728-
text: &str,
729728
max_width: Option<LogicalLength>,
730729
text_wrap: TextWrap,
731730
) -> LogicalSize {
@@ -738,13 +737,14 @@ impl RendererSealed for SoftwareRenderer {
738737
match (font, parley_disabled()) {
739738
#[cfg(feature = "software-renderer-systemfonts")]
740739
(fonts::Font::VectorFont(_), false) => {
741-
sharedparley::text_size(self, text_item, item_rc, text, max_width, text_wrap)
740+
sharedparley::text_size(self, text_item, item_rc, max_width, text_wrap)
742741
}
743742
#[cfg(feature = "software-renderer-systemfonts")]
744743
(fonts::Font::VectorFont(vf), true) => {
745744
let layout = fonts::text_layout_for_font(&vf, &font_request, scale_factor);
745+
let text = text_item.text();
746746
let (longest_line_width, height) = layout.text_size(
747-
text,
747+
text.as_str(),
748748
max_width.map(|max_width| (max_width.cast() * scale_factor).cast()),
749749
text_wrap,
750750
);
@@ -753,8 +753,9 @@ impl RendererSealed for SoftwareRenderer {
753753
}
754754
(fonts::Font::PixelFont(pf), _) => {
755755
let layout = fonts::text_layout_for_font(&pf, &font_request, scale_factor);
756+
let text = text_item.text();
756757
let (longest_line_width, height) = layout.text_size(
757-
text,
758+
text.as_str(),
758759
max_width.map(|max_width| (max_width.cast() * scale_factor).cast()),
759760
text_wrap,
760761
);
@@ -773,7 +774,7 @@ impl RendererSealed for SoftwareRenderer {
773774
let Some(scale_factor) = self.scale_factor() else {
774775
return LogicalSize::default();
775776
};
776-
let font_request = FontRequest::new(text_item, item_rc);
777+
let font_request = text_item.font_request(item_rc);
777778
let font = fonts::match_font(&font_request, scale_factor);
778779

779780
match (font, parley_disabled()) {

internal/core/textlayout/sharedparley.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,18 +1358,18 @@ pub fn draw_text_input(
13581358

13591359
pub fn text_size(
13601360
renderer: &dyn RendererSealed,
1361-
text_item: Pin<&dyn crate::item_rendering::HasFont>,
1361+
text_item: Pin<&dyn crate::item_rendering::RenderString>,
13621362
item_rc: &crate::item_tree::ItemRc,
1363-
text: &str,
13641363
max_width: Option<LogicalLength>,
13651364
text_wrap: TextWrap,
13661365
) -> LogicalSize {
13671366
let Some(scale_factor) = renderer.scale_factor() else {
13681367
return LogicalSize::default();
13691368
};
13701369
let font_request = text_item.font_request(item_rc);
1370+
let text = text_item.text();
13711371
let layout = layout(
1372-
Text::PlainText(text),
1372+
Text::PlainText(text.as_str()),
13731373
scale_factor,
13741374
LayoutOptions {
13751375
max_width,
@@ -1393,7 +1393,7 @@ pub fn char_size(
13931393
item_rc: &crate::item_tree::ItemRc,
13941394
ch: char,
13951395
) -> Option<LogicalSize> {
1396-
let font_request = FontRequest::new(text_item, item_rc);
1396+
let font_request = text_item.font_request(item_rc);
13971397
let font = font_request.query_fontique()?;
13981398

13991399
let char_map = font.charmap()?;

internal/renderers/femtovg/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,12 @@ impl<B: GraphicsBackend> FemtoVGRenderer<B> {
284284
impl<B: GraphicsBackend> RendererSealed for FemtoVGRenderer<B> {
285285
fn text_size(
286286
&self,
287-
text_item: Pin<&dyn i_slint_core::item_rendering::HasFont>,
287+
text_item: Pin<&dyn i_slint_core::item_rendering::RenderString>,
288288
item_rc: &ItemRc,
289-
text: &str,
290289
max_width: Option<LogicalLength>,
291290
text_wrap: TextWrap,
292291
) -> LogicalSize {
293-
sharedparley::text_size(self, text_item, item_rc, text, max_width, text_wrap)
292+
sharedparley::text_size(self, text_item, item_rc, max_width, text_wrap)
294293
}
295294

296295
fn char_size(

internal/renderers/skia/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,12 @@ impl SkiaRenderer {
824824
impl i_slint_core::renderer::RendererSealed for SkiaRenderer {
825825
fn text_size(
826826
&self,
827-
text_item: Pin<&dyn i_slint_core::item_rendering::HasFont>,
827+
text_item: Pin<&dyn i_slint_core::item_rendering::RenderString>,
828828
item_rc: &ItemRc,
829-
text: &str,
830829
max_width: Option<LogicalLength>,
831830
text_wrap: TextWrap,
832831
) -> LogicalSize {
833-
sharedparley::text_size(self, text_item, item_rc, text, max_width, text_wrap)
832+
sharedparley::text_size(self, text_item, item_rc, max_width, text_wrap)
834833
}
835834

836835
fn char_size(

0 commit comments

Comments
 (0)