Skip to main content

TextEdit

Struct TextEdit 

Source
pub struct TextEdit { /* private fields */ }
Expand description

A handle of a text edit, returned by the text shape’s editor methods (content, append, prepend, insert, rewrite).

The edit is already applied (the committed text is updated immediately, like ordinary setters), and the handle itself dereferences into Shape — in the static builder chain it behaves like a shape. To turn the edit into a transition on the timeline, append an animation: spawn (instant), typing (per-character typing) or smooth (a crossfade with a width morph).

let tl = Timeline::new(320, 120, Color::BLACK, 30.0);
let title = Shape::text("").on(&tl);
// Type "Hello" over a second, then smoothly change to "Bye".
tl.sequence(vec![
    title.content("Hello").typing(1.0, Easing::Linear),
    title.content("Bye").smooth(0.5, Easing::CubicInOut),
]);

Implementations§

Source§

impl TextEdit

Source

pub fn content(self, content: impl Into<String>) -> TextEdit

Fully replaces the content (see Shape::content), continuing this handle’s edit chain while preserving the original “from”.

Source

pub fn append(self, content: impl Into<String>) -> TextEdit

Appends content to the end (see Shape::append), continuing this handle’s edit chain.

Source

pub fn prepend(self, content: impl Into<String>) -> TextEdit

Inserts content at the beginning (see Shape::prepend), continuing this handle’s edit chain.

Source

pub fn insert(self, char_index: usize, content: impl Into<String>) -> TextEdit

Inserts content before character char_index (see Shape::insert), continuing this handle’s edit chain.

Source

pub fn rewrite( self, from: impl Into<TextPos>, to: impl Into<TextPos>, content: impl Into<String>, ) -> TextEdit

Replaces the range [from, to) (see Shape::rewrite), continuing this handle’s edit chain.

Source

pub fn spawn(self) -> Action

An instant spawn: the new text appears immediately at this moment of the timeline. It has no duration, so over/easing are not needed; before the moment the previous text is shown, after it — the new one.

Source

pub fn typing(self, over: f64, easing: Easing) -> Action

Typing: characters appear one by one over over seconds (the speed is set by the duration). The prefix already shared with the previous text stays in place — only the added “tail” is typed; the block’s width expands as it is typed.

Source

pub fn smooth(self, over: f64, easing: Easing) -> Action

Smoothing (as in Motion Canvas): over over seconds the old text fades to zero in the first half, the new one appears in the second half; all the while the space for the text morphs from the old width to the new.

Methods from Deref<Target = Shape>§

Source

pub fn at(&self, x: f32, y: f32) -> Self

Sets the position of the top-left corner. For a single axis (including animation) use x / y.

Source

pub fn size(&self, w: f32, h: f32) -> Self

Sets explicit sizes. A value <= 0 leaves the axis on “auto”. For a single axis (including animation) use width / height.

Source

pub fn direction(&self, d: Direction) -> Self

The children’s layout axis.

Source

pub fn justify(&self, j: Justify) -> Self

Distribution along the main axis.

Source

pub fn align(&self, a: Align) -> Self

Alignment along the cross axis.

Source

pub fn child(&self, c: impl Into<Shape>) -> Self

Adds a child shape. Accepts both Shape and property handles (Tween, PaddingTween) — they dereference into a shape.

Source

pub fn children<C: IntoChildren>(&self, cs: C) -> Self

Adds child shapes. Thanks to IntoChildren it accepts both one nested shape (including the property handles Tween/PaddingTween) and a collection/iterator of any Into<Shape> — so it suits both nesting one ready-made group into another and adding a list:

let group = Shape::rect().children(vec![Shape::circle().size(8.0, 8.0)]);
// Nest a ready-made group as the single child:
let window = Shape::rect().children(group);
Source

pub fn on(&self, tl: &Timeline) -> Self

Registers the shape on the timeline tl for drawing and returns it, to stay in the fluent chain. Since a shape is an Rc, the timeline holds only a reference, and properties can still be animated and read.

At the same time the whole subgraph (the shape itself and all its children at the moment of the call) remembers this timeline, so an animation built from any of them can be added by simply writing it as an expression — without [sequence]/[parallel]:

let tl = Timeline::new(320, 160, Color::BLACK, 30.0);
let box_ = Shape::rect().size(40.0, 40.0).on(&tl);
// A single animation appends itself to the end of the timeline:
box_.x(200.0).over(1.0, Easing::CubicInOut);
// Several simultaneous ones — still via parallel:
tl.parallel(vec![box_.y(40.0).over(1.0, Easing::CubicInOut)]);
Source

pub fn x(&self, value: f32) -> Tween<f32>

X coordinate. x(100.0) sets it immediately; x(100.0).over(1.0, Easing::CubicInOut) animates.

Source

pub fn y(&self, value: f32) -> Tween<f32>

Y coordinate. See x.

Source

pub fn width(&self, value: Length) -> Tween<f32>

Width — as a Length value: pixels (Length::pixel, <= 0 — “auto”) or a fraction of the parent’s content area (Length::percent, Length::percent(100.0) — 100%). A pixel width can be animated via over (see x), and it cancels any previously set fraction; a fraction, on the other hand, is set instantly (not animated), resolved on the second layout pass relative to the parent and overrides the pixel width (clamped by min_width/max_width).

Source

pub fn height(&self, value: Length) -> Tween<f32>

Height — as a Length value (pixels or a fraction of the parent). See width.

Source

pub fn min_width(&self, value: f32) -> Tween<f32>

Lower bound of the width (<= 0 — no limit): the final width does not drop below it. On conflict with max_width the minimum takes priority (as in CSS). Animatable. See x.

Source

pub fn max_width(&self, value: f32) -> Tween<f32>

Upper bound of the width (<= 0 — no limit): the final width does not exceed it. Animatable. See min_width.

Source

pub fn min_height(&self, value: f32) -> Tween<f32>

Lower bound of the height (<= 0 — no limit). Animatable. See min_width.

Source

pub fn max_height(&self, value: f32) -> Tween<f32>

Upper bound of the height (<= 0 — no limit). Animatable. See min_width.

Source

pub fn background(&self, value: Color) -> Tween<Color>

Background color. See x.

Source

pub fn radius(&self, value: f32) -> Tween<f32>

Corner radius. See x.

Source

pub fn opacity(&self, value: f32) -> Tween<f32>

Opacity 0..=1 (multiplied by the parent’s opacity). See x.

Source

pub fn rotation(&self, value: f32) -> Tween<f32>

Rotation around the center in degrees. See x.

Source

pub fn scale(&self, value: f32) -> Tween<f32>

Scale around the center (1.0 — no change). Applies to the whole subtree, like rotation. See x.

Source

pub fn gap(&self, value: f32) -> Tween<f32>

Gap between children. See x.

Source

pub fn pad_top(&self, value: f32) -> Tween<f32>

Top inner padding. See x.

Source

pub fn pad_right(&self, value: f32) -> Tween<f32>

Right inner padding. See x.

Source

pub fn pad_bottom(&self, value: f32) -> Tween<f32>

Bottom inner padding. See x.

Source

pub fn pad_left(&self, value: f32) -> Tween<f32>

Left inner padding. See x.

Source

pub fn padding<P: Into<Padding>>(&self, value: P) -> PaddingTween

Inner padding. Accepts the CSS-like shorthands Padding:

  • padding(16.0) — the same on all sides;
  • padding((10.0, 20.0))(vertical, horizontal);
  • padding((5.0, 10.0, 15.0, 20.0))(top, right, bottom, left).

Sets the padding immediately and returns a PaddingTween: append over to animate it, e.g. padding(24.0).over(1.0, Easing::CubicInOut).

Source

pub fn content(&self, content: impl Into<String>) -> TextEdit

Fully replaces the text content with new content. Lines are separated by \n.

Sets the text immediately and returns a TextEdit: append spawn / typing / smooth to animate the change on the timeline.

Panics if called on a non-text shape (see Shape::text).

Source

pub fn append(&self, content: impl Into<String>) -> TextEdit

Appends content to the end of the current content.

Returns a TextEdit (see content). When animated with typing, only the added “tail” is typed.

Panics if called on a non-text shape.

Source

pub fn prepend(&self, content: impl Into<String>) -> TextEdit

Inserts content at the beginning of the current content.

Returns a TextEdit (see content).

Panics if called on a non-text shape.

Source

pub fn insert(&self, char_index: usize, content: impl Into<String>) -> TextEdit

Inserts content before the character at index char_index (0-based, clamped to [0, length]).

Returns a TextEdit (see content).

Panics if called on a non-text shape.

Source

pub fn rewrite( &self, from: impl Into<TextPos>, to: impl Into<TextPos>, content: impl Into<String>, ) -> TextEdit

Replaces the content of the half-open range [from, to) with content.

The bounds are TextPos: a bare character index (0-based, via Into<TextPos>), the start of a line line(n) or the end of the text infinite() (for to). The range is half-open: the character at position to is not included in the replacement.

// "foo\nbar" → replace the whole first line (with its `\n`) with "X":
let t = Shape::text("foo\nbar").rewrite(0, line(1), "X");

Returns a TextEdit (see content). Panics if called on a non-text shape.

Source

pub fn font(&self, bytes: impl Into<Rc<Vec<u8>>>) -> Self

Sets the font from the bytes of a .ttf/.otf file (CSS font-family). Accepts a Vec<u8> or a shared Rc<Vec<u8>> — the latter is handy for sharing one font across several texts without copying the bytes.

Panics if called on a non-text shape.

Source

pub fn font_collection(&self, bytes: impl Into<Rc<Vec<u8>>>, index: u32) -> Self

Sets the font from the bytes of a collection (.ttc), selecting the face by index. For a regular single-font file use font.

Panics if called on a non-text shape.

Source

pub fn text_align(&self, align: TextAlign) -> Self

Alignment of lines within the block (CSS text-align).

Panics if called on a non-text shape.

Source

pub fn font_size(&self, value: f32) -> Tween<f32>

Font size in pixels (CSS font-size). Animatable. See x.

Panics if called on a non-text shape.

Source

pub fn color(&self, value: Color) -> Tween<Color>

Glyph fill color (CSS color). Animatable. See x.

Panics if called on a non-text shape or on a code shape (code has no single color — configure the highlighting via palette).

Source

pub fn palette(&self, palette: Palette) -> Self

The code shape’s highlight palette (see Palette). Set instantly and returns the shape itself to continue the chain.

Panics if called on a non-code shape (see Shape::code).

Source

pub fn language(&self, language: Language) -> Self

The code shape’s highlight language (see Language). Set instantly and returns the shape itself.

Panics if called on a non-code shape (see Shape::code).

Source

pub fn letter_spacing(&self, value: f32) -> Tween<f32>

Letter spacing — extra gap between characters in pixels (CSS letter-spacing). Animatable. See x.

Panics if called on a non-text shape.

Source

pub fn line_height(&self, value: f32) -> Tween<f32>

Line spacing as a multiplier of the font’s natural line height (CSS line-height, 1.0 — no change). Animatable. See x.

Panics if called on a non-text shape.

Source

pub fn highlight( &self, from: impl Into<TextPos>, to: impl Into<TextPos>, ) -> HighlightEdit

Marks the highlighted character range [from, to) and returns a HighlightEdit — this is a timeline animation, not a static property. Highlighted glyphs are drawn at full strength, the rest are dimmed.

Append over to highlight smoothly over a given time; without it the edit is applied immediately. Unlike .selection in Motion Canvas, there can be any number of ranges — highlight each with its own highlight(..).over(..) in a single parallel: they merge into one consistent transition. The highlighting is removed with clear_highlight.

The bounds are TextPos: a bare character index (0-based, via Into<TextPos>), the start of a line line(n) or the end of the text infinite(). The range is half-open: the character at position to is not highlighted.

let tl = Timeline::new(320, 120, Color::BLACK, 30.0);
let code = Shape::code("let answer = 42;").on(&tl);
// Highlight "42" over half a second, dimming the rest:
code.highlight(13, 15).over(0.5, Easing::CubicInOut);

Works on both text and code (colors are preserved, only opacity changes). Panics if called on a non-text shape (see Shape::text).

Source

pub fn clear_highlight(&self) -> HighlightEdit

Removes the highlighting (see highlight): returns a HighlightEdit whose over smoothly returns the whole text to full strength; without over the highlighting is removed immediately.

Panics if called on a non-text shape.

Source

pub fn font_size_signal(&self) -> Signal<f32>

The font-size signal. Panics if called on a non-text shape.

Source

pub fn color_signal(&self) -> Signal<Color>

The text-color signal. Panics if called on a non-text shape.

Source

pub fn letter_spacing_signal(&self) -> Signal<f32>

The letter-spacing signal. Panics if called on a non-text shape.

Source

pub fn line_height_signal(&self) -> Signal<f32>

The line-height signal. Panics if called on a non-text shape.

Source

pub fn x_signal(&self) -> Signal<f32>

The X-coordinate signal.

Source

pub fn y_signal(&self) -> Signal<f32>

The Y-coordinate signal.

Source

pub fn width_signal(&self) -> Signal<f32>

The width signal.

Source

pub fn height_signal(&self) -> Signal<f32>

The height signal.

Source

pub fn min_width_signal(&self) -> Signal<f32>

The width lower-bound signal.

Source

pub fn max_width_signal(&self) -> Signal<f32>

The width upper-bound signal.

Source

pub fn min_height_signal(&self) -> Signal<f32>

The height lower-bound signal.

Source

pub fn max_height_signal(&self) -> Signal<f32>

The height upper-bound signal.

Source

pub fn background_signal(&self) -> Signal<Color>

The background-color signal.

Source

pub fn radius_signal(&self) -> Signal<f32>

The corner-radius signal.

Source

pub fn opacity_signal(&self) -> Signal<f32>

The opacity signal.

Source

pub fn rotation_signal(&self) -> Signal<f32>

The rotation signal (degrees).

Source

pub fn scale_signal(&self) -> Signal<f32>

The scale signal.

Source

pub fn gap_signal(&self) -> Signal<f32>

The child-gap signal.

Source

pub fn pad_top_signal(&self) -> Signal<f32>

The top inner-padding signal.

Source

pub fn pad_right_signal(&self) -> Signal<f32>

The right inner-padding signal.

Source

pub fn pad_bottom_signal(&self) -> Signal<f32>

The bottom inner-padding signal.

Source

pub fn pad_left_signal(&self) -> Signal<f32>

The left inner-padding signal.

Trait Implementations§

Source§

impl Deref for TextEdit

Source§

type Target = Shape

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Shape

Dereferences the value.
Source§

impl From<TextEdit> for Shape

Source§

fn from(t: TextEdit) -> Shape

Converts to this type from the input type.
Source§

impl IntoChildren for TextEdit

Source§

fn into_children(self) -> Vec<Shape>

Unfolds the value into a list of child shapes.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.