diff --git a/src/ChartImage.cs b/src/ChartImage.cs index b247691..d62664d 100644 --- a/src/ChartImage.cs +++ b/src/ChartImage.cs @@ -13,8 +13,6 @@ using log4net ; public class ChartImage { - // NOTE: This table maps shortcuts (e.g. Ctrl-Shift-C) to ChartImage's. - private static Dictionary< Tuple, List > mShortcuts = new Dictionary< Tuple, List >() ; private string mFullPath ; private JsonConfig mJsonConfig ; @@ -33,14 +31,17 @@ public class ChartImage ILog logger = LogManager.GetLogger( "shortcuts" ) ; string val = mJsonConfig.getStringVal( new string[]{"shortcut"} ) ; if ( val != "" ) { - Tuple shortcut = parseShortcut( val ) ; - if ( shortcut == null ) + Keys? keys = Shortcut.parseShortcutString( val ) ; + if ( keys == null ) Program.logStartupMsg( "bad-shortcut", $"{key}: {val}" ) ; else { - if ( ! mShortcuts.ContainsKey( shortcut ) ) - mShortcuts[ shortcut ] = new List() ; - logger.Info( $"Registering shortcut: {shortcutString(shortcut.Item1,shortcut.Item2)} => {caption()}" ) ; - mShortcuts[ shortcut ].Add( this ) ; + Shortcut shortcut = Shortcut.findRegisteredShortcut( keys.Value ) ; + if ( shortcut == null ) { + shortcut = new ChartImageShortcut( keys.Value ) ; + Shortcut.registerShortcut( shortcut ) ; + } + logger.Info( $"Registering shortcut: {shortcut.ToString()} => {caption()}" ) ; + ((ChartImageShortcut)shortcut).addChartImage( this ) ; } } @@ -141,52 +142,6 @@ public class ChartImage return totalScore ; } - private Tuple parseShortcut( string val ) - { - // parse the shortcut - // FUDGE! Can't just pass in '-' since a Mono-compiled EXE has trouble running on Windows :-/ - string[] parts = val.ToLower().Split( new char[]{'-'} ) ; - Keys modifiers = 0 ; - for ( int i=0 ; i < parts.Length-1 ; ++i ) { - if ( parts[i] == "ctrl" ) - modifiers |= Keys.Control ; - else if ( parts[i] == "alt" ) - modifiers |= Keys.Alt ; - else if ( parts[i] == "shift" ) - modifiers |= Keys.Shift ; - else - return null ; - } - Keys key ; - bool rc = Enum.TryParse( parts[parts.Length-1], true, out key ) ; - if ( ! rc ) - return null ; - return new Tuple( modifiers, key ) ; - } - - public static List checkShortcut( Keys modifiers, Keys key ) - { - // check if there are any ChartImage's associated with the specified shortcut - List chartImages ; - if ( ! mShortcuts.TryGetValue( new Tuple( modifiers, key ), out chartImages ) ) - return null ; - return chartImages ; - } - - public static string shortcutString( Keys modifiers, Keys key ) - { - // return the shortcut as a string - StringBuilder buf = new StringBuilder() ; - if ( (modifiers & Keys.Control) != 0 ) - buf.Append( "Ctrl-" ) ; - if ( (modifiers & Keys.Shift) != 0 ) - buf.Append( "Shift-" ) ; - if ( (modifiers & Keys.Alt) != 0 ) - buf.Append( "Alt-" ) ; - buf.Append( key ) ; - return buf.ToString() ; - } - public string caption() { // return the ChartImage's caption @@ -199,4 +154,5 @@ public class ChartImage public JsonConfig jsonConfig { get { return mJsonConfig ; } } public Image image { get { return mImage ; } } public ImageListViewItem imageListViewItem { get { return mImageListViewItem ; } } + } diff --git a/src/MainForm.cs b/src/MainForm.cs index 894f525..6f4ebb6 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -32,6 +32,7 @@ public partial class MainForm : Form private double mCurrZoom, mMinZoom, mMaxZoom ; private DateTime mLastKeyPressTimeStamp = DateTime.Now ; private bool mDisableProcessCmdKey = false ; + private bool mSearchQueryTextChangedDisabled = false ; public MainForm() { @@ -166,10 +167,10 @@ public partial class MainForm : Form List results2 = new List() ; foreach ( var r in results ) results2.Add( r.Item1 ) ; - loadSearchResults( results2, searchQuery ) ; + loadSearchResults( results2, searchQuery, false ) ; } - private void loadSearchResults( IEnumerable chartImages, string key ) + public void loadSearchResults( IEnumerable chartImages, string key, bool clearSearchQuery ) { // check if we need to reload the search results if ( key == mSearchResultsKey ) @@ -191,6 +192,13 @@ public partial class MainForm : Form } mSearchResults.ResumeLayout( true ) ; mSearchResultsKey = key ; + + // clear the search query + if ( clearSearchQuery ) { + mSearchQueryTextChangedDisabled = true ; + mSearchQuery.Text = "" ; + mSearchQueryTextChangedDisabled = false ; + } } private void setChartImagePanelScrollPos( int? hscrollPos, int? vscrollPos ) diff --git a/src/MainForm.ui.cs b/src/MainForm.ui.cs index 13d4c7e..6775efa 100644 --- a/src/MainForm.ui.cs +++ b/src/MainForm.ui.cs @@ -179,25 +179,15 @@ public partial class MainForm : Form } if ( keyCode == Keys.Escape ) { // clear the search query - mSearchQuery.Text = "" ; - loadSearchResults( mChartImages.Values, "SHOW-ALL" ) ; + loadSearchResults( mChartImages.Values, "SHOW-ALL", true ) ; return true ; } if ( keyCode == Keys.Return ) return true ; // nb: stop Windows from beeping :-/ - // check for ChartImage shortcuts - List chartImages = ChartImage.checkShortcut( modifierKeys, keyCode ) ; - if ( chartImages != null ) { - // yup - show them as search results - ILog logger = LogManager.GetLogger( "shortcuts" ) ; - logger.Info( $"Found ChartImage's for shortcut: {ChartImage.shortcutString(modifierKeys,keyCode)}" ) ; - foreach ( ChartImage chartImage in chartImages ) - logger.Info( $"- {chartImage.caption()}" ) ; - mSearchQuery.Text = "" ; - loadSearchResults( chartImages, "HOTKEY:"+modifierKeys+":"+keyCode ) ; + // handle shortcuts + if ( Shortcut.handleShortcut( keyData ) ) return true ; - } // check if we should update the search query // FUDGE! This is incredibly annoying :-/ We get a *key code*, which is not the same as an ASCII character, @@ -467,6 +457,8 @@ public partial class MainForm : Form private void SearchQuery_TextChanged( object sender, EventArgs e ) { // update the search results + if ( mSearchQueryTextChangedDisabled ) + return ; updateSearchResults( mSearchQuery.Text.Trim() ) ; } diff --git a/src/Shortcut.cs b/src/Shortcut.cs new file mode 100644 index 0000000..511546d --- /dev/null +++ b/src/Shortcut.cs @@ -0,0 +1,112 @@ +using System ; +using System.Text ; +using System.Collections.Generic ; +using System.Windows.Forms ; + +using log4net ; + +// -------------------------------------------------------------------- + +public abstract class Shortcut +{ + + private static Dictionary mRegisteredShortcuts = new Dictionary() ; + + private Keys mKeys ; + + public Shortcut( Keys keys ) + { + // initialize + mKeys = keys ; + } + + public static Keys? parseShortcutString( string val ) + { + // parse the shortcut + // FUDGE! We can't just pass in '-' since a Mono-compiled EXE has trouble running on Windows :-/ + string[] parts = val.ToLower().Split( new char[]{'-'} ) ; + if ( parts.Length <= 1 ) + return null ; + Keys keys ; + bool rc = Enum.TryParse( parts[parts.Length-1], true, out keys ) ; + if ( ! rc ) + return null ; + for ( int i=0 ; i < parts.Length-1 ; ++i ) { + if ( parts[i] == "ctrl" ) + keys |= Keys.Control ; + else if ( parts[i] == "alt" ) + keys |= Keys.Alt ; + else if ( parts[i] == "shift" ) + keys |= Keys.Shift ; + else + return null ; + } + return keys ; + } + + public static bool handleShortcut( Keys keys ) + { + // look for a registered shortcut + Shortcut shortcut = findRegisteredShortcut( keys ) ; + if ( shortcut == null ) + return false ; + + // found one - execute it + ILog logger = LogManager.GetLogger( "shortcuts" ) ; + logger.Info( $"Executing shortcut: {shortcut}" ) ; + shortcut.executeShortcut() ; + return true ; + } + + public static void registerShortcut( Shortcut shortcut ) + { + // register the shortcut + mRegisteredShortcuts[ shortcut.mKeys ] = shortcut ; + } + + public static Shortcut findRegisteredShortcut( Keys keys ) + { + // look for a registered shortcut + Shortcut shortcut ; + if ( ! mRegisteredShortcuts.TryGetValue( keys, out shortcut ) ) + return null ; + return shortcut ; + } + + public override string ToString() + { + // return the Shortcut as a string + StringBuilder buf = new StringBuilder() ; + if ( (mKeys & Keys.Control) != 0 ) + buf.Append( "Ctrl-" ) ; + if ( (mKeys & Keys.Shift) != 0 ) + buf.Append( "Shift-" ) ; + if ( (mKeys & Keys.Alt) != 0 ) + buf.Append( "Alt-" ) ; + buf.Append( mKeys & ~(Keys.Control | Keys.Shift | Keys.Alt) ) ; + return buf.ToString() ; + } + + public abstract void executeShortcut() ; + +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +public class ChartImageShortcut : Shortcut +{ + private List mChartImages = new List() ; + + public ChartImageShortcut( Keys keys ) : base( keys ) { } + + public override void executeShortcut() + { + // execute the shortcut + ILog logger = LogManager.GetLogger( "shortcuts" ) ; + foreach ( ChartImage chartImage in mChartImages ) + logger.Debug( $"- {chartImage.caption()}" ) ; + Program.mainForm.loadSearchResults( mChartImages, "HOTKEY:"+this, true ) ; + } + + public void addChartImage( ChartImage ci ) { mChartImages.Add( ci ) ; } +}