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

commonMain.com.github.panpf.sketch.painter.MaskProgressPainter.kt Maven / Gradle / Ivy

Go to download

Sketch is an image loading library specially designed for Compose Multiplatform and Android View

There is a newer version: 4.0.0
Show newest version
/*
 * Copyright (C) 2024 panpf 
 *
 * 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 com.github.panpf.sketch.painter

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.toArgb
import com.github.panpf.sketch.ability.PROGRESS_INDICATOR_HIDDEN_WHEN_COMPLETED
import com.github.panpf.sketch.ability.PROGRESS_INDICATOR_HIDDEN_WHEN_INDETERMINATE
import com.github.panpf.sketch.ability.PROGRESS_INDICATOR_MASK_COLOR
import com.github.panpf.sketch.ability.PROGRESS_INDICATOR_STEP_ANIMATION_DURATION
import com.github.panpf.sketch.painter.internal.AbsProgressPainter

/**
 * Create a [MaskProgressPainter] and remember it
 *
 * @see com.github.panpf.sketch.extensions.compose.common.test.painter.MaskProgressPainterTest.testRememberMaskProgressPainter
 */
@Composable
fun rememberMaskProgressPainter(
    maskColor: Color = Color(PROGRESS_INDICATOR_MASK_COLOR),
    hiddenWhenIndeterminate: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_INDETERMINATE,
    hiddenWhenCompleted: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_COMPLETED,
    stepAnimationDuration: Int = PROGRESS_INDICATOR_STEP_ANIMATION_DURATION,
): MaskProgressPainter {
    return remember(
        maskColor,
        hiddenWhenIndeterminate,
        hiddenWhenCompleted,
        stepAnimationDuration
    ) {
        MaskProgressPainter(
            maskColor = maskColor,
            hiddenWhenIndeterminate = hiddenWhenIndeterminate,
            hiddenWhenCompleted = hiddenWhenCompleted,
            stepAnimationDuration = stepAnimationDuration
        )
    }
}

/**
 * A [ProgressPainter] that uses a mask to hide the progress
 *
 * @see com.github.panpf.sketch.extensions.compose.common.test.painter.MaskProgressPainterTest
 */
@Stable
class MaskProgressPainter(
    private val maskColor: Color = Color(PROGRESS_INDICATOR_MASK_COLOR),
    hiddenWhenIndeterminate: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_INDETERMINATE,
    hiddenWhenCompleted: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_COMPLETED,
    stepAnimationDuration: Int = PROGRESS_INDICATOR_STEP_ANIMATION_DURATION,
) : AbsProgressPainter(
    hiddenWhenIndeterminate = hiddenWhenIndeterminate,
    hiddenWhenCompleted = hiddenWhenCompleted,
    stepAnimationDuration = stepAnimationDuration
), SketchPainter {

    override val intrinsicSize: Size = Size.Unspecified

    override fun DrawScope.drawProgress(drawProgress: Float) {
        val progressHeight = drawProgress * size.height
        drawRect(
            color = maskColor,
            topLeft = Offset(0f, progressHeight),
            size = Size(size.width, size.height - progressHeight)
        )
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || this::class != other::class) return false
        other as MaskProgressPainter
        if (maskColor != other.maskColor) return false
        if (hiddenWhenIndeterminate != other.hiddenWhenIndeterminate) return false
        if (hiddenWhenCompleted != other.hiddenWhenCompleted) return false
        if (stepAnimationDuration != other.stepAnimationDuration) return false
        return true
    }

    override fun hashCode(): Int {
        var result = maskColor.hashCode()
        result = 31 * result + hiddenWhenIndeterminate.hashCode()
        result = 31 * result + hiddenWhenCompleted.hashCode()
        result = 31 * result + stepAnimationDuration.hashCode()
        return result
    }

    override fun toString(): String {
        return "MaskProgressPainter(" +
                "maskColor=${maskColor.toArgb()}, " +
                "hiddenWhenIndeterminate=$hiddenWhenIndeterminate, " +
                "hiddenWhenCompleted=$hiddenWhenCompleted, " +
                "stepAnimationDuration=$stepAnimationDuration" +
                ")"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy