OverScrollConfiguration.kt

/*
 * Copyright 2021 The Android Open Source Project
 *
 * 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 androidx.compose.foundation.gestures

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color

/**
 * Metadata for overscroll effects for android platform.
 *
 * @param glowColor color for the glow effect, if the platform effect is a glow effect, otherwise
 * ignored.
 * @param forceShowAlways force show overscroll even if content doesn't scroll (is smaller
 * than a scrollable container itself)
 * @param drawPadding the amount of padding to apply from scrollable container bounds to
 * effect before drawing it
 */
@ExperimentalFoundationApi
@Stable
class OverScrollConfiguration(
    val glowColor: Color = Color(0xff666666), // taken from EdgeEffect.java defaults
    val forceShowAlways: Boolean = false,
    val drawPadding: PaddingValues = PaddingValues()
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as OverScrollConfiguration

        if (glowColor != other.glowColor) return false
        if (forceShowAlways != other.forceShowAlways) return false
        if (drawPadding != other.drawPadding) return false

        return true
    }

    override fun hashCode(): Int {
        var result = glowColor.hashCode()
        result = 31 * result + forceShowAlways.hashCode()
        result = 31 * result + drawPadding.hashCode()
        return result
    }

    override fun toString(): String {
        return "OverScrollConfiguration(glowColor=$glowColor, " +
            "forceShowAlways=$forceShowAlways, drawPadding=$drawPadding)"
    }
}

/**
 * Composition local to provide configuration for scrolling containers down the
 * hierarchy. `null` means there will be no overscroll at all.
 */
@Suppress("EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET")
@ExperimentalFoundationApi
@get:ExperimentalFoundationApi
val LocalOverScrollConfiguration = compositionLocalOf<OverScrollConfiguration?> {
    OverScrollConfiguration()
}