use cranpose_foundation::{
Constraints, InvalidationKind, Measurable, ModifierNodeContext, NodeCapabilities, PlainElement,
PlainLayout, PlainLayoutFns, PrepareAt, Size,
};
use cranpose_ui_layout::{LayoutDirection, round_to_px};
use super::measure_pass_through;
use crate::modifier::Point;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OffsetElement {
x: f32,
y: f32,
flags: u32,
}
const RTL_AWARE: u32 = 1;
const PLACED: u32 = 1 << 1;
impl OffsetElement {
pub fn new(x: f32, y: f32, rtl_aware: bool) -> Self {
Self {
x,
y,
flags: if rtl_aware { RTL_AWARE } else { 0 },
}
}
pub fn offset(&self, direction: LayoutDirection) -> Point {
Point {
x: if self.rtl_aware() && direction.is_rtl() {
-self.x
} else {
self.x
},
y: self.y,
}
}
pub fn rtl_aware(&self) -> bool {
self.flags & RTL_AWARE != 0
}
pub(crate) fn placed(&self, at: PrepareAt) -> Point {
if self.flags & PLACED != 0 {
return Point {
x: self.x,
y: self.y,
};
}
let offset = self.offset(at.direction);
Point {
x: round_to_px(offset.x, at.density),
y: round_to_px(offset.y, at.density),
}
}
}
impl PlainElement for OffsetElement {
const WORDS: usize = 3;
const CAPABILITIES: NodeCapabilities = NodeCapabilities::LAYOUT;
const UPDATE_INVALIDATION: Option<InvalidationKind> = Some(InvalidationKind::Layout);
const LAYOUT: Option<&'static PlainLayoutFns> = Some(&PlainLayoutFns::of::<Self>());
#[inline]
fn to_words(&self, words: &mut [u32]) {
words.copy_from_slice(&[self.x.to_bits(), self.y.to_bits(), self.flags]);
}
#[inline]
fn from_words(words: &[u32]) -> Self {
match *words {
[x, y, flags, ..] => Self {
x: f32::from_bits(x),
y: f32::from_bits(y),
flags,
},
_ => Self::new(0.0, 0.0, false),
}
}
}
impl PlainLayout for OffsetElement {
const PREPARES: bool = true;
fn prepared(self, at: PrepareAt) -> Self {
let Point { x, y } = self.placed(at);
Self {
x,
y,
flags: PLACED,
}
}
fn measure_hold(
&self,
_density: f32,
_constraints: Constraints,
_size: Size,
wrapped: cranpose_ui_layout::WrappedHold,
) -> Option<cranpose_ui_layout::ConstraintsHold> {
wrapped.hold
}
fn measure(
&self,
context: &mut dyn ModifierNodeContext,
measurable: &dyn Measurable,
constraints: Constraints,
) -> cranpose_ui_layout::LayoutModifierMeasureResult {
let offset = self.placed(PrepareAt {
density: context.density(),
direction: LayoutDirection::Ltr,
});
measure_pass_through(measurable, constraints, |_| (offset.x, offset.y))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FractionalOffsetElement {
x_fraction: f32,
y_fraction: f32,
}
impl FractionalOffsetElement {
pub fn new(x_fraction: f32, y_fraction: f32) -> Self {
Self {
x_fraction,
y_fraction,
}
}
}
impl PlainElement for FractionalOffsetElement {
const WORDS: usize = 2;
const CAPABILITIES: NodeCapabilities = NodeCapabilities::LAYOUT;
const UPDATE_INVALIDATION: Option<InvalidationKind> = Some(InvalidationKind::Layout);
const LAYOUT: Option<&'static PlainLayoutFns> = Some(&PlainLayoutFns::of::<Self>());
#[inline]
fn to_words(&self, words: &mut [u32]) {
words.copy_from_slice(&[self.x_fraction.to_bits(), self.y_fraction.to_bits()]);
}
#[inline]
fn from_words(words: &[u32]) -> Self {
match *words {
[x, y, ..] => Self::new(f32::from_bits(x), f32::from_bits(y)),
_ => Self::new(0.0, 0.0),
}
}
}
impl PlainLayout for FractionalOffsetElement {
fn measure_hold(
&self,
_density: f32,
_constraints: Constraints,
_size: Size,
wrapped: cranpose_ui_layout::WrappedHold,
) -> Option<cranpose_ui_layout::ConstraintsHold> {
wrapped.hold
}
fn measure(
&self,
_context: &mut dyn ModifierNodeContext,
measurable: &dyn Measurable,
constraints: Constraints,
) -> cranpose_ui_layout::LayoutModifierMeasureResult {
measure_pass_through(measurable, constraints, |size| {
(self.x_fraction * size.width, self.y_fraction * size.height)
})
}
}