From dcfaad3c1f634cec2d40b9f6d9a4c0ca62c50190 Mon Sep 17 00:00:00 2001 From: "K.F. Li" Date: Tue, 9 Apr 2024 11:17:07 -0700 Subject: [PATCH] Some bug fixes. --- .../eduhk/typeduck/ime/core/EditorInstance.kt | 2 +- .../eduhk/typeduck/ime/keyboard/Keyboard.java | 9 ++-- .../typeduck/ime/keyboard/KeyboardView.java | 6 ++- .../eduhk/typeduck/util/KeyboardSizeUtils.kt | 51 +++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/hk/eduhk/typeduck/util/KeyboardSizeUtils.kt diff --git a/app/src/main/java/hk/eduhk/typeduck/ime/core/EditorInstance.kt b/app/src/main/java/hk/eduhk/typeduck/ime/core/EditorInstance.kt index 96fc8ddf1d..3a9dabd59a 100644 --- a/app/src/main/java/hk/eduhk/typeduck/ime/core/EditorInstance.kt +++ b/app/src/main/java/hk/eduhk/typeduck/ime/core/EditorInstance.kt @@ -274,7 +274,7 @@ class EditorInstance(private val ims: InputMethodService) { val ic = inputConnection ?: return false val isHalfShapePunct = Rime.showAsciiPunch() // In Gboard, an ideographic full stop is only produced after a letter or digit (with Unicode categories L* or Nd). - return (isHalfShapePunct || ic.getTextBeforeCursor(2, 0)?.firstOrNull()?.isLetterOrDigit() == true) + return (ic.getTextBeforeCursor(2, 0)?.firstOrNull()?.isLetterOrDigit() == true) && ic.deleteSurroundingText(1, 0) && ic.commitText(if (isHalfShapePunct) ". " else "。", 1) } diff --git a/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/Keyboard.java b/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/Keyboard.java index 26fc530b63..2099a04760 100644 --- a/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/Keyboard.java +++ b/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/Keyboard.java @@ -27,6 +27,7 @@ import hk.eduhk.typeduck.data.theme.Config; import hk.eduhk.typeduck.util.ConfigGetter; import hk.eduhk.typeduck.util.DimensionsKt; +import hk.eduhk.typeduck.util.KeyboardSizeUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -97,18 +98,18 @@ public class Keyboard { // 3. 由于高度只能取整数,缩放后仍然存在余数的,由 auto_height_index 指定的行吸收(遵循四舍五入) // 特别的,当值为负数时,为倒序序号(-1即倒数第一个);当值大于按键行数时,为最后一行 private int autoHeightIndex, keyboardHeight; - - public static float adjustRatio = Math.min(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight()) / 1000f; + public static float adjustRatio = Math.min(KeyboardSizeUtils.getScreenWidth(), KeyboardSizeUtils.getScreenHeight()) / 1000f; public static float adjustRatioSmall = (float) Math.cbrt(adjustRatio); public int getPadding() { - return (ScreenUtils.getScreenWidth() - mDisplayWidth) / 2; + return (KeyboardSizeUtils.getScreenWidth() - mDisplayWidth) / 2; } /** Creates a keyboard from the given xml key layout file. */ public Keyboard() { // some magic - final double width = ScreenUtils.getScreenWidth(), height = ScreenUtils.getScreenHeight(); + KeyboardSizeUtils.refreshSize(); + final double width = KeyboardSizeUtils.getScreenWidth(), height = KeyboardSizeUtils.getScreenHeight(); final double widthHeightProduct = width * height; final double screenNarrowness = 2.0 / (height / width + width / height); diff --git a/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/KeyboardView.java b/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/KeyboardView.java index 4418297b60..4d957146ce 100644 --- a/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/KeyboardView.java +++ b/app/src/main/java/hk/eduhk/typeduck/ime/keyboard/KeyboardView.java @@ -1254,7 +1254,11 @@ private void showKey(final int keyIndex, final int type) { previewPopup.setWidth(popupWidth); previewPopup.setHeight(popupHeight); previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY, mPopupPreviewX, mPopupPreviewY); - mPreviewText.setOutlineSpotShadowColor(mShadowColor); + if (Build.VERSION.SDK_INT >= 28) { + // This function exists only if API level >= 28. + // Thing looks okay without it if API level < 28. + mPreviewText.setOutlineSpotShadowColor(mShadowColor); + } mPreviewText.setVisibility(VISIBLE); } diff --git a/app/src/main/java/hk/eduhk/typeduck/util/KeyboardSizeUtils.kt b/app/src/main/java/hk/eduhk/typeduck/util/KeyboardSizeUtils.kt new file mode 100644 index 0000000000..b3258e9174 --- /dev/null +++ b/app/src/main/java/hk/eduhk/typeduck/util/KeyboardSizeUtils.kt @@ -0,0 +1,51 @@ +package hk.eduhk.typeduck.util + +import android.annotation.SuppressLint +import android.graphics.Point +import android.os.Build +import android.view.WindowInsets +import hk.eduhk.typeduck.ime.core.Trime +import kotlin.math.floor +import splitties.systemservices.windowManager + + +object KeyboardSizeUtils { + + private val point = Point(-1, -1) + + @SuppressLint("NewApi") + @JvmStatic + fun refreshSize() { + val windowManager = Trime.getService().windowManager?: return + var currentVersion = Build.VERSION.SDK_INT + + if (currentVersion >= Build.VERSION_CODES.R) { + val metrics = windowManager.currentWindowMetrics + val bounds = metrics.bounds + val insets = metrics.windowInsets.getInsets( + WindowInsets.Type.navigationBars() or + WindowInsets.Type.displayCutout() + ) + point.set(bounds.width() - insets.left - insets.right, + bounds.height() - insets.top - insets.bottom) + } else { + windowManager.defaultDisplay.getSize(point) + } + } + + @JvmStatic + fun getScreenWidth(): Int { + if (point.x < 0) { + refreshSize() + } + return point.x + } + + @JvmStatic + fun getScreenHeight(): Int { + if (point.y < 0) { + refreshSize() + } + return if (point.x <= point.y) point.y else point.y - floor(0.2 * point.y).toInt() + } +} \ No newline at end of file