Skip to content

Commit 9bac031

Browse files
committed
fixing jumping and minimizing show() in PyRadioKeyboardConfig
1 parent ae5e396 commit 9bac031

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

pyradio/config_window.py

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,6 +3452,7 @@ def __init__(
34523452
logger.error(f'{keys_file = }')
34533453
with open(keys_file, 'r', encoding='utf-8') as f:
34543454
self._classes = json.load(f)
3455+
self._needs_update = False
34553456

34563457

34573458
def item(self, an_item_id=None):
@@ -3532,12 +3533,12 @@ def _focus_previous(self):
35323533
def _go_top(self):
35333534
self._start = 0
35343535
self._selection = 1
3535-
self.show()
3536+
self._needs_update = True
35363537

35373538
def _go_bottom(self):
35383539
self._selection = len(self._list) -1
35393540
self._start = self._selection - self._number_of_lines + 1
3540-
self.show()
3541+
self._needs_update = True
35413542

35423543
def _go_down(self, step=1):
35433544
logger.error(f'go_down: {step = }')
@@ -3555,7 +3556,7 @@ def _go_down(self, step=1):
35553556
if next_selection >= len(self._list):
35563557
self._selection = 1
35573558
self._start = 0
3558-
self.show()
3559+
self._needs_update = True
35593560
return
35603561
line = next_selection - self._start + 2
35613562
logger.error(f'{line = }, {self._number_of_lines = }')
@@ -3564,7 +3565,7 @@ def _go_down(self, step=1):
35643565
while line > self._number_of_lines + 1:
35653566
self._start += (next_selection - self._selection)
35663567
self._selection = next_selection
3567-
self.show()
3568+
self._needs_update = True
35683569
return
35693570
if 2 < line <= self._number_of_lines + 1:
35703571
logger.error('=== between')
@@ -3591,7 +3592,7 @@ def _go_up(self, step=1):
35913592
if next_selection < 0:
35923593
self._selection = len(self._list) - 1
35933594
self._start = len(self._list) - self._number_of_lines
3594-
self.show()
3595+
self._needs_update = True
35953596
return
35963597
line = next_selection - self._start - 2
35973598
line = next_selection - self._start + 1
@@ -3601,7 +3602,7 @@ def _go_up(self, step=1):
36013602
if self._selection in self._headers:
36023603
self._selection =- 1
36033604
self._start = self._selection
3604-
self.show()
3605+
self._needs_update = True
36053606
return
36063607
if 1 <= line <= self._number_of_lines:
36073608
logger.error('=== between')
@@ -3648,28 +3649,40 @@ def _init_win(self):
36483649
self._make_selection_visible()
36493650

36503651
def _make_selection_visible(self):
3651-
chk = self._start - self._selection
3652-
logger.error(f'{self._start= }')
3653-
logger.error(f'{self._selection = }')
3654-
logger.error(f'{chk = }')
3652+
# Check if the current selection is already visible
3653+
if self._start <= self._selection < self._start + self._number_of_lines:
3654+
# Ensure that we use the expanded window space effectively
3655+
if self._start + self._number_of_lines > len(self._list):
3656+
# Adjust `self._start` to fill the screen if the end of the list is reached
3657+
self._start = max(0, len(self._list) - self._number_of_lines)
3658+
return
3659+
3660+
# Selection is not visible, adjust `self._start` as per existing behavior
3661+
chk = self._start - self._selection
3662+
3663+
# Case 1: Selection is near the bottom of the list
36553664
if self._selection > len(self._list) - self._number_of_lines:
3656-
self._start = len(self._list) - self._number_of_lines
3657-
else:
3658-
if chk < 0:
3659-
# start is after selection
3660-
self._start = self._selection
3661-
if (self._start - 1) in self._headers:
3662-
self._start -= 1
3663-
else:
3664-
# start is before selection
3665-
# is it too far?
3666-
if chk > self._number_of_lines:
3667-
self._start = int((self._selection - self._number_of_lines) / 2)
3665+
self._start = max(0, len(self._list) - self._number_of_lines)
3666+
3667+
# Case 2: Selection is below the visible range
3668+
elif chk < 0:
3669+
# Try to center the selection in the visible area
3670+
proposed_start = self._selection - self._number_of_lines // 2
3671+
3672+
# Ensure no empty lines at the bottom
3673+
self._start = min(proposed_start, len(self._list) - self._number_of_lines)
3674+
3675+
# Ensure `self._start` is not negative
3676+
self._start = max(0, self._start)
3677+
3678+
# Case 3: Selection is above the visible range
3679+
elif chk > self._number_of_lines:
3680+
self._start = max(0, int((self._selection - self._number_of_lines) / 2))
36683681

36693682
def _go_to_line(self, a_line):
36703683
self._selection = a_line
36713684
self._make_selection_visible()
3672-
self.show()
3685+
self._needs_update = True
36733686

36743687
def _select_line(self, a_line):
36753688
self._win.chgat(a_line, 2, self.maxX-4, curses.color_pair(6))
@@ -3679,6 +3692,7 @@ def _unselect_line(self, a_line):
36793692
self._win.chgat(a_line, self._max_length, self.maxX-self._max_length-2, curses.color_pair(4))
36803693

36813694
def show(self, parent=None):
3695+
logger.error('def show()')
36823696
if parent is not None:
36833697
self._parent = parent
36843698
self._init_win()
@@ -3942,6 +3956,7 @@ def keypress(self, char):
39423956
1: Continue
39433957
2: Display help
39443958
'''
3959+
self._needs_update = False
39453960
if char == ord('0'):
39463961
if self.existing_conflict:
39473962
self._editing = False
@@ -4006,15 +4021,15 @@ def keypress(self, char):
40064021
elif char == ord('x'):
40074022
self._list[self._selection][3] = self._list[self._selection][2]
40084023
self._list[self._selection][6] = self._list[self._selection][5]
4009-
self.show()
4024+
self._needs_update = True
40104025
elif char == ord(']'):
40114026
self._get_after_header()
40124027
self._make_selection_visible()
4013-
self.show()
4028+
self._needs_update = True
40144029
elif char == ord('['):
40154030
self._get_after_header(next=False)
40164031
self._make_selection_visible()
4017-
self.show()
4032+
self._needs_update = True
40184033
elif char in (curses.KEY_HOME, kbkey['g']):
40194034
if self._focus == 0:
40204035
self._go_top()
@@ -4071,6 +4086,10 @@ def keypress(self, char):
40714086
logger.error(f'{self._start = }')
40724087
logger.error(f'{self._selection = }')
40734088
logger.error('line = {}'.format(self._selection - self._start + 2))
4089+
4090+
# Centralized UI update
4091+
if self._needs_update:
4092+
self.show()
40744093
return 1
40754094

40764095
# pymode:lint_ignore=W901

0 commit comments

Comments
 (0)