Skip to content

Commit

Permalink
Simplify focus finder code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gouvernathor committed Mar 17, 2023
1 parent 8342a66 commit 19fa815
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions renpy/test/testfocus.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def find_focus(pattern):
If `pattern` could not be found, returns None.
"""

pattern = pattern.casefold()

def match(f):

if pattern is None:
Expand All @@ -54,27 +56,24 @@ def match(f):
else:
t = f.widget._tts_all()

if pattern.lower() in t.lower():
if pattern in t.casefold():
return t
else:
return None

# A list of alt_text, focus pairs.
matching = [ ]
# {focus : (len(alt), alt)}
matching = {}

for f in renpy.display.focus.focus_list:

alt = match(f)

if alt is not None:
matching.append((alt, f))

if not matching:
return None
matching[f] = (len(alt), alt)

# This gets the matching displayable with the shortest alt text, which
# is likely what we want.
return min(matching, key=lambda a : (len(a[0]), a[0]))[1]
return min(matching, key=matching.get, default=None) # type: ignore


def relative_position(x, posx, width):
Expand Down

0 comments on commit 19fa815

Please sign in to comment.