Skip to content

Commit

Permalink
#3250: Paint the search text red when nothing in the tree matches the…
Browse files Browse the repository at this point in the history
… criteria.
  • Loading branch information
codereader committed Jan 15, 2021
1 parent 69e2c46 commit 383c12a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
19 changes: 10 additions & 9 deletions libs/wxutil/dataview/ResourceTreeView.cpp
Expand Up @@ -200,7 +200,7 @@ void ResourceTreeView::PopulateContextMenu(wxutil::PopupMenu& popupMenu)
);
}

void ResourceTreeView::SetFilterText(const wxString& filterText)
bool ResourceTreeView::SetFilterText(const wxString& filterText)
{
// We use the lower-case copy of the given filter text
_filterText = filterText.Lower();
Expand All @@ -218,18 +218,16 @@ void ResourceTreeView::SetFilterText(const wxString& filterText)
if (!_filterText.empty() && !TreeModel::RowContainsString(row, _filterText, _colsToSearch, true))
{
// The selected row is not relevant anymore
JumpToFirstFilterMatch();
return;
return JumpToFirstFilterMatch();
}

// Try to keep whatever selection we had before
Select(item);
EnsureVisible(item);
return true;
}
else
{
JumpToFirstFilterMatch();
}

return JumpToFirstFilterMatch();
}

void ResourceTreeView::UpdateTreeVisibility()
Expand All @@ -241,16 +239,19 @@ void ResourceTreeView::UpdateTreeVisibility()
}
}

void ResourceTreeView::JumpToFirstFilterMatch()
bool ResourceTreeView::JumpToFirstFilterMatch()
{
if (_filterText.empty()) return;
if (_filterText.empty()) return false;

auto item = _treeModelFilter->FindNextString(_filterText, _colsToSearch);

if (item.IsOk())
{
JumpToSearchMatch(item);
return true;
}

return false;
}

void ResourceTreeView::JumpToNextFilterMatch()
Expand Down
6 changes: 4 additions & 2 deletions libs/wxutil/dataview/ResourceTreeView.h
Expand Up @@ -108,7 +108,8 @@ class ResourceTreeView :
// this string will match against the default iconAndName column,
// all rows not containing the string will be hidden.
// Filtering happens case-insensitively.
virtual void SetFilterText(const wxString& filterText);
// As feedback this method returns true if the filter has any matches.
virtual bool SetFilterText(const wxString& filterText);

// Removes the string filter
virtual void ClearFilterText();
Expand Down Expand Up @@ -140,7 +141,8 @@ class ResourceTreeView :
// hook as an alternative to this method.
void AddCustomMenuItem(const ui::IMenuItemPtr& item);

virtual void JumpToFirstFilterMatch();
// Returns true if there is a first filter match
virtual bool JumpToFirstFilterMatch();
virtual void JumpToNextFilterMatch();
virtual void JumpToPrevFilterMatch();

Expand Down
31 changes: 24 additions & 7 deletions libs/wxutil/dataview/ResourceTreeViewToolbar.cpp
Expand Up @@ -41,13 +41,7 @@ ResourceTreeViewToolbar::ResourceTreeViewToolbar(wxWindow* parent, ResourceTreeV

_filterEntry = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
_filterEntry->SetMinSize(wxSize(100, -1));
_filterEntry->Bind(wxEVT_TEXT, [this](wxCommandEvent& ev)
{
if (_treeView != nullptr)
{
_treeView->SetFilterText(ev.GetString());
}
});
_filterEntry->Bind(wxEVT_TEXT, &ResourceTreeViewToolbar::_onEntryText, this);
_filterEntry->Bind(wxEVT_CHAR, &ResourceTreeViewToolbar::_onEntryChar, this);
_filterEntry->SetToolTip(_("Enter search text to filter the tree,\nuse arrow keys to navigate"));

Expand Down Expand Up @@ -139,6 +133,29 @@ void ResourceTreeViewToolbar::_onEntryChar(wxKeyEvent& ev)
}
}

void ResourceTreeViewToolbar::_onEntryText(wxCommandEvent& ev)
{
if (_treeView == nullptr)
{
return;
}

auto filterText = ev.GetString();
bool filterResult = _treeView->SetFilterText(filterText);

if (!filterText.empty() && !filterResult)
{
// No match, set the text to red for user feedback
_filterEntry->SetForegroundColour(wxColor(220, 0, 0));
}
else
{
_filterEntry->SetForegroundColour(wxNullColour);
}

_filterEntry->Refresh();
}

void ResourceTreeViewToolbar::_onFilterButtonToggled(wxCommandEvent& ev)
{
if (_treeView == nullptr) return;
Expand Down
1 change: 1 addition & 0 deletions libs/wxutil/dataview/ResourceTreeViewToolbar.h
Expand Up @@ -38,6 +38,7 @@ class ResourceTreeViewToolbar :
void JumpToPrevFilterMatch();

void _onEntryChar(wxKeyEvent& ev);
void _onEntryText(wxCommandEvent& ev);
void _onFilterButtonToggled(wxCommandEvent& ev);

void UpdateFromTreeView();
Expand Down

0 comments on commit 383c12a

Please sign in to comment.