diff --git a/src/Project.cpp b/src/Project.cpp index b1f825b5b..c9b7c4be6 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -1351,10 +1351,6 @@ void AudacityProject::UpdatePrefs() if (mRuler) { mRuler->UpdatePrefs(); } - - // The toolbars will be recreated, so make sure we don't leave - // a stale pointer hanging around. - mLastFocusedWindow = NULL; } void AudacityProject::SetMissingAliasFileDialog(wxDialog *dialog) @@ -2416,11 +2412,6 @@ void AudacityProject::OnActivate(wxActivateEvent & event) mActive = event.GetActive(); -#if defined(__WXGTK__) - // See bug #294 for explanation - mTrackPanel->SetFocus(); -#endif - // Under Windows, focus can be "lost" when returning to // Audacity from a different application. // @@ -2428,17 +2419,11 @@ void AudacityProject::OnActivate(wxActivateEvent & event) // then ALT+TAB to return to Audacity. Focus will be given to the // project window frame which is not at all useful. // - // So, when the project window receives a deactivate event, we - // remember which child had the focus. Then, when we receive the + // So, we use ToolManager's observation of focus changes in a wxEventFilter. + // Then, when we receive the // activate event, we restore that focus to the child or the track // panel if no child had the focus (which probably should never happen). if (!mActive) { - // We only want to remember the last focused window if FindFocus() returns - // a window within the current project frame. - wxWindow *w = FindFocus(); - if (wxGetTopLevelParent(w) ==this) { - mLastFocusedWindow = w; - } #ifdef __WXMAC__ if (IsIconized()) MacShowUndockedToolbars(false); @@ -2446,16 +2431,11 @@ void AudacityProject::OnActivate(wxActivateEvent & event) } else { SetActiveProject(this); - if (mLastFocusedWindow) { - mLastFocusedWindow->SetFocus(); - } - else { + if ( ! GetToolManager()->RestoreFocus() ) { if (mTrackPanel) { mTrackPanel->SetFocus(); } } - // No longer need to remember the last focused window - mLastFocusedWindow = NULL; #ifdef __WXMAC__ MacShowUndockedToolbars(true); @@ -2616,7 +2596,6 @@ void AudacityProject::OnCloseWindow(wxCloseEvent & event) // its size change. SaveWindowSize(); - mLastFocusedWindow = NULL; mIsDeleting = true; // Mac: we never quit as the result of a close. diff --git a/src/Project.h b/src/Project.h index 3c7a06051..ad24a97db 100644 --- a/src/Project.h +++ b/src/Project.h @@ -710,9 +710,6 @@ private: bool mLockPlayRegion; - // See AudacityProject::OnActivate() for an explanation of this. - wxWindow *mLastFocusedWindow{}; - std::unique_ptr mImportXMLTagHandler; // Last auto-save file name and path (empty if none) diff --git a/src/toolbars/ToolManager.cpp b/src/toolbars/ToolManager.cpp index a16a22ac0..9628d0a0b 100644 --- a/src/toolbars/ToolManager.cpp +++ b/src/toolbars/ToolManager.cpp @@ -1491,11 +1491,19 @@ void ToolManager::DoneDragging() mDidDrag = false; mClicked = false; + RestoreFocus(); +} + +bool ToolManager::RestoreFocus() +{ if (mLastFocus) { auto temp1 = AButton::TemporarilyAllowFocus(); auto temp2 = ASlider::TemporarilyAllowFocus(); auto temp3 = Meter::TemporarilyAllowFocus(); auto parent = mLastFocus->GetParent(); mLastFocus->SetFocus(); + return true; } + + return false; } diff --git a/src/toolbars/ToolManager.h b/src/toolbars/ToolManager.h index c4c09703e..3e91d0b26 100644 --- a/src/toolbars/ToolManager.h +++ b/src/toolbars/ToolManager.h @@ -71,6 +71,8 @@ class ToolManager final : public wxEvtHandler, public wxEventFilter int FilterEvent(wxEvent &event) override; + bool RestoreFocus(); + private: ToolBar *Float( ToolBar *t, wxPoint & pos );