From 50492e1d0330cac1a87260463ca3e9f7396dd971 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 2 Aug 2024 14:30:45 -0700 Subject: [PATCH] Change the text input defaults to match the natural input experience --- include/SDL3/SDL_keyboard.h | 6 +++--- src/video/SDL_video.c | 15 +++++++++++++-- test/testime.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/include/SDL3/SDL_keyboard.h b/include/SDL3/SDL_keyboard.h index 67c4f5842e..14d1446e60 100644 --- a/include/SDL3/SDL_keyboard.h +++ b/include/SDL3/SDL_keyboard.h @@ -426,9 +426,9 @@ typedef enum SDL_Capitalization * 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_CAPITALIZATION_NUMBER` - an SDL_Capitalization value that describes how text should be capitalized, defaults to SDL_CAPITALIZE_NONE. - * - `SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN` - true to enable auto completion and auto correction. - * - `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_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, defaults to SDL_TRUE. + * - `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: * diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index fa6707f99e..5dfe7dabd7 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -5137,12 +5137,23 @@ SDL_TextInputType SDL_GetTextInputType(SDL_PropertiesID props) SDL_Capitalization SDL_GetTextInputCapitalization(SDL_PropertiesID props) { - return (SDL_Capitalization)SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE); + if (SDL_HasProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER)) { + 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) { - 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) diff --git a/test/testime.c b/test/testime.c index e5ae2e9141..f6cc4f0368 100644 --- a/test/testime.c +++ b/test/testime.c @@ -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) { SDL_Renderer *renderer = ctx->renderer; @@ -772,7 +801,7 @@ static void DrawSettings(WindowState *ctx) for (i = 0; i < SDL_arraysize(settings); ++i) { 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) { SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); SDL_RenderFillRect(renderer, &checkbox);