Skip to content

Commit

Permalink
fix: fix float window size limit
Browse files Browse the repository at this point in the history
fix: fix mini keyboard auto hiding
  • Loading branch information
cabins authored and Bambooin committed Dec 4, 2023
1 parent 6dff1ba commit 5dde264
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
66 changes: 26 additions & 40 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -966,13 +962,7 @@ private boolean composeEvent(@NonNull KeyEvent event) {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Timber.i("\t<TrimeInput>\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);
}

Expand All @@ -981,11 +971,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
Timber.i("\t<TrimeInput>\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);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/theme_color_preference.xml
Expand Up @@ -13,7 +13,7 @@
app:iconSpaceReserved="false"
android:title="@string/keyboard__auto_dark_title"
android:defaultValue="false"/>
<SwitchPreferenceCompat android:key="looks__use_mini_keyboard"
<SwitchPreferenceCompat android:key="theme_use_mini_keyboard"
app:iconSpaceReserved="false"
android:title="@string/keyboard__use_mini_keyboard_title"
android:defaultValue="false"/>
Expand Down

0 comments on commit 5dde264

Please sign in to comment.