Permalink
Browse files

Do not rebuild the search results so often

Unless they have changed + Added a placeholder text for the search panel
  • Loading branch information...
1 parent 661aa15 commit ca68b67bc8c767357195eb8bb174ca6af9c41a11 @robotboy655 robotboy655 committed May 3, 2016
@@ -4,6 +4,8 @@ AddCSLuaFile()
PANEL.Base = "Panel"
local ContentPanel = nil
+local CurrentSearch = ""
+local OldResults = -1
function PANEL:Init()
@@ -15,7 +17,7 @@ function PANEL:Init()
self.Search:Dock( FILL )
self.Search.OnEnter = function() self:RefreshResults() end
- self.Search.OnFocusChanged = function( _, b ) if ( b ) then self:RefreshResults() end end
+ self.Search.OnFocusChanged = function( _, b ) if ( b ) then ContentPanel:SwitchPanel( self.PropPanel ) end end
self.Search:SetTooltip( "Press enter to search" )
local btn = self.Search:Add( "DImageButton" )
@@ -60,28 +62,54 @@ function PANEL:Init()
hook.Add( "SearchUpdate", "SearchUpdate", function()
if ( !g_SpawnMenu:IsVisible() ) then return end
- self:RefreshResults()
+ self:RefreshResults( CurrentSearch )
end )
self.PropPanel = vgui.Create( "ContentContainer", self )
self.PropPanel:SetVisible( false )
self.PropPanel:SetTriggerSpawnlistChange( false )
+ -- Some sort of placeholder
+ local Header = self:Add( "ContentHeader" )
+ Header:SetText( "Press Enter to search" )
+ self.PropPanel:Add( Header )
+
g_SpawnMenu.SearchPropPanel = self.PropPanel
end
-function PANEL:RefreshResults()
+function PANEL:RefreshResults( str )
+
+ if ( !str ) then -- User tried to search for something
+ CurrentSearch = self.Search:GetText()
+ str = CurrentSearch
+ OldResults = -1
+ else
+ -- Don't force open the search when you click away from search while this function is called from cl_search_models.lua
+ if ( ContentPanel.SelectedPanel != self.PropPanel ) then
+ return
+ end
+ end
- if ( self.Search:GetText() == "" ) then return end
+ if ( !str or str == "" ) then return end
- self.PropPanel:Clear()
+ local results = search.GetResults( str )
+ for id, result in pairs( results ) do
+ result.icon:SetParent( GetHUDPanel() ) -- Don't parent the icons to search panel prematurely
+ end
- local results = search.GetResults( self.Search:GetText() )
+ -- I know this is not perfect, but this is the best I am willing to do with how the search library was set up
+ if ( OldResults == #results ) then -- No updates, don't rebuild
+ for id, result in pairs( results ) do result.icon:Remove() end -- Kill all icons
+ return
+ end
+ OldResults = #results
+
+ self.PropPanel:Clear()
local Header = self:Add( "ContentHeader" )
- Header:SetText( #results .. " Results for \"" .. self.Search:GetText() .. "\"" )
+ Header:SetText( #results .. " Results for \"" .. str .. "\"" )
self.PropPanel:Add( Header )
for k, v in pairs( results ) do

0 comments on commit ca68b67

Please sign in to comment.