@@ -3,6 +3,7 @@ package dev.swiftcrossui.androidbackend
33import android.R
44import android.app.Activity
55import android.content.res.Configuration
6+ import android.os.Build
67import android.util.TypedValue
78import android.view.WindowInsets
89import android.widget.TextView
@@ -18,69 +19,82 @@ class AndroidBackendHelpers {
1819
1920 fun getSafeWindowWidth (activity : Activity ): Int {
2021 val windowMetrics = activity.getWindowManager().getCurrentWindowMetrics()
22+ val displayMetrics = activity.resources.displayMetrics
2123 val insets =
2224 windowMetrics
2325 .getWindowInsets()
2426 .getInsetsIgnoringVisibility(WindowInsets .Type .systemBars())
2527 // density is very frequently a fractional value like 1.5, so cast to int after division
2628 // instead of before
2729 return ((windowMetrics.getBounds().width() - insets.left - insets.right).toFloat() /
28- windowMetrics .density)
30+ displayMetrics .density)
2931 .toInt()
3032 }
3133
3234 fun getSafeWindowHeight (activity : Activity ): Int {
3335 val windowMetrics = activity.getWindowManager().getCurrentWindowMetrics()
36+ val displayMetrics = activity.resources.displayMetrics
3437 val insets =
3538 windowMetrics
3639 .getWindowInsets()
3740 .getInsetsIgnoringVisibility(WindowInsets .Type .systemBars())
3841 return ((windowMetrics.getBounds().height() - insets.top - insets.bottom).toFloat() /
39- windowMetrics .density)
42+ displayMetrics .density)
4043 .toInt()
4144 }
4245
4346 fun getFullWindowWidth (activity : Activity ): Int {
4447 val windowMetrics = activity.getWindowManager().getCurrentWindowMetrics()
48+ val displayMetrics = activity.resources.displayMetrics
4549 // density is very frequently a fractional value like 1.5, so cast to int after division
4650 // instead of before
47- return (windowMetrics.getBounds().width().toFloat() / windowMetrics .density).toInt()
51+ return (windowMetrics.getBounds().width().toFloat() / displayMetrics .density).toInt()
4852 }
4953
5054 fun getFullWindowHeight (activity : Activity ): Int {
5155 val windowMetrics = activity.getWindowManager().getCurrentWindowMetrics()
52- return (windowMetrics.getBounds().height().toFloat() / windowMetrics.density).toInt()
56+ val displayMetrics = activity.resources.displayMetrics
57+ return (windowMetrics.getBounds().height().toFloat() / displayMetrics.density).toInt()
5358 }
5459
5560 fun getSafeAreaLeftInset (activity : Activity ): Int {
5661 val windowMetrics = activity.getWindowManager().getCurrentWindowMetrics()
62+ val displayMetrics = activity.resources.displayMetrics
5763 val insets =
5864 windowMetrics
5965 .getWindowInsets()
6066 .getInsetsIgnoringVisibility(WindowInsets .Type .systemBars())
61- return (insets.left.toFloat() / windowMetrics .density).toInt()
67+ return (insets.left.toFloat() / displayMetrics .density).toInt()
6268 }
6369
6470 fun getSafeAreaTopInset (activity : Activity ): Int {
6571 val windowMetrics = activity.getWindowManager().getCurrentWindowMetrics()
72+ val displayMetrics = activity.resources.displayMetrics
6673 val insets =
6774 windowMetrics
6875 .getWindowInsets()
6976 .getInsetsIgnoringVisibility(WindowInsets .Type .systemBars())
70- return (insets.top.toFloat() / windowMetrics .density).toInt()
77+ return (insets.top.toFloat() / displayMetrics .density).toInt()
7178 }
7279
7380 private var largeTextSize: Float? = null
7481 private var titleTextSize: Float? = null
7582 private var mediumTextSize: Float? = null
7683 private var smallTextSize: Float? = null
7784
78- private fun getFontSizeFromResource (activity : Activity , resId : Int ) =
79- TypedValue .deriveDimension(
80- TypedValue .COMPLEX_UNIT_SP ,
81- TextView (activity, null , 0 , resId).paint.textSize,
82- activity.resources.displayMetrics,
83- )
85+ private fun getFontSizeFromResource (activity : Activity , resId : Int ): Float {
86+ val sizePixels = TextView (activity, null , 0 , resId).paint.textSize
87+ val displayMetrics = activity.resources.displayMetrics
88+ if (Build .VERSION .SDK_INT >= 34 ) {
89+ return TypedValue .deriveDimension(
90+ TypedValue .COMPLEX_UNIT_SP ,
91+ sizePixels,
92+ displayMetrics,
93+ )
94+ } else {
95+ return sizePixels / displayMetrics.scaledDensity
96+ }
97+ }
8498
8599 fun clearTextSizeCache () {
86100 largeTextSize = null
0 commit comments