1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-09-17 16:50:26 +02:00

Define registry item class and visitor for Preference pages

This commit is contained in:
Paul Licameli 2019-01-14 13:58:24 -05:00
parent a729dfec3b
commit 1664463444
2 changed files with 59 additions and 9 deletions

View File

@ -39,6 +39,7 @@
#include "../AudioIOBase.h"
#include "../Prefs.h"
#include "../ShuttleGui.h"
#include "../commands/CommandManager.h"
#include "PrefsPanel.h"
@ -455,15 +456,17 @@ namespace {
struct Entry{
unsigned sequenceNumber;
PrefsDialog::PrefsNode node;
bool operator < ( const Entry &other ) const
{ return sequenceNumber < other.sequenceNumber; }
};
using Entries = std::vector< Entry >;
Entries &Registry()
{
static Entries result;
return result;
namespace Prefs {
Entries &Registry()
{
static Entries result;
return result;
}
}
}
@ -472,7 +475,7 @@ PrefsPanel::Registration::Registration( unsigned sequenceNumber,
unsigned nChildren,
bool expanded )
{
auto &registry = Registry();
auto &registry = Prefs::Registry();
Entry entry{ sequenceNumber, { factory, nChildren, expanded } };
const auto end = registry.end();
// Find insertion point:
@ -482,6 +485,53 @@ PrefsPanel::Registration::Registration( unsigned sequenceNumber,
registry.insert( iter, entry );
}
namespace {
struct PrefsItem final : Registry::ConcreteGroupItem<false> {
PrefsPanel::Factory factory;
bool expanded{ false };
PrefsItem( const wxString &name,
const PrefsPanel::Factory &factory_, bool expanded_ )
: ConcreteGroupItem<false>{ name }
, factory{ factory_ }, expanded{ expanded_ }
{}
};
// Collects registry tree nodes into a vector, in preorder.
struct PrefsItemVisitor final : Registry::Visitor {
PrefsItemVisitor( PrefsDialog::Factories &factories_ )
: factories{ factories_ }
{
childCounts.push_back( 0 );
}
void BeginGroup( Registry::GroupItem &item, const Path & ) override
{
auto pItem = dynamic_cast<PrefsItem*>( &item );
if (!pItem)
return;
indices.push_back( factories.size() );
factories.emplace_back( pItem->factory, 0, pItem->expanded );
++childCounts.back();
childCounts.push_back( 0 );
}
void EndGroup( Registry::GroupItem &item, const Path & ) override
{
auto pItem = dynamic_cast<PrefsItem*>( &item );
if (!pItem)
return;
auto &factory = factories[ indices.back() ];
factory.nChildren = childCounts.back();
childCounts.pop_back();
indices.pop_back();
}
PrefsDialog::Factories &factories;
std::vector<size_t> childCounts;
std::vector<size_t> indices;
};
}
PrefsDialog::Factories
&PrefsDialog::DefaultFactories()
{
@ -489,7 +539,7 @@ PrefsDialog::Factories
static std::once_flag flag;
std::call_once( flag, []{
for ( const auto &entry : Registry() ) {
for ( const auto &entry : Prefs::Registry() ) {
factories.push_back( entry.node );
}
} );

View File

@ -39,8 +39,8 @@ class PrefsDialog /* not final */ : public wxDialogWrapper
std::function< PrefsPanel * (
wxWindow *parent, wxWindowID winid, AudacityProject *) >;
Factory factory;
unsigned nChildren;
bool expanded;
size_t nChildren{ 0 };
bool expanded{ false };
PrefsNode(const Factory &factory_,
unsigned nChildren_ = 0,