Merge pull request #29979 from flomar/master

Line edit placeholders now react properly to translation changes.
This commit is contained in:
Rémi Verschelde 2019-06-25 08:59:31 +02:00 committed by GitHub
commit b966ee49b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 11 deletions

View file

@ -650,6 +650,11 @@ void LineEdit::_notification(int p_what) {
set_cursor_position(get_cursor_position());
} break;
case NOTIFICATION_TRANSLATION_CHANGED: {
placeholder_translated = tr(placeholder);
update_placeholder_width();
update();
} break;
case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
window_has_focus = true;
draw_caret = true;
@ -728,7 +733,7 @@ void LineEdit::_notification(int p_what) {
Color font_color_selected = get_color("font_color_selected");
Color cursor_color = get_color("cursor_color");
const String &t = using_placeholder ? placeholder : text;
const String &t = using_placeholder ? placeholder_translated : text;
// draw placeholder color
if (using_placeholder)
font_color.a *= placeholder_alpha;
@ -1148,16 +1153,9 @@ String LineEdit::get_text() const {
void LineEdit::set_placeholder(String p_text) {
placeholder = tr(p_text);
if ((max_length <= 0) || (placeholder.length() <= max_length)) {
Ref<Font> font = get_font("font");
cached_placeholder_width = 0;
if (font != NULL) {
for (int i = 0; i < placeholder.length(); i++) {
cached_placeholder_width += font->get_char_size(placeholder[i]).width;
}
}
}
placeholder = p_text;
placeholder_translated = tr(placeholder);
update_placeholder_width();
update();
}
@ -1546,6 +1544,18 @@ void LineEdit::_emit_text_change() {
text_changed_dirty = false;
}
void LineEdit::update_placeholder_width() {
if ((max_length <= 0) || (placeholder_translated.length() <= max_length)) {
Ref<Font> font = get_font("font");
cached_placeholder_width = 0;
if (font != NULL) {
for (int i = 0; i < placeholder_translated.length(); i++) {
cached_placeholder_width += font->get_char_size(placeholder_translated[i]).width;
}
}
}
}
void LineEdit::_clear_redo() {
_create_undo_state();
if (undo_stack_pos == NULL) {

View file

@ -72,6 +72,7 @@ private:
String undo_text;
String text;
String placeholder;
String placeholder_translated;
String secret_character;
float placeholder_alpha;
String ime_text;
@ -126,6 +127,8 @@ private:
void _emit_text_change();
bool expand_to_text_length;
void update_placeholder_width();
bool caret_blink_enabled;
bool draw_caret;
bool window_has_focus;