package com.base.scanqr.utils

import android.content.Context
import android.content.Context.WINDOW_SERVICE
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager

object DialogUtils {

    /**
     * 判断视图在上半边还是下半边屏幕
     */
    fun Context.viewIsTopHalf(view: View): Boolean {
        val location = IntArray(2)
        view.getLocationOnScreen(location)

        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        val screenHeight = dm.heightPixels

        // 判断视图的顶部是否在屏幕的上半部分
        val isTopHalf = location[1] < screenHeight / 2

        // 判断视图的底部是否在屏幕的上半部分
        val isBottomHalf = location[1] + view.height < screenHeight / 2

        return if (isTopHalf) {
            // 视图的顶部在屏幕的上半部分
            true
        } else if (isBottomHalf) {
            // 视图的底部在屏幕的上半部分
            true
        } else {
            // 视图在屏幕的下半部分
            false
        }

    }
}