Skip to main content

cranpose_ui/modifier/
offset.rs

1use super::{Modifier, Point, inspector_metadata};
2use crate::modifier_nodes::{FractionalOffsetElement, OffsetElement};
3
4impl Modifier {
5    /// Offset the content by (x, y). The offsets can be positive or negative.
6    ///
7    /// This modifier is RTL-aware: positive x offsets move content right in LTR
8    /// and left in RTL layouts.
9    ///
10    /// Matches Kotlin: `Modifier.offset(x: Dp, y: Dp)`
11    ///
12    /// Example: `Modifier::empty().offset(10.0, 20.0)`
13    pub fn offset(self, x: f32, y: f32) -> Self {
14        let modifier = Self::with_element(OffsetElement::new(x, y, true)).with_inspector_metadata(
15            inspector_metadata("offset", move |info| {
16                info.add_offset_components("offsetX", "offsetY", Point { x, y });
17            }),
18        );
19        self.then(modifier)
20    }
21
22    /// Offset the content by (x, y) without considering layout direction.
23    ///
24    /// Positive x always moves content to the right regardless of RTL.
25    ///
26    /// Matches Kotlin: `Modifier.absoluteOffset(x: Dp, y: Dp)`
27    ///
28    /// Example: `Modifier::empty().absolute_offset(10.0, 20.0)`
29    pub fn absolute_offset(self, x: f32, y: f32) -> Self {
30        let modifier = Self::with_element(OffsetElement::new(x, y, false)).with_inspector_metadata(
31            inspector_metadata("absoluteOffset", move |info| {
32                info.add_offset_components("absoluteOffsetX", "absoluteOffsetY", Point { x, y });
33            }),
34        );
35        self.then(modifier)
36    }
37
38    /// Offset the content by a fraction of its own measured size.
39    ///
40    /// `x_fraction` / `y_fraction` are multiplied by the measured width /
41    /// height of the content when it is placed, so `offset_fraction(0.0,
42    /// -0.5)` moves the content up by half its own height. Like
43    /// [`Modifier::offset`], this only affects placement, not measurement.
44    ///
45    /// There is no direct Jetpack Compose equivalent; Compose's
46    /// `slideInVertically`/`slideOutVertically` receive the measured size via
47    /// a lambda instead. This modifier backs `slide_in_vertically` /
48    /// `slide_out_vertically` in `AnimatedVisibility`.
49    ///
50    /// Example: `Modifier::empty().offset_fraction(0.0, -0.5)`
51    pub fn offset_fraction(self, x_fraction: f32, y_fraction: f32) -> Self {
52        let modifier = Self::with_element(FractionalOffsetElement::new(x_fraction, y_fraction))
53            .with_inspector_metadata(inspector_metadata("offsetFraction", move |info| {
54                info.add_property("offsetFractionX", x_fraction.to_string());
55                info.add_property("offsetFractionY", y_fraction.to_string());
56            }));
57        self.then(modifier)
58    }
59}