package im.angry.openeuicc.util import android.app.Activity import android.content.res.Resources import android.graphics.Rect import android.view.ViewGroup import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import im.angry.openeuicc.OpenEuiccApplication val Activity.openEuiccApplication: OpenEuiccApplication get() = application as OpenEuiccApplication val Fragment.openEuiccApplication: OpenEuiccApplication get() = requireActivity().openEuiccApplication // Source: /** * Call this method (in onActivityCreated or later) to set * the width of the dialog to a percentage of the current * screen width. */ fun DialogFragment.setWidthPercent(percentage: Int) { val percent = percentage.toFloat() / 100 val dm = Resources.getSystem().displayMetrics val rect = dm.run { Rect(0, 0, widthPixels, heightPixels) } val percentWidth = rect.width() * percent dialog?.window?.setLayout(percentWidth.toInt(), ViewGroup.LayoutParams.WRAP_CONTENT) } /** * Call this method (in onActivityCreated or later) * to make the dialog near-full screen. */ fun DialogFragment.setFullScreen() { dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) }