Break cycle: TrackPanelResizerCell, TrackPanelResizeHandle, TrackView

This commit is contained in:
Paul Licameli 2019-07-09 14:45:42 -04:00
parent 586eb0d4cb
commit db16150366
6 changed files with 43 additions and 35 deletions

View File

@ -182,8 +182,13 @@ private:
long mValue;
};
using AttachedTrackObjects = ClientData::Site<
Track, ClientData::Base, ClientData::SkipCopying, std::shared_ptr
>;
class AUDACITY_DLL_API Track /* not final */
: public XMLTagHandler
, public AttachedTrackObjects
, public std::enable_shared_from_this<Track> // see SharedPointer()
{
friend class TrackList;
@ -207,6 +212,7 @@ class AUDACITY_DLL_API Track /* not final */
public:
using AttachedObjects = ::AttachedTrackObjects;
using ChannelType = XMLValueChecker::ChannelType;
static const auto LeftChannel = XMLValueChecker::LeftChannel;

View File

@ -73,6 +73,7 @@ is time to refresh some aspect of the screen.
#include "RefreshCode.h"
#include "TrackArtist.h"
#include "TrackPanelAx.h"
#include "TrackPanelResizerCell.h"
#include "WaveTrack.h"
#ifdef EXPERIMENTAL_MIDI_OUT
#include "NoteTrack.h"
@ -1151,7 +1152,7 @@ struct ChannelGroup final : TrackPanelGroup {
yy += height;
refinement.emplace_back(
yy - kSeparatorThickness,
TrackView::Get( *channel ).GetResizer() );
TrackPanelResizerCell::Get( *channel ).shared_from_this() );
}
}
@ -1270,8 +1271,9 @@ struct ResizingChannelGroup final : TrackPanelGroup {
{ rect.GetTop(),
std::make_shared< LabeledChannelGroup >( mpTrack, mLeftOffset ) },
{ rect.GetTop() + rect.GetHeight() - kSeparatorThickness,
TrackView::Get( **TrackList::Channels( mpTrack.get() ).rbegin() )
.GetResizer() }
TrackPanelResizerCell::Get(
**TrackList::Channels( mpTrack.get() ).rbegin() ).shared_from_this()
}
} }; }
std::shared_ptr< Track > mpTrack;
wxCoord mLeftOffset;

View File

@ -26,8 +26,8 @@ Paul Licameli split from TrackPanel.cpp
#include <wx/mousestate.h>
TrackPanelResizerCell::TrackPanelResizerCell(
const std::shared_ptr<TrackView> &pView )
: mwView{ pView }
const std::shared_ptr<Track> &pTrack )
: mwTrack{ pTrack }
{}
std::vector<UIHandlePtr> TrackPanelResizerCell::HitTest
@ -47,10 +47,7 @@ std::vector<UIHandlePtr> TrackPanelResizerCell::HitTest
std::shared_ptr<Track> TrackPanelResizerCell::DoFindTrack()
{
const auto pView = mwView.lock();
if ( pView )
return pView->FindTrack();
return {};
return mwTrack.lock();
}
void TrackPanelResizerCell::Draw(
@ -112,3 +109,20 @@ void TrackPanelResizerCell::Draw(
}
}
}
static const AttachedTrackObjects::RegisteredFactory key{
[]( Track &track ){
return std::make_shared<TrackPanelResizerCell>(
track.shared_from_this() );
}
};
TrackPanelResizerCell &TrackPanelResizerCell::Get( Track &track )
{
return track.AttachedObjects::Get< TrackPanelResizerCell >( key );
}
const TrackPanelResizerCell &TrackPanelResizerCell::Get( const Track &track )
{
return Get( const_cast< Track & >( track ) );
}

View File

@ -11,19 +11,26 @@
#ifndef __AUDACITY_TRACK_PANEL_RESIZER_CELL__
#define __AUDACITY_TRACK_PANEL_RESIZER_CELL__
#include "tracks/ui/CommonTrackPanelCell.h"
#include "ClientData.h" // to inherit
#include "tracks/ui/CommonTrackPanelCell.h" // to inherit
class Track;
class TrackPanelResizeHandle;
class TrackView;
class TrackPanelResizerCell : public CommonTrackPanelCell
class TrackPanelResizerCell
: public CommonTrackPanelCell
, public std::enable_shared_from_this< TrackPanelResizerCell >
, public ClientData::Base
{
TrackPanelResizerCell(const TrackPanelResizerCell&) = delete;
TrackPanelResizerCell &operator= (const TrackPanelResizerCell&) = delete;
public:
static TrackPanelResizerCell &Get( Track &track );
static const TrackPanelResizerCell &Get( const Track &track );
explicit
TrackPanelResizerCell( const std::shared_ptr<TrackView> &pView );
TrackPanelResizerCell( const std::shared_ptr<Track> &pTrack );
std::vector<UIHandlePtr> HitTest
(const TrackPanelMouseState &, const AudacityProject *) override;
@ -33,7 +40,7 @@ protected:
private:
// back-pointer is weak to break a cycle
std::weak_ptr<TrackView> mwView;
std::weak_ptr<Track> mwTrack;
// TrackPanelDrawable implementation
void Draw(

View File

@ -12,7 +12,6 @@ Paul Licameli split from TrackPanel.cpp
#include "../../Track.h"
#include "TrackControls.h"
#include "../../TrackPanelResizerCell.h"
#include "../../ClientData.h"
#include "../../Project.h"
@ -135,20 +134,6 @@ std::shared_ptr<const TrackVRulerControls> TrackView::GetVRulerControls() const
return const_cast< TrackView* >( this )->GetVRulerControls();
}
#include "../../TrackPanelResizeHandle.h"
std::shared_ptr<TrackPanelCell> TrackView::GetResizer()
{
if (!mpResizer)
// create on demand
mpResizer = std::make_shared<TrackPanelResizerCell>( shared_from_this() );
return mpResizer;
}
std::shared_ptr<const TrackPanelCell> TrackView::GetResizer() const
{
return const_cast<TrackView*>(this)->GetResizer();
}
void TrackView::DoSetY(int y)
{
mY = y;

View File

@ -63,11 +63,6 @@ public:
std::shared_ptr<const TrackVRulerControls> GetVRulerControls() const;
// Return another, associated TrackPanelCell object that implements the
// click and drag to resize
std::shared_ptr<TrackPanelCell> GetResizer();
std::shared_ptr<const TrackPanelCell> GetResizer() const;
void WriteXMLAttributes( XMLWriter & ) const override;
bool HandleXMLAttribute( const wxChar *attr, const wxChar *value ) override;
@ -92,7 +87,6 @@ protected:
virtual std::shared_ptr<TrackVRulerControls> DoGetVRulerControls() = 0;
std::shared_ptr<TrackVRulerControls> mpVRulerControls;
std::shared_ptr<TrackPanelResizerCell> mpResizer;
private:
bool mMinimized{ false };