Skip to content

Commit 64da010

Browse files
committed
view: Fix focus before and after search.
This commit corrects what the code for focus_index_before_search was intended to do. The focus index was still being updated even in the search results. This is fixed by adding a boolean `in_search_mode` which does not call `get_focus` untill the search is over. Slight reordering was done in `__init__` to fit the boolean in an ideal place w.r.t. readability. Fixes zulip#975
1 parent b8e910d commit 64da010

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

zulipterminal/ui_tools/views.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,17 @@ def __init__(self) -> None:
285285
class StreamsView(urwid.Frame):
286286
def __init__(self, streams_btn_list: List[Any], view: Any) -> None:
287287
self.view = view
288-
self.log = urwid.SimpleFocusListWalker(streams_btn_list)
288+
# Create Stream List Box
289289
self.streams_btn_list = streams_btn_list
290-
self.focus_index_before_search = 0
290+
self.log = urwid.SimpleFocusListWalker(streams_btn_list)
291291
list_box = urwid.ListBox(self.log)
292+
# Create Stream Search Box
292293
self.stream_search_box = PanelSearchBox(
293294
self, "SEARCH_STREAMS", self.update_streams
294295
)
296+
self.focus_index_before_search = 0
297+
self.in_search_mode = False
298+
# Create Stream View Frame
295299
super().__init__(
296300
list_box,
297301
header=urwid.LineBox(
@@ -364,6 +368,7 @@ def mouse_event(
364368

365369
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
366370
if is_command_key("SEARCH_STREAMS", key):
371+
self.in_search_mode = True
367372
self.set_focus("header")
368373
return key
369374
elif is_command_key("GO_BACK", key):
@@ -373,9 +378,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
373378
self.set_focus("body")
374379
self.log.set_focus(self.focus_index_before_search)
375380
self.view.controller.update_screen()
381+
self.in_search_mode = False
376382
return key
377383
return_value = super().keypress(size, key)
378-
_, self.focus_index_before_search = self.log.get_focus()
384+
if not self.in_search_mode:
385+
_, self.focus_index_before_search = self.log.get_focus()
379386
return return_value
380387

381388

@@ -384,17 +391,21 @@ def __init__(
384391
self, topics_btn_list: List[Any], view: Any, stream_button: Any
385392
) -> None:
386393
self.view = view
387-
self.log = urwid.SimpleFocusListWalker(topics_btn_list)
388-
self.topics_btn_list = topics_btn_list
389394
self.stream_button = stream_button
390-
self.focus_index_before_search = 0
395+
# Create Topic List Box
396+
self.topics_btn_list = topics_btn_list
397+
self.log = urwid.SimpleFocusListWalker(topics_btn_list)
391398
self.list_box = urwid.ListBox(self.log)
399+
# Create Topic Search Box
392400
self.topic_search_box = PanelSearchBox(
393401
self, "SEARCH_TOPICS", self.update_topics
394402
)
403+
self.focus_index_before_search = 0
404+
self.in_search_mode = False
395405
self.header_list = urwid.Pile(
396406
[self.stream_button, urwid.Divider("─"), self.topic_search_box]
397407
)
408+
# Create Topic View Frame
398409
super().__init__(
399410
self.list_box,
400411
header=urwid.LineBox(
@@ -476,6 +487,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
476487
self.view.show_left_panel(visible=False)
477488
self.view.body.focus_col = 1
478489
if is_command_key("SEARCH_TOPICS", key):
490+
self.in_search_mode = True
479491
self.set_focus("header")
480492
self.header_list.set_focus(2)
481493
return key
@@ -486,9 +498,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
486498
self.set_focus("body")
487499
self.log.set_focus(self.focus_index_before_search)
488500
self.view.controller.update_screen()
501+
self.in_search_mode = False
489502
return key
490503
return_value = super().keypress(size, key)
491-
_, self.focus_index_before_search = self.log.get_focus()
504+
if not self.in_search_mode:
505+
_, self.focus_index_before_search = self.log.get_focus()
492506
return return_value
493507

494508

0 commit comments

Comments
 (0)