Skip to content

ui::ColorAnimator

Header: winui/ui/Animation.hpp.

Runs a BrushTransition for a control that owns a background. It remembers the color on screen when the background changed and blends from it to the new one. It animates only between two colors. A background appearing or disappearing changes at once.

Example:

class Swatch : public ui::Element
{
    // Changing color: remember what is on screen now, then blend to the new one.
    void set_color(ui::Color c)
    {
        animator_.retarget(animator_.current(color_), c, ui::BrushTransition{});
        color_ = c;
        mark_dirty(ui::DirtyFlags::Visual);
    }
    void tick_override(ui::Duration delta) override
    {
        if (animator_.advance(delta))
            mark_dirty(ui::DirtyFlags::Visual);
    }
    void render_override(ui::Renderer& r, const ui::RenderContext&) const override
    {
        if (const auto c = animator_.current(color_))
            r.fill_rounded_rect(bounds(), 4.0f, *c);
    }
    // measure_override() etc. omitted

    std::optional<ui::Color> color_;
    ui::ColorAnimator        animator_;
};

retarget

void retarget(std::optional<Color> from, std::optional<Color> to, const std::optional<BrushTransition>& transition)

The background is changing from from (what current() draws right now) to to. Starts a blend only when transition is set, both colors are set and differ, and animations are on; otherwise stops any running blend so the new color shows at once.

advance

bool advance(Duration delta)

Advances the blend by delta; returns true while blending (including the step that finishes it), so the owner knows to repaint.

current

std::optional<Color> current(const std::optional<Color>& target) const

The color to draw for a background whose value is target: the blend from the remembered color while active, target itself otherwise.

active

bool active() const

Whether a blend is in progress.