Skip to content

Commit

Permalink
Disable create folder confirm button when folder name is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Schweini07 committed Apr 10, 2023
1 parent 7d217f1 commit e26e1f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/TGUI/Widgets/FileDialog.hpp
Expand Up @@ -533,7 +533,7 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Creates a folder in a given directory
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void createFolder(const tgui::String& name);
void createFolder(const String& name);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Handles a press of the create folder button
Expand All @@ -545,6 +545,11 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void destroyCreateFolderDialog();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check if the name of a new folder is valid
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool isValidFolderName(const tgui::String& name);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initializes the widget pointers after copying or loading the dialog
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
25 changes: 25 additions & 0 deletions src/Widgets/FileDialog.cpp
Expand Up @@ -1015,10 +1015,17 @@ namespace tgui
Button::Ptr confirm_button = Button::create("Confirm");
confirm_button->setPosition("75%", "75%");
confirm_button->setOrigin(0.5f, 0.5f);
confirm_button->setEnabled(false);

folder_name_edit_box->onTextChange([this, folder_name_edit_box, confirm_button] {
bool isValid = isValidFolderName(folder_name_edit_box->getText());
confirm_button->setEnabled(isValid);
} );

cancel_button->onPress([this] {
destroyCreateFolderDialog();
} );

confirm_button->onPress([this, folder_name_edit_box] {
createFolder(folder_name_edit_box->getText());
destroyCreateFolderDialog();
Expand Down Expand Up @@ -1047,6 +1054,23 @@ namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool FileDialog::isValidFolderName(const tgui::String& name)
{
if (name.empty() || (name == U".") || (name == U".."))
return false;

#ifdef TGUI_SYSTEM_WINDOWS
if (name.find_first_of(U"\\/:*?\"<>|") != tgui::String::npos)
#else
if (name.contains(U'/'))
#endif
return false;

return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FileDialog::identifyChildWidgets()
{
m_buttonBack = get<Button>("#TGUI_INTERNAL$ButtonBack#");
Expand Down Expand Up @@ -1092,6 +1116,7 @@ namespace tgui
});
m_buttonUp->onPress([this]{
auto parent = m_currentDirectory.getParentPath();

#ifdef TGUI_SYSTEM_WINDOWS
if (parent.asString() == m_currentDirectory.asString())
parent = Filesystem::Path("");
Expand Down

0 comments on commit e26e1f0

Please sign in to comment.