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

boxes: Disable cycling using arrow keys if recipient is invalid. #864

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zee-bit
Copy link
Member

@zee-bit zee-bit commented Jan 2, 2021

This PR disables cycling from the recipient box to any other box inside the WriteBox view using arrow keys, if the recipient name is invalid. This is very similar to CYCLE_COMPOSE_FOCUS using Tab key. Tests have also been updated and amended.

This PR fixes #779.

@zulipbot zulipbot added size: XL [Automatic label added by zulipbot] bug Something isn't working enhancement New feature or request labels Jan 2, 2021
@zee-bit zee-bit force-pushed the issue_779 branch 2 times, most recently from 834b1ae to 67d5df8 Compare January 6, 2021 18:13
@neiljp neiljp added the PR needs review PR requires feedback to proceed label Jan 6, 2021
@neiljp
Copy link
Collaborator

neiljp commented Jan 16, 2021

@zee-bit Just some quick feedback:

  • is there a lot of duplication here? Could we reuse parts to make this a lot simpler?
  • the tests look large and complex too
  • is there some way we can trigger on 'losing focus', to avoid having all these cases? (that might cover the mouse as well, as a minor side-effect)

Copy link
Member

@preetmishra preetmishra left a comment

Choose a reason for hiding this comment

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

@zee-bit Hey, thanks for working on this. I pulled your changes locally and it seems to work well functionally. 👍

However, echoing what @neiljp mentioned, we could benefit from a general approach if feasible. In any case, we would want to reduce the apparent redundancy.

@preetmishra preetmishra removed the PR needs review PR requires feedback to proceed label Jan 17, 2021
@zee-bit
Copy link
Member Author

zee-bit commented Jan 25, 2021

@neiljp Regarding your feedback:

  • I have made two functions(footer_notify_invalid_stream & footer_notify_invalid_emails), they have somewhat increased code reusability. The main logic, I think, could not be simplified further if I am to proceed with the current implementation.
  • The tests are very much related to that of CYCLE_COMPOSE_FOCUS as it is derived from there, but there were few places with unnecessary conditions and checks that I have removed.
  • I am not quite sure if there is any other way to trigger on 'loosing focus'(without all those conditions). If you or @preetmishra have some other implementation in mind, I would love to learn :). Re mouse event, I can try extending this for mouse events as well, if this implementation looks fine.

@zee-bit
Copy link
Member Author

zee-bit commented Jan 25, 2021

@zulipbot add "PR needs review"

@zulipbot zulipbot added the PR needs review PR requires feedback to proceed label Jan 25, 2021
@neiljp neiljp added this to the Release after next milestone Jan 28, 2021
Base automatically changed from master to main January 30, 2021 20:31
Copy link
Collaborator

@neiljp neiljp left a comment

Choose a reason for hiding this comment

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

@zee-bit This needs rebasing to work around conflicts and potentially update for the latest improvements in the PM UI/data, but the implementation also demonstrates both the duplication and what look like issues due to not duplicating extra code. This could be refactored variously, but we could also "up" from the message content, which might affect how the refactoring could go.

zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
@neiljp
Copy link
Collaborator

neiljp commented Feb 17, 2021

I should say that it does seem to work OK 👍 I've not found a signal emitted within urwid that would solve this for us as it stands.

@neiljp neiljp added PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback and removed PR needs review PR requires feedback to proceed labels Feb 17, 2021
@zee-bit
Copy link
Member Author

zee-bit commented Mar 14, 2021

@zulipbot add "PR needs review"
@zulipbot remove "PR awaiting update"

@zulipbot zulipbot added PR needs review PR requires feedback to proceed and removed PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback labels Mar 14, 2021
Copy link
Member

@prah23 prah23 left a comment

Choose a reason for hiding this comment

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

@zee-bit Thanks for this!
This works as expected and restricts the user from bypassing the validation quite well. 👍
I've dropped a couple of comments inline, as some of these changes would also benefit #937.

zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@neiljp neiljp left a comment

Choose a reason for hiding this comment

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

@zee-bit Extracting the helper(s) for the footer simplifies things to some degree, but the overlap between tab and right/down is still very similar. I'm currently fairly open to this PR since you have a test. However, the code is highly duplicated and I'd much prefer a solution which combined the two branches if we are able, to avoid that. We can always refactor later, but then we accumulate "technical debt".

zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
@neiljp neiljp removed the PR needs review PR requires feedback to proceed label Mar 20, 2021
@neiljp neiljp added the PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback label Mar 20, 2021
@zee-bit zee-bit force-pushed the issue_779 branch 3 times, most recently from 13b0a91 to e32c9d1 Compare March 23, 2021 11:42
@zee-bit
Copy link
Member Author

zee-bit commented Mar 28, 2021

@zulipbot add "PR needs review"
@zulipbot remove "PR awaiting update"

@zulipbot zulipbot added PR needs review PR requires feedback to proceed and removed PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback labels Mar 28, 2021
Copy link
Member

@preetmishra preetmishra left a comment

Choose a reason for hiding this comment

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

@zee-bit Thanks for your work. I pulled code locally and it seems to work well! 👍

While the PR is functional, we could simplify the code a bit. I have left a few in-line comments. Let me know if there's anything that needs clarification.

zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
@@ -709,7 +737,8 @@ def test_keypress_CYCLE_COMPOSE_FOCUS(self, write_box, tab_key,
write_box.stream_box_view(stream_id)
else:
write_box.private_box_view()
size = widget_size(write_box)
# FIXME: Use widget_size(), instead of hard-coded size.
Copy link
Member

Choose a reason for hiding this comment

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

Why aren't we using widget_size?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, I tried using widget_size, but for some cases, the write-box changes to box widget instead of flow and returns a (maxcol, maxrow) size instead of just (maxcol, ). This made the tests fail for such cases. Let me try to dig more into this and see if I can get a fix for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for the late reply. I got busy with other PR's since finding the root+fix of this particular issue was taking forever. :(
I have tried to dig more on this, but it seems like it's an internal urwid issue that I am unable to resolve. While looking at this, I realized that the whole write_box is sometimes recognized as "flow widget" for all tests (that's when all the tests pass), but sometimes the write_box is determined as "box widget" for all tests (2 tests fail in this case).

I also found that in the latter case, the tests only fail for "DOWN" keypresses, because it can't be handled properly by super() i.e. urwid's container.py. I can fix the whole issue by not letting the "DOWN" keypress being handled by urwid and returning early after manually handling, but it seems to have other side-effects and does not look clean.

zulipterminal/ui_tools/boxes.py Outdated Show resolved Hide resolved
('code', primary_key_for_command('AUTOCOMPLETE_REVERSE')),
' to autocomplete.'
]
self.view.set_footer_text(footer_message, 3)
Copy link
Member

Choose a reason for hiding this comment

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

The method is calling an existing method, set_footer_text, with a particular error message. This could lead us to a state where we have multiple methods that essentially do the same thing with different arguments for different error types.

Another way could be to just have an ERROR_TEMPLATES data structure that could be used to pass an appropriate error message to the existing set_footer_text method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it make sense to define the ERROR_TEMPLATES in a separate module?

@preetmishra preetmishra added PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback and removed PR needs review PR requires feedback to proceed labels May 27, 2021
This commit refactors code for footer notification in case of invalid
recipients (stream/email) in the WriteBox to separate helper function.
This prevents duplication of code and allows reusability.

This function can now be called with a custom error message that will
be displayed in the footer after appending it with a common typeahead
suggestion.

Tests amended.
This commit disables cycling from the recipient box (Stream/To) using
arrow keys if recipient is invalid, similar to Tab. Cycling through the
MessageBox now looks for arrow key events and checks for recipient
name's validity when losing focus from the recipient box. This should
prevent sending of messages to incorrect recipients.

Currently, only RIGHT and DOWN keypress is monitored since these are the
only keys that can cycle focus from the recipients box to some other box,
according to our current MessageBox layout.

Tests amended.

Fixes zulip#779.
@zulipbot
Copy link
Member

Heads up @zee-bit, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the upstream/main branch and resolve your pull request's merge conflicts accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request has conflicts PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback PR blocks other PR size: XL [Automatic label added by zulipbot]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disallow cycling from recipient box using arrow keys if recipient is invalid
5 participants