From f48694ef47d30d91b64ea5ae8d65f5fd9c230a46 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 3 May 2016 22:30:13 -0400 Subject: [PATCH] Move the code that scrolls during scrub out of Scrubbing.cpp --- src/Project.cpp | 55 ++++++++++++++++++++++++++++++++++--- src/Project.h | 22 +++++++++++++++ src/tracks/ui/Scrubbing.cpp | 33 +++++++--------------- src/tracks/ui/Scrubbing.h | 3 ++ 4 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/Project.cpp b/src/Project.cpp index fbc3d07b8..753e94b68 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -933,14 +933,18 @@ AudacityProject::AudacityProject(wxWindow * parent, wxWindowID id, mCursorOverlay = std::make_unique(this); #ifdef EXPERIMENTAL_SCRUBBING_BASIC - // This must follow construction of *mIndicatorOverlay, because it must - // attach its timer event handler later (so that its handler is invoked - // earlier) mScrubOverlay = std::make_unique(this); mScrubber = std::make_unique(this); #endif - // This must follow construction of *mScrubOverlay, because it must + // More order dependencies here... + // This must follow construction of *mIndicatorOverlay, because it must + // attach its timer event handler later (so that its handler is invoked + // earlier) + mPlaybackScroller = std::make_unique(this); + + // This must follow construction of *mPlaybackScroller, + // because it must // attach its timer event handler later (so that its handler is invoked // earlier) this->Connect(EVT_TRACK_PANEL_TIMER, @@ -5315,3 +5319,46 @@ int AudacityProject::GetEstimatedRecordingMinsLeftOnDisk() { int iRecMins = (int)(dRecTime / 60.0); return iRecMins; } + +AudacityProject::PlaybackScroller::PlaybackScroller(AudacityProject *project) +: mProject(project) +{ + mProject->Connect(EVT_TRACK_PANEL_TIMER, + wxCommandEventHandler(PlaybackScroller::OnTimer), + NULL, + this); +} + +AudacityProject::PlaybackScroller::~PlaybackScroller() +{ + mProject->Disconnect(EVT_TRACK_PANEL_TIMER, + wxCommandEventHandler(PlaybackScroller::OnTimer), + NULL, + this); +} + +void AudacityProject::PlaybackScroller::OnTimer(wxCommandEvent &event) +{ + // Let other listeners get the notification + event.Skip(); + +#ifdef EXPERIMENTAL_SCRUBBING_SMOOTH_SCROLL + if (mActive && mProject->IsAudioActive()) + { + // Pan the view, so that we center the play indicator. + + ViewInfo &viewInfo = mProject->GetViewInfo(); + TrackPanel *const trackPanel = mProject->GetTrackPanel(); + const int posX = viewInfo.TimeToPosition(viewInfo.mRecentStreamTime); + int width; + trackPanel->GetTracksUsableArea(&width, NULL); + const int deltaX = posX - width / 2; + viewInfo.h = + viewInfo.OffsetTimeByPixels(viewInfo.h, deltaX, true); + if (!viewInfo.bScrollBeyondZero) + // Can't scroll too far left + viewInfo.h = std::max(0.0, viewInfo.h); + trackPanel->Refresh(false); + } +#endif +} diff --git a/src/Project.h b/src/Project.h index 069221099..f86cc5521 100644 --- a/src/Project.h +++ b/src/Project.h @@ -720,6 +720,28 @@ public: const Scrubber &GetScrubber() const { return *mScrubber; } #endif + class PlaybackScroller final : public wxEvtHandler + { + public: + explicit PlaybackScroller(AudacityProject *project); + ~PlaybackScroller(); + + void Activate(bool active) + { + mActive = active; + } + + private: + void OnTimer(wxCommandEvent &event); + + AudacityProject *mProject; + bool mActive { false }; + }; + std::unique_ptr mPlaybackScroller; + +public: + PlaybackScroller &GetPlaybackScroller() { return *mPlaybackScroller; } + DECLARE_EVENT_TABLE() }; diff --git a/src/tracks/ui/Scrubbing.cpp b/src/tracks/ui/Scrubbing.cpp index 681466839..ba041a8f1 100644 --- a/src/tracks/ui/Scrubbing.cpp +++ b/src/tracks/ui/Scrubbing.cpp @@ -200,7 +200,7 @@ void Scrubber::MarkScrubStart( // needed for the decision to start scrubbing later when handling // drag events. #ifdef EXPERIMENTAL_SCRUBBING_SMOOTH_SCROLL - mSmoothScrollingScrub = smoothScrolling; + SetScrollScrubbing (smoothScrolling); #endif mAlwaysSeeking = alwaysSeeking; mScrubStartPosition = xx; @@ -356,25 +356,6 @@ void Scrubber::ContinueScrubbing() if (mScrubSpeedDisplayCountdown > 0) --mScrubSpeedDisplayCountdown; } - -#ifdef EXPERIMENTAL_SCRUBBING_SMOOTH_SCROLL - if (mSmoothScrollingScrub) { - // Pan the view, so that we center the play indicator. - - ViewInfo &viewInfo = mProject->GetViewInfo(); - TrackPanel *const trackPanel = mProject->GetTrackPanel(); - const int posX = viewInfo.TimeToPosition(viewInfo.mRecentStreamTime); - int width; - trackPanel->GetTracksUsableArea(&width, NULL); - const int deltaX = posX - width / 2; - viewInfo.h = - viewInfo.OffsetTimeByPixels(viewInfo.h, deltaX, true); - if (!viewInfo.bScrollBeyondZero) - // Can't scroll too far left - viewInfo.h = std::max(0.0, viewInfo.h); - trackPanel->Refresh(false); - } -#endif } void Scrubber::StopScrubbing() @@ -382,7 +363,7 @@ void Scrubber::StopScrubbing() UncheckAllMenuItems(); mScrubStartPosition = -1; - mSmoothScrollingScrub = false; + SetScrollScrubbing (false); if (!IsScrubbing()) { @@ -393,6 +374,12 @@ void Scrubber::StopScrubbing() } } +void Scrubber::SetScrollScrubbing(bool scrollScrubbing) +{ + mSmoothScrollingScrub = scrollScrubbing; + mProject->GetPlaybackScroller().Activate(scrollScrubbing); +} + bool Scrubber::IsScrubbing() const { if (mScrubToken <= 0) @@ -403,6 +390,7 @@ bool Scrubber::IsScrubbing() const const_cast(*this).mScrubToken = -1; const_cast(*this).mScrubStartPosition = -1; #ifdef EXPERIMENTAL_SCRUBBING_SMOOTH_SCROLL + // Don't call SetScrollScrubbing const_cast(*this).mSmoothScrollingScrub = false; #endif return false; @@ -557,7 +545,6 @@ void ScrubbingOverlay::Draw dc.DrawText(mLastScrubSpeedText, mLastScrubRect.GetX(), mLastScrubRect.GetY()); } - void ScrubbingOverlay::OnTimer(wxCommandEvent &event) { // Let other listeners get the notification @@ -666,7 +653,7 @@ void Scrubber::DoScrub(bool scroll, bool seek) MarkScrubStart(xx, scroll, seek); } else if(!match) { - mSmoothScrollingScrub = scroll; + SetScrollScrubbing(scroll); mAlwaysSeeking = seek; UncheckAllMenuItems(); CheckMenuItem(); diff --git a/src/tracks/ui/Scrubbing.h b/src/tracks/ui/Scrubbing.h index 67c02b90f..8bffd763b 100644 --- a/src/tracks/ui/Scrubbing.h +++ b/src/tracks/ui/Scrubbing.h @@ -54,8 +54,11 @@ public: bool HasStartedScrubbing() const { return GetScrubStartPosition() >= 0; } bool IsScrubbing() const; + bool IsScrollScrubbing() const // If true, implies HasStartedScrubbing() { return mSmoothScrollingScrub; } + void SetScrollScrubbing(bool scrollScrubbing); + bool IsAlwaysSeeking() const { return mAlwaysSeeking; }