Skip to content

refactor: extract ShiftHandler from GeneralKeyboardIME (Part 13) - #426 - #688

Merged
andrewtavis merged 3 commits into
scribe-org:mainfrom
prince-0408:refactor/extract-shift-handler-426
Sep 25, 2026
Merged

andrewtavis merged 3 commits into
scribe-org:mainfrom
prince-0408:refactor/extract-shift-handler-426

Conversation

@prince-0408

Copy link
Copy Markdown
Collaborator

Description

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

It extracts shift key state machine management (SHIFT_OFF ➔ SHIFT_ON_ONE_CHAR ➔ SHIFT_ON_PERMANENT), caps lock double-tap timing logic (lastShiftPressTS, shiftPermToggleSpeed), and keyboard mode switching (handleKeyboardLetters, handleModeChange) out of GeneralKeyboardIME.kt into a standalone helper class ShiftHandler.

Detailed Changes Table

File / Component Changes Applied Detailed Impact
ShiftHandler.kt Created standalone helper encapsulating timing state (lastShiftPressTS, shiftPermToggleSpeed), shift state cycling (handleKeyboardLetters), and layout mode switching between letter keyboard and symbol keyboard pages (handleModeChange). Extracts shift key state machine management and keyboard layout mode transitions out of GeneralKeyboardIME.kt into a dedicated helper class.
GeneralKeyboardIME.kt Instantiated shiftHandler and delegated shift and mode switching methods (handleKeyboardLetters, handleModeChange). Simplified KEYCODE_SHIFT key handling in event listener and updated getPrimarySymbolKeyboardLayoutXML() to internal visibility. Fulfills the core goal of #426 by decoupling shift state and layout mode switching from GeneralKeyboardIME.kt while maintaining 100% backward compatibility for all callers.
ShiftHandlerTest.kt Added unit tests covering shift timing state (lastShiftPressTS) and double-tap caps lock lock timing logic. Ensures unit test coverage for shift state helper logic.
CHANGELOG.md Added entry under ### ♻️ Code Refactoring detailing the extraction of ShiftHandler from GeneralKeyboardIME. Satisfies ci_changelog_check workflow requirement for pull requests targeting main.

Key Benefits

  • Decoupled Shift & Layout Switching Logic: Removes shift state machine toggling, double-tap caps lock timing calculations, and letter/symbol keyboard mode switching from GeneralKeyboardIME.kt.
  • Deduplicated Event Listener Logic: Eliminates duplicate shift handling code inside the key press event listener in GeneralKeyboardIME.kt.
  • Improved Maintainability: Shift timing and keyboard page mode switching can now be tested and maintained independently of IME service lifecycles.

Related Issue

Refactors part of #426

@Roniscend

Copy link
Copy Markdown
Collaborator

@prince-0408 Does ShiftHandler accept an injectable time source for the double-tap logic, or is it calling System.currentTimeMillis() directly? Injecting a clock would keep those timing tests deterministic.

@prince-0408

Copy link
Copy Markdown
Collaborator Author

@prince-0408 Does ShiftHandler accept an injectable time source for the double-tap logic, or is it calling System.currentTimeMillis() directly? Injecting a clock would keep those timing tests deterministic.

@Roniscend

At present ShiftHandler directly uses System.currentTimeMillis().

Providing the possibility of injecting timeProvider (for example private val timeProvider: () -> Long = { System.currentTimeMillis() }) via constructor is a great idea. This way, it becomes possible to inject a time provider for the purpose of testing the difference between single tap and double tap lock shift without relying on actual delay times.

I will change ShiftHandler to accept timeProvider in its constructor and create deterministic unit tests for double tap time in PR #688.

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

Copy link
Copy Markdown
Member

Would you be able to fix the merge conflicts, @prince-0408? Would be great if we could then finalize this and maybe @Roniscend could do a quick review 😊

@prince-0408
prince-0408 force-pushed the refactor/extract-shift-handler-426 branch from 3d01faf to 6b5cbf4 Compare September 5, 2026 18:16
@andrewtavis andrewtavis added the no-changelog No changelog entry is needed for this pull request label Sep 13, 2026
@Roniscend

Copy link
Copy Markdown
Collaborator

After merging of part 12 there are some merge conflicts here can you look into it @prince-0408

@prince-0408

Copy link
Copy Markdown
Collaborator Author

After merging of part 12 there are some merge conflicts here can you look into it @prince-0408

Thank you for pointing this out @Roniscend. I will check on the merge conflicts and resolve them shortly.

@Roniscend
Roniscend requested a lite review from Copilot September 23, 2026 16:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Roniscend
Roniscend self-requested a review September 23, 2026 16:43
class ShiftHandler(
private val ime: GeneralKeyboardIME,
) {
var lastShiftPressTS: Long = 0L

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.

Caps lock engages spuriously, handler keeps its own lastShiftPressTS while KeyHandler.resetShiftIfNeeded resets the IME's original field, so double-tap latches caps lock instead of one-char shift


class ShiftHandlerTest {
@Test
fun initialLastShiftPressTS_isZero() {

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.

New tests assert on local variables without ever instantiating ShiftHandler, so they'd pass even with finding 1's bug still present.

@Roniscend

Copy link
Copy Markdown
Collaborator

After these changes , this pr will also be good to merge

@Roniscend

Copy link
Copy Markdown
Collaborator

@andrewtavis Requesting a final review from you and then this pr will be good to merge

@andrewtavis andrewtavis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

praise: Functionality and code wise this looks great, @prince-0408! Thanks for the work, and thanks for the review, @Roniscend! 😊

@andrewtavis
andrewtavis merged commit cd32304 into scribe-org:main Sep 25, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog No changelog entry is needed for this pull request refactor Refactor code to improve quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants