Android: Add `field_enter_after_edit[]` formspec element (#13836)

This commit is contained in:
Gregor Parzefall 2023-10-01 11:20:50 +02:00 committed by GitHub
parent 94eba15c34
commit 56965bc814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 56 additions and 8 deletions

View File

@ -833,7 +833,7 @@ function store.get_formspec(dlgdata)
"container[0.375,0.375]",
"field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
"field_close_on_enter[search_string;false]",
"field_enter_after_edit[search_string;true]",
"image_button[7.3,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
"image_button[8.125,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
"dropdown[9.175,0;2.7875,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",

View File

@ -84,6 +84,7 @@ local function make_field(converter, validator, stringifier)
local fs = ("field[0,0.3;%f,0.8;%s;%s;%s]"):format(
avail_w - 1.5, setting.name, get_label(setting), core.formspec_escape(value))
fs = fs .. ("field_enter_after_edit[%s;true]"):format(setting.name)
fs = fs .. ("button[%f,0.3;1.5,0.8;%s;%s]"):format(avail_w - 1.5, "set_" .. setting.name, fgettext("Set"))
return fs, 1.1

View File

@ -477,6 +477,7 @@ local function get_formspec(dialogdata)
"field[0.25,0.25;", tostring(search_width), ",0.75;search_query;;",
core.formspec_escape(dialogdata.query or ""), "]",
"field_enter_after_edit[search_query;true]",
"container[", tostring(search_width + 0.25), ", 0.25]",
"image_button[0,0;0.75,0.75;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
"image_button[0.75,0;0.75,0.75;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";search_clear;]",

View File

@ -67,6 +67,7 @@ local function get_formspec(tabview, name, tabdata)
local retval =
-- Search
"field[0.25,0.25;7,0.75;te_search;;" .. core.formspec_escape(tabdata.search_for) .. "]" ..
"field_enter_after_edit[te_search;true]" ..
"container[7.25,0.25]" ..
"image_button[0,0;0.75,0.75;" .. core.formspec_escape(defaulttexturedir .. "search.png") .. ";btn_mp_search;]" ..
"image_button[0.75,0;0.75,0.75;" .. core.formspec_escape(defaulttexturedir .. "clear.png") .. ";btn_mp_clear;]" ..

View File

@ -2643,6 +2643,9 @@ Version History
* Added padding[] element
* Formspec version 6 (5.6.0):
* Add nine-slice images, animated_image, and fgimg_middle
* Formspec version 7 (5.8.0):
* style[]: Add focused state for buttons
* Add field_enter_after_edit[] (experimental)
Elements
--------
@ -2890,7 +2893,7 @@ Elements
### `pwdfield[<X>,<Y>;<W>,<H>;<name>;<label>]`
* Textual password style field; will be sent to server when a button is clicked
* When enter is pressed in field, fields.key_enter_field will be sent with the
* When enter is pressed in field, `fields.key_enter_field` will be sent with the
name of this field.
* With the old coordinate system, fields are a set height, but will be vertically
centered on `H`. With the new coordinate system, `H` will modify the height.
@ -2923,12 +2926,21 @@ Elements
* A "Proceed" button will be added automatically
* See `field_close_on_enter` to stop enter closing the formspec
### `field_enter_after_edit[<name>;<enter_after_edit>]`
* Experimental, may be subject to change or removal at any time.
* Only affects Android clients.
* `<name>` is the name of the field.
* If `<enter_after_edit>` is true, pressing the "Done" button in the Android
text input dialog will simulate an <kbd>Enter</kbd> keypress.
* Defaults to false when not specified (i.e. no tag for a field).
### `field_close_on_enter[<name>;<close_on_enter>]`
* <name> is the name of the field
* if <close_on_enter> is false, pressing enter in the field will submit the
form but not close it.
* defaults to true when not specified (ie: no tag for a field)
* `<name>` is the name of the field.
* If `<close_on_enter>` is false, pressing <kbd>Enter</kbd> in the field will
submit the form but not close it.
* Defaults to true when not specified (i.e. no tag for a field).
### `textarea[<X>,<Y>;<W>,<H>;<name>;<label>;<default>]`

View File

@ -236,6 +236,7 @@ local function get_formspec(page, name)
"label[9,5.1;"..F(S("Trash:")).."]" ..
"list[detached:chest_of_everything_trash_"..name..";main;9,5.5;1,1]" ..
"field[2.2,5.75;4,1;search;;"..F(search_text).."]" ..
"field_enter_after_edit[search;true]" ..
"field_close_on_enter[search;false]" ..
"button[6,5.45;1.6,1;search_button_start;"..F(S("Search")).."]" ..
"button[7.6,5.45;0.8,1;search_button_reset;"..F(S("X")).."]" ..

View File

@ -1379,6 +1379,15 @@ void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element
}
}
void GUIFormSpecMenu::parseFieldEnterAfterEdit(parserData *data, const std::string &element)
{
std::vector<std::string> parts;
if (!precheckElement("field_enter_after_edit", element, 2, 2, parts))
return;
field_enter_after_edit[parts[0]] = is_yes(parts[1]);
}
void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
{
std::vector<std::string> parts;
@ -2903,6 +2912,11 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
return;
}
if (type == "field_enter_after_edit") {
parseFieldEnterAfterEdit(data, description);
return;
}
if (type == "field_close_on_enter") {
parseFieldCloseOnEnter(data, description);
return;
@ -3082,6 +3096,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
theme_by_name.clear();
theme_by_type.clear();
m_clickthrough_elements.clear();
field_enter_after_edit.clear();
field_close_on_enter.clear();
m_dropdown_index_event.clear();
@ -3480,8 +3495,23 @@ bool GUIFormSpecMenu::getAndroidUIInput()
if (!element || element->getType() != irr::gui::EGUIET_EDIT_BOX)
return false;
gui::IGUIEditBox *editbox = (gui::IGUIEditBox *)element;
std::string text = porting::getInputDialogValue();
((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
editbox->setText(utf8_to_wide(text).c_str());
bool enter_after_edit = false;
auto iter = field_enter_after_edit.find(fieldname);
if (iter != field_enter_after_edit.end()) {
enter_after_edit = iter->second;
}
if (enter_after_edit && editbox->getParent()) {
SEvent enter;
enter.EventType = EET_GUI_EVENT;
enter.GUIEvent.Caller = editbox;
enter.GUIEvent.Element = nullptr;
enter.GUIEvent.EventType = gui::EGET_EDITBOX_ENTER;
editbox->getParent()->OnEvent(enter);
}
}
return false;
}

View File

@ -331,6 +331,7 @@ protected:
std::vector<GUIInventoryList *> m_inventorylists;
std::vector<ListRingSpec> m_inventory_rings;
std::unordered_map<std::string, bool> field_enter_after_edit;
std::unordered_map<std::string, bool> field_close_on_enter;
std::unordered_map<std::string, bool> m_dropdown_index_event;
std::vector<FieldSpec> m_fields;
@ -448,6 +449,7 @@ private:
void parseTable(parserData* data, const std::string &element);
void parseTextList(parserData* data, const std::string &element);
void parseDropDown(parserData* data, const std::string &element);
void parseFieldEnterAfterEdit(parserData *data, const std::string &element);
void parseFieldCloseOnEnter(parserData *data, const std::string &element);
void parsePwdField(parserData* data, const std::string &element);
void parseField(parserData* data, const std::string &element, const std::string &type);

View File

@ -241,7 +241,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// base64-encoded SHA-1 (27+\0).
// See also formspec [Version History] in doc/lua_api.md
#define FORMSPEC_API_VERSION 6
#define FORMSPEC_API_VERSION 7
#define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-"