Skip to content
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

refactor: extract Binding equality to original classes #17668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BrayanDSO
Copy link
Member

so it's possible to compare bindings without a MappableBinding

How Has This Been Tested?

I'm trusting the unit tests. Went around and tested some keybinds and gestures and they still work.

I couldn't test some keybinds like * (mark default) and ! (suspend default) even before these changes. They are only recognized as Shift+1 and Shift+8 here

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

return false
}

return screen.screenEquals(other.screen)
}

override fun hashCode(): Int {
// don't include the modifierKeys or mSide
Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't included the modifier keys because this should be a non functional change.

But why the modifier keys are checked in equals but aren't added in the hash code? @david-allison

Copy link
Member

Choose a reason for hiding this comment

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

Fallout from combining 'unicode' and 'keycode' lookups into the same HashMap


If we have "@" mapped, we convert the user input to a class and look it up in a hashmap to see if there is a command attached.

From memory: We lookup both based on keycode, and the system-defined unicode symbol from the KeyEvent

If using unicode:

  • We do NOT want to match: [Ctrl || Alt] + Shift + 2
  • We want to match Shift + 2
  • We want to match a keyboard which has a dedicated @ key: !Shift + @

This is implemented by overriding shiftMatches to return true, meaning multiple input values would match.

This is easily handled by overriding shiftMatches. Harder to handle with getHashCode, so trade off a theoretical bucket collision with ease of implementation.

Copy link
Member Author

@BrayanDSO BrayanDSO Dec 26, 2024

Choose a reason for hiding this comment

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

We want to match Shift + 2

At least in my ABNT2 keyboards, this doesn't happen. @, ! aren't recognized at all when pressing shift +1, 2, etc

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@Arthur-Milchior Arthur-Milchior left a comment

Choose a reason for hiding this comment

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

Nice improvement. But I suspect it could be even simpler

@@ -35,6 +36,10 @@ sealed interface Binding {
append(GESTURE_PREFIX)
append(gesture)
}

override fun equals(other: Any?): Boolean = other is GestureInput && gesture == other.gesture
Copy link
Member

Choose a reason for hiding this comment

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

I’m sorry but I don’t understand why you implement them.
According to the documentation the equals and hashCode are directly implemented using the values of the primary constructor for data class.
So you are recreating functions that are generated by default in a similar way.
Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

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

equals and hashCode are directly implemented using the values of the primary constructor for data class.

Forgot about that. Thanks a lot!

Copy link
Member Author

Choose a reason for hiding this comment

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

Had to keep the hashCode method for the KeyBindings because of #17668 (comment)

@BrayanDSO BrayanDSO marked this pull request as draft December 26, 2024 21:24
@BrayanDSO BrayanDSO added the Needs Author Reply Waiting for a reply from the original author label Dec 26, 2024
@BrayanDSO BrayanDSO added Needs Review and removed Needs Author Reply Waiting for a reply from the original author labels Dec 26, 2024
@BrayanDSO BrayanDSO marked this pull request as ready for review December 26, 2024 21:35
@@ -179,6 +188,10 @@ sealed interface Binding {
)
}

override fun equals(other: Any?): Boolean = other is ModifierKeys && semiStructuralEquals(other)

override fun hashCode(): Int = Objects.hash(ctrl, alt, shift, shiftMatches(true), shiftMatches(false))
Copy link
Member

@david-allison david-allison Jan 4, 2025

Choose a reason for hiding this comment

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

object 1:

  • shiftMatches(true) => true
  • shiftMatches(false) => true

object 2:

  • shiftMatches(true) => true
  • shiftMatches(false) => false

The hashcodes would not be equal, but this.shiftMatches(true) == keys.shiftMatches(true) would be true, implying equality

Probably warrants a regression test

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Author Reply Waiting for a reply from the original author Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants