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::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}