Skip to main content

cranpose_ui/modifier/
offset.rs

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