mirui 0.20.3

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
Documentation
//! Frosted-glass blur of the framebuffer pixels behind this widget.
//! Must come after the content it blurs in children-array order;
//! skips silently when the renderer can't sample its own target.

use crate::draw::command::DrawCommand;
use crate::draw::renderer::Renderer;
use crate::draw::sw::blur::{alpha_for_radius, iir_blur_inplace};
use crate::ecs::{Entity, World};
use crate::types::{Point, Rect};
use crate::widget::view::{View, ViewCtx};

pub struct BackgroundBlur {
    pub radius: u8,
}

impl BackgroundBlur {
    pub fn new(radius: u8) -> Self {
        Self { radius }
    }
}

fn background_blur_render(
    renderer: &mut dyn Renderer,
    world: &World,
    entity: Entity,
    rect: &Rect,
    ctx: &mut ViewCtx,
) {
    let Some(bg) = world.get::<BackgroundBlur>(entity) else {
        return;
    };
    if bg.radius == 0 {
        return;
    }

    // Animated transforms must sample the on-screen rect, not the
    // layout rect, or the blur source freezes at the original spot.
    let sample_rect = ctx.transform.apply_rect_bbox(*rect);
    let Some(mut tmp) = renderer.sample_target_region(&sample_rect) else {
        return;
    };
    iir_blur_inplace(&mut tmp, alpha_for_radius(bg.radius));

    renderer.draw(
        &DrawCommand::Blit {
            pos: Point::new(rect.x, rect.y),
            size: Point::new(rect.w, rect.h),
            transform: ctx.transform,
            quad: ctx.quad,
            texture: &tmp,
        },
        ctx.clip,
    );
}

pub fn view() -> View {
    View::new("BackgroundBlur", 60, background_blur_render)
}