Change the text input defaults to match the natural input experience

This commit is contained in:
Sam Lantinga 2024-08-02 14:30:45 -07:00
parent 81f8e6aba6
commit 50492e1d03
3 changed files with 46 additions and 6 deletions

View File

@ -426,9 +426,9 @@ typedef enum SDL_Capitalization
* These are the supported properties: * These are the supported properties:
* *
* - `SDL_PROP_TEXTINPUT_TYPE_NUMBER` - an SDL_TextInputType value that describes text being input, defaults to SDL_TEXTINPUT_TYPE_TEXT. * - `SDL_PROP_TEXTINPUT_TYPE_NUMBER` - an SDL_TextInputType value that describes text being input, defaults to SDL_TEXTINPUT_TYPE_TEXT.
* - `SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER` - an SDL_Capitalization value that describes how text should be capitalized, defaults to SDL_CAPITALIZE_NONE. * - `SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER` - an SDL_Capitalization value that describes how text should be capitalized, defaults to SDL_CAPITALIZE_SENTENCES for normal text entry, SDL_CAPITALIZE_WORDS for SDL_TEXTINPUT_TYPE_TEXT_NAME, and SDL_CAPITALIZE_NONE for e-mail addresses, usernames, and passwords.
* - `SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN` - true to enable auto completion and auto correction. * - `SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN` - true to enable auto completion and auto correction, defaults to SDL_TRUE.
* - `SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN` - true if multiple lines of text are allowed. This defaults to true if SDL_HINT_RETURN_KEY_HIDES_IME is "0" or is not set, and defaults to false if SDL_HINT_RETURN_KEY_HIDES_IME is "1". * - `SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN` - true if multiple lines of text are allowed. This defaults to SDL_TRUE if SDL_HINT_RETURN_KEY_HIDES_IME is "0" or is not set, and defaults to SDL_FALSE if SDL_HINT_RETURN_KEY_HIDES_IME is "1".
* *
* On Android you can directly specify the input type: * On Android you can directly specify the input type:
* *

View File

@ -5137,12 +5137,23 @@ SDL_TextInputType SDL_GetTextInputType(SDL_PropertiesID props)
SDL_Capitalization SDL_GetTextInputCapitalization(SDL_PropertiesID props) SDL_Capitalization SDL_GetTextInputCapitalization(SDL_PropertiesID props)
{ {
if (SDL_HasProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER)) {
return (SDL_Capitalization)SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE); return (SDL_Capitalization)SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE);
}
switch (SDL_GetTextInputType(props)) {
case SDL_TEXTINPUT_TYPE_TEXT:
return SDL_CAPITALIZE_SENTENCES;
case SDL_TEXTINPUT_TYPE_TEXT_NAME:
return SDL_CAPITALIZE_WORDS;
default:
return SDL_CAPITALIZE_NONE;
}
} }
SDL_bool SDL_GetTextInputAutocorrect(SDL_PropertiesID props) SDL_bool SDL_GetTextInputAutocorrect(SDL_PropertiesID props)
{ {
return SDL_GetBooleanProperty(props, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, SDL_FALSE); return SDL_GetBooleanProperty(props, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, SDL_TRUE);
} }
SDL_bool SDL_GetTextInputMultiline(SDL_PropertiesID props) SDL_bool SDL_GetTextInputMultiline(SDL_PropertiesID props)

View File

@ -759,6 +759,35 @@ static void ToggleSettings(WindowState *ctx)
} }
} }
static int GetDefaultSetting(SDL_PropertiesID props, const char *setting)
{
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_TYPE_NUMBER) == 0) {
return SDL_TEXTINPUT_TYPE_TEXT;
}
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER) == 0) {
switch (SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) {
case SDL_TEXTINPUT_TYPE_TEXT:
return SDL_CAPITALIZE_SENTENCES;
case SDL_TEXTINPUT_TYPE_TEXT_NAME:
return SDL_CAPITALIZE_WORDS;
default:
return SDL_CAPITALIZE_NONE;
}
}
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN) == 0) {
return SDL_TRUE;
}
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN) == 0) {
return SDL_TRUE;
}
SDL_assert(!"Unknown setting");
return 0;
}
static void DrawSettings(WindowState *ctx) static void DrawSettings(WindowState *ctx)
{ {
SDL_Renderer *renderer = ctx->renderer; SDL_Renderer *renderer = ctx->renderer;
@ -772,7 +801,7 @@ static void DrawSettings(WindowState *ctx)
for (i = 0; i < SDL_arraysize(settings); ++i) { for (i = 0; i < SDL_arraysize(settings); ++i) {
if (settings[i].setting) { if (settings[i].setting) {
int value = (int)SDL_GetNumberProperty(ctx->text_settings, settings[i].setting, 0); int value = (int)SDL_GetNumberProperty(ctx->text_settings, settings[i].setting, GetDefaultSetting(ctx->text_settings, settings[i].setting));
if (value == settings[i].value) { if (value == settings[i].value) {
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderFillRect(renderer, &checkbox); SDL_RenderFillRect(renderer, &checkbox);