From 949c9df0a0bf6aa5c56e5045eff040927f3757be Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Sat, 20 Sep 2025 12:49:55 -0400 Subject: [PATCH] cache text buf --- src/core/text.rs | 21 ++++++++++++--------- src/util/change.rs | 30 ++++++++++++++++++++++++++++++ src/util/mod.rs | 2 ++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/util/change.rs diff --git a/src/core/text.rs b/src/core/text.rs index 4437f53..ba1bb69 100644 --- a/src/core/text.rs +++ b/src/core/text.rs @@ -1,9 +1,9 @@ use cosmic_text::{Attrs, Family, FontSystem, Metrics, Shaping}; -use crate::prelude::*; +use crate::{prelude::*, util::MutDetect}; pub struct Text { - pub content: String, + pub content: MutDetect, pub attrs: TextAttrs, /// inner alignment of text region (within where its drawn) pub align: Align, @@ -32,7 +32,7 @@ impl Text { pub fn new(content: impl Into) -> Self { let attrs = TextAttrs::default(); Self { - content: content.into(), + content: content.into().into(), buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)), attrs, align: Align::Center, @@ -49,12 +49,15 @@ impl Text { font_system, Metrics::new(self.attrs.font_size, self.attrs.line_height), ); - self.buf.set_text( - font_system, - &self.content, - &Attrs::new().family(self.attrs.family), - Shaping::Advanced, - ); + if self.content.changed { + self.content.changed = false; + self.buf.set_text( + font_system, + &self.content, + &Attrs::new().family(self.attrs.family), + Shaping::Advanced, + ); + } } } diff --git a/src/util/change.rs b/src/util/change.rs new file mode 100644 index 0000000..f1a01bd --- /dev/null +++ b/src/util/change.rs @@ -0,0 +1,30 @@ +use std::ops::{Deref, DerefMut}; + +pub struct MutDetect { + inner: T, + pub changed: bool, +} + +impl Deref for MutDetect { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl DerefMut for MutDetect { + fn deref_mut(&mut self) -> &mut Self::Target { + self.changed = true; + &mut self.inner + } +} + +impl From for MutDetect { + fn from(inner: T) -> Self { + MutDetect { + inner, + changed: true, + } + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index f465f42..411a294 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,9 +1,11 @@ mod borrow; +mod change; mod id; mod math; mod refcount; pub(crate) use borrow::*; +pub use change::*; pub(crate) use id::*; pub(crate) use math::*; pub(crate) use refcount::*;