Skip to content

Commit

Permalink
#5078 Automatically dismiss the search popup after a few seconds. Som…
Browse files Browse the repository at this point in the history
…e refactoring.
  • Loading branch information
codereader committed Dec 27, 2019
1 parent e5894e5 commit eb6b1f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
44 changes: 32 additions & 12 deletions libs/wxutil/TreeView.cpp
Expand Up @@ -2,12 +2,17 @@

#include <wx/popupwin.h>
#include <wx/sizer.h>
#include <wx/timer.h>
#include <wx/textctrl.h>
#include <algorithm>

namespace wxutil
{

namespace
{
const int MSECS_TO_AUTO_CLOSE_POPUP = 6000;
}

TreeView::TreeView(wxWindow* parent, TreeModel::Ptr model, long style) :
wxDataViewCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style),
_searchPopup(NULL)
Expand Down Expand Up @@ -69,10 +74,10 @@ void TreeView::TriggerColumnSizeEvent(const wxDataViewItem& item)
wxDataViewItemArray children;
GetModel()->GetChildren(item, children);

std::for_each(children.begin(), children.end(), [&](wxDataViewItem& item)
for (const auto& child : children)
{
GetModel()->ItemChanged(item);
});
GetModel()->ItemChanged(child);
}
}

void TreeView::ExpandTopLevelItems()
Expand All @@ -85,10 +90,10 @@ void TreeView::ExpandTopLevelItems()
wxDataViewItemArray children;
model->GetChildren(model->GetRoot(), children);

std::for_each(children.begin(), children.end(), [&](const wxDataViewItem& item)
for (const auto& item : children)
{
Expand(item);
});
}
}

void TreeView::ResetSortingOnAllColumns()
Expand Down Expand Up @@ -144,7 +149,7 @@ void TreeView::Rebuild()
void TreeView::_onItemExpanded(wxDataViewEvent& ev)
{
// This should force a recalculation of the column width
if (GetModel() != NULL)
if (GetModel() != nullptr)
{
GetModel()->ItemChanged(ev.GetItem());
}
Expand Down Expand Up @@ -203,20 +208,27 @@ class TreeView::SearchPopupWindow :
}
};

class TreeView::Search
class TreeView::Search :
public wxEvtHandler
{
private:
TreeView& _treeView;
SearchPopupWindow* _popup;
wxDataViewItem _curSearchMatch;
wxTimer _closeTimer;

public:
TreeView::Search(TreeView& treeView) :
_treeView(treeView)
_treeView(treeView),
_closeTimer(this)
{
_popup = new SearchPopupWindow(&_treeView, *this);
_popup->Show();
_curSearchMatch = wxDataViewItem();

Bind(wxEVT_TIMER, std::bind(&Search::onIntervalReached, this, std::placeholders::_1));

_closeTimer.Start(MSECS_TO_AUTO_CLOSE_POPUP);
}

~Search()
Expand All @@ -226,8 +238,18 @@ class TreeView::Search
_curSearchMatch = wxDataViewItem();
}

void onIntervalReached(wxTimerEvent& ev)
{
// Disconnect the timing event
_closeTimer.Stop();

_treeView.CloseSearch();
}

void HighlightMatch(const wxDataViewItem& item)
{
_closeTimer.Start(MSECS_TO_AUTO_CLOSE_POPUP); // restart

_curSearchMatch = item;
_treeView.JumpToSearchMatch(_curSearchMatch);
}
Expand Down Expand Up @@ -294,7 +316,7 @@ void TreeView::CloseSearch()

void TreeView::_onChar(wxKeyEvent& ev)
{
if (GetModel() == NULL || _colsToSearch.empty())
if (GetModel() == nullptr || _colsToSearch.empty())
{
ev.Skip();
return;
Expand All @@ -307,8 +329,6 @@ void TreeView::_onChar(wxKeyEvent& ev)
if (uc != WXK_NONE && uc >= 32 && !_search)
{
_search = std::make_unique<Search>(*this);

// TODO: Add timeout to hide search window
}

if (_search)
Expand Down
3 changes: 2 additions & 1 deletion libs/wxutil/TreeView.h
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <wx/dataview.h>
#include <wx/windowptr.h>

Expand Down Expand Up @@ -44,7 +45,7 @@ class TreeView :
virtual ~TreeView();

// override wxDataViewCtrl to make it more robust
virtual bool AssociateModel(wxDataViewModel* model);
virtual bool AssociateModel(wxDataViewModel* model) override;

// Enable the automatic recalculation of column widths
void EnableAutoColumnWidthFix(bool enable = true);
Expand Down

0 comments on commit eb6b1f0

Please sign in to comment.