From aff9aa6fa6d4c73139d13a3d536fb1cf16fe6af5 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 19 Nov 2021 05:21:32 -0500 Subject: [PATCH] Add TextField::password property, make PasswordField simply set that property. --- include/rack.hpp | 1 - include/ui/PasswordField.hpp | 17 ----------------- include/ui/TextField.hpp | 9 +++++++++ src/ui/PasswordField.cpp | 17 ----------------- src/ui/TextField.cpp | 10 +++++++++- 5 files changed, 18 insertions(+), 36 deletions(-) delete mode 100644 include/ui/PasswordField.hpp delete mode 100644 src/ui/PasswordField.cpp diff --git a/include/rack.hpp b/include/rack.hpp index 5490291d..4525e5f9 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -64,7 +64,6 @@ Directly including Rack headers other than rack.hpp in your plugin is unsupporte #include #include #include -#include #include #include diff --git a/include/ui/PasswordField.hpp b/include/ui/PasswordField.hpp deleted file mode 100644 index b8c0b723..00000000 --- a/include/ui/PasswordField.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include -#include - - -namespace rack { -namespace ui { - - -/** A TextField that hides/replaces all characters with "*" */ -struct PasswordField : TextField { - void draw(const DrawArgs& args) override; -}; - - -} // namespace ui -} // namespace rack diff --git a/include/ui/TextField.hpp b/include/ui/TextField.hpp index b9b40680..2d1fabf0 100644 --- a/include/ui/TextField.hpp +++ b/include/ui/TextField.hpp @@ -11,6 +11,8 @@ namespace ui { struct TextField : widget::OpaqueWidget { std::string text; std::string placeholder; + /** Masks text with "*". */ + bool password = false; bool multiline = false; /** The index of the text cursor */ int cursor = 0; @@ -48,5 +50,12 @@ struct TextField : widget::OpaqueWidget { }; +struct PasswordField : TextField { + PasswordField() { + password = true; + } +}; + + } // namespace ui } // namespace rack diff --git a/src/ui/PasswordField.cpp b/src/ui/PasswordField.cpp deleted file mode 100644 index d127f7dc..00000000 --- a/src/ui/PasswordField.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include - - -namespace rack { -namespace ui { - - -void PasswordField::draw(const DrawArgs& args) { - std::string textTmp = text; - text = std::string(textTmp.size(), '*'); - TextField::draw(args); - text = textTmp; -} - - -} // namespace ui -} // namespace rack diff --git a/src/ui/TextField.cpp b/src/ui/TextField.cpp index 52ac2f52..e431256b 100644 --- a/src/ui/TextField.cpp +++ b/src/ui/TextField.cpp @@ -69,7 +69,15 @@ void TextField::draw(const DrawArgs& args) { int begin = std::min(cursor, selection); int end = std::max(cursor, selection); - bndTextField(args.vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, text.c_str(), begin, end); + std::string drawText; + if (password) { + drawText = std::string(text.size(), '*'); + } + else { + drawText = text; + } + + bndTextField(args.vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, drawText.c_str(), begin, end); // Draw placeholder text if (text.empty()) {