Skip to content

Commit 652653d

Browse files
committed
views: Ignore mouse click if compose box is open.
Clicking on messages/user names when the compose box is open causes the current message to be lost. This commits ignores the mouse click to prevent the typed data to be lost, though better handling can be done when the drafts feature is implemented. Also, stored controller as an attribute for UsersView. Tests amended for this. Test added for mouse click event. Fixes #652.
1 parent 8884f66 commit 652653d

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

tests/ui/test_ui_tools.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ class TestUsersView:
686686
@pytest.fixture
687687
def user_view(self, mocker):
688688
mocker.patch(VIEWS + ".urwid.SimpleFocusListWalker", return_value=[])
689-
return UsersView("USER_BTN_LIST")
689+
controller = mocker.Mock()
690+
return UsersView(controller, "USER_BTN_LIST")
690691

691692
def test_mouse_event(self, mocker, user_view):
692693
mocker.patch.object(user_view, 'keypress')
@@ -702,15 +703,32 @@ def test_mouse_event(self, mocker, user_view):
702703
user_view.mouse_event(size, "mouse press", 5, col, row, focus)
703704
user_view.keypress.assert_called_with(size, "down")
704705

706+
@pytest.mark.parametrize('compose_box_open', [True, False])
707+
def test_mouse_event_left_click(self, mocker, user_view,
708+
compose_box_open):
709+
super_mouse_event = mocker.patch(
710+
'zulipterminal.ui.urwid.ListBox.mouse_event')
711+
user_view.controller.is_in_editor_mode.return_value = (
712+
compose_box_open
713+
)
714+
size = (20, )
715+
focus = mocker.Mock()
716+
717+
user_view.mouse_event(size, 'mouse press', 1, 1, 1, focus)
718+
719+
if compose_box_open:
720+
super_mouse_event.assert_not_called()
721+
else:
722+
super_mouse_event.assert_called_once_with(size, 'mouse press', 1,
723+
1, 1, focus)
724+
705725
@pytest.mark.parametrize('event, button', [
706726
('mouse release', 0),
707-
('mouse press', 1),
708727
('mouse press', 3),
709728
('mouse release', 4),
710729
],
711730
ids=[
712731
'unsupported_mouse_release_action',
713-
'unsupported_left_click_mouse_press_action',
714732
'unsupported_right_click_mouse_press_action',
715733
'invalid_event_button_combination',
716734
]
@@ -1048,7 +1066,8 @@ def test_users_view(self, users, users_btn_len, editor_mode, status,
10481066
color='user_' + self.view.users[0]['status'],
10491067
count=1
10501068
)
1051-
users_view.assert_called_once_with(right_col_view.users_btn_list)
1069+
users_view.assert_called_once_with(self.view.controller,
1070+
right_col_view.users_btn_list)
10521071
assert len(right_col_view.users_btn_list) == users_btn_len
10531072

10541073
@pytest.mark.parametrize('key', keys_for_command('SEARCH_PEOPLE'))
@@ -2248,21 +2267,30 @@ def test_footlinks_view(self, message_fixture, message_links,
22482267
assert footlinks is None
22492268
assert not hasattr(footlinks, 'original_widget')
22502269

2270+
@pytest.mark.parametrize('compose_box_open', [True, False])
22512271
@pytest.mark.parametrize(
22522272
'key', keys_for_command('ENTER'),
22532273
ids=lambda param: 'left_click-key:{}'.format(param)
22542274
)
2255-
def test_mouse_event_left_click(self, mocker, msg_box, key):
2275+
def test_mouse_event_left_click(self, mocker, msg_box, key,
2276+
compose_box_open):
22562277
size = (20, )
22572278
col = 1
22582279
row = 1
22592280
focus = mocker.Mock()
22602281
mocker.patch(BOXES + '.keys_for_command', return_value=[key])
22612282
mocker.patch.object(msg_box, 'keypress')
2283+
msg_box.model = mocker.Mock()
2284+
msg_box.model.controller.is_in_editor_mode.return_value = (
2285+
compose_box_open
2286+
)
22622287

22632288
msg_box.mouse_event(size, 'mouse press', 1, col, row, focus)
22642289

2265-
msg_box.keypress.assert_called_once_with(size, key)
2290+
if compose_box_open:
2291+
msg_box.keypress.assert_not_called()
2292+
else:
2293+
msg_box.keypress.assert_called_once_with(size, key)
22662294

22672295

22682296
class TestTopButton:

zulipterminal/ui_tools/boxes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ def mouse_event(self, size: urwid_Size, event: str, button: int,
833833
col: int, row: int, focus: bool) -> bool:
834834
if event == 'mouse press':
835835
if button == 1:
836+
if self.model.controller.is_in_editor_mode():
837+
return True
836838
self.keypress(size, keys_for_command('ENTER').pop())
837839
return True
838840
elif event == 'mouse drag':

zulipterminal/ui_tools/views.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,17 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
440440

441441

442442
class UsersView(urwid.ListBox):
443-
def __init__(self, users_btn_list: List[Any]) -> None:
443+
def __init__(self, controller: Any, users_btn_list: List[Any]) -> None:
444444
self.log = urwid.SimpleFocusListWalker(users_btn_list)
445+
self.controller = controller
445446
super().__init__(self.log)
446447

447448
def mouse_event(self, size: urwid_Size, event: str, button: int, col: int,
448449
row: int, focus: bool) -> bool:
449450
if event == 'mouse press':
451+
if button == 1:
452+
if self.controller.is_in_editor_mode():
453+
return True
450454
if button == 4:
451455
for _ in range(5):
452456
self.keypress(size, 'up')
@@ -642,7 +646,7 @@ def users_view(self, users: Any=None) -> Any:
642646
count=unread_count
643647
)
644648
)
645-
user_w = UsersView(users_btn_list)
649+
user_w = UsersView(self.view.controller, users_btn_list)
646650
# Donot reset them while searching.
647651
if reset_default_view_users:
648652
self.users_btn_list = users_btn_list
@@ -657,7 +661,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
657661
elif is_command_key('GO_BACK', key):
658662
self.user_search.reset_search_text()
659663
self.allow_update_user_list = True
660-
self.body = UsersView(self.users_btn_list)
664+
self.body = UsersView(self.view.controller,
665+
self.users_btn_list)
661666
self.set_body(self.body)
662667
self.set_focus('body')
663668
self.view.controller.update_screen()

0 commit comments

Comments
 (0)