1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-20 17:19:01 +00:00

refactor(core/rust): force recalculate Paragraphs visibility when updating content

This commit is contained in:
matejcik 2025-02-01 14:28:04 +01:00 committed by Vít Obrusník
parent 2982f6db37
commit 741731638b
11 changed files with 44 additions and 32 deletions

View File

@ -86,8 +86,10 @@ where
&self.source
}
pub fn inner_mut(&mut self) -> &mut T {
&mut self.source
pub fn mutate<R>(&mut self, func: impl Fn(&mut T) -> R) -> R {
let result = func(&mut self.source);
self.recalculate_pages();
result
}
pub fn area(&self) -> Rect {
@ -186,6 +188,27 @@ where
chr = 0;
}
}
fn recalculate_pages(&mut self) {
if self.area.is_empty() {
return;
}
let total_pages = self.break_pages_from_start().count().max(1);
self.pager = Pager::new(total_pages as u16);
self.offset = PageOffset::default();
self.change_offset(self.offset);
}
}
impl<'a> Paragraphs<Paragraph<'a>> {
pub fn content(&self) -> &TString<'a> {
self.source.content()
}
pub fn update<T: Into<TString<'a>>>(&mut self, content: T) {
self.source.update(content);
self.recalculate_pages();
}
}
impl<'a, T> Component for Paragraphs<T>
@ -198,11 +221,8 @@ where
let recalc = self.area != bounds;
self.area = bounds;
if recalc {
self.offset = PageOffset::default();
let total_pages = self.break_pages_from_start().count().max(1);
self.pager = Pager::new(total_pages as u16);
self.recalculate_pages();
}
self.change_offset(self.offset);
self.area
}

View File

@ -99,10 +99,9 @@ impl AddressDetails {
let mut dummy_ctx = EventCtx::new();
self.xpub_view.update_title(&mut dummy_ctx, self.xpubs[i].0);
self.xpub_view.update_content(&mut dummy_ctx, |p| {
p.inner_mut().update(self.xpubs[i].1);
let npages = p.page_count();
p.update(self.xpubs[i].1);
p.change_page(page);
npages
p.page_count()
})
}

View File

@ -116,7 +116,9 @@ where
pub fn with_paragraph(mut self, para: Paragraph<'static>) -> Self {
if !para.content().is_empty() {
self.paragraphs.inner_mut().add(para);
self.paragraphs.mutate(|p| {
p.add(para);
});
}
self
}

View File

@ -56,10 +56,8 @@ where
fn update_text(&mut self, ctx: &mut EventCtx, value: u32) {
let text = (self.description_func)(value);
self.paragraphs.mutate(ctx, move |ctx, para| {
para.inner_mut().update(text);
// Recompute bounding box.
para.change_page(0);
self.paragraphs.mutate(ctx, |ctx, p| {
p.update(text);
ctx.request_paint()
});
self.paragraphs_pad.clear();

View File

@ -81,9 +81,8 @@ impl Component for Progress {
ctx.request_paint();
}
self.description.mutate(ctx, |ctx, para| {
if para.inner_mut().content() != &new_description {
para.inner_mut().update(new_description);
para.change_page(0); // Recompute bounding box.
if para.content() != &new_description {
para.update(new_description);
ctx.request_paint();
self.description_pad.clear();
}

View File

@ -158,7 +158,7 @@ impl AddressDetails {
let i = self.current_page - 2;
self.xpub_view.update_title(ctx, self.xpubs[i].0);
self.xpub_view.update_content(ctx, |p| {
p.inner_mut().update(self.xpubs[i].1);
p.update(self.xpubs[i].1);
p.change_page(0)
});
}

View File

@ -4,7 +4,6 @@ use crate::{
strutil::TString,
ui::{
component::{
paginated::Paginate,
text::paragraphs::{Paragraph, Paragraphs},
Child, Component, Event, EventCtx, Label, Never, Pad,
},
@ -119,9 +118,8 @@ impl Component for Progress {
self.description.mutate(ctx, |ctx, para| {
// NOTE: not doing any change for empty new descriptions
// (currently, there is no use-case for deleting the description)
if !new_description.is_empty() && para.inner_mut().content() != &new_description {
para.inner_mut().update(new_description);
para.change_page(0); // Recompute bounding box.
if !new_description.is_empty() && para.content() != &new_description {
para.update(new_description);
ctx.request_paint();
self.description_pad.clear();
}

View File

@ -92,10 +92,9 @@ impl AddressDetails {
let mut dummy_ctx = EventCtx::new();
self.xpub_view.update_title(&mut dummy_ctx, self.xpubs[i].0);
self.xpub_view.update_content(&mut dummy_ctx, |_ctx, p| {
p.inner_mut().update(self.xpubs[i].1);
let npages = p.pager().total() as usize;
p.update(self.xpubs[i].1);
p.change_page(page);
npages
p.pager().total() as usize
})
}

View File

@ -64,7 +64,7 @@ impl<F: Fn() -> TString<'static>> Component for FidoCredential<F> {
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
if let Event::Attach(_) = event {
self.text.inner_mut()[1].update((self.get_account)());
self.text.mutate(|p| p[1].update((self.get_account)()));
ctx.request_paint();
}
self.app_icon.event(ctx, event);

View File

@ -4,7 +4,6 @@ use crate::{
strutil::TString,
ui::{
component::{
paginated::Paginate,
text::paragraphs::{Paragraph, Paragraphs},
Component, Event, EventCtx, Label, Never, Pad,
},
@ -77,9 +76,8 @@ impl Component for Progress {
if !animation_disabled() {
ctx.request_paint();
}
if self.description.inner_mut().content() != &new_description {
self.description.inner_mut().update(new_description);
self.description.change_page(0); // Recompute bounding box.
if self.description.content() != &new_description {
self.description.update(new_description);
ctx.request_paint();
self.description_pad.clear();
}

View File

@ -37,8 +37,7 @@ where
fn update_text(&mut self, ctx: &mut EventCtx) {
let text = (self.info_func)();
self.paragraphs.inner_mut().update(text);
self.paragraphs.change_page(0);
self.paragraphs.update(text);
ctx.request_paint();
}
}