All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.peanuuutz.fork.ui.foundation.layout.Offset.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 The Android Open Source Project
 * Modifications Copyright 2022 Peanuuutz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.peanuuutz.fork.ui.foundation.layout

import androidx.compose.runtime.Stable
import net.peanuuutz.fork.ui.inspection.InspectInfo
import net.peanuuutz.fork.ui.ui.layout.Constraints
import net.peanuuutz.fork.ui.ui.layout.Measurable
import net.peanuuutz.fork.ui.ui.layout.MeasureResult
import net.peanuuutz.fork.ui.ui.modifier.Modifier
import net.peanuuutz.fork.ui.ui.modifier.ModifierNodeElement
import net.peanuuutz.fork.ui.ui.node.LayoutModifierNode
import net.peanuuutz.fork.ui.ui.node.ModifierNode
import net.peanuuutz.fork.ui.ui.unit.IntOffset

@Stable
fun Modifier.offset(
    x: Int = 0,
    y: Int = 0
): Modifier {
    return offset(IntOffset(x, y))
}

@Stable
fun Modifier.offset(offset: IntOffset): Modifier {
    return this then OffsetModifier(offset)
}

@Stable
fun Modifier.offset(
    offset: () -> IntOffset
): Modifier {
    return this then OffsetBlockModifier(offset)
}

// ======== Internal ========

private data class OffsetModifier(
    val offset: IntOffset
) : ModifierNodeElement() {
    override fun create(): OffsetModifierNode {
        return OffsetModifierNode(offset)
    }

    override fun update(node: OffsetModifierNode) {
        node.offset = offset
    }

    override fun InspectInfo.inspect() {
        set("offset", offset)
    }
}

private class OffsetModifierNode(
    var offset: IntOffset
) : ModifierNode(), LayoutModifierNode {
    override fun measure(measurable: Measurable, constraints: Constraints): MeasureResult {
        val placeable = measurable.measure(constraints)
        return MeasureResult(placeable.width, placeable.height) {
            placeable.place(offset)
        }
    }
}

private data class OffsetBlockModifier(
    val offset: () -> IntOffset
) : ModifierNodeElement() {
    override fun create(): OffsetBlockModifierNode {
        return OffsetBlockModifierNode(offset)
    }

    override fun update(node: OffsetBlockModifierNode) {
        node.offset = offset
    }
}

private class OffsetBlockModifierNode(
    var offset: () -> IntOffset
) : ModifierNode(), LayoutModifierNode {
    override fun measure(measurable: Measurable, constraints: Constraints): MeasureResult {
        val placeable = measurable.measure(constraints)
        return MeasureResult(placeable.width, placeable.height) {
            val offset = offset()
            placeable.place(offset)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy