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

feat: add onTappedSquare to Chessboard widget, add squareHighlights field #66

Merged
merged 12 commits into from
Jan 12, 2025

Conversation

tom-anders
Copy link
Contributor

We'll need this if we want to switch the coordinate trainer in the app
from ChessboardEditor to Chessboard in order to fix lichess-org/mobile#1017

We'll need this if we want to switch the coordinate trainer in the app
from `ChessboardEditor` to `Chessboard` in order to fix lichess-org/mobile#1017
@tom-anders tom-anders changed the title feat: add onTappedSquare to Chessboard widget, add sqaureHighlights field feat: add onTappedSquare to Chessboard widget, add squareHighlights field Dec 26, 2024
@@ -729,13 +752,17 @@ class _BoardState extends State<Chessboard> {
return;
}

final square = widget.offsetSquare(details.localPosition);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we want to put that after the test if current pointer down event is null. Because we want to ensure the tap up event is coming from the same pointer as the down event pointer.

And it made me think of something else. I think we're missing another check: we don't want to trigger the callback if the square found on tap up is not the same as the one found on tap down. IE: the user taps a square and drags the finger to another square before releasing, in that case the onTappedSquare should not be called.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I realise this makes the logic more complex, but can we avoid it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we want to put that after the test if current pointer down event is null. Because we want to ensure the tap up event is coming from the same pointer as the down event pointer.

This doesn't work, because if the board is non-interactive and shape drawing is disabled, the pointerDown listener is disabled entirely, so current pointer down event will always be nullptr. Or should we just enable the pointerDown handler always as well?

Copy link
Collaborator

@veloce veloce Jan 9, 2025

Choose a reason for hiding this comment

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

We probably want to enable all the Listener events all the time, otherwise it will be messy.

the pointerDown handler already exit if the board is not interactive. So for the tapped square callback you want to put the logic before that line. Adding a new variable and register the square on tap down to compare it with the square on tap up should be enough.

Normally the board logic is well tested, and this change should not break the existing tests hopefully.

This PR will need new tests to ensure the logic I mentioned above is working properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

All tests still pass, and these lines verify that no we don't call onTappedSquare if pointer down and up are different squares (which is the case when dragging a piece)

Anything else that should be tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add a test of course that tests the logic directly, just want to check to avoid redundant work

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a dedicated test would be good indeed. It adds another layer of documentation to understand the code and also we should test this in the case the board is not interactive (as opposed to during a piece drag where this callback is not really useful btw).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test 👍

@veloce
Copy link
Collaborator

veloce commented Jan 12, 2025

@tom-anders while testing this I realised we could not use a tap event with the coordinate trainer to recognise squares.

The reason is this feature is a race against the clock and people want to beat their score. A tap is slower than a bare touch, so we'd receive a lot of complaint that it is slow ;) Also on website it works with touch too.

So I pushed a commit with a suggested change.

@tom-anders
Copy link
Contributor Author

@tom-anders while testing this I realised we could not use a tap event with the coordinate trainer to recognise squares.

The reason is this feature is a race against the clock and people want to beat their score. A tap is slower than a bare touch, so we'd receive a lot of complaint that it is slow ;) Also on website it works with touch too.

So I pushed a commit with a suggested change.

Ah yes that makes sense, thanks 👍

@veloce veloce merged commit 3eeb08d into lichess-org:main Jan 12, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sliding your finger in coordinate trainer continues to select squares
2 participants