use super::*;
impl Default for PaintEffectEntry {
#[inline(always)]
fn default() -> Self {
Self {
shadow: None,
gradient: None,
transform: Transform2D::default(),
focus_ring: None,
backdrop_blur: 0.0,
}
}
}
impl PaintEffectEntry {
#[must_use]
#[inline(always)]
pub fn from_paint_and_blur(paint: &PaintStyle, backdrop_blur: f32) -> Self {
Self {
shadow: paint.box_shadow(),
gradient: paint.background_gradient(),
transform: paint.get_transform(),
focus_ring: paint.focus_ring_border(),
backdrop_blur: if backdrop_blur.is_finite() {
backdrop_blur.max(0.0)
} else {
0.0
},
}
}
#[must_use]
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.box_shadow().is_none()
&& self.background_gradient().is_none()
&& self.get_transform().is_identity()
&& self.focus_ring_border().is_none()
&& self.get_backdrop_blur() <= 0.0
}
#[must_use]
#[inline(always)]
pub fn box_shadow(&self) -> Option<Shadow> {
let Self { shadow, .. } = *self;
shadow
}
#[must_use]
#[inline(always)]
pub fn background_gradient(&self) -> Option<LinearGradient> {
let Self { gradient, .. } = *self;
gradient
}
#[must_use]
#[inline(always)]
pub fn focus_ring_border(&self) -> Option<Border> {
let Self { focus_ring, .. } = *self;
focus_ring
}
}
impl EffectTable {
#[must_use]
#[inline(always)]
pub fn new() -> Self {
Self::default()
}
#[must_use]
#[inline(always)]
pub fn len(&self) -> usize {
self.get_live_count()
}
#[must_use]
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.get_live_count() == 0
}
#[must_use]
#[inline(always)]
pub fn get(&self, effect_id: u32) -> Option<&PaintEffectEntry> {
if effect_id == EFFECT_ID_NONE {
return None;
}
let idx: usize = (effect_id - 1) as usize;
self.get_entries().get(idx).and_then(Option::as_ref)
}
pub fn insert(&mut self, entry: PaintEffectEntry) -> u32 {
if entry.is_empty() {
return EFFECT_ID_NONE;
}
let slot_idx: u32 = if let Some(recycled) = self.get_mut_free_slots().pop() {
self.get_mut_entries()[recycled as usize] = Some(entry);
recycled
} else {
let next_idx: u32 = self.get_entries().len() as u32;
self.get_mut_entries().push(Some(entry));
next_idx
};
self.set_live_count(self.get_live_count().saturating_add(1));
slot_idx + 1
}
pub fn update(&mut self, current_id: u32, entry: PaintEffectEntry) -> u32 {
if entry.is_empty() {
self.remove(current_id);
return EFFECT_ID_NONE;
}
if current_id != EFFECT_ID_NONE {
let idx: usize = (current_id - 1) as usize;
if idx < self.get_entries().len() {
let was_none: bool = self.get_entries()[idx].is_none();
self.get_mut_entries()[idx] = Some(entry);
if was_none {
let next_live: usize = self.get_live_count().saturating_add(1);
let _: &mut Self = self.set_live_count(next_live);
}
return current_id;
}
}
self.insert(entry)
}
pub fn remove(&mut self, effect_id: u32) {
if effect_id == EFFECT_ID_NONE {
return;
}
let idx: usize = (effect_id - 1) as usize;
let removed: bool = if let Some(slot) = self.get_mut_entries().get_mut(idx) {
slot.take().is_some()
} else {
false
};
if removed {
self.get_mut_free_slots().push(effect_id - 1);
self.set_live_count(self.get_live_count().saturating_sub(1));
}
}
}
impl Default for CompactPaintStyle {
#[inline(always)]
fn default() -> Self {
Self {
background: Color::default(),
border_color: Color::default(),
border_width: 0.0,
radii_q4: [0; 4],
opacity: 1.0,
effect_id: EFFECT_ID_NONE,
flags: 0,
}
}
}
impl CompactPaintStyle {
#[must_use]
#[inline(always)]
fn quantize_radius(value: f32) -> u16 {
if !value.is_finite() || value <= 0.0 {
return 0;
}
let scaled: f32 = (value * COMPACT_RADIUS_SCALE).round();
if scaled >= u16::MAX as f32 {
u16::MAX
} else {
scaled as u16
}
}
#[must_use]
#[inline(always)]
const fn encode_cursor(cursor: CursorIcon) -> u32 {
match cursor {
CursorIcon::Default => 0,
CursorIcon::Pointer => 1,
CursorIcon::Text => 2,
CursorIcon::Crosshair => 3,
CursorIcon::Move => 4,
CursorIcon::NotAllowed => 5,
CursorIcon::EwResize => 6,
CursorIcon::NsResize => 7,
CursorIcon::Grab => 8,
CursorIcon::Grabbing => 9,
}
}
#[must_use]
#[inline(always)]
const fn decode_cursor(bits: u32) -> CursorIcon {
match bits {
1 => CursorIcon::Pointer,
2 => CursorIcon::Text,
3 => CursorIcon::Crosshair,
4 => CursorIcon::Move,
5 => CursorIcon::NotAllowed,
6 => CursorIcon::EwResize,
7 => CursorIcon::NsResize,
8 => CursorIcon::Grab,
9 => CursorIcon::Grabbing,
_ => CursorIcon::Default,
}
}
#[must_use]
#[inline(always)]
const fn encode_pointer_events(policy: PointerEvents) -> u32 {
match policy {
PointerEvents::Auto => 0,
PointerEvents::None => 1,
}
}
#[must_use]
#[inline(always)]
const fn decode_pointer_events(bits: u32) -> PointerEvents {
match bits {
1 => PointerEvents::None,
_ => PointerEvents::Auto,
}
}
#[must_use]
#[inline(always)]
pub fn from_paint(paint: &PaintStyle, effect_id: u32) -> Self {
let bg_opt: Option<Color> = paint.background_color();
let border: Border = paint.get_border();
let radius: CornerRadius = paint.get_radius();
let mut flags: u32 = 0;
if bg_opt.is_some() {
flags |= COMPACT_PAINT_HAS_BACKGROUND;
}
if paint.get_stop_scroll_propagation() {
flags |= COMPACT_PAINT_STOP_SCROLL;
}
if paint.get_stop_event_propagation() {
flags |= COMPACT_PAINT_STOP_EVENT;
}
flags |= Self::encode_cursor(paint.get_cursor()) << COMPACT_PAINT_CURSOR_SHIFT;
flags |=
Self::encode_pointer_events(paint.get_pointer_events()) << COMPACT_PAINT_POINTER_SHIFT;
Self {
background: bg_opt.unwrap_or_default(),
border_color: border.get_color(),
border_width: border.get_width(),
radii_q4: [
Self::quantize_radius(radius.get_top_left()),
Self::quantize_radius(radius.get_top_right()),
Self::quantize_radius(radius.get_bottom_right()),
Self::quantize_radius(radius.get_bottom_left()),
],
opacity: paint.get_opacity(),
effect_id,
flags,
}
}
#[must_use]
#[inline(always)]
pub fn background_color(&self) -> Option<Color> {
if (self.get_flags() & COMPACT_PAINT_HAS_BACKGROUND) != 0 {
Some(self.get_background())
} else {
None
}
}
#[must_use]
#[inline(always)]
pub fn border(&self) -> Border {
Border::new(self.get_border_width(), self.get_border_color())
}
#[must_use]
#[inline(always)]
pub fn corner_radius(&self) -> CornerRadius {
let q: [u16; 4] = self.get_radii_q4();
CornerRadius::new(
q[0] as f32 * COMPACT_RADIUS_INV_SCALE,
q[1] as f32 * COMPACT_RADIUS_INV_SCALE,
q[2] as f32 * COMPACT_RADIUS_INV_SCALE,
q[3] as f32 * COMPACT_RADIUS_INV_SCALE,
)
}
#[must_use]
#[inline(always)]
pub fn cursor(&self) -> CursorIcon {
let bits: u32 =
(self.get_flags() >> COMPACT_PAINT_CURSOR_SHIFT) & COMPACT_PAINT_CURSOR_MASK;
Self::decode_cursor(bits)
}
#[must_use]
#[inline(always)]
pub fn pointer_events(&self) -> PointerEvents {
let bits: u32 =
(self.get_flags() >> COMPACT_PAINT_POINTER_SHIFT) & COMPACT_PAINT_POINTER_MASK;
Self::decode_pointer_events(bits)
}
#[must_use]
#[inline(always)]
pub fn stop_scroll_propagation(&self) -> bool {
(self.get_flags() & COMPACT_PAINT_STOP_SCROLL) != 0
}
#[must_use]
#[inline(always)]
pub fn stop_event_propagation(&self) -> bool {
(self.get_flags() & COMPACT_PAINT_STOP_EVENT) != 0
}
}