Skip to content

Commit

Permalink
Improve if/return checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Oct 21, 2024
1 parent f08e194 commit 5722df8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
12 changes: 6 additions & 6 deletions pygame_menu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ def _update_widget_position(self) -> None:

# Check if the maximum number of elements was reached, if so raise an exception
# If menu has frames, this check is disabled
if not has_frame and not i_index < self._max_row_column_elements:
elif not has_frame and not i_index < self._max_row_column_elements:
raise _MenuWidgetOverflow(max_elements_msg.replace('[widg]', str(i_index)))

# Set the widget column/row position
Expand Down Expand Up @@ -1499,7 +1499,7 @@ def get_rect(wid: 'Widget') -> 'pygame.Rect':
continue

# If widget within frame update col/row position
if widget.get_frame() is not None:
elif widget.get_frame() is not None:
# noinspection PyProtectedMember
widget._set_position_relative_to_frame(index)
continue
Expand Down Expand Up @@ -1540,7 +1540,7 @@ def get_rect(wid: 'Widget') -> 'pygame.Rect':
_, r, _ = r_widget.get_col_row_index()
if r >= row:
break
if (
elif (
r_widget.is_visible() and
not r_widget.is_floating() and
not r_widget.get_frame() is not None
Expand Down Expand Up @@ -1866,7 +1866,7 @@ def center_content(self) -> 'Menu':
if len(self._widgets) == 0: # If this happens, get_widget_max returns an immense value
self._widget_offset[1] = 0
return self
if self._widgets_surface is None:
elif self._widgets_surface is None:
self._update_widget_position() # For position (max/min)
available = self.get_height(inner=True)
widget_height = self.get_height(widget=True)
Expand Down Expand Up @@ -2602,7 +2602,7 @@ def update(self, events: EventVectorType) -> bool:
if self._keyboard_ignore_nonphysical and not check_key_pressed_valid(event):
continue

if self._ctrl.move_down(event, self):
elif self._ctrl.move_down(event, self):
if self._current._down(apply_sound=True):
self._current._last_update_mode.append(_events.MENU_LAST_MOVE_DOWN)
updated = True
Expand Down Expand Up @@ -3323,7 +3323,7 @@ def _select(
return False

# This stores +/-1 if the index increases or decreases, used by non-selectable selection
if dwidget == 0:
elif dwidget == 0:
if new_index < self._index:
dwidget = -1
else:
Expand Down
20 changes: 9 additions & 11 deletions pygame_menu/widgets/widget/textinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,10 @@ def _render_cursor(self) -> None:
return

# Cursor surface does not exist
if self._cursor_surface is None:
elif self._cursor_surface is None:
if self._rect.height == 0: # If Menu has not been initialized this error can occur
return
if self._cursor_size is not None:
elif self._cursor_size is not None:
self._cursor_surface = make_surface(*self._cursor_size)
else:
self._cursor_surface = make_surface(self._font_size / 20 + 1, self._rect.height - 2)
Expand Down Expand Up @@ -769,7 +769,6 @@ def _get_input_string(self, add_ellipsis: bool = True) -> str:
:return: String
"""
string = self._get_input_string_filtered()

if self._maxwidth != 0 and len(string) > self._maxwidth:
text = string[self._renderbox[0]:self._renderbox[1]]
if add_ellipsis:
Expand All @@ -778,8 +777,7 @@ def _get_input_string(self, add_ellipsis: bool = True) -> str:
if self._ellipsis_left():
text = self._ellipsis + text
return text
else:
return string
return string

def _update_renderbox(
self,
Expand Down Expand Up @@ -814,7 +812,7 @@ def _update_renderbox(
return

# Move cursor to start
if start:
elif start:
self._renderbox[0] = 0
self._renderbox[1] = min(len_string, self._maxwidth)
self._renderbox[2] = 0
Expand All @@ -825,7 +823,7 @@ def _update_renderbox(
return

# If no overflow
if len_string <= self._maxwidth:
elif len_string <= self._maxwidth:
if right < 0 and self._renderbox[2] == len_string: # If del at the end of string
return
if left < 0 and self._renderbox[2] == 0: # If cursor is at beginning
Expand All @@ -849,7 +847,7 @@ def _update_renderbox(
if right < 0 and self._renderbox[2] == self._maxwidth:
return
# If backspace at beginning of string
if left < 0 and self._renderbox[2] == 0:
elif left < 0 and self._renderbox[2] == 0:
return

# If user deletes something and it is in the end
Expand Down Expand Up @@ -1064,7 +1062,7 @@ def set_value(self, text: Any) -> None:
"""
if self._password and text != '':
raise ValueError('value cannot be set in password type')
if self._check_input_type(text):
elif self._check_input_type(text):
default_text = str(text)

# Filter valid chars
Expand Down Expand Up @@ -1120,7 +1118,7 @@ def _check_input_type(self, string: Any) -> bool:

if string == '-':
return True
if conv is None:
elif conv is None:
return False

try:
Expand Down Expand Up @@ -1479,7 +1477,7 @@ def _push_key_input(self, keychar: str, sounds: bool = True) -> bool:
return False

# Check if char is valid
if self._valid_chars is not None and keychar not in self._valid_chars:
elif self._valid_chars is not None and keychar not in self._valid_chars:
if sounds:
self._sound.play_event_error()
return False
Expand Down

0 comments on commit 5722df8

Please sign in to comment.