From 77eaa75db1f310ba31913102655ff3169b687c6e Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@craftyjon.com>
Date: Tue, 2 Apr 2024 18:51:19 -0400
Subject: [PATCH] Show all library children if library name matches search

(cherry picked from commit a042d1aab4422527669039ce1fb8097c6333ba2d)
---
 common/lib_tree_model.cpp | 20 +++++++++++++++-----
 include/lib_tree_model.h  |  2 ++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/common/lib_tree_model.cpp b/common/lib_tree_model.cpp
index 575109420a..3d4678e787 100644
--- a/common/lib_tree_model.cpp
+++ b/common/lib_tree_model.cpp
@@ -295,18 +295,18 @@ LIB_TREE_NODE_ITEM& LIB_TREE_NODE_LIBRARY::AddItem( LIB_TREE_ITEM* aItem )
 void LIB_TREE_NODE_LIBRARY::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
                                          std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
 {
-    int newScore = 0;
+    int maxChildScore = 0;
 
     for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
     {
         child->UpdateScore( aMatcher, aLib, aFilter );
-        newScore = std::max( newScore, child->m_Score );
+        maxChildScore = std::max( maxChildScore, child->m_Score );
     }
 
     // Each time UpdateScore is called for a library, child (item) scores may go up or down.
     // If the all go down to zero, we need to make sure to drop the library from the list.
-    if( newScore > 0 )
-        m_Score = std::max( m_Score, newScore );
+    if( maxChildScore > 0 )
+        m_Score = std::max( m_Score, maxChildScore );
     else
         m_Score = 0;
 
@@ -320,7 +320,17 @@ void LIB_TREE_NODE_LIBRARY::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const w
 
     // aMatcher test is additive
     if( aMatcher )
-        m_Score += aMatcher->ScoreTerms( m_SearchTerms );
+    {
+        int ownScore = aMatcher->ScoreTerms( m_SearchTerms );
+        m_Score += ownScore;
+
+        // If we have a hit on a library, show all children in that library
+        if( maxChildScore <= 0 && ownScore > 0 )
+        {
+            for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
+                child->ForceScore( 1 );
+        }
+    }
 
     // show all nodes if no search/filter/etc. criteria are given
     if( m_Children.empty() && !aMatcher && aLib.IsEmpty() && ( !aFilter || (*aFilter)(*this) ) )
diff --git a/include/lib_tree_model.h b/include/lib_tree_model.h
index 613afe8578..fc1e4d71c0 100644
--- a/include/lib_tree_model.h
+++ b/include/lib_tree_model.h
@@ -89,6 +89,8 @@ public:
      */
     virtual void ResetScore();
 
+    virtual void ForceScore( int aScore ) { m_Score = aScore; }
+
     /**
      * Store intrinsic ranks on all children of this node. See m_IntrinsicRank
      * member doc for more information.