-
Notifications
You must be signed in to change notification settings - Fork 122
refactor: extract KeyboardLayoutHandler from GeneralKeyboardIME (Part 15) - #426 #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prince-0408
wants to merge
2
commits into
scribe-org:main
Choose a base branch
from
prince-0408:refactor/extract-keyboard-layout-handler-426
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
app/src/keyboards/java/be/scri/helpers/KeyboardLayoutHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| package be.scri.helpers | ||
|
|
||
| import android.text.InputType.TYPE_CLASS_DATETIME | ||
| import android.text.InputType.TYPE_CLASS_NUMBER | ||
| import android.text.InputType.TYPE_CLASS_PHONE | ||
| import android.text.InputType.TYPE_MASK_CLASS | ||
| import be.scri.R | ||
| import be.scri.models.ScribeState | ||
|
|
||
| private const val DATA_SIZE_2 = 2 | ||
| private const val DATA_SIZE_3 = 3 | ||
|
|
||
| /** | ||
| * Encapsulates keyboard XML layout resolution, symbol layout mapping, | ||
| * keyboard width calculations, state-based layout XML selection, and view re-creation. | ||
| */ | ||
| class KeyboardLayoutHandler( | ||
| private val ime: KeyboardIMEContext, | ||
| ) { | ||
| /** | ||
| * Resolves the XML resource ID for the active keyboard layout. | ||
| * | ||
| * @return The XML layout resource ID. | ||
| */ | ||
| fun getCurrentKeyboardLayoutXML(): Int = | ||
| when (ime.keyboardMode) { | ||
| ime.keyboardSymbols -> getPrimarySymbolKeyboardLayoutXML() | ||
| ime.keyboardSymbolShift -> R.xml.keys_symbols_shift | ||
| else -> ime.getKeyboardLayoutXML() | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the primary symbol or numeric layout XML resource ID. | ||
| * | ||
| * @return The XML layout resource ID. | ||
| */ | ||
| fun getPrimarySymbolKeyboardLayoutXML(): Int = | ||
| if (ime.isNumericKeyboardActive()) { | ||
| R.xml.keys_numeric | ||
| } else { | ||
| R.xml.keys_symbols | ||
| } | ||
|
|
||
| /** | ||
| * Determines which keyboard layout XML to use based on the current [ScribeState]. | ||
| * | ||
| * @param state The current state of the Scribe keyboard. | ||
| * @param isSubsequentArea true if this is for a secondary conjugation view. | ||
| * @param dataSize The number of items to display, used to select an appropriate layout. | ||
| * @return The resource ID of the keyboard layout XML. | ||
| */ | ||
| fun getKeyboardLayoutForState( | ||
| state: ScribeState, | ||
| isSubsequentArea: Boolean = false, | ||
| dataSize: Int = 0, | ||
| ): Int = | ||
| when (state) { | ||
| ScribeState.SELECT_VERB_CONJUNCTION -> { | ||
| ime.saveConjugateModeType(ime.language) | ||
| if (!isSubsequentArea && dataSize == 0) { | ||
| ime.defaultConjugateLayoutXML | ||
| } else { | ||
| when (dataSize) { | ||
| DATA_SIZE_2 -> R.xml.conjugate_view_2x1 | ||
| DATA_SIZE_3 -> R.xml.conjugate_view_1x3 | ||
| else -> R.xml.conjugate_view_2x2 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| else -> { | ||
| ime.getKeyboardLayoutXML() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the width of the keyboard container. | ||
| * | ||
| * @return The keyboard width in pixels. | ||
| */ | ||
| fun getKeyboardWidth(): Int = | ||
| if (ime.isFloatingMode) { | ||
| val density = ime.getImeResources().displayMetrics.density | ||
| val screenWidth = ime.getImeResources().displayMetrics.widthPixels | ||
| val floatWidth = (320f * density).toInt() | ||
| Math.min(floatWidth, (screenWidth * 0.85f).toInt()) | ||
| } else { | ||
| ime.getImeResources().displayMetrics.widthPixels | ||
| } | ||
|
|
||
| /** | ||
| * Re-instantiates the [KeyboardBase] and applies the updated shift state and layout. | ||
| */ | ||
| fun recreateKeyboard() { | ||
| if (!ime.isUiManagerInitialized) return | ||
|
|
||
| val xmlId = getCurrentKeyboardLayoutXML() | ||
| val currentShiftState = ime.keyboard?.mShiftState ?: SHIFT_OFF | ||
| ime.keyboard = KeyboardBase(ime.imeContext, xmlId, ime.enterKeyType, getKeyboardWidth()) | ||
| ime.keyboard?.setShifted(currentShiftState) | ||
| ime.keyboardView?.setKeyboard(ime.keyboard!!) | ||
|
|
||
| if (xmlId == R.xml.keys_symbols) { | ||
| ime.uiManager.setupCurrencySymbol(ime.language) | ||
| } | ||
| ime.keyboardView?.invalidateAllKeys() | ||
| } | ||
|
|
||
| companion object { | ||
| /** | ||
| * Returns true if the given [inputType] requires a numeric keyboard layout. | ||
| */ | ||
| fun shouldUseNumericKeyboard(inputType: Int): Boolean = | ||
| when (inputType and TYPE_MASK_CLASS) { | ||
| TYPE_CLASS_NUMBER, TYPE_CLASS_DATETIME, TYPE_CLASS_PHONE -> true | ||
| else -> false | ||
| } | ||
|
|
||
| /** | ||
| * Returns the appropriate keyboard layout XML resource ID for the given [inputType]. | ||
| * Falls back to [letterKeyboardLayoutXML] when a numeric layout is not required. | ||
| */ | ||
| fun getKeyboardLayoutXMLForInputType( | ||
| inputType: Int, | ||
| letterKeyboardLayoutXML: Int, | ||
| ): Int = | ||
| if (shouldUseNumericKeyboard(inputType)) { | ||
| R.xml.keys_numeric | ||
| } else { | ||
| letterKeyboardLayoutXML | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couples to the concrete GeneralKeyboardIME instead of KeyboardIMEContext like every sibling,needs handler,getKeyboardLayoutXML(), defaultConjugateLayoutXML, enterKeyType, isFloatingMode, and isNumericKeyboardActive as a property added to the interface first.