cache text buf
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
use cosmic_text::{Attrs, Family, FontSystem, Metrics, Shaping};
|
use cosmic_text::{Attrs, Family, FontSystem, Metrics, Shaping};
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::{prelude::*, util::MutDetect};
|
||||||
|
|
||||||
pub struct Text {
|
pub struct Text {
|
||||||
pub content: String,
|
pub content: MutDetect<String>,
|
||||||
pub attrs: TextAttrs,
|
pub attrs: TextAttrs,
|
||||||
/// inner alignment of text region (within where its drawn)
|
/// inner alignment of text region (within where its drawn)
|
||||||
pub align: Align,
|
pub align: Align,
|
||||||
@@ -32,7 +32,7 @@ impl Text {
|
|||||||
pub fn new(content: impl Into<String>) -> Self {
|
pub fn new(content: impl Into<String>) -> Self {
|
||||||
let attrs = TextAttrs::default();
|
let attrs = TextAttrs::default();
|
||||||
Self {
|
Self {
|
||||||
content: content.into(),
|
content: content.into().into(),
|
||||||
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
|
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
|
||||||
attrs,
|
attrs,
|
||||||
align: Align::Center,
|
align: Align::Center,
|
||||||
@@ -49,12 +49,15 @@ impl Text {
|
|||||||
font_system,
|
font_system,
|
||||||
Metrics::new(self.attrs.font_size, self.attrs.line_height),
|
Metrics::new(self.attrs.font_size, self.attrs.line_height),
|
||||||
);
|
);
|
||||||
self.buf.set_text(
|
if self.content.changed {
|
||||||
font_system,
|
self.content.changed = false;
|
||||||
&self.content,
|
self.buf.set_text(
|
||||||
&Attrs::new().family(self.attrs.family),
|
font_system,
|
||||||
Shaping::Advanced,
|
&self.content,
|
||||||
);
|
&Attrs::new().family(self.attrs.family),
|
||||||
|
Shaping::Advanced,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
src/util/change.rs
Normal file
30
src/util/change.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
pub struct MutDetect<T> {
|
||||||
|
inner: T,
|
||||||
|
pub changed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for MutDetect<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> DerefMut for MutDetect<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
self.changed = true;
|
||||||
|
&mut self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<T> for MutDetect<T> {
|
||||||
|
fn from(inner: T) -> Self {
|
||||||
|
MutDetect {
|
||||||
|
inner,
|
||||||
|
changed: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
mod borrow;
|
mod borrow;
|
||||||
|
mod change;
|
||||||
mod id;
|
mod id;
|
||||||
mod math;
|
mod math;
|
||||||
mod refcount;
|
mod refcount;
|
||||||
|
|
||||||
pub(crate) use borrow::*;
|
pub(crate) use borrow::*;
|
||||||
|
pub use change::*;
|
||||||
pub(crate) use id::*;
|
pub(crate) use id::*;
|
||||||
pub(crate) use math::*;
|
pub(crate) use math::*;
|
||||||
pub(crate) use refcount::*;
|
pub(crate) use refcount::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user