From c9d959d856826acda72cf62d0727a5836f2f0634 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Mon, 18 Mar 2024 14:35:19 +0100 Subject: [PATCH] fixup! refactor(core): prepare for non mutable paint function --- .../rust/src/ui/component/text/formatted.rs | 32 ++++--------------- core/embed/rust/src/ui/component/text/op.rs | 25 ++++++++------- .../src/ui/model_tr/component/flow_pages.rs | 2 +- 3 files changed, 21 insertions(+), 38 deletions(-) diff --git a/core/embed/rust/src/ui/component/text/formatted.rs b/core/embed/rust/src/ui/component/text/formatted.rs index aa360a4e2..58613f3ae 100644 --- a/core/embed/rust/src/ui/component/text/formatted.rs +++ b/core/embed/rust/src/ui/component/text/formatted.rs @@ -11,11 +11,9 @@ use super::{ op::OpTextLayout, }; -use core::cell::RefCell; - #[derive(Clone)] pub struct FormattedText { - op_layout: RefCell>, + op_layout: OpTextLayout, vertical: Alignment, char_offset: usize, y_offset: i16, @@ -24,7 +22,7 @@ pub struct FormattedText { impl FormattedText { pub fn new(op_layout: OpTextLayout) -> Self { Self { - op_layout: RefCell::new(op_layout), + op_layout, vertical: Alignment::Start, char_offset: 0, y_offset: 0, @@ -36,14 +34,13 @@ impl FormattedText { self } - fn layout_content(&self, sink: &mut dyn LayoutSink) -> LayoutFit { + pub(crate) fn layout_content(&self, sink: &mut dyn LayoutSink) -> LayoutFit { self.op_layout - .borrow_mut() .layout_ops(self.char_offset, Offset::y(self.y_offset), sink) } fn align_vertically(&mut self, content_height: i16) { - let bounds_height = self.op_layout.borrow().layout.bounds.height(); + let bounds_height = self.op_layout.layout.bounds.height(); if content_height >= bounds_height { self.y_offset = 0; return; @@ -125,7 +122,7 @@ impl Component for FormattedText { type Msg = Never; fn place(&mut self, bounds: Rect) -> Rect { - self.op_layout.borrow_mut().place(bounds); + self.op_layout.place(bounds); let height = self.layout_content(&mut TextNoOp).height(); self.align_vertically(height); bounds @@ -141,27 +138,12 @@ impl Component for FormattedText { #[cfg(feature = "ui_bounds")] fn bounds(&self, sink: &mut dyn FnMut(Rect)) { - sink(self.op_layout.borrow().layout.bounds) + sink(self.op_layout.layout.bounds) } } // DEBUG-ONLY SECTION BELOW -#[cfg(feature = "ui_debug")] -impl FormattedText { - /// Is the same as layout_content, but does not use `&mut self` - /// to be compatible with `trace`. - /// Therefore it has to do the `clone` of `op_layout`. - pub fn layout_content_debug(&self, sink: &mut dyn LayoutSink) -> LayoutFit { - // TODO: how to solve it "properly", without the `clone`? - // (changing `trace` to `&mut self` had some other isses...) - self.op_layout - .borrow() - .clone() - .layout_content(self.char_offset, sink) - } -} - #[cfg(feature = "ui_debug")] impl crate::trace::Trace for FormattedText { fn trace(&self, t: &mut dyn crate::trace::Tracer) { @@ -170,7 +152,7 @@ impl crate::trace::Trace for FormattedText { let fit: Cell> = Cell::new(None); t.component("FormattedText"); t.in_list("text", &|l| { - let result = self.layout_content_debug(&mut TraceSink(l)); + let result = self.layout_content(&mut TraceSink(l)); fit.set(Some(result)); }); t.bool("fits", matches!(fit.get(), Some(LayoutFit::Fitting { .. }))); diff --git a/core/embed/rust/src/ui/component/text/op.rs b/core/embed/rust/src/ui/component/text/op.rs index a008100b6..ccaad640b 100644 --- a/core/embed/rust/src/ui/component/text/op.rs +++ b/core/embed/rust/src/ui/component/text/op.rs @@ -56,7 +56,7 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { /// Perform some operations defined on `Op` for a list of those `Op`s /// - e.g. changing the color, changing the font or rendering the text. pub fn layout_ops( - &mut self, + &self, skip_bytes: usize, offset: Offset, sink: &mut dyn LayoutSink, @@ -65,25 +65,26 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { let cursor = &mut (self.layout.initial_cursor() + offset); let init_cursor = *cursor; let mut total_processed_chars = 0; + let mut layout = self.layout; // Do something when it was not skipped for op in Self::filter_skipped_ops(self.ops.iter(), skip_bytes) { match op { // Changing color Op::Color(color) => { - self.layout.style.text_color = color; + layout.style.text_color = color; } // Changing font Op::Font(font) => { - self.layout.style.text_font = font; + layout.style.text_font = font; } // Changing line/text alignment Op::Alignment(line_alignment) => { - self.layout.align = line_alignment; + layout.align = line_alignment; } // Changing line breaking Op::LineBreaking(line_breaking) => { - self.layout.style.line_breaking = line_breaking; + layout.style.line_breaking = line_breaking; } // Moving the cursor Op::CursorOffset(offset) => { @@ -91,10 +92,10 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { cursor.y += offset.y; } Op::Chunkify(chunks) => { - self.layout.style.chunks = chunks; + layout.style.chunks = chunks; } Op::LineSpacing(line_spacing) => { - self.layout.style.line_spacing = line_spacing; + layout.style.line_spacing = line_spacing; } // Moving to the next page Op::NextPage => { @@ -103,7 +104,7 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { total_processed_chars += PROCESSED_CHARS_ONE; return LayoutFit::OutOfBounds { processed_chars: total_processed_chars, - height: self.layout.layout_height(init_cursor, *cursor), + height: layout.layout_height(init_cursor, *cursor), }; } // Drawing text @@ -113,9 +114,9 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { // Inserting the ellipsis at the very beginning of the text if needed // (just for incomplete texts that were separated) - self.layout.continues_from_prev_page = continued; + layout.continues_from_prev_page = continued; - let fit = self.layout.layout_text(text.as_ref(), cursor, sink); + let fit = layout.layout_text(text.as_ref(), cursor, sink); match fit { LayoutFit::Fitting { @@ -130,7 +131,7 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { return LayoutFit::OutOfBounds { processed_chars: total_processed_chars, - height: self.layout.layout_height(init_cursor, *cursor), + height: layout.layout_height(init_cursor, *cursor), }; } } @@ -140,7 +141,7 @@ impl<'a, T: StringType + Clone + 'a> OpTextLayout { LayoutFit::Fitting { processed_chars: total_processed_chars, - height: self.layout.layout_height(init_cursor, *cursor), + height: layout.layout_height(init_cursor, *cursor), } } diff --git a/core/embed/rust/src/ui/model_tr/component/flow_pages.rs b/core/embed/rust/src/ui/model_tr/component/flow_pages.rs index 62f8b7314..25eeb91e8 100644 --- a/core/embed/rust/src/ui/model_tr/component/flow_pages.rs +++ b/core/embed/rust/src/ui/model_tr/component/flow_pages.rs @@ -243,7 +243,7 @@ where t.int("active_page", self.current_page as i64); t.int("page_count", self.page_count as i64); t.in_list("text", &|l| { - let result = self.formatted.layout_content_debug(&mut TraceSink(l)); + let result = self.formatted.layout_content(&mut TraceSink(l)); fit.set(Some(result)); }); }