yandex-music-rs/src/engine/types.rs
2026-11-27 13:17:37 +03:00

1148 lines
32 KiB
Rust

use bytemuck::{Pod, Zeroable};
use std::ops::{Add, Div, Mul, Neg, Sub};
/// Straight (unpremultiplied) sRGB RGBA8 color.
///
/// Our Vulkan SDF shader (`shaders/ui_sdf.wgsl`) composites linearly in sRGB space
/// onto a UNORM swapchain (premultiplied output `vec4(rgb * a, a)` with `ONE / ONE_MINUS_SRC_ALPHA`),
/// matching Chromium's CSS `rgba(r, g, b, a)` compositing 1-to-1 with zero gamma inflation.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Pod, Zeroable)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const TRANSPARENT: Self = Self { r: 0, g: 0, b: 0, a: 0 };
pub const BLACK: Self = Self { r: 0, g: 0, b: 0, a: 255 };
pub const WHITE: Self = Self { r: 255, g: 255, b: 255, a: 255 };
pub const YELLOW: Self = Self { r: 255, g: 255, b: 0, a: 255 };
pub const RED: Self = Self { r: 255, g: 0, b: 0, a: 255 };
pub const GREEN: Self = Self { r: 0, g: 255, b: 0, a: 255 };
pub const BLUE: Self = Self { r: 0, g: 0, b: 255, a: 255 };
#[inline]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 255 }
}
#[inline]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 255 }
}
#[inline]
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
#[inline]
pub const fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
#[inline]
pub const fn from_gray(v: u8) -> Self {
Self { r: v, g: v, b: v, a: 255 }
}
/// Construct an opaque color from 24-bit `0xRRGGBB`.
#[inline]
pub const fn from_hex(hex: u32) -> Self {
Self {
r: ((hex >> 16) & 0xFF) as u8,
g: ((hex >> 8) & 0xFF) as u8,
b: (hex & 0xFF) as u8,
a: 255,
}
}
/// Parse `#RRGGBB`, `RRGGBB`, or `#RGB` into a `Color`.
pub fn from_hex_str(s: &str) -> Option<Self> {
let hex = s.trim().trim_start_matches('#');
if hex.len() == 6 {
let v = u32::from_str_radix(hex, 16).ok()?;
Some(Self::from_hex(v))
} else if hex.len() == 3 {
let mut chars = hex.chars();
let r = chars.next()?.to_digit(16)? as u8;
let g = chars.next()?.to_digit(16)? as u8;
let b = chars.next()?.to_digit(16)? as u8;
Some(Self::rgb(r * 17, g * 17, b * 17))
} else {
None
}
}
/// Convert HSL (`h_deg` in 0..360, `s` in 0..1, `l` in 0..1) to opaque sRGB `Color`.
pub fn from_hsl(h_deg: f32, s: f32, l: f32) -> Self {
let h = ((h_deg % 360.0) + 360.0) % 360.0;
let s = s.clamp(0.0, 1.0);
let l = l.clamp(0.0, 1.0);
let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
let m = l - c * 0.5;
let (r1, g1, b1) = if h < 60.0 {
(c, x, 0.0)
} else if h < 120.0 {
(x, c, 0.0)
} else if h < 180.0 {
(0.0, c, x)
} else if h < 240.0 {
(0.0, x, c)
} else if h < 300.0 {
(x, 0.0, c)
} else {
(c, 0.0, x)
};
Self::rgb(
((r1 + m).clamp(0.0, 1.0) * 255.0).round() as u8,
((g1 + m).clamp(0.0, 1.0) * 255.0).round() as u8,
((b1 + m).clamp(0.0, 1.0) * 255.0).round() as u8,
)
}
/// Convert to `(h_deg [0..360], s [0..1], l [0..1])`.
pub fn to_hsl(self) -> (f32, f32, f32) {
let r = self.r as f32 / 255.0;
let g = self.g as f32 / 255.0;
let b = self.b as f32 / 255.0;
let max = r.max(g).max(b);
let min = r.min(g).min(b);
let l = (max + min) * 0.5;
let d = max - min;
if d < 1e-5 {
return (0.0, 0.0, l);
}
let s = if l > 0.5 {
d / (2.0 - max - min)
} else {
d / (max + min)
};
let mut h = if (max - r).abs() < 1e-5 {
(g - b) / d + if g < b { 6.0 } else { 0.0 }
} else if (max - g).abs() < 1e-5 {
(b - r) / d + 2.0
} else {
(r - g) / d + 4.0
};
h *= 60.0;
(h, s, l)
}
#[inline]
pub const fn r(self) -> u8 {
self.r
}
#[inline]
pub const fn g(self) -> u8 {
self.g
}
#[inline]
pub const fn b(self) -> u8 {
self.b
}
#[inline]
pub const fn a(self) -> u8 {
self.a
}
#[inline]
pub const fn with_alpha(self, a: u8) -> Self {
Self { a, ..self }
}
#[inline]
pub fn with_alpha_f32(self, a: f32) -> Self {
Self {
a: (a.clamp(0.0, 1.0) * 255.0).round() as u8,
..self
}
}
#[inline]
pub fn scale_alpha(self, factor: f32) -> Self {
let a = ((self.a as f32) * factor.clamp(0.0, 1.0)).round() as u8;
Self { a, ..self }
}
#[inline]
pub fn lerp(self, other: Self, t: f32) -> Self {
let t = t.clamp(0.0, 1.0);
let inv = 1.0 - t;
Self {
r: (self.r as f32 * inv + other.r as f32 * t).round() as u8,
g: (self.g as f32 * inv + other.g as f32 * t).round() as u8,
b: (self.b as f32 * inv + other.b as f32 * t).round() as u8,
a: (self.a as f32 * inv + other.a as f32 * t).round() as u8,
}
}
#[inline]
pub const fn to_array(self) -> [u8; 4] {
[self.r, self.g, self.b, self.a]
}
/// Pack into little-endian RGBA8 `u32` consumed by `shaders/ui_sdf.wgsl`.
#[inline]
pub const fn to_packed_u32(self) -> u32 {
(self.r as u32)
| ((self.g as u32) << 8)
| ((self.b as u32) << 16)
| ((self.a as u32) << 24)
}
#[inline]
pub fn to_f32_rgba(self) -> [f32; 4] {
[
self.r as f32 / 255.0,
self.g as f32 / 255.0,
self.b as f32 / 255.0,
self.a as f32 / 255.0,
]
}
#[inline]
pub fn to_premul_f32(self) -> [f32; 4] {
let a = self.a as f32 / 255.0;
[
(self.r as f32 / 255.0) * a,
(self.g as f32 / 255.0) * a,
(self.b as f32 / 255.0) * a,
a,
]
}
}
/// CSS-accurate `rgba(r, g, b, a)` helper matching Chromium compositing 1-to-1 on our UNORM Vulkan swapchain.
#[inline]
pub const fn css_rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
Color { r, g, b, a }
}
/// 2D point in logical pixels (origin top-left, +X right, +Y down).
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, Pod, Zeroable)]
pub struct Pos2 {
pub x: f32,
pub y: f32,
}
impl Pos2 {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
#[inline]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
#[inline]
pub const fn to_vec2(self) -> Vec2 {
Vec2 { x: self.x, y: self.y }
}
#[inline]
pub fn distance(self, other: Self) -> f32 {
(self - other).length()
}
}
#[inline]
pub const fn pos2(x: f32, y: f32) -> Pos2 {
Pos2 { x, y }
}
/// 2D vector / size in logical pixels.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, Pod, Zeroable)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
impl Vec2 {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
#[inline]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
#[inline]
pub const fn splat(v: f32) -> Self {
Self { x: v, y: v }
}
#[inline]
pub fn length(self) -> f32 {
(self.x * self.x + self.y * self.y).sqrt()
}
#[inline]
pub fn length_sq(self) -> f32 {
self.x * self.x + self.y * self.y
}
#[inline]
pub fn min(self, other: Self) -> Self {
Self { x: self.x.min(other.x), y: self.y.min(other.y) }
}
#[inline]
pub fn max(self, other: Self) -> Self {
Self { x: self.x.max(other.x), y: self.y.max(other.y) }
}
}
#[inline]
pub const fn vec2(x: f32, y: f32) -> Vec2 {
Vec2 { x, y }
}
impl Add<Vec2> for Pos2 {
type Output = Pos2;
#[inline]
fn add(self, rhs: Vec2) -> Pos2 {
Pos2 { x: self.x + rhs.x, y: self.y + rhs.y }
}
}
impl Sub<Vec2> for Pos2 {
type Output = Pos2;
#[inline]
fn sub(self, rhs: Vec2) -> Pos2 {
Pos2 { x: self.x - rhs.x, y: self.y - rhs.y }
}
}
impl Sub<Pos2> for Pos2 {
type Output = Vec2;
#[inline]
fn sub(self, rhs: Pos2) -> Vec2 {
Vec2 { x: self.x - rhs.x, y: self.y - rhs.y }
}
}
impl Add for Vec2 {
type Output = Vec2;
#[inline]
fn add(self, rhs: Vec2) -> Vec2 {
Vec2 { x: self.x + rhs.x, y: self.y + rhs.y }
}
}
impl Sub for Vec2 {
type Output = Vec2;
#[inline]
fn sub(self, rhs: Vec2) -> Vec2 {
Vec2 { x: self.x - rhs.x, y: self.y - rhs.y }
}
}
impl Mul<f32> for Vec2 {
type Output = Vec2;
#[inline]
fn mul(self, rhs: f32) -> Vec2 {
Vec2 { x: self.x * rhs, y: self.y * rhs }
}
}
impl Div<f32> for Vec2 {
type Output = Vec2;
#[inline]
fn div(self, rhs: f32) -> Vec2 {
Vec2 { x: self.x / rhs, y: self.y / rhs }
}
}
impl Neg for Vec2 {
type Output = Vec2;
#[inline]
fn neg(self) -> Vec2 {
Vec2 { x: -self.x, y: -self.y }
}
}
/// Axis-aligned rectangle in logical pixels (`x, y, w, h`).
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, Pod, Zeroable)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}
impl Rect {
pub const ZERO: Self = Self { x: 0.0, y: 0.0, w: 0.0, h: 0.0 };
pub const UNIT_UV: Self = Self { x: 0.0, y: 0.0, w: 1.0, h: 1.0 };
#[inline]
pub const fn from_xywh(x: f32, y: f32, w: f32, h: f32) -> Self {
Self { x, y, w, h }
}
#[inline]
pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
Self { x, y, w, h }
}
#[inline]
pub const fn from_min_size(min: Pos2, size: Vec2) -> Self {
Self { x: min.x, y: min.y, w: size.x, h: size.y }
}
#[inline]
pub fn from_min_max(min: Pos2, max: Pos2) -> Self {
Self {
x: min.x,
y: min.y,
w: (max.x - min.x).max(0.0),
h: (max.y - min.y).max(0.0),
}
}
#[inline]
pub fn from_center_size(center: Pos2, size: Vec2) -> Self {
Self {
x: center.x - size.x * 0.5,
y: center.y - size.y * 0.5,
w: size.x,
h: size.y,
}
}
#[inline]
pub const fn left(self) -> f32 {
self.x
}
#[inline]
pub const fn top(self) -> f32 {
self.y
}
#[inline]
pub const fn right(self) -> f32 {
self.x + self.w
}
#[inline]
pub const fn bottom(self) -> f32 {
self.y + self.h
}
#[inline]
pub const fn width(self) -> f32 {
self.w
}
#[inline]
pub const fn height(self) -> f32 {
self.h
}
#[inline]
pub const fn size(self) -> Vec2 {
Vec2 { x: self.w, y: self.h }
}
#[inline]
pub const fn min(self) -> Pos2 {
Pos2 { x: self.x, y: self.y }
}
#[inline]
pub const fn max(self) -> Pos2 {
Pos2 { x: self.x + self.w, y: self.y + self.h }
}
#[inline]
pub const fn left_top(self) -> Pos2 {
Pos2 { x: self.x, y: self.y }
}
#[inline]
pub const fn right_top(self) -> Pos2 {
Pos2 { x: self.x + self.w, y: self.y }
}
#[inline]
pub const fn left_bottom(self) -> Pos2 {
Pos2 { x: self.x, y: self.y + self.h }
}
#[inline]
pub const fn right_bottom(self) -> Pos2 {
Pos2 { x: self.x + self.w, y: self.y + self.h }
}
#[inline]
pub fn center(self) -> Pos2 {
Pos2 { x: self.x + self.w * 0.5, y: self.y + self.h * 0.5 }
}
#[inline]
pub fn center_x(self) -> f32 {
self.x + self.w * 0.5
}
#[inline]
pub fn center_y(self) -> f32 {
self.y + self.h * 0.5
}
#[inline]
pub fn is_empty(self) -> bool {
self.w <= 0.0 || self.h <= 0.0
}
#[inline]
pub fn contains(self, p: Pos2) -> bool {
p.x >= self.x && p.x <= self.right() && p.y >= self.y && p.y <= self.bottom()
}
#[inline]
pub fn intersects(self, other: Self) -> bool {
self.left() < other.right()
&& self.right() > other.left()
&& self.top() < other.bottom()
&& self.bottom() > other.top()
}
#[inline]
pub fn intersect(self, other: Self) -> Self {
let x0 = self.left().max(other.left());
let y0 = self.top().max(other.top());
let x1 = self.right().min(other.right());
let y1 = self.bottom().min(other.bottom());
Self {
x: x0,
y: y0,
w: (x1 - x0).max(0.0),
h: (y1 - y0).max(0.0),
}
}
#[inline]
pub fn union(self, other: Self) -> Self {
let x0 = self.left().min(other.left());
let y0 = self.top().min(other.top());
let x1 = self.right().max(other.right());
let y1 = self.bottom().max(other.bottom());
Self {
x: x0,
y: y0,
w: (x1 - x0).max(0.0),
h: (y1 - y0).max(0.0),
}
}
#[inline]
pub fn shrink(self, d: f32) -> Self {
Self {
x: self.x + d,
y: self.y + d,
w: (self.w - d * 2.0).max(0.0),
h: (self.h - d * 2.0).max(0.0),
}
}
#[inline]
pub fn shrink2(self, d: Vec2) -> Self {
Self {
x: self.x + d.x,
y: self.y + d.y,
w: (self.w - d.x * 2.0).max(0.0),
h: (self.h - d.y * 2.0).max(0.0),
}
}
#[inline]
pub fn expand(self, d: f32) -> Self {
Self {
x: self.x - d,
y: self.y - d,
w: (self.w + d * 2.0).max(0.0),
h: (self.h + d * 2.0).max(0.0),
}
}
#[inline]
pub fn expand2(self, d: Vec2) -> Self {
Self {
x: self.x - d.x,
y: self.y - d.y,
w: (self.w + d.x * 2.0).max(0.0),
h: (self.h + d.y * 2.0).max(0.0),
}
}
#[inline]
pub fn translate(self, d: Vec2) -> Self {
Self { x: self.x + d.x, y: self.y + d.y, w: self.w, h: self.h }
}
#[inline]
pub fn inset(self, left: f32, top: f32, right: f32, bottom: f32) -> Self {
Self {
x: self.x + left,
y: self.y + top,
w: (self.w - left - right).max(0.0),
h: (self.h - top - bottom).max(0.0),
}
}
#[inline]
pub const fn to_array(self) -> [f32; 4] {
[self.x, self.y, self.w, self.h]
}
/// Convert `[x, y, w, h]` into UV bounds `[u0, v0, u1, v1]`.
#[inline]
pub const fn to_uv_array(self) -> [f32; 4] {
[self.x, self.y, self.x + self.w, self.y + self.h]
}
}
/// Per-corner border radii in logical pixels: `[top_left, top_right, bottom_right, bottom_left]`.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, Pod, Zeroable)]
pub struct CornerRadii {
pub tl: f32,
pub tr: f32,
pub br: f32,
pub bl: f32,
}
impl CornerRadii {
pub const ZERO: Self = Self { tl: 0.0, tr: 0.0, br: 0.0, bl: 0.0 };
#[inline]
pub const fn same(r: f32) -> Self {
Self { tl: r, tr: r, br: r, bl: r }
}
#[inline]
pub const fn new(tl: f32, tr: f32, br: f32, bl: f32) -> Self {
Self { tl, tr, br, bl }
}
#[inline]
pub const fn top(r: f32) -> Self {
Self { tl: r, tr: r, br: 0.0, bl: 0.0 }
}
#[inline]
pub const fn bottom(r: f32) -> Self {
Self { tl: 0.0, tr: 0.0, br: r, bl: r }
}
#[inline]
pub const fn left(r: f32) -> Self {
Self { tl: r, tr: 0.0, br: 0.0, bl: r }
}
#[inline]
pub const fn right(r: f32) -> Self {
Self { tl: 0.0, tr: r, br: r, bl: 0.0 }
}
#[inline]
pub const fn to_array(self) -> [f32; 4] {
[self.tl, self.tr, self.br, self.bl]
}
}
impl From<f32> for CornerRadii {
#[inline]
fn from(r: f32) -> Self {
Self::same(r)
}
}
/// Opaque GPU texture identifier managed by `TextureRegistry` and `UiVulkanRenderer`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct TextureId(pub u32);
pub const NONE_TEXTURE_ID: TextureId = TextureId(0);
pub const VIBE_TEXTURE_ID: TextureId = TextureId(1);
pub const GLYPH_ATLAS_TEXTURE_ID: TextureId = TextureId(2);
pub const BLUR_TEXTURE_ID: TextureId = TextureId(3);
pub const FIRST_USER_TEXTURE_ID: u32 = 16;
/// Pixel format for GPU texture uploads.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureFormat {
/// 4 bytes/pixel, linear-sRGB premultiplied RGBA8 (`VK_FORMAT_R8G8B8A8_UNORM`).
Rgba8Premul,
/// 1 byte/pixel coverage mask (`VK_FORMAT_R8_UNORM`), used by the cosmic-text glyph atlas.
R8,
}
/// Pending GPU texture command queued by `TextureRegistry` and executed by `UiVulkanRenderer`.
#[derive(Debug, Clone)]
pub enum TextureUpload {
CreateOrReplace {
id: TextureId,
width: u32,
height: u32,
format: TextureFormat,
pixels: Vec<u8>,
},
UpdateRegion {
id: TextureId,
x: u32,
y: u32,
width: u32,
height: u32,
format: TextureFormat,
pixels: Vec<u8>,
},
Free {
id: TextureId,
},
}
/// Window cursor icon requested by the UI engine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CursorKind {
#[default]
Default,
Pointer,
Text,
Grab,
Grabbing,
ResizeHorizontal,
ResizeVertical,
NotAllowed,
}
impl CursorKind {
pub fn to_winit(self) -> winit::window::CursorIcon {
match self {
CursorKind::Default => winit::window::CursorIcon::Default,
CursorKind::Pointer => winit::window::CursorIcon::Pointer,
CursorKind::Text => winit::window::CursorIcon::Text,
CursorKind::Grab => winit::window::CursorIcon::Grab,
CursorKind::Grabbing => winit::window::CursorIcon::Grabbing,
CursorKind::ResizeHorizontal => winit::window::CursorIcon::EwResize,
CursorKind::ResizeVertical => winit::window::CursorIcon::NsResize,
CursorKind::NotAllowed => winit::window::CursorIcon::NotAllowed,
}
}
}
// Shader mode constants matching `shaders/ui_sdf.wgsl`.
pub const MODE_SOLID: u32 = 0;
pub const MODE_TEXTURED: u32 = 1;
pub const MODE_GLYPH: u32 = 2;
pub const MODE_BACKDROP_BLUR: u32 = 3;
pub const MODE_PLUS_GRADIENT: u32 = 4;
pub const MODE_SHADOW: u32 = 5;
pub const MODE_LINE: u32 = 6;
pub const FLAG_RING_ONLY: u32 = 1 << 0;
/// GPU instance record uploaded directly to the `UiVulkanRenderer` SSBO (exact 112 bytes, std430-aligned).
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, Pod, Zeroable)]
pub struct UiInstance {
/// `[x, y, w, h]` in logical pixels (or `[x0, y0, x1, y1]` for `MODE_LINE`).
pub rect: [f32; 4],
/// Per-corner radii `[tl, tr, br, bl]`.
pub radii: [f32; 4],
/// Mode-specific parameters:
/// - `MODE_SOLID`: gradient direction `[dx, dy, 0, 0]` (zero = solid)
/// - `MODE_TEXTURED` / `MODE_GLYPH`: UV bounds `[u0, v0, u1, v1]`
/// - `MODE_SHADOW`: `[offset_x, offset_y, blur_radius, spread]`
/// - `MODE_PLUS_GRADIENT`: `[angle_cos, angle_sin, 0, 0]`
pub uv_or_params: [f32; 4],
/// Per-instance clip rect `[x, y, w, h]` in logical pixels (`w <= 0` disables clip).
pub clip_rect: [f32; 4],
/// Packed RGBA8 colors: `[color_start, color_end, border_color, reserved]`.
pub colors: [u32; 4],
/// `[border_width, clip_radius, extra0, extra1]`.
pub metrics: [f32; 4],
/// `[mode, texture_id, flags, reserved]`.
pub meta: [u32; 4],
}
/// Marks a point in `DisplayList::instances` where the Vulkan renderer captures the
/// already-rendered framebuffer and runs the 2-pass Dual Kawase Blur for `backdrop-filter`.
#[derive(Debug, Clone, Copy)]
pub struct BlurRegion {
pub instance_index: usize,
pub rect: Rect,
pub radius: f32,
}
/// Retained-order frame draw list emitted by the UI engine and consumed by `UiVulkanRenderer`.
#[derive(Debug, Clone, Default)]
pub struct DisplayList {
pub instances: Vec<UiInstance>,
pub blur_regions: Vec<BlurRegion>,
clip_stack: Vec<(Rect, f32)>,
}
impl DisplayList {
pub fn new() -> Self {
Self {
instances: Vec::with_capacity(2048),
blur_regions: Vec::with_capacity(8),
clip_stack: Vec::with_capacity(16),
}
}
pub fn clear(&mut self) {
self.instances.clear();
self.blur_regions.clear();
self.clip_stack.clear();
}
/// Push a clip rect intersected with any active parent clip.
pub fn push_clip(&mut self, rect: Rect, radius: f32) {
let effective = if let Some(&(parent_rect, parent_r)) = self.clip_stack.last() {
let inter = parent_rect.intersect(rect);
let r = if radius > 0.0 { radius } else { parent_r };
(inter, r)
} else {
(rect, radius)
};
self.clip_stack.push(effective);
}
/// Push an unconstrained clip rect (e.g., for popovers/modals escaping a parent scroll area).
pub fn push_clip_raw(&mut self, rect: Rect, radius: f32) {
self.clip_stack.push((rect, radius));
}
pub fn pop_clip(&mut self) {
self.clip_stack.pop();
}
#[inline]
pub fn current_clip(&self) -> (Rect, f32) {
self.clip_stack
.last()
.copied()
.unwrap_or((Rect::ZERO, 0.0))
}
#[inline]
fn clip_fields(&self) -> ([f32; 4], f32) {
if let Some(&(r, rad)) = self.clip_stack.last() {
// Note: encode empty intersection as tiny negative-offscreen clip so clipped items are discarded!
if r.w <= 0.0 || r.h <= 0.0 {
([-10000.0, -10000.0, 0.001, 0.001], 0.0)
} else {
(r.to_array(), rad)
}
} else {
([0.0, 0.0, 0.0, 0.0], 0.0)
}
}
#[inline]
pub fn push_raw(&mut self, inst: UiInstance) {
self.instances.push(inst);
}
/// Append another `DisplayList` (preserving `BlurRegion` indices for Z-layer compositing).
pub fn extend(&mut self, other: &DisplayList) {
let offset = self.instances.len();
self.instances.extend_from_slice(&other.instances);
for br in &other.blur_regions {
self.blur_regions.push(BlurRegion {
instance_index: br.instance_index + offset,
rect: br.rect,
radius: br.radius,
});
}
}
pub fn push_rect(&mut self, rect: Rect, radii: impl Into<CornerRadii>, fill: Color) {
if rect.is_empty() || fill.a == 0 {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = fill.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [0.0; 4],
clip_rect,
colors: [c, c, 0, 0],
metrics: [0.0, clip_radius, 0.0, 0.0],
meta: [MODE_SOLID, NONE_TEXTURE_ID.0, 0, 0],
});
}
pub fn push_rect_bordered(
&mut self,
rect: Rect,
radii: impl Into<CornerRadii>,
fill: Color,
border_width: f32,
border_color: Color,
) {
if rect.is_empty() || (fill.a == 0 && (border_width <= 0.0 || border_color.a == 0)) {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = fill.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [0.0; 4],
clip_rect,
colors: [c, c, border_color.to_packed_u32(), 0],
metrics: [border_width.max(0.0), clip_radius, 0.0, 0.0],
meta: [MODE_SOLID, NONE_TEXTURE_ID.0, 0, 0],
});
}
/// 2-stop linear gradient rounded rect (`dir`: e.g. `Vec2::new(0.0, 1.0)` for top->bottom, `(1.0, 0.0)` for left->right).
pub fn push_gradient_rect(
&mut self,
rect: Rect,
radii: impl Into<CornerRadii>,
color_start: Color,
color_end: Color,
dir: Vec2,
) {
self.push_gradient_rect_bordered(rect, radii, color_start, color_end, dir, 0.0, Color::TRANSPARENT);
}
pub fn push_gradient_rect_bordered(
&mut self,
rect: Rect,
radii: impl Into<CornerRadii>,
color_start: Color,
color_end: Color,
dir: Vec2,
border_width: f32,
border_color: Color,
) {
if rect.is_empty() {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let len = dir.length();
let (dx, dy) = if len > 1e-5 { (dir.x / len, dir.y / len) } else { (0.0, 1.0) };
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [dx, dy, 0.0, 0.0],
clip_rect,
colors: [
color_start.to_packed_u32(),
color_end.to_packed_u32(),
border_color.to_packed_u32(),
0,
],
metrics: [border_width.max(0.0), clip_radius, 0.0, 0.0],
meta: [MODE_SOLID, NONE_TEXTURE_ID.0, 0, 0],
});
}
pub fn push_circle(&mut self, center: Pos2, radius: f32, fill: Color) {
if radius <= 0.0 || fill.a == 0 {
return;
}
let r = Rect::from_center_size(center, Vec2::splat(radius * 2.0));
self.push_rect(r, CornerRadii::same(radius), fill);
}
pub fn push_circle_stroke(
&mut self,
center: Pos2,
radius: f32,
stroke_width: f32,
stroke_color: Color,
) {
if radius <= 0.0 || stroke_width <= 0.0 || stroke_color.a == 0 {
return;
}
let r = Rect::from_center_size(center, Vec2::splat(radius * 2.0));
self.push_rect_bordered(r, CornerRadii::same(radius), Color::TRANSPARENT, stroke_width, stroke_color);
}
/// Exact 4-stop Yandex Plus horizontal gradient (`#ff5c4d -> #eb469f 30% -> #8341ef 75% -> #3f68f9`).
pub fn push_plus_gradient_rect(&mut self, rect: Rect, radii: impl Into<CornerRadii>, alpha: f32) {
if rect.is_empty() || alpha <= 0.0 {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let tint = Color::WHITE.with_alpha_f32(alpha).to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [1.0, 0.0, 0.0, 0.0],
clip_rect,
colors: [tint, tint, 0, 0],
metrics: [0.0, clip_radius, 0.0, 0.0],
meta: [MODE_PLUS_GRADIENT, NONE_TEXTURE_ID.0, 0, 0],
});
}
/// Exact 4-stop Yandex Plus gradient avatar ring.
pub fn push_plus_ring(&mut self, center: Pos2, radius: f32, thickness: f32, alpha: f32) {
if radius <= 0.0 || thickness <= 0.0 || alpha <= 0.0 {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let rect = Rect::from_center_size(center, Vec2::splat(radius * 2.0));
let tint = Color::WHITE.with_alpha_f32(alpha).to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: CornerRadii::same(radius).to_array(),
uv_or_params: [0.7071, 0.7071, 0.0, 0.0],
clip_rect,
colors: [tint, tint, tint, 0],
metrics: [thickness, clip_radius, 0.0, 0.0],
meta: [MODE_PLUS_GRADIENT, NONE_TEXTURE_ID.0, FLAG_RING_ONLY, 0],
});
}
/// Analytical Gaussian rounded-box drop shadow.
pub fn push_shadow(
&mut self,
rect: Rect,
radii: impl Into<CornerRadii>,
blur: f32,
spread: f32,
offset: Vec2,
color: Color,
) {
if rect.is_empty() || color.a == 0 {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = color.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [offset.x, offset.y, blur.max(0.5), spread],
clip_rect,
colors: [c, c, 0, 0],
metrics: [0.0, clip_radius, 0.0, 0.0],
meta: [MODE_SHADOW, NONE_TEXTURE_ID.0, 0, 0],
});
}
/// SDF-clipped textured quad (covers, SVG icons, Vibe orb, official PNGs).
pub fn push_image(
&mut self,
rect: Rect,
texture: TextureId,
radii: impl Into<CornerRadii>,
tint: Color,
) {
self.push_image_uv(rect, texture, Rect::UNIT_UV, radii, tint);
}
pub fn push_image_uv(
&mut self,
rect: Rect,
texture: TextureId,
uv: Rect,
radii: impl Into<CornerRadii>,
tint: Color,
) {
if rect.is_empty() || tint.a == 0 || texture == NONE_TEXTURE_ID {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = tint.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: uv.to_uv_array(),
clip_rect,
colors: [c, c, 0, 0],
metrics: [0.0, clip_radius, 0.0, 0.0],
meta: [MODE_TEXTURED, texture.0, 0, 0],
});
}
pub fn push_image_bordered(
&mut self,
rect: Rect,
texture: TextureId,
radii: impl Into<CornerRadii>,
tint: Color,
border_width: f32,
border_color: Color,
) {
if rect.is_empty() || texture == NONE_TEXTURE_ID {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = tint.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [0.0, 0.0, 1.0, 1.0],
clip_rect,
colors: [c, c, border_color.to_packed_u32(), 0],
metrics: [border_width.max(0.0), clip_radius, 0.0, 0.0],
meta: [MODE_TEXTURED, texture.0, 0, 0],
});
}
/// Single glyph quad sampled from the R8 cosmic-text glyph atlas (`GLYPH_ATLAS_TEXTURE_ID`).
pub fn push_glyph(&mut self, rect: Rect, uv: Rect, color: Color) {
if rect.is_empty() || color.a == 0 {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = color.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: [0.0; 4],
uv_or_params: uv.to_uv_array(),
clip_rect,
colors: [c, c, 0, 0],
metrics: [0.0, clip_radius, 0.0, 0.0],
meta: [MODE_GLYPH, GLYPH_ATLAS_TEXTURE_ID.0, 0, 0],
});
}
/// Real Dual Kawase `backdrop-filter: blur(...)` panel with translucent tint & optional border.
pub fn push_backdrop_blur(
&mut self,
rect: Rect,
radii: impl Into<CornerRadii>,
blur_radius: f32,
tint: Color,
border_width: f32,
border_color: Color,
) {
if rect.is_empty() {
return;
}
let idx = self.instances.len();
self.blur_regions.push(BlurRegion {
instance_index: idx,
rect,
radius: blur_radius.clamp(0.5, 4.0),
});
let (clip_rect, clip_radius) = self.clip_fields();
let c = tint.to_packed_u32();
self.instances.push(UiInstance {
rect: rect.to_array(),
radii: radii.into().to_array(),
uv_or_params: [0.0, 0.0, 1.0, 1.0],
clip_rect,
colors: [c, c, border_color.to_packed_u32(), 0],
metrics: [border_width.max(0.0), clip_radius, 0.0, 0.0],
meta: [MODE_BACKDROP_BLUR, BLUR_TEXTURE_ID.0, 0, 0],
});
}
/// Analytical anti-aliased line segment (capsule SDF) from `a` to `b`.
pub fn push_line(&mut self, a: Pos2, b: Pos2, thickness: f32, color: Color) {
if thickness <= 0.0 || color.a == 0 {
return;
}
let (clip_rect, clip_radius) = self.clip_fields();
let c = color.to_packed_u32();
self.instances.push(UiInstance {
rect: [a.x, a.y, b.x, b.y],
radii: [0.0; 4],
uv_or_params: [0.0; 4],
clip_rect,
colors: [c, c, 0, 0],
metrics: [thickness, clip_radius, 0.0, 0.0],
meta: [MODE_LINE, NONE_TEXTURE_ID.0, 0, 0],
});
}
}