repose-ui 0.31.24

UI widgets and libs for Repose
Documentation
#![allow(non_snake_case)]

use repose_core::*;

pub(crate) fn open_url(url: &str) {
    let _ = webbrowser::open(url);
}

/// Old focus ring rendering.
#[deprecated(
    since = "0.27.7",
    note = "Focus is shown via StateColors/StateElevation `focused`; the external ring is removed"
)]
#[allow(dead_code)]
pub(crate) fn push_focus_ring(scene: &mut Scene, rect: repose_core::Rect, radius_dp: [Dp; 4]) {
    let width = Dp(2.0).to_px();
    let offset = Dp(2.0).to_px();
    let expand = (offset + width).0;
    let inflated = repose_core::Rect {
        x: rect.x - expand,
        y: rect.y - expand,
        w: rect.w + 2.0 * expand,
        h: rect.h + 2.0 * expand,
    };
    let radius = clamp_radii_px(
        [
            radius_dp[0].to_px().0 + expand,
            radius_dp[1].to_px().0 + expand,
            radius_dp[2].to_px().0 + expand,
            radius_dp[3].to_px().0 + expand,
        ],
        inflated.w,
        inflated.h,
    );
    scene.nodes.push(SceneNode::Border {
        rect: inflated,
        brush: Brush::Solid(locals::theme().focus),
        width,
        radius,
    });
}

/// Focus ring radius (dp), taken from the full per-corner clip radius so the ring
/// matches the component shape (including asymmetric corners).
///
/// # Deprecated
/// The focus-ring visual is no longer drawn. Focus is shown via the focused
/// state layer instead..
#[deprecated(
    since = "0.27.7",
    note = "Focus is shown via StateColors/StateElevation `focused`; the external ring is removed"
)]
#[allow(dead_code)]
pub(crate) fn focus_radius(modifier: &Modifier) -> [Dp; 4] {
    modifier.clip_rounded.unwrap_or([Dp(6.0); 4])
}

/// Associate a `FocusRequester` (if present on the modifier) with the view.
pub(crate) fn set_focus_requester(modifier: &Modifier, view_id: u64) {
    if let Some(ref fr) = modifier.focus_requester {
        FocusManager::set_requester_target(fr, view_id);
    }
}

pub(crate) fn mul_alpha_color(c: Color, a: f32) -> Color {
    Color(c.0, c.1, c.2, ((c.3 as f32) * a).clamp(0.0, 255.0) as u8)
}
pub(crate) fn mul_alpha_brush(b: Brush, a: f32) -> Brush {
    match b {
        Brush::Solid(c) => Brush::Solid(mul_alpha_color(c, a)),
        Brush::Linear {
            start,
            end,
            start_color,
            end_color,
        } => Brush::Linear {
            start,
            end,
            start_color: mul_alpha_color(start_color, a),
            end_color: mul_alpha_color(end_color, a),
        },
        Brush::LinearNormalized {
            start,
            end,
            start_color,
            end_color,
        } => Brush::LinearNormalized {
            start,
            end,
            start_color: mul_alpha_color(start_color, a),
            end_color: mul_alpha_color(end_color, a),
        },
        Brush::Radial {
            center,
            radius,
            start_color,
            end_color,
        } => Brush::Radial {
            center,
            radius,
            start_color: mul_alpha_color(start_color, a),
            end_color: mul_alpha_color(end_color, a),
        },
        Brush::Sweep {
            center,
            start_color,
            end_color,
        } => Brush::Sweep {
            center,
            start_color: mul_alpha_color(start_color, a),
            end_color: mul_alpha_color(end_color, a),
        },
        _ => b,
    }
}

pub(crate) fn clamp_radius(r: f32, w: f32, h: f32) -> f32 {
    r.max(0.0).min(0.5 * w.max(0.0)).min(0.5 * h.max(0.0))
}
pub(crate) fn clamp_radii(r: [f32; 4], w: f32, h: f32) -> [f32; 4] {
    [
        clamp_radius(r[0], w, h),
        clamp_radius(r[1], w, h),
        clamp_radius(r[2], w, h),
        clamp_radius(r[3], w, h),
    ]
}
/// [`Px`]-typed variant of [`clamp_radii`] for paint-space radii.
pub(crate) fn clamp_radii_px(r: [f32; 4], w: f32, h: f32) -> [Px; 4] {
    let c = clamp_radii(r, w, h);
    [Px(c[0]), Px(c[1]), Px(c[2]), Px(c[3])]
}
/// [`Px`]-typed variant of [`max_radii`] for paint-space radii.
pub(crate) fn max_radii_px(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
    max_radii(a, b)
}
pub(crate) fn max_radii(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
    [
        a[0].max(b[0]),
        a[1].max(b[1]),
        a[2].max(b[2]),
        a[3].max(b[3]),
    ]
}