From 69813f9f46588879a8f873a174ab1a3310c52bf9 Mon Sep 17 00:00:00 2001 From: Taka Date: Sat, 20 Jul 2019 06:39:52 +0000 Subject: [PATCH] Reworked how the search query TTL is implemented. --- src/MainForm.cs | 25 ++++++++++++++++++------- src/MainForm.ui.cs | 20 +++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/MainForm.cs b/src/MainForm.cs index a31fd9d..3d46955 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -180,6 +180,12 @@ public partial class MainForm : Form if ( key == mSearchResultsKey ) return ; + // initialize + if ( chartImages == null ) { + chartImages = mChartImages.Values ; + key = "SHOW-ALL" ; + } + // clear the search results mSearchResults.SuspendLayout() ; mSearchResults.Items.Clear() ; @@ -191,18 +197,15 @@ public partial class MainForm : Form foreach( ChartImage chartImage in chartImages ) { ImageListViewItem item = chartImage.imageListViewItem ; mSearchResults.Items.Add( item ) ; - if ( nItems++ == 0 && key != "" ) + if ( nItems++ == 0 && key != "" && key != "SHOW-ALL" ) item.Selected = true ; } mSearchResults.ResumeLayout( true ) ; mSearchResultsKey = key ; // clear the search query - if ( clearSearchQuery ) { - mSearchQueryTextChangedDisabled = true ; - mSearchQuery.Text = "" ; - mSearchQueryTextChangedDisabled = false ; - } + if ( clearSearchQuery ) + setSearchQuery( "", false ) ; } public void showShortcuts() @@ -267,7 +270,15 @@ public partial class MainForm : Form mWebBrowser.BringToFront() ; } - public void setSearchQuery( string s ) { mSearchQuery.Text = s ; } + public void setSearchQuery( string queryString, bool raiseEvents=true ) + { + // set the search query string + if ( ! raiseEvents ) + mSearchQueryTextChangedDisabled = true ; + mSearchQuery.Text = queryString ; + if ( ! raiseEvents ) + mSearchQueryTextChangedDisabled = false ; + } private void setChartImagePanelScrollPos( int? hscrollPos, int? vscrollPos ) { diff --git a/src/MainForm.ui.cs b/src/MainForm.ui.cs index c946763..ec51b80 100644 --- a/src/MainForm.ui.cs +++ b/src/MainForm.ui.cs @@ -93,6 +93,7 @@ public partial class MainForm : Form this.Load += new EventHandler( this.MainForm_Load ) ; this.FormClosing += new FormClosingEventHandler( this.MainForm_FormClosing ) ; this.Resize += new EventHandler( this.MainForm_Resize ) ; + this.mSearchQuery.KeyPress += this.SearchQuery_KeyPress ; this.mSearchQuery.TextChanged += new EventHandler( this.SearchQuery_TextChanged ) ; this.mSearchResults.SelectionChanged += new EventHandler( this.SearchResults_SelectionChanged ) ; @@ -185,7 +186,7 @@ public partial class MainForm : Form } if ( keyCode == Keys.Escape ) { // clear the search query - loadSearchResults( mChartImages.Values, "SHOW-ALL", true ) ; + loadSearchResults( null, "", true ) ; return true ; } if ( keyCode == Keys.Return ) @@ -212,15 +213,10 @@ public partial class MainForm : Form if ( (ch >= 65 && ch <= 90) || ch == 32 ) sendKey = ((char)ch).ToString().ToLower() ; else if ( ch >= 48 && ch <= 57 ) - sendKey = keyCode.ToString().Substring( 1 ) ; + sendKey = keyCode.ToString().Substring( 1 ) ; // nb: this will also detect Shift 0-9 :-/ else if ( keyCode == Keys.Back ) sendKey = "{BKSP}" ; if ( sendKey != "" ) { - // check if it's time to start a new search query - int ttl = Program.appConfig.getIntVal( new string[]{"SearchQueryTTL"}, 5 ) ; - if ( (DateTime.Now - mLastKeyPressTimeStamp).TotalSeconds > ttl ) - mSearchQuery.Text = "" ; - mLastKeyPressTimeStamp = DateTime.Now ; // send the keypress to the search query textbox mSearchQuery.Focus() ; mDisableProcessCmdKey = true ; @@ -458,6 +454,16 @@ public partial class MainForm : Form doMainFormResize( null ) ; } + private void SearchQuery_KeyPress( object sender, KeyPressEventArgs e ) + { + // check if it's time to start a new search query + int ttl = Program.appConfig.getIntVal( new string[]{"SearchQueryTTL"}, 5 ) ; + if ( (DateTime.Now - mLastKeyPressTimeStamp).TotalSeconds > ttl ) { + setSearchQuery( "", false ) ; + } + mLastKeyPressTimeStamp = DateTime.Now ; + } + private void SearchQuery_TextChanged( object sender, EventArgs e ) { // update the search results