From 8168dce551e6d329ca85f7308f8f07157146bd4a Mon Sep 17 00:00:00 2001 From: James Crook Date: Wed, 5 Feb 2020 14:24:35 +0000 Subject: [PATCH] Fix Big Time TimerToolBar, Stage I - Update on idle (new idiom from Paul) - Dock at x1 or x2 size - Smooth resizing - Take some account of width when resizing - Promote Resizable docking code to ToolBar class --- src/toolbars/MeterToolBar.cpp | 15 ---- src/toolbars/MeterToolBar.h | 4 +- src/toolbars/TimerToolBar.cpp | 75 +++++++++++++------ src/toolbars/TimerToolBar.h | 6 +- src/toolbars/ToolBar.cpp | 16 ++++ src/toolbars/ToolBar.h | 4 + src/widgets/NumericTextCtrl.h | 2 + win/Projects/Audacity/Audacity.vcxproj | 1 + .../Audacity/Audacity.vcxproj.filters | 15 ++-- 9 files changed, 89 insertions(+), 49 deletions(-) diff --git a/src/toolbars/MeterToolBar.cpp b/src/toolbars/MeterToolBar.cpp index a869f8b93..9ad5b1f13 100644 --- a/src/toolbars/MeterToolBar.cpp +++ b/src/toolbars/MeterToolBar.cpp @@ -248,21 +248,6 @@ bool MeterToolBar::Expose( bool show ) return ToolBar::Expose( show ); } -wxSize MeterToolBar::GetDockedSize() -{ - const int tbs = toolbarSingle + toolbarGap; - wxSize sz = GetSize(); - wxSize sz2 = GetMinSize(); - sz.x = wxMax( sz.x, sz2.x ); - sz.y = wxMax( sz.y, sz2.y ); - // 50 is the size where we switch from expanded to compact. - if( sz.y < 50 ) - sz.y = tbs-1; - else - sz.y = 2 * tbs -1; - return sz; -} - // The meter's sizing code does not take account of the resizer // Hence after docking we need to enlarge the bar (using fit) // so that the resizer can be reached. diff --git a/src/toolbars/MeterToolBar.h b/src/toolbars/MeterToolBar.h index 1752622ba..315e4544b 100644 --- a/src/toolbars/MeterToolBar.h +++ b/src/toolbars/MeterToolBar.h @@ -50,7 +50,9 @@ class MeterToolBar final : public ToolBar { int GetInitialWidth() override {return (mWhichMeters == (kWithRecordMeter + kWithPlayMeter)) ? 338 : 460;} // Separate bars used to be smaller. int GetMinToolbarWidth() override { return 150; } - wxSize GetDockedSize() override; + wxSize GetDockedSize() override { + return GetSmartDockedSize(); + }; virtual void SetDocked(ToolDock *dock, bool pushed)override; private: diff --git a/src/toolbars/TimerToolBar.cpp b/src/toolbars/TimerToolBar.cpp index 6cd9e0a76..a8588a48d 100644 --- a/src/toolbars/TimerToolBar.cpp +++ b/src/toolbars/TimerToolBar.cpp @@ -43,7 +43,10 @@ #include "../KeyboardCapture.h" #include "../Prefs.h" #include "../Project.h" +#include "../ProjectAudioIO.h" +#include "../ProjectSettings.h" #include "../Snap.h" +#include "../ViewInfo.h" #include "../widgets/NumericTextCtrl.h" #include "../AllThemeResources.h" @@ -59,8 +62,9 @@ enum { }; BEGIN_EVENT_TABLE(TimerToolBar, ToolBar) -EVT_SIZE(TimerToolBar::OnSize) -EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, TimerToolBar::OnCaptureKey) + EVT_SIZE(TimerToolBar::OnSize) + EVT_IDLE( TimerToolBar::OnIdle ) + EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, TimerToolBar::OnCaptureKey) END_EVENT_TABLE() TimerToolBar::TimerToolBar( AudacityProject &project ) @@ -119,26 +123,36 @@ void TimerToolBar::UpdatePrefs() ToolBar::UpdatePrefs(); } -void TimerToolBar::OnSize(wxSizeEvent & event) +void TimerToolBar::OnSize(wxSizeEvent & ev) { - event.Skip(); + ev.Skip(); + + if (!mAudioTime) + return; - int sh = GetSize().GetHeight() - 10; - - if (mAudioTime) - { - mAudioTime->SetDigitSize( sh*.63, sh ); - wxSize ms = mAudioTime->GetSize(); - //int mw = ms.GetWidth(); - //mAudioTime->SetMinSize(GetSizer()->GetMinSize()); - //printf("(size) %i %i\n", GetSizer()->GetSize()); - } - //SetMinSize( GetSizer()->GetMinSize() ); - //Layout(); - //Fit(); - - //Refresh(true); - //evt.Skip(); + // This 'OnSize' function is also called during moving the + // toolbar. + + // 10 and 40 are magic numbers to allow some space around + // the numeric control. The horizontal space reserved + // is deliberately not quite enough. Size of font is set + // primarily by the height of the toolbar. The width + // calculations just stop the font width being + // ridiculously too large to fit. + // In practice a user will drag the size to get a good font + // size and adjust the width so that part of the control + // does not disappear. + float f = mAudioTime->GetAspectRatio(); + int sh = wxMax( 5, ev.GetSize().GetHeight() - 10); + int sw = wxMax( sh, ev.GetSize().GetWidth() - 40)/f; + sh = wxMin( sh, sw ); + mAudioTime->SetDigitSize( sh*0.63, sh ); + + // Refresh and update immediately, so that we don't get + // to see grot from partly redrawn toolbar during + // the resizing. + Refresh(false); + Update(); } void TimerToolBar::SetTimes(double audio) @@ -169,9 +183,24 @@ void TimerToolBar::OnCaptureKey(wxCommandEvent &event) event.Skip(); } -void TimerToolBar::SetDocked(ToolDock *dock, bool pushed) { - ToolBar::SetDocked(dock, pushed); - Fit(); +void TimerToolBar::OnIdle( wxIdleEvent &evt ) +{ + evt.Skip(); + auto &project = mProject; + + double audioTime; + + auto &projectAudioIO = ProjectAudioIO::Get( project ); + if ( projectAudioIO.IsAudioActive() ){ + auto gAudioIO = AudioIOBase::Get(); + audioTime = gAudioIO->GetStreamTime(); + } + else { + const auto &playRegion = ViewInfo::Get( project ).playRegion; + audioTime = playRegion.GetStart(); + } + + SetTimes( audioTime); } diff --git a/src/toolbars/TimerToolBar.h b/src/toolbars/TimerToolBar.h index 8d22c62e1..d65507e79 100644 --- a/src/toolbars/TimerToolBar.h +++ b/src/toolbars/TimerToolBar.h @@ -37,6 +37,10 @@ public: void SetTimes(double audio); void RegenerateTooltips() override {}; + wxSize GetDockedSize() override { + return GetSmartDockedSize(); + }; + private: NumericTextCtrl * AddTime( const TranslatableString &Name, int id); @@ -44,8 +48,8 @@ private: void OnFocus(wxFocusEvent &event); void OnCaptureKey(wxCommandEvent &event); void OnSize(wxSizeEvent &evt); + void OnIdle( wxIdleEvent &evt ); void OnSnapTo(wxCommandEvent & event); - virtual void SetDocked(ToolDock *dock, bool pushed) override; SelectionBarListener * mListener; double mRate; diff --git a/src/toolbars/ToolBar.cpp b/src/toolbars/ToolBar.cpp index dfbb737f7..522a728d4 100644 --- a/src/toolbars/ToolBar.cpp +++ b/src/toolbars/ToolBar.cpp @@ -483,6 +483,22 @@ void ToolBar::SetToDefaultSize(){ SetSize( sz ); } +wxSize ToolBar::GetSmartDockedSize() +{ + const int tbs = toolbarSingle + toolbarGap; + wxSize sz = GetSize(); + wxSize sz2 = GetMinSize(); + sz.x = wxMax( sz.x, sz2.x ); + sz.y = wxMax( sz.y, sz2.y ); + // 46 is the size where we switch from expanded to compact. + if( sz.y < 46 ) + sz.y = tbs-1; + else + sz.y = 2 * tbs -1; + return sz; +} + + void ToolBar::ReCreateButtons() { wxSize sz3 = GetSize(); diff --git a/src/toolbars/ToolBar.h b/src/toolbars/ToolBar.h index e51a66ad7..4d1d01eed 100644 --- a/src/toolbars/ToolBar.h +++ b/src/toolbars/ToolBar.h @@ -141,6 +141,10 @@ public: virtual int GetInitialWidth() { return -1; } virtual int GetMinToolbarWidth() { return GetInitialWidth(); } virtual wxSize GetDockedSize() { return GetMinSize(); } + + // Utility function for certain resizable toolbars. + // Allows them to dock at normal or double size. + wxSize GetSmartDockedSize(); public: static diff --git a/src/widgets/NumericTextCtrl.h b/src/widgets/NumericTextCtrl.h index 49af8d2ab..2f4793663 100644 --- a/src/widgets/NumericTextCtrl.h +++ b/src/widgets/NumericTextCtrl.h @@ -216,6 +216,8 @@ class NumericTextCtrl final : public wxControl, public NumericConverter int GetFocusedField() { return mLastField; } int GetFocusedDigit() { return mFocusedDigit; } + // give a sane aspect ratio even if zero height. + float GetAspectRatio() { return mHeight ? (mWidth / mHeight):10; } private: diff --git a/win/Projects/Audacity/Audacity.vcxproj b/win/Projects/Audacity/Audacity.vcxproj index 39fb49fe2..0bef6771f 100755 --- a/win/Projects/Audacity/Audacity.vcxproj +++ b/win/Projects/Audacity/Audacity.vcxproj @@ -596,6 +596,7 @@ + diff --git a/win/Projects/Audacity/Audacity.vcxproj.filters b/win/Projects/Audacity/Audacity.vcxproj.filters index de0201398..51f32f134 100755 --- a/win/Projects/Audacity/Audacity.vcxproj.filters +++ b/win/Projects/Audacity/Audacity.vcxproj.filters @@ -2467,6 +2467,9 @@ src\effects\lv2\zix + + src\toolbars + @@ -2481,15 +2484,6 @@ - - nyquist - - - nyquist - - - nyquist - nyquist @@ -2624,6 +2618,9 @@ nyquist + + +