Skip to content

Commit

Permalink
#3250: Try to make the filter search feel more alive by focusing on t…
Browse files Browse the repository at this point in the history
…he first matching item (unless the selected item already matches the filter text). Tweak the toolbar a bit.
  • Loading branch information
codereader committed Jan 10, 2021
1 parent a77d3da commit c0f5a44
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
51 changes: 47 additions & 4 deletions libs/wxutil/dataview/ResourceTreeView.cpp
Expand Up @@ -204,11 +204,50 @@ void ResourceTreeView::SetFilterText(const std::string& filterText)
{
// We use the lower-case copy of the given filter text
_filterText = string::to_lower_copy(filterText);

wxDataViewItem item = GetSelection();

Rebuild();

// Keep the previous selection if not filtered out and is meaningful
if (item.IsOk() && _treeModelFilter->ItemIsVisible(item))
{
TreeModel::Row row(item, *GetModel());

if (!_filterText.empty() && !RowContainsSearchString(row))
{
// The selected row is not relevant anymore
JumpToFirstFilterMatch();
return;
}

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

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

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

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

void ResourceTreeView::ClearFilterText()
{
_filterText.clear();

Rebuild();
}

std::string ResourceTreeView::GetSelectedFullname()
Expand Down Expand Up @@ -456,17 +495,21 @@ bool ResourceTreeView::IsTreeModelRowVisible(wxutil::TreeModel::Row& row)
return !IsTreeModelRowFilteredRecursively(row);
}

bool ResourceTreeView::IsTreeModelRowFilteredRecursively(wxutil::TreeModel::Row& row)
bool ResourceTreeView::RowContainsSearchString(wxutil::TreeModel::Row& row)
{
wxDataViewIconText iconAndName = row[_columns.iconAndName];

auto displayString = iconAndName.GetText().ToStdString();
string::to_lower(displayString);
//rMessage() << "displayString: " << displayString << ": " << displayString.find(_filterText) << std::endl;

if (displayString.find(_filterText) != std::string::npos)
return displayString.find(_filterText) != std::string::npos;
}

bool ResourceTreeView::IsTreeModelRowFilteredRecursively(wxutil::TreeModel::Row& row)
{
if (RowContainsSearchString(row))
{
return false; // row itself is visible, no need to check child nodes
return false;
}

// This node might also be visible if a single child node is visible, dive into it
Expand Down
2 changes: 2 additions & 0 deletions libs/wxutil/dataview/ResourceTreeView.h
Expand Up @@ -152,6 +152,8 @@ class ResourceTreeView :

// Returns true if the given row is filtered (i.e. node and all child nodes are invisible)
bool IsTreeModelRowFilteredRecursively(wxutil::TreeModel::Row& row);
bool RowContainsSearchString(wxutil::TreeModel::Row& row);
void JumpToFirstFilterMatch();

void _onContextMenu(wxDataViewEvent& ev);
void _onTreeStorePopulationProgress(TreeModel::PopulationProgressEvent& ev);
Expand Down
30 changes: 22 additions & 8 deletions libs/wxutil/dataview/ResourceTreeViewToolbar.h
Expand Up @@ -4,6 +4,8 @@
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/radiobut.h>
#include <wx/artprov.h>
#include <wx/statbmp.h>
#include <wx/textctrl.h>
#include "ResourceTreeView.h"

Expand All @@ -29,29 +31,41 @@ class ResourceTreeViewToolbar :
_showAll(nullptr),
_showFavourites(nullptr)
{
// Hbox for the favourites selection widgets
SetSizer(new wxBoxSizer(wxHORIZONTAL));
auto* grid = new wxFlexGridSizer(2);
grid->AddGrowableCol(1);

SetSizer(grid);

// Hbox for the favourites selection widgets
auto* favourites = new wxBoxSizer(wxHORIZONTAL);
_showAll = new wxRadioButton(this, wxID_ANY, _("Show All"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
_showFavourites = new wxRadioButton(this, wxID_ANY, _("Show Favourites"));

_showAll->Bind(wxEVT_RADIOBUTTON, &ResourceTreeViewToolbar::_onFilterButtonToggled, this);
_showFavourites->Bind(wxEVT_RADIOBUTTON, &ResourceTreeViewToolbar::_onFilterButtonToggled, this);

GetSizer()->Add(_showAll, 0, wxRIGHT, 0);
GetSizer()->Add(_showFavourites, 0, wxLEFT, 6);
favourites->Add(_showAll, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 0);
favourites->Add(_showFavourites, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, 6);

// Filter text entry box
auto* filterBox = new wxTextCtrl(this, wxID_ANY);
filterBox->SetMinSize(wxSize(60, -1));
filterBox->Bind(wxEVT_TEXT, [this](wxCommandEvent& ev)
auto* filterBox = new wxBoxSizer(wxHORIZONTAL);

auto* filterImage = new wxStaticBitmap(this, wxID_ANY, wxArtProvider::GetBitmap(wxART_FIND, wxART_TOOLBAR, wxSize(16, 16)));

auto* filterEntry = new wxTextCtrl(this, wxID_ANY);
filterEntry->SetMinSize(wxSize(100, -1));
filterEntry->Bind(wxEVT_TEXT, [this](wxCommandEvent& ev)
{
if (_treeView != nullptr)
{
_treeView->SetFilterText(ev.GetString().ToStdString());
}
});
GetSizer()->Add(filterBox, 1, wxLEFT, 6);
filterBox->Add(filterImage, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 6);
filterBox->Add(filterEntry, 0, wxALIGN_CENTER_VERTICAL, 6);

grid->Add(favourites, 0, wxALIGN_CENTER_VERTICAL| wxALIGN_LEFT | wxRIGHT, 6);
grid->Add(filterBox, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT, 6);

AssociateToTreeView(treeView);
}
Expand Down
3 changes: 2 additions & 1 deletion libs/wxutil/dataview/TreeView.h
Expand Up @@ -88,11 +88,12 @@ class TreeView :
// Synthesises a selection change event to notify any handlers
void SendSelectionChangeEvent(const wxDataViewItem& item);

void JumpToSearchMatch(const wxDataViewItem& item);

private:
void CollapseChildren(const wxDataViewItem& item);

void CloseSearch();
void JumpToSearchMatch(const wxDataViewItem& item);

void _onItemCollapsing(wxDataViewEvent& ev);
void _onItemExpanded(wxDataViewEvent& ev);
Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/mediabrowser/MediaBrowser.cpp
Expand Up @@ -60,7 +60,7 @@ void MediaBrowser::construct()
_treeView = new MediaBrowserTreeView(_mainWidget);
auto* toolbar = new wxutil::ResourceTreeViewToolbar(_mainWidget, _treeView);

_mainWidget->GetSizer()->Add(toolbar, 0, wxALIGN_LEFT | wxALL, 6);
_mainWidget->GetSizer()->Add(toolbar, 0, wxALIGN_LEFT | wxEXPAND | wxALL, 6);
_mainWidget->GetSizer()->Add(_treeView, 1, wxEXPAND);

// Connect up the selection changed callback
Expand Down

0 comments on commit c0f5a44

Please sign in to comment.