Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichText: Invalidate layout on Env change #1907

Merged
merged 4 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions druid/src/text/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::ops::Range;
use crate::piet::{Color, FontFamily, FontStyle, FontWeight, TextAttribute as PietAttr};
use crate::{Command, Env, FontDescriptor, KeyOrValue};

use super::EnvUpdateCtx;

/// A clickable range of text with an associated [`Command`].
#[derive(Debug, Clone)]
pub struct Link {
Expand Down Expand Up @@ -179,6 +181,16 @@ impl AttributeSpans {
items.sort_by(|a, b| a.0.start.cmp(&b.0.start));
items
}

pub(crate) fn env_update(&self, ctx: &EnvUpdateCtx) -> bool {
self.size
.iter()
.any(|span_attr| ctx.env_key_changed(&span_attr.attr))
|| self
.fg_color
.iter()
.any(|span_attr| ctx.env_key_changed(&span_attr.attr))
maan2003 marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<T: Clone> SpanSet<T> {
Expand Down
7 changes: 6 additions & 1 deletion druid/src/text/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use std::ops::Range;
use std::rc::Rc;

use super::{Link, TextStorage};
use super::{EnvUpdateCtx, Link, TextStorage};
use crate::kurbo::{Line, Point, Rect, Size};
use crate::piet::{
Color, PietText, PietTextLayout, Text as _, TextAlignment, TextAttribute, TextLayout as _,
Expand Down Expand Up @@ -347,6 +347,11 @@ impl<T: TextStorage> TextLayout<T> {
.text_size_override
.as_ref()
.map(|k| ctx.env_key_changed(k))
.unwrap_or(false)
|| self
.text
.as_ref()
.map(|text| text.env_update(&EnvUpdateCtx::for_update(ctx)))
.unwrap_or(false);

if rebuild {
Expand Down
2 changes: 1 addition & 1 deletion druid/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ pub use self::movement::movement;
pub use input_component::{EditSession, TextComponent};
pub use input_methods::ImeHandlerRef;
pub use rich_text::{AttributesAdder, RichText, RichTextBuilder};
pub use storage::{ArcStr, TextStorage};
pub use storage::{ArcStr, EnvUpdateCtx, TextStorage};

pub(crate) use input_methods::TextFieldRegistration;
6 changes: 5 additions & 1 deletion druid/src/text/rich_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::ops::{Range, RangeBounds};
use std::sync::Arc;

use super::attribute::Link;
use super::{Attribute, AttributeSpans, TextStorage};
use super::{Attribute, AttributeSpans, EnvUpdateCtx, TextStorage};
use crate::piet::{
util, Color, FontFamily, FontStyle, FontWeight, PietTextLayoutBuilder, TextLayoutBuilder,
TextStorage as PietTextStorage,
Expand Down Expand Up @@ -93,6 +93,10 @@ impl TextStorage for RichText {
builder
}

fn env_update(&self, ctx: &EnvUpdateCtx) -> bool {
self.attrs.env_update(ctx)
}

fn links(&self) -> &[Link] {
&self.links
}
Expand Down
27 changes: 27 additions & 0 deletions druid/src/text/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

use std::sync::Arc;

use crate::env::KeyLike;
use crate::piet::{PietTextLayoutBuilder, TextStorage as PietTextStorage};
use crate::{Data, Env};

use super::attribute::Link;
use crate::UpdateCtx;

/// A type that represents text that can be displayed.
pub trait TextStorage: PietTextStorage + Data {
Expand All @@ -30,6 +32,13 @@ pub trait TextStorage: PietTextStorage + Data {
builder
}

/// This is called whenever the Env changes and should return true
/// if the layout should be rebuilt.
#[allow(unused_variables)]
fn env_update(&self, ctx: &EnvUpdateCtx) -> bool {
false
}

/// Any additional [`Link`] attributes on this text.
///
/// If this `TextStorage` object manages link attributes, it should implement this
Expand All @@ -45,6 +54,24 @@ pub trait TextStorage: PietTextStorage + Data {
}
}

/// Provides information about keys change for more fine grained invalidation
pub struct EnvUpdateCtx<'a, 'b>(&'a UpdateCtx<'a, 'b>);
maan2003 marked this conversation as resolved.
Show resolved Hide resolved

impl<'a, 'b> EnvUpdateCtx<'a, 'b> {
/// Create an EnvChangeCtx for Widget::update
pub(crate) fn for_update(ctx: &'a UpdateCtx<'a, 'b>) -> Self {
Self(ctx)
}

/// Returns `true` if the given key has changed since the last [`env_update`]
/// call.
///
/// See [`UpdateCtx::env_key_changed`] for more details.
pub fn env_key_changed<T>(&self, key: &impl KeyLike<T>) -> bool {
self.0.env_key_changed(key)
}
}

/// A reference counted string slice.
///
/// This is a data-friendly way to represent strings in druid. Unlike `String`
Expand Down