From 32099748e5a5e2b8a0b93a113b21a24b87684a33 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sun, 1 Jun 2025 13:36:14 +0200 Subject: [PATCH] move PopupSuggestionsView to Kotlin and rename to MoreSuggestionsView --- .../keyboard/keyboard/KeyboardView.java | 4 +- .../helium314/keyboard/latin/InputView.java | 6 +- .../latin/suggestions/MoreSuggestions.java | 4 +- .../latin/suggestions/MoreSuggestionsView.kt | 85 ++++++++++++++ .../suggestions/PopupSuggestionsView.java | 109 ------------------ .../latin/suggestions/SuggestionStripView.kt | 4 +- app/src/main/res/layout/more_suggestions.xml | 2 +- 7 files changed, 95 insertions(+), 119 deletions(-) create mode 100644 app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestionsView.kt delete mode 100644 app/src/main/java/helium314/keyboard/latin/suggestions/PopupSuggestionsView.java diff --git a/app/src/main/java/helium314/keyboard/keyboard/KeyboardView.java b/app/src/main/java/helium314/keyboard/keyboard/KeyboardView.java index 2df204403..07f39ffd4 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/KeyboardView.java +++ b/app/src/main/java/helium314/keyboard/keyboard/KeyboardView.java @@ -38,7 +38,7 @@ import helium314.keyboard.latin.common.Constants; import helium314.keyboard.latin.common.StringUtilsKt; import helium314.keyboard.latin.settings.Settings; import helium314.keyboard.latin.suggestions.MoreSuggestions; -import helium314.keyboard.latin.suggestions.PopupSuggestionsView; +import helium314.keyboard.latin.suggestions.MoreSuggestionsView; import helium314.keyboard.latin.utils.TypefaceUtils; import java.util.HashSet; @@ -109,7 +109,7 @@ public class KeyboardView extends View { final TypedArray keyboardViewAttr = context.obtainStyledAttributes(attrs, R.styleable.KeyboardView, defStyle, R.style.KeyboardView); - if (this instanceof PopupSuggestionsView) + if (this instanceof MoreSuggestionsView) mKeyBackground = mColors.selectAndColorDrawable(keyboardViewAttr, ColorType.MORE_SUGGESTIONS_WORD_BACKGROUND); else if (this instanceof PopupKeysKeyboardView) mKeyBackground = mColors.selectAndColorDrawable(keyboardViewAttr, ColorType.POPUP_KEYS_BACKGROUND); diff --git a/app/src/main/java/helium314/keyboard/latin/InputView.java b/app/src/main/java/helium314/keyboard/latin/InputView.java index fe2645766..9ccedb806 100644 --- a/app/src/main/java/helium314/keyboard/latin/InputView.java +++ b/app/src/main/java/helium314/keyboard/latin/InputView.java @@ -19,7 +19,7 @@ import helium314.keyboard.accessibility.AccessibilityUtils; import helium314.keyboard.keyboard.MainKeyboardView; import helium314.keyboard.latin.common.ColorType; import helium314.keyboard.latin.settings.Settings; -import helium314.keyboard.latin.suggestions.PopupSuggestionsView; +import helium314.keyboard.latin.suggestions.MoreSuggestionsView; import helium314.keyboard.latin.suggestions.SuggestionStripView; import kotlin.Unit; @@ -230,8 +230,8 @@ public final class InputView extends FrameLayout { /** * This class forwards {@link MotionEvent}s happened in the {@link MainKeyboardView} to - * {@link SuggestionStripView} when the {@link PopupSuggestionsView} is showing. - * {@link SuggestionStripView} dismisses {@link PopupSuggestionsView} when it receives any event + * {@link SuggestionStripView} when the {@link MoreSuggestionsView} is showing. + * {@link SuggestionStripView} dismisses {@link MoreSuggestionsView} when it receives any event * outside of it. */ private static class MoreSuggestionsViewCanceler diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestions.java b/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestions.java index 2799c1f7f..0d52736f5 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestions.java +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestions.java @@ -168,12 +168,12 @@ public final class MoreSuggestions extends Keyboard { } public static final class Builder extends KeyboardBuilder { - private final PopupSuggestionsView mPaneView; + private final MoreSuggestionsView mPaneView; private SuggestedWords mSuggestedWords; private int mFromIndex; private int mToIndex; - public Builder(final Context context, final PopupSuggestionsView paneView) { + public Builder(final Context context, final MoreSuggestionsView paneView) { super(context, new MoreSuggestionsParam()); mPaneView = paneView; } diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestionsView.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestionsView.kt new file mode 100644 index 000000000..ffa11dd75 --- /dev/null +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/MoreSuggestionsView.kt @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * modified + * SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only + */ +package helium314.keyboard.latin.suggestions + +import android.content.Context +import android.util.AttributeSet +import helium314.keyboard.keyboard.Key +import helium314.keyboard.keyboard.Keyboard +import helium314.keyboard.keyboard.KeyboardActionListener +import helium314.keyboard.keyboard.PopupKeysKeyboardView +import helium314.keyboard.latin.R +import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo +import helium314.keyboard.latin.suggestions.MoreSuggestions.MoreSuggestionKey +import helium314.keyboard.latin.utils.Log + +/** + * A view that renders a virtual [MoreSuggestions]. It handles rendering of keys and detecting + * key presses and touch movements. + */ +class MoreSuggestionsView @JvmOverloads constructor( + context: Context, attrs: AttributeSet?, + defStyle: Int = R.attr.popupKeysKeyboardViewStyle +) : PopupKeysKeyboardView(context, attrs, defStyle) { + abstract class MoreSuggestionsListener : KeyboardActionListener.Adapter() { + abstract fun onSuggestionSelected(wordInfo: SuggestedWordInfo) + } + + var isInModalMode = false + private set + + // TODO: Remove redundant override method. + override fun setKeyboard(keyboard: Keyboard) { + super.setKeyboard(keyboard) + isInModalMode = false + // With accessibility mode off, mAccessibilityDelegate is set to null at the above PopupKeysKeyboardView#setKeyboard call. + // With accessibility mode on, mAccessibilityDelegate is set to a PopupKeysKeyboardAccessibilityDelegate object at the above + // PopupKeysKeyboardView#setKeyboard call. + if (mAccessibilityDelegate != null) { + mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions) + mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions) + } + } + + override fun getDefaultCoordX() = (keyboard as MoreSuggestions).mOccupiedWidth / 2 + + fun updateKeyboardGeometry(keyHeight: Int) { + updateKeyDrawParams(keyHeight) + } + + fun setModalMode() { + isInModalMode = true + // Set vertical correction to zero (Reset popup keys keyboard sliding allowance R#dimen.config_popup_keys_keyboard_slide_allowance). + mKeyDetector.setKeyboard(keyboard, -paddingLeft.toFloat(), -paddingTop.toFloat()) + } + + override fun onKeyInput(key: Key, x: Int, y: Int) { + if (key !is MoreSuggestionKey) { + Log.e(TAG, "Expected key is MoreSuggestionKey, but found ${key.javaClass.name}") + return + } + val keyboard = keyboard + if (keyboard !is MoreSuggestions) { + Log.e(TAG, "Expected keyboard is MoreSuggestions, but found ${keyboard?.javaClass?.name}") + return + } + val suggestedWords = keyboard.mSuggestedWords + val index = key.mSuggestedWordIndex + if (index < 0 || index >= suggestedWords.size()) { + Log.e(TAG, "Selected suggestion has an illegal index: $index") + return + } + if (mListener !is MoreSuggestionsListener) { + Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " + mListener.javaClass.name) + return + } + (mListener as MoreSuggestionsListener).onSuggestionSelected(suggestedWords.getInfo(index)) + } + + companion object { + private val TAG = MoreSuggestionsView::class.java.simpleName + } +} diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/PopupSuggestionsView.java b/app/src/main/java/helium314/keyboard/latin/suggestions/PopupSuggestionsView.java deleted file mode 100644 index fb4ff9844..000000000 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/PopupSuggestionsView.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * modified - * SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only - */ - -package helium314.keyboard.latin.suggestions; - -import android.content.Context; -import android.util.AttributeSet; -import helium314.keyboard.latin.utils.Log; - -import androidx.annotation.NonNull; - -import helium314.keyboard.keyboard.Key; -import helium314.keyboard.keyboard.Keyboard; -import helium314.keyboard.keyboard.KeyboardActionListener; -import helium314.keyboard.keyboard.PopupKeysKeyboardView; -import helium314.keyboard.latin.R; -import helium314.keyboard.latin.SuggestedWords; -import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo; -import helium314.keyboard.latin.suggestions.MoreSuggestions.MoreSuggestionKey; - -/** - * A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting - * key presses and touch movements. - */ -public final class PopupSuggestionsView extends PopupKeysKeyboardView { - private static final String TAG = PopupSuggestionsView.class.getSimpleName(); - - public static abstract class MoreSuggestionsListener extends KeyboardActionListener.Adapter { - public abstract void onSuggestionSelected(final SuggestedWordInfo info); - } - - private boolean mIsInModalMode; - - public PopupSuggestionsView(final Context context, final AttributeSet attrs) { - this(context, attrs, R.attr.popupKeysKeyboardViewStyle); - } - - public PopupSuggestionsView(final Context context, final AttributeSet attrs, - final int defStyle) { - super(context, attrs, defStyle); - } - - // TODO: Remove redundant override method. - @Override - public void setKeyboard(@NonNull final Keyboard keyboard) { - super.setKeyboard(keyboard); - mIsInModalMode = false; - // With accessibility mode off, {@link #mAccessibilityDelegate} is set to null at the - // above {@link PopupKeysKeyboardView#setKeyboard(Keyboard)} call. - // With accessibility mode on, {@link #mAccessibilityDelegate} is set to a - // {@link PopupKeysKeyboardAccessibilityDelegate} object at the above - // {@link PopupKeysKeyboardView#setKeyboard(Keyboard)} call. - if (mAccessibilityDelegate != null) { - mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions); - mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions); - } - } - - @Override - protected int getDefaultCoordX() { - final MoreSuggestions pane = (MoreSuggestions) getKeyboard(); - return pane.mOccupiedWidth / 2; - } - - public void updateKeyboardGeometry(final int keyHeight) { - updateKeyDrawParams(keyHeight); - } - - public void setModalMode() { - mIsInModalMode = true; - // Set vertical correction to zero (Reset popup keys keyboard sliding allowance - // {@link R#dimen.config_popup_keys_keyboard_slide_allowance}). - mKeyDetector.setKeyboard(getKeyboard(), -getPaddingLeft(), -getPaddingTop()); - } - - public boolean isInModalMode() { - return mIsInModalMode; - } - - @Override - protected void onKeyInput(final Key key, final int x, final int y) { - if (!(key instanceof MoreSuggestionKey)) { - Log.e(TAG, "Expected key is MoreSuggestionKey, but found " - + key.getClass().getName()); - return; - } - final Keyboard keyboard = getKeyboard(); - if (!(keyboard instanceof MoreSuggestions)) { - Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " - + keyboard.getClass().getName()); - return; - } - final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords; - final int index = ((MoreSuggestionKey)key).mSuggestedWordIndex; - if (index < 0 || index >= suggestedWords.size()) { - Log.e(TAG, "Selected suggestion has an illegal index: " + index); - return; - } - if (!(mListener instanceof MoreSuggestionsListener)) { - Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " - + mListener.getClass().getName()); - return; - } - ((MoreSuggestionsListener)mListener).onSuggestionSelected(suggestedWords.getInfo(index)); - } -} diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt index ea183df68..5cfcd10c8 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt @@ -47,7 +47,7 @@ import helium314.keyboard.latin.define.DebugFlags import helium314.keyboard.latin.settings.DebugSettings import helium314.keyboard.latin.settings.Defaults import helium314.keyboard.latin.settings.Settings -import helium314.keyboard.latin.suggestions.PopupSuggestionsView.MoreSuggestionsListener +import helium314.keyboard.latin.suggestions.MoreSuggestionsView.MoreSuggestionsListener import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.ToolbarKey import helium314.keyboard.latin.utils.ToolbarMode @@ -180,7 +180,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) // related to more suggestions // todo: maybe put most of this in a separate class? - private val moreSuggestionsView: PopupSuggestionsView = moreSuggestionsContainer.findViewById(R.id.more_suggestions_view) + private val moreSuggestionsView: MoreSuggestionsView = moreSuggestionsContainer.findViewById(R.id.more_suggestions_view) private val moreSuggestionsBuilder = MoreSuggestions.Builder(context, moreSuggestionsView) // todo: why actually here? private val moreSuggestionsModalTolerance = context.resources.getDimensionPixelOffset(R.dimen.config_more_suggestions_modal_tolerance) private val moreSuggestionsListener = object : MoreSuggestionsListener() { diff --git a/app/src/main/res/layout/more_suggestions.xml b/app/src/main/res/layout/more_suggestions.xml index ce0535179..b76032c85 100644 --- a/app/src/main/res/layout/more_suggestions.xml +++ b/app/src/main/res/layout/more_suggestions.xml @@ -11,7 +11,7 @@ android:layout_height="wrap_content" android:orientation="vertical" > -