1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
// Copyright 2020 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Storing text.
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 {
/// If this TextStorage object manages style spans, it should implement
/// this method and update the provided builder with its spans, as required.
#[allow(unused_variables)]
fn add_attributes(&self, builder: PietTextLayoutBuilder, env: &Env) -> PietTextLayoutBuilder {
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
/// method and return any attached [`Link`]s.
///
/// Unlike other attributes, links are managed in Druid, not in [`piet`]; as such they
/// require a separate API.
///
/// [`Link`]: super::attribute::Link
/// [`piet`]: crate::piet
fn links(&self) -> &[Link] {
&[]
}
}
/// Provides information about keys change for more fine grained invalidation
pub struct EnvUpdateCtx<'a, 'b>(&'a UpdateCtx<'a, 'b>);
impl<'a, 'b> EnvUpdateCtx<'a, 'b> {
/// Create an [`EnvUpdateCtx`] for [`Widget::update`].
///
/// [`Widget::update`]: crate::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.
///
/// [`env_update`]: TextStorage::env_update
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`
/// it cannot be mutated, but unlike `String` it can be cheaply cloned.
pub type ArcStr = Arc<str>;
impl TextStorage for ArcStr {}
impl TextStorage for String {}
impl TextStorage for Arc<String> {}