From 5dde2640ea3e09f8b91ec98433a5a067bc0d60ce Mon Sep 17 00:00:00 2001 From: cabins Date: Tue, 28 Nov 2023 13:30:51 +0800 Subject: [PATCH] fix: fix float window size limit fix: fix mini keyboard auto hiding --- .../java/com/osfans/trime/ime/core/Trime.java | 66 ++++++++----------- .../main/res/xml/theme_color_preference.xml | 2 +- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/osfans/trime/ime/core/Trime.java b/app/src/main/java/com/osfans/trime/ime/core/Trime.java index 3f1e7729f5..0c17e7d805 100644 --- a/app/src/main/java/com/osfans/trime/ime/core/Trime.java +++ b/app/src/main/java/com/osfans/trime/ime/core/Trime.java @@ -19,6 +19,7 @@ package com.osfans.trime.ime.core; import static android.graphics.Color.parseColor; +import static splitties.systemservices.SystemServicesKt.getWindowManager; import android.app.AlarmManager; import android.app.Dialog; @@ -37,6 +38,7 @@ import android.os.Message; import android.text.InputType; import android.text.TextUtils; +import android.util.DisplayMetrics; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -141,23 +143,22 @@ private AppPrefs getPrefs() { public void run() { if (mCandidateRoot == null || mCandidateRoot.getWindowToken() == null) return; if (!isPopupWindowEnabled) return; - int x = 0, y = 0; - final int[] candidateLocation = new int[2]; - mCandidateRoot.getLocationOnScreen(candidateLocation); + final int minX = popupMarginH; final int minY = popupMargin; - final int maxX = mCandidateRoot.getWidth() - mPopupWindow.getWidth() - minX; - final int maxY = candidateLocation[1] - mPopupWindow.getHeight() - minY; + + DisplayMetrics displayMetrics = new DisplayMetrics(); + getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); + final int maxX = displayMetrics.widthPixels - mPopupWindow.getWidth() - minX; + final int maxY = displayMetrics.heightPixels - mPopupWindow.getHeight() - minY; + + int x = minX, y = minY; if (isWinFixed() || !isCursorUpdated) { - // setCandidatesViewShown(true); switch (popupWindowPos) { + case TOP_LEFT: + break; case TOP_RIGHT: x = maxX; - y = minY; - break; - case TOP_LEFT: - x = minX; - y = minY; break; case BOTTOM_RIGHT: x = maxX; @@ -170,42 +171,37 @@ public void run() { case FIXED: case BOTTOM_LEFT: default: - x = minX; y = maxY; break; } } else { - // setCandidatesViewShown(false); switch (popupWindowPos) { case LEFT: + x = (int) mPopupRectF.left; + y = (int) mPopupRectF.bottom + popupMargin; + break; case LEFT_UP: x = (int) mPopupRectF.left; + y = (int) mPopupRectF.top - mPopupWindow.getHeight() - popupMargin; break; case RIGHT: - case RIGHT_UP: x = (int) mPopupRectF.right; - break; - default: - Timber.wtf("UNREACHABLE BRANCH"); - } - x = Math.min(maxX, x); - x = Math.max(minX, x); - switch (popupWindowPos) { - case LEFT: - case RIGHT: y = (int) mPopupRectF.bottom + popupMargin; break; - case LEFT_UP: case RIGHT_UP: + default: + x = (int) mPopupRectF.right; y = (int) mPopupRectF.top - mPopupWindow.getHeight() - popupMargin; break; - default: - Timber.wtf("UNREACHABLE BRANCH"); } - y = Math.min(maxY, y); - y = Math.max(minY, y); } + + // 只要修正一次就可以,别让悬浮窗超出了屏幕界限 + x = Math.max(minX, x); + x = Math.min(maxX, x); y -= BarUtils.getStatusBarHeight(); // 不包含狀態欄 + y = Math.max(minY, y); + y = Math.min(maxY, y); if (!mPopupWindow.isShowing()) { mPopupWindow.showAtLocation(mCandidateRoot, Gravity.START | Gravity.TOP, x, y); @@ -966,13 +962,7 @@ private boolean composeEvent(@NonNull KeyEvent event) { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Timber.i("\t\tonKeyDown()\tkeycode=%d, event=%s", keyCode, event.toString()); - if (composeEvent(event) && onKeyEvent(event)) { - if (!isWindowShown) { - return super.onKeyDown(keyCode, event); - } else { - return true; - } - } + if (composeEvent(event) && onKeyEvent(event) && isWindowShown) return true; return super.onKeyDown(keyCode, event); } @@ -981,11 +971,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) { Timber.i("\t\tonKeyUp()\tkeycode=%d, event=%s", keyCode, event.toString()); if (composeEvent(event) && textInputManager.getNeedSendUpRimeKey()) { textInputManager.onRelease(keyCode); - if (!isWindowShown) { - return super.onKeyUp(keyCode, event); - } else { - return true; - } + if (isWindowShown) return true; } return super.onKeyUp(keyCode, event); } diff --git a/app/src/main/res/xml/theme_color_preference.xml b/app/src/main/res/xml/theme_color_preference.xml index d2f0237cc3..fbd426b01e 100644 --- a/app/src/main/res/xml/theme_color_preference.xml +++ b/app/src/main/res/xml/theme_color_preference.xml @@ -13,7 +13,7 @@ app:iconSpaceReserved="false" android:title="@string/keyboard__auto_dark_title" android:defaultValue="false"/> -