Skip to content

Commit

Permalink
PPSSPP UI: Implement password masking in popup text inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Sep 26, 2024
1 parent f020d1d commit 21ec20b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Common/UI/PopupScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ EventReturn PopupTextInputChoice::HandleClick(EventParams &e) {

// Choose method depending on platform capabilities.
if (System_GetPropertyBool(SYSPROP_HAS_TEXT_INPUT_DIALOG)) {
System_InputBoxGetString(token_, text_, *value_, passwordDisplay_, [=](const std::string &enteredValue, int) {
System_InputBoxGetString(token_, text_, *value_, passwordMasking_, [=](const std::string &enteredValue, int) {
*value_ = StripSpaces(enteredValue);
EventParams params{};
OnChange.Trigger(params);
Expand All @@ -537,6 +537,7 @@ EventReturn PopupTextInputChoice::HandleClick(EventParams &e) {
}

TextEditPopupScreen *popupScreen = new TextEditPopupScreen(value_, placeHolder_, ChopTitle(text_), maxLen_);
popupScreen->SetPasswordMasking(passwordMasking_);
if (System_GetPropertyBool(SYSPROP_KEYBOARD_IS_SOFT)) {
popupScreen->SetAlignTop(true);
}
Expand Down Expand Up @@ -570,6 +571,7 @@ void TextEditPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
edit_ = new TextEdit(textEditValue_, Title(), placeholder_, new LinearLayoutParams(1.0f));
edit_->SetMaxLen(maxLen_);
edit_->SetTextColor(dc.theme->popupStyle.fgColor);
edit_->SetPasswordMasking(passwordMasking_);
lin->Add(edit_);

UI::SetFocusedView(edit_);
Expand Down Expand Up @@ -628,7 +630,7 @@ void AbstractChoiceWithValueDisplay::Draw(UIContext &dc) {

std::string valueText = ValueText();

if (passwordDisplay_) {
if (passwordMasking_) {
// Replace all characters with stars.
memset(&valueText[0], '*', valueText.size());
}
Expand Down
5 changes: 5 additions & 0 deletions Common/UI/PopupScreens.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ class TextEditPopupScreen : public PopupScreen {

const char *tag() const override { return "TextEditPopup"; }

void SetPasswordMasking(bool masking) {
passwordMasking_ = masking;
}

Event OnChange;

private:
Expand All @@ -159,6 +163,7 @@ class TextEditPopupScreen : public PopupScreen {
std::string textEditValue_;
std::string placeholder_;
int maxLen_;
bool passwordMasking_ = false;
};

struct ContextMenuItem {
Expand Down
11 changes: 9 additions & 2 deletions Common/UI/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,18 +1142,25 @@ void TextEdit::Draw(UIContext &dc) {
Bounds textBounds = bounds_;
textBounds.x = textX - scrollPos_;

std::string textToDisplay = text_;
if (passwordMasking_) {
for (int i = 0; i < textToDisplay.size(); i++) {
textToDisplay[i] = '*';
}
}

if (text_.empty()) {
if (placeholderText_.size()) {
uint32_t c = textColor & 0x50FFFFFF;
dc.DrawTextRect(placeholderText_, bounds_, c, ALIGN_CENTER);
}
} else {
dc.DrawTextRect(text_, textBounds, textColor, ALIGN_VCENTER | ALIGN_LEFT | align_);
dc.DrawTextRect(textToDisplay, textBounds, textColor, ALIGN_VCENTER | ALIGN_LEFT | align_);
}

if (HasFocus()) {
// Hack to find the caret position. Might want to find a better way...
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.substr(0, caret_), &w, &h, ALIGN_VCENTER | ALIGN_LEFT | align_);
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, textToDisplay.substr(0, caret_), &w, &h, ALIGN_VCENTER | ALIGN_LEFT | align_);
float caretX = w - scrollPos_;
if (caretX > bounds_.w) {
scrollPos_ += caretX - bounds_.w;
Expand Down
8 changes: 6 additions & 2 deletions Common/UI/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,14 +825,14 @@ class AbstractChoiceWithValueDisplay : public Choice {
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;

void SetPasswordDisplay() {
passwordDisplay_ = true;
passwordMasking_ = true;
}
protected:
virtual std::string ValueText() const = 0;

float CalculateValueScale(const UIContext &dc, std::string_view valueText, float availWidth) const;

bool passwordDisplay_ = false;
bool passwordMasking_ = false;
};

class ChoiceWithCallbackValueDisplay : public AbstractChoiceWithValueDisplay {
Expand Down Expand Up @@ -1026,6 +1026,9 @@ class TextEdit : public View {
const std::string &GetText() const { return text_; }
void SetMaxLen(size_t maxLen) { maxLen_ = maxLen; }
void SetTextAlign(int align) { align_ = align; } // Only really useful for setting FLAG_DYNAMIC_ASCII
void SetPasswordMasking(bool masking) {
passwordMasking_ = masking;
}

void FocusChanged(int focusFlags) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
Expand All @@ -1050,6 +1053,7 @@ class TextEdit : public View {
int scrollPos_ = 0;
size_t maxLen_;
bool ctrlDown_ = false; // TODO: Make some global mechanism for this.
bool passwordMasking_ = false;
int align_ = 0;
// TODO: Selections
};
Expand Down
2 changes: 1 addition & 1 deletion UI/RetroAchievementScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void RetroAchievementsSettingsScreen::CreateAccountTab(UI::ViewGroup *viewGroup)
Achievements::Logout();
return UI::EVENT_DONE;
});
} else if (false && System_GetPropertyBool(SYSPROP_HAS_LOGIN_DIALOG)) {
} else if (System_GetPropertyBool(SYSPROP_HAS_LOGIN_DIALOG)) {
viewGroup->Add(new Choice(di->T("Log in")))->OnClick.Add([=](UI::EventParams &) -> UI::EventReturn {
std::string title = StringFromFormat("RetroAchievements: %s", di->T_cstr("Log in"));
System_AskUsernamePassword(GetRequesterToken(), title, [](const std::string &value, int) {
Expand Down

0 comments on commit 21ec20b

Please sign in to comment.