Skip to content

refactor: extract SuggestionUIHandler from GeneralKeyboardIME (Part 11) (#426) - #685

Open
prince-0408 wants to merge 1 commit into
scribe-org:mainfrom
prince-0408:refactor/extract-suggestion-ui-handler-426
Open

refactor: extract SuggestionUIHandler from GeneralKeyboardIME (Part 11) (#426)#685
prince-0408 wants to merge 1 commit into
scribe-org:mainfrom
prince-0408:refactor/extract-suggestion-ui-handler-426

Conversation

@prince-0408

Copy link
Copy Markdown
Collaborator

Description

This PR is Part 11 in modularizing GeneralKeyboardIME for #426.

It extracts auto-suggestion layout rendering, button styling, visibility toggling (between command buttons, gender suggestions, and emoji recommendations), and autocomplete button click handlers out of GeneralKeyboardIME.kt into a standalone helper class SuggestionUIHandler.

Detailed Changes Table

File / Component Changes Applied Detailed Impact
SuggestionUIHandler.kt Created standalone helper class encapsulating auto-suggest dispatcher (updateAutoSuggestText), single/dual suggestion button styling (handleSingleType, handleMultipleNounFormats), word prediction updates (handleWordSuggestions), and autocomplete completions (updateAutocompleteSuggestions, clearAutocomplete). Extracts ~430 lines of UI rendering and button styling logic out of GeneralKeyboardIME.kt into a focused helper component.
SuggestionUIHandlerTest.kt Added Robolectric unit tests verifying state updates, auto-suggest toggling, and autocomplete clearing. Ensures test coverage and regression prevention for SuggestionUIHandler.
GeneralKeyboardIME.kt Instantiated suggestionUIHandler and delegated updateAutoSuggestText, updateAutocompleteSuggestions, and clearAutocomplete to it. Removed private helper methods and unused extension functions. Reduces GeneralKeyboardIME.kt size by ~400 lines (from 2,061 lines down to 1,662 lines) while maintaining 100% backward compatibility.

Key Benefits

  • Decoupled Suggestion UI Architecture: Separates UI layout rendering, button styling, and click listeners from the core IME service lifecycle.
  • Dedicated Test Coverage: Added SuggestionUIHandlerTest.kt testing layout toggles and state synchronization.

Related Issue

Refactors part of #426

@prince-0408
prince-0408 force-pushed the refactor/extract-suggestion-ui-handler-426 branch 2 times, most recently from 74bf7dc to e5ec7f6 Compare August 24, 2026 19:51
@andrewtavis

Copy link
Copy Markdown
Member

Thanks for the PR, @prince-0408! We'll get this PR in soon after bringing in the autosuggestion PRs 😊

@angrezichatterbox angrezichatterbox added the refactor Refactor code to improve quality label Aug 30, 2026
@andrewtavis

Copy link
Copy Markdown
Member

question: Would you be able to rebase on main, @prince-0408? We'd then get to the review :)

note: Sorry that you'll likely need to do so much rebasing here, but great that we can bring all of these in soon!

@prince-0408
prince-0408 force-pushed the refactor/extract-suggestion-ui-handler-426 branch 2 times, most recently from d1107db to c3ac1c3 Compare September 6, 2026 22:51
@prince-0408

Copy link
Copy Markdown
Collaborator Author

question: Would you be able to rebase on main, @prince-0408? We'd then get to the review :)

note: Sorry that you'll likely need to do so much rebasing here, but great that we can bring all of these in soon!

Hi @andrewtavis @angrezichatterbox,

I have rebased the branch onto the latest main and resolved the merge conflicts in CHANGELOG.md and GeneralKeyboardIME.kt.

This PR is now ready for re-review! 🙌

@andrewtavis

Copy link
Copy Markdown
Member

Thanks for the update, @prince-0408! We'll get to the review soon. CC also @Roniscend in case you have a moment :)

*/
@Suppress("TooManyFunctions")
class SuggestionUIHandler(
private val ime: GeneralKeyboardIME,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this take KeyboardIMEContext instead of GeneralKeyboardIME, like the other handlers since #693? If the interface is missing a member or two it needs, adding them there would keep the decoupling consistent.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this take KeyboardIMEContext instead of GeneralKeyboardIME, like the other handlers since #693? If the interface is missing a member or two it needs, adding them there would keep the decoupling consistent.

Good catch! Updated SuggestionUIHandler to take KeyboardIMEContext and added themeManager to the interface to maintain consistency with the other handlers.

val default1 = baseSuggestions.getOrNull(0).orEmpty()
val default2 = baseSuggestions.getOrNull(1).orEmpty()
setSuggestionButton(uiManager.binding.conjugateBtn, default1)
uiManager.pluralBtn?.let { setSuggestionButton(it, default2) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This drops the autoSuggestEmojis check the old code had here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This drops the autoSuggestEmojis check the old code had here

Nice catch! Restored the autoSuggestEmojis check in handleWordSuggestions and added a unit test covering this scenario.

@prince-0408
prince-0408 force-pushed the refactor/extract-suggestion-ui-handler-426 branch from c3ac1c3 to 9e93350 Compare September 12, 2026 15:24
@prince-0408

Copy link
Copy Markdown
Collaborator Author

Hi @Roniscend @andrewtavis @angrezichatterbox,

Ready for re-review and merge! 🙌

private val ime: KeyboardIMEContext,
) {
companion object {
const val NOUN_TYPE_SIZE = 20f

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The font-size constants are now defined twice, NOUN_TYPE_SIZE is now unused and can be deleted

@@ -0,0 +1,126 @@
// SPDX-License-Identifier: GPL-3.0-or-later

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests mostly check that fields were copied, not how the strip is drawn, Please update them

@prince-0408
prince-0408 force-pushed the refactor/extract-suggestion-ui-handler-426 branch from 9e93350 to 149ea9d Compare September 12, 2026 17:35

@Roniscend Roniscend left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prince-0408 Two things are left the four commandOptionsBar == GONE assertions pass even if the handler does nothing (it starts out gone and disableAutoSuggest() never touches it), so please check the default suggestion text on the buttons instead. And the emoji test no longer covers the gender + no-word-suggestions + emojis case, so could you restore that setup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Refactor code to improve quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants