Skip to content

Commit b76e1a1

Browse files
committed
more accurately check that a row of text ends with a single space or not.
1 parent 65cfadf commit b76e1a1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pygame_gui/core/text/text_box_layout.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ def get_cursor_pos_move_up_one_row(self, last_cursor_horiz_index):
14521452
row_above = self.layout_rows[i - 1]
14531453
cursor_index -= row_above.letter_count
14541454
row_above_end = row_above.letter_count
1455-
if (row_above.row_text_ends_with_a_space() or
1455+
if (row_above.row_text_ends_with_a_single_space() or
14561456
(len(row_above.items) > 0 and
14571457
isinstance(row_above.items[-1], LineBreakLayoutRect))):
14581458
row_above_end = row_above.letter_count - 1
@@ -1481,7 +1481,7 @@ def get_cursor_pos_move_down_one_row(self, last_cursor_horiz_index):
14811481
row_below = self.layout_rows[i + 1]
14821482
cursor_index += row.letter_count
14831483
row_below_end = row_below.letter_count
1484-
if (row_below.row_text_ends_with_a_space() or
1484+
if (row_below.row_text_ends_with_a_single_space() or
14851485
(len(row_below.items) > 0 and
14861486
isinstance(row_below.items[-1], LineBreakLayoutRect))):
14871487
row_below_end = row_below.letter_count - 1

pygame_gui/core/text/text_box_layout_row.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def find_cursor_pos_from_click_pos(self, click_pos: Tuple[int, int], num_rows: i
428428
if (not found_chunk and scrolled_click_pos[0] >= self.right) or (letter_acc == self.letter_count):
429429
# if we have more than two rows check if we are on right of whole row and if row has space at the end.
430430
# If so stick the edit cursor before the space because this is how it works.
431-
if num_rows > 1 and self.row_text_ends_with_a_space():
431+
if num_rows > 1 and self.row_text_ends_with_a_single_space():
432432
letter_acc -= 1
433433
last_chunk = self.get_last_text_chunk()
434434
if last_chunk is not None:
@@ -535,10 +535,18 @@ def insert_linebreak_after_chunk(self, chunk_to_insert_after: Union[TextLineChun
535535
empty_text_chunk = parser.create_styled_text_chunk('')
536536
self.items.insert(chunk_insert_index + 1, empty_text_chunk)
537537

538-
def row_text_ends_with_a_space(self):
538+
@staticmethod
539+
def string_ends_with_a_single_space(string_to_check: str) -> bool:
540+
if string_to_check[-1] == " " and (
541+
len(string_to_check) == 1 or string_to_check[-2] != " "
542+
):
543+
return True
544+
return False
545+
546+
def row_text_ends_with_a_single_space(self):
539547
for item in reversed(self.items):
540548
if isinstance(item, TextLineChunkFTFont):
541-
if len(item.text) > 0 and item.text[-1] == " ":
549+
if len(item.text) > 0 and TextBoxLayoutRow.string_ends_with_a_single_space(item.text):
542550
return True
543551
return False
544552

0 commit comments

Comments
 (0)