Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Fix" password blinking but break alignment and efficiency #2317

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions arcade/gui/experimental/password_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,61 @@

from typing import Optional

import arcade.color
from arcade.gui import Surface, UIEvent, UIInputText, UITextInputEvent
from arcade.types import Color, RGBOrA255


class UIPasswordInput(UIInputText):
"""A password input field. The text is hidden with asterisks."""

def __init__(
self,
*,
x: float = 0,
y: float = 0,
width: float = 100,
height: float = 24,
text: str = "",
font_name=("Arial",),
font_size: float = 12,
text_color: RGBOrA255 = (0, 0, 0, 255),
multiline=False,
caret_color: RGBOrA255 = (0, 0, 0, 255),
size_hint=None,
size_hint_min=None,
size_hint_max=None,
**kwargs,
):
replaced = "*" * len(text)
super().__init__(
x=x,
y=y,
width=width,
height=height,
text=replaced,
font_name=font_name,
font_size=font_size,
text_color=arcade.color.TRANSPARENT_BLACK,
multiline=multiline,
caret_color=caret_color,
size_hint=size_hint,
size_hint_min=size_hint_min,
size_hint_max=size_hint_max,
**kwargs,
)
self._font_name = font_name
self._font_size = font_size
self._asterisk_color = Color.from_iterable(text_color)

@property
def text_color(self) -> Color:
return self._asterisk_color

@text_color.setter
def text_color(self, new_value: RGBOrA255):
self._asterisk_color = Color.from_iterable(new_value)

def on_event(self, event: UIEvent) -> Optional[bool]:
"""Remove new lines from the input, which are not allowed in passwords."""
if isinstance(event, UITextInputEvent):
Expand All @@ -16,11 +65,19 @@ def on_event(self, event: UIEvent) -> Optional[bool]:

def do_render(self, surface: Surface):
"""Override to render the text as asterisks."""
self.layout.begin_update()
position = self.caret.position
text = self.text
self.text = "*" * len(self.text)
asterisks = "*" * len(self.text)
# FIXME: I fixed blinking, but broke efficiency and alignment :D
with surface.activate():
arcade.draw_text(
asterisks,
x=self.rect.left,
y=self.rect.bottom,
anchor_x="left",
anchor_y="bottom",
align="left",
width=int(self.width),
font_name=self._font_name,
font_size=self._font_size,
color=self._asterisk_color,
)
super().do_render(surface)
self.text = text
self.caret.position = position
self.layout.end_update()
2 changes: 2 additions & 0 deletions arcade/gui/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ def on_event(self, event: UIEvent) -> Optional[bool]:
if self._active:
# Act on events if active
if isinstance(event, UITextInputEvent):
if not self.layout.multiline:
event.text = event.text.replace("\r", "").replace("\n", "")
self.caret.on_text(event.text)
self.trigger_full_render()
elif isinstance(event, UITextMotionEvent):
Expand Down
Loading