Audacity 3.2.0
TrackFocus.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 TrackFocus.cpp
6
7 Leland Lucius
8 and lots of other contributors
9
10*//*******************************************************************/
11#include "TrackFocus.h"
12
13#include "BasicUI.h"
14#include "Project.h"
15#include "Track.h"
16
18
20{
21 return TrackList::Get( mProject );
22}
23
25{
26 return TrackList::Get( mProject );
27}
28
29// Returns currently focused track
30// if that track no longer exists, if there is a track at
31// the same position, use that, else if there is a first
32// track, use that.
33std::shared_ptr<Track> TrackFocus::GetFocus()
34{
35 auto focusedTrack = mFocusedTrack.lock();
36 if( !focusedTrack ) {
37 if (mNumFocusedTrack >=1) {
38 // This prevents the focus from being unnecessarily set to track 1
39 // when effects are applied. (Applying an effect can change
40 // the pointers of the selected tracks.)
41 focusedTrack = FindTrack(mNumFocusedTrack);
42 }
43 if (!focusedTrack) {
44 focusedTrack =
45 Track::SharedPointer(*GetTracks().Any().first);
46 // only call SetFocus if the focus has changed to avoid
47 // unnecessary focus events
48 if (focusedTrack)
49 focusedTrack = SetFocus();
50 }
51 }
52
53 if( !TrackNum( focusedTrack ) )
54 {
55 mFocusedTrack.reset();
56 return {};
57 }
58
59 return( focusedTrack );
60}
61
62std::shared_ptr<Track> TrackFocus::PeekFocus() const
63{
64 return mFocusedTrack.lock();
65}
66
67// Changes focus to a specified track
68std::shared_ptr<Track> TrackFocus::SetFocus(
69 std::shared_ptr<Track> track, bool focusPanel)
70{
71 if (mpCallbacks)
72 mpCallbacks->BeginChangeFocus();
73
74 if (!track)
76
77 const bool focusChanged = (PeekFocus() != track);
78 if (focusChanged) {
79 mFocusedTrack = track;
80 }
81 if (focusChanged || focusPanel)
83 wFocus = weak_from_this(), focusPanel
84 ]{
85 if (auto pFocus = wFocus.lock())
86 pFocus->Publish({ focusPanel });
87 });
89
90 if (mpCallbacks)
91 mpCallbacks->EndChangeFocus(track);
92
93 return track;
94}
95
96int TrackFocus::TrackNum(const std::shared_ptr<Track> &target) const
97{
98 // Find 1-based position of the target in the visible tracks, or 0 if not
99 // found
100 int ndx = 0;
101
102 for (auto t : GetTracks()) {
103 ndx++;
104 if( t == target.get() )
105 {
106 return ndx;
107 }
108 }
109
110 return 0;
111}
112
113std::shared_ptr<Track> TrackFocus::FindTrack(int num) const
114{
115 int ndx = 0;
116
117 for (auto t : TrackList::Get(mProject)) {
118 ndx++;
119 if( ndx == num )
120 return t->SharedPointer();
121 }
122
123 return {};
124}
125
127 [](AudacityProject &parent){
128 return std::make_shared<TrackFocus>(parent);
129 }
130};
131
133{
134 return project.AttachedObjects::Get< TrackFocus >( key );
135}
136
138{
139 return Get( const_cast< AudacityProject & >( project ) );
140}
141
143 : mProject{ project }
144{
145}
146
148{
149}
150
151void TrackFocus::SetCallbacks(std::unique_ptr<TrackFocusCallbacks> pCallbacks)
152{
153 mpCallbacks = move(pCallbacks);
154}
155
157{
158 return GetFocus().get();
159}
160
161void TrackFocus::Set(Track *pTrack, bool focusPanel)
162{
163 SetFocus(Track::SharedPointer(pTrack), focusPanel);
164}
165
167{
168 if (mpCallbacks)
169 mpCallbacks->MessageForScreenReader(message);
170}
171
173{
174 if (mpCallbacks)
175 mpCallbacks->UpdateAccessibility();
176}
Toolkit-neutral facade for basic user interface services.
const auto project
declares abstract base class Track, TrackList, and iterators over TrackList
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: TrackFocus.cpp:126
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:275
std::shared_ptr< Track > PeekFocus() const
Definition: TrackFocus.cpp:62
void SetCallbacks(std::unique_ptr< TrackFocusCallbacks > pCallbacks)
Definition: TrackFocus.cpp:151
void UpdateAccessibility()
Called to signal changes to a track.
Definition: TrackFocus.cpp:172
void MessageForScreenReader(const TranslatableString &message)
Definition: TrackFocus.cpp:166
TrackFocus(AudacityProject &project)
Definition: TrackFocus.cpp:142
~TrackFocus() final
Definition: TrackFocus.cpp:147
int TrackNum(const std::shared_ptr< Track > &track) const
Definition: TrackFocus.cpp:96
std::weak_ptr< Track > mFocusedTrack
Definition: TrackFocus.h:100
std::unique_ptr< TrackFocusCallbacks > mpCallbacks
Definition: TrackFocus.h:99
int mNumFocusedTrack
Definition: TrackFocus.h:101
std::shared_ptr< Track > FindTrack(int num) const
Definition: TrackFocus.cpp:113
AudacityProject & mProject
Definition: TrackFocus.h:98
std::shared_ptr< Track > GetFocus()
Definition: TrackFocus.cpp:33
Track * Get()
Definition: TrackFocus.cpp:156
void Set(Track *pTrack, bool focusPanel=false)
Set the track focus to a given track or to null.
Definition: TrackFocus.cpp:161
const TrackList & GetTracks() const
Definition: TrackFocus.cpp:24
std::shared_ptr< Track > SetFocus(std::shared_ptr< Track > track={}, bool focusPanel=false)
Definition: TrackFocus.cpp:68
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
std::shared_ptr< Subclass > SharedPointer()
Definition: Track.h:146
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:850
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
Holds a msgid for the translation catalog; may also bind format arguments.
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:213
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
virtual ~TrackFocusCallbacks()