Skip to content

Commit c7042f9

Browse files
committed
Add reveal secret text button to EditorPropertyText
1 parent 594d64e commit c7042f9

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

editor/editor_properties.cpp

+48-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ void EditorPropertyText::_text_changed(const String &p_string) {
9191

9292
// Set tooltip so that the full text is displayed in a tooltip if hovered.
9393
// This is useful when using a narrow inspector, as the text can be trimmed otherwise.
94-
text->set_tooltip_text(get_tooltip_string(text->get_text()));
94+
if (!is_secret || !text->is_secret()) {
95+
text->set_tooltip_text(get_tooltip_string(text->get_text()));
96+
}
9597

9698
if (string_name) {
9799
emit_changed(get_edited_property(), StringName(p_string));
@@ -100,13 +102,41 @@ void EditorPropertyText::_text_changed(const String &p_string) {
100102
}
101103
}
102104

105+
void EditorPropertyText::_show_secret_toggled(const bool &p_toggled) {
106+
text->set_secret(!p_toggled);
107+
if (p_toggled) {
108+
text->set_tooltip_text(get_tooltip_string(text->get_text()));
109+
show_secret->set_button_icon(get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
110+
} else {
111+
text->set_tooltip_text("");
112+
show_secret->set_button_icon(get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
113+
}
114+
}
115+
116+
void EditorPropertyText::_notification(int p_what) {
117+
switch (p_what) {
118+
case NOTIFICATION_ENTER_TREE:
119+
case NOTIFICATION_THEME_CHANGED: {
120+
if (is_secret) {
121+
if (show_secret->is_pressed()) {
122+
show_secret->set_button_icon(get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
123+
} else {
124+
show_secret->set_button_icon(get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
125+
}
126+
}
127+
} break;
128+
}
129+
}
130+
103131
void EditorPropertyText::update_property() {
104132
String s = get_edited_property_value();
105133
updating = true;
106134
if (text->get_text() != s) {
107135
int caret = text->get_caret_column();
108136
text->set_text(s);
109-
text->set_tooltip_text(get_tooltip_string(s));
137+
if (!is_secret || !text->is_secret()) {
138+
text->set_tooltip_text(get_tooltip_string(s));
139+
}
110140
text->set_caret_column(caret);
111141
}
112142
text->set_editable(!is_read_only());
@@ -125,7 +155,23 @@ void EditorPropertyText::set_string_name(bool p_enabled) {
125155
}
126156

127157
void EditorPropertyText::set_secret(bool p_enabled) {
158+
is_secret = p_enabled;
159+
if (p_enabled) {
160+
show_secret = memnew(Button);
161+
show_secret->set_clip_text(true);
162+
show_secret->set_toggle_mode(true);
163+
text->get_parent()->add_child(show_secret);
164+
add_focusable(text);
165+
show_secret->connect(SceneStringName(toggled), callable_mp(this, &EditorPropertyText::_show_secret_toggled));
166+
} else {
167+
text->set_tooltip_text(get_tooltip_string(text->get_text()));
168+
}
128169
text->set_secret(p_enabled);
170+
if (show_secret != nullptr) {
171+
show_secret->set_visible(p_enabled);
172+
show_secret->set_button_icon(get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
173+
show_secret->set_pressed_no_signal(false);
174+
}
129175
}
130176

131177
void EditorPropertyText::set_placeholder(const String &p_string) {

editor/editor_properties.h

+4
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ class EditorPropertyNil : public EditorProperty {
5757
class EditorPropertyText : public EditorProperty {
5858
GDCLASS(EditorPropertyText, EditorProperty);
5959
LineEdit *text = nullptr;
60+
Button *show_secret = nullptr;
6061

6162
bool updating = false;
6263
bool string_name = false;
64+
bool is_secret = false;
6365
void _text_changed(const String &p_string);
6466
void _text_submitted(const String &p_string);
67+
void _show_secret_toggled(const bool &p_toggled);
6568

6669
protected:
6770
virtual void _set_read_only(bool p_read_only) override;
71+
void _notification(int p_what);
6872

6973
public:
7074
void set_string_name(bool p_enabled);

0 commit comments

Comments
 (0)