Skip to content

Commit

Permalink
Use WindowMetrics to get screen size
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Oct 3, 2024
1 parent 1f6d755 commit 6d2913f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
30 changes: 26 additions & 4 deletions app/src/main/java/org/jyutping/jyutping/ComposeKeyboardView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jyutping.jyutping

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.AbstractComposeView
Expand All @@ -21,6 +23,7 @@ import org.jyutping.jyutping.keyboard.SettingsScreen
import org.jyutping.jyutping.keyboard.StrokeKeyboard
import org.jyutping.jyutping.keyboard.SymbolicKeyboard
import org.jyutping.jyutping.presets.PresetConstant
import splitties.systemservices.windowManager

class ComposeKeyboardView(context: Context) : AbstractComposeView(context) {

Expand All @@ -47,15 +50,34 @@ class ComposeKeyboardView(context: Context) : AbstractComposeView(context) {

@Composable
private fun keyboardHeight(): Dp {
val keyRowsHeight = responsiveKeyHeight() * 4
return keyRowsHeight + PresetConstant.ToolBarHeight.dp
val keyRowHeight: Dp = responsiveKeyHeight()
val summedHeight: Dp = keyRowHeight * 4
return summedHeight + PresetConstant.ToolBarHeight.dp
}

@Composable
private fun responsiveKeyHeight(): Dp {
private fun responsiveKeyHeight(): Dp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) api34ResponsiveKeyHeight() else legacyResponsiveKeyHeight()

@Composable
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
private fun api34ResponsiveKeyHeight(): Dp {
val windowMetrics = context.windowManager.currentWindowMetrics
val bounds = windowMetrics.bounds
val density = windowMetrics.density
val screenWidth: Int = (bounds.width() / density).toInt()
val screenHeight: Int = (bounds.height() / density).toInt()
val isPhoneLandscape: Boolean = (screenHeight < 500) && (screenWidth > screenHeight)
if (isPhoneLandscape) return 40.dp
val value: Int = 50 + ((screenWidth - 300) / 20)
return value.dp
}

@Composable
private fun legacyResponsiveKeyHeight(): Dp {
val screenWidth = LocalConfiguration.current.screenWidthDp
val screenHeight = LocalConfiguration.current.screenHeightDp
if (screenWidth > screenHeight) return 40.dp
val isPhoneLandscape: Boolean = (screenHeight < 500) && (screenWidth > screenHeight)
if (isPhoneLandscape) return 40.dp
val value: Int = 50 + ((screenWidth - 300) / 20)
return value.dp
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jyutping.jyutping.keyboard

import android.os.Build
import android.view.HapticFeedbackConstants
import android.view.SoundEffectConstants
import androidx.compose.foundation.background
Expand Down Expand Up @@ -40,6 +41,7 @@ import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.JyutpingInputMethodService
import org.jyutping.jyutping.R
import org.jyutping.jyutping.presets.PresetColor
import splitties.systemservices.windowManager

private fun Candidate.width(): Dp = when (this.type) {
CandidateType.Cantonese -> (this.text.length * 20 + 32).dp
Expand All @@ -52,10 +54,15 @@ private class CandidateRow(val identifier: Int, val candidates: List<Candidate>,
fun CandidateBoard(height: Dp) {
val collapseWidth: Dp = 44.dp
val collapseHeight: Dp = 44.dp
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val interactionSource = remember { MutableInteractionSource() }
val view = LocalView.current
val context = LocalContext.current as JyutpingInputMethodService
val screenWidth: Dp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val windowMetrics = context.windowManager.currentWindowMetrics
(windowMetrics.bounds.width() / windowMetrics.density).dp
} else {
LocalConfiguration.current.screenWidthDp.dp
}
val isDarkMode = remember { context.isDarkMode }
val state = rememberLazyListState()
LaunchedEffect(context.candidateState.intValue) {
Expand Down

0 comments on commit 6d2913f

Please sign in to comment.