package com.zxhy.fastfilemanagerpro.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.util.AttributeSet
import android.widget.LinearLayout


class GrayLinearLayout : LinearLayout {
    private val mPaint = Paint()
    private var flag = false
    var filter: ColorFilter =
        PorterDuffColorFilter(Color.rgb(34, 136, 201), PorterDuff.Mode.OVERLAY)

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        val cm = ColorMatrix()
        cm.setSaturation(0f)
        mPaint.setColorFilter(ColorMatrixColorFilter(cm))
//        mPaint.setColorFilter(filter)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onDraw(canvas: Canvas) {
        if (flag) {
            canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG)
        }
        super.onDraw(canvas)
    }

    override fun dispatchDraw(canvas: Canvas) {
        if (flag) {
            canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG)
            super.dispatchDraw(canvas)
            canvas.restore()
        }
        super.dispatchDraw(canvas)
    }

    fun setGray(flag: Boolean) {
        this.flag = flag
        invalidate()
    }
}