Save and restore the main window position.

master
Pacman Ghost 5 years ago
parent 7d2b85f519
commit 2c793ef6fd
  1. 20
      src/JsonConfig.cs
  2. 29
      src/MainForm.ui.cs
  3. 2
      src/Program.cs

@ -9,12 +9,14 @@ using log4net ;
public class JsonConfig
{
private string mFilename ;
private dynamic mData ;
public JsonConfig( string caption, string fname )
{
// initialize
ILog logger = LogManager.GetLogger( "startup" ) ;
mFilename = fname ;
// load the JSON config
string data ;
@ -38,6 +40,7 @@ public class JsonConfig
}
return (string) curr ;
}
public void setStringVal( string[] keys, string val, bool save=true ) { setVal( keys, val, save ) ; }
public int getIntVal( string[] keys, int defaultVal=0 )
{
@ -49,6 +52,7 @@ public class JsonConfig
return defaultVal ;
}
}
public void setIntVal( string[] keys, int val, bool save=true ) { setVal( keys, val, save ) ; }
public float getFloatVal( string[] keys, float defaultVal=0 )
{
@ -61,5 +65,21 @@ public class JsonConfig
}
}
private void setVal( string[] keys, dynamic val, bool save )
{
// set the specified value
JToken curr = mData ;
for ( int i=0 ; i < keys.Length-1 ; ++i ) {
if ( curr[ keys[i] ] == null )
curr[ keys[i] ] = (JToken) JsonConvert.DeserializeObject( "{}" ) ;
curr = curr[ keys[i] ] ;
}
curr[ keys[keys.Length-1] ] = val ;
// save the updated config
if ( save )
File.WriteAllText( mFilename, data.ToString() ) ;
}
public dynamic data { get { return mData ; } }
}

@ -70,6 +70,7 @@ public partial class MainForm : Form
// initialize handlers
this.Load += new EventHandler( this.MainForm_Load ) ;
this.FormClosing += new FormClosingEventHandler( this.MainForm_FormClosing ) ;
this.Resize += new EventHandler( this.MainForm_Resize ) ;
this.mSearchQuery.TextChanged += new EventHandler( this.SearchQuery_TextChanged ) ;
this.mSearchQuery.KeyDown += new KeyEventHandler( this.SearchQuery_KeyDown ) ;
@ -109,6 +110,22 @@ public partial class MainForm : Form
private void MainForm_Load( object sender, EventArgs e )
{
// restore the window state
string windowStateStr = Program.appConfig.getStringVal( new string[]{"MainWindow","WindowState"} ) ;
if ( windowStateStr != "" ) {
FormWindowState windowState ;
if ( Enum.TryParse( windowStateStr, out windowState ) ) {
DesktopBounds = new Rectangle(
Program.appConfig.getIntVal( new string[]{"MainWindow","X"}, 50 ),
Program.appConfig.getIntVal( new string[]{"MainWindow","Y"}, 50 ),
Program.appConfig.getIntVal( new string[]{"MainWindow","Width"}, MinimumSize.Width ),
Program.appConfig.getIntVal( new string[]{"MainWindow","Height"}, MinimumSize.Height )
) ;
if ( windowState == FormWindowState.Maximized )
WindowState = windowState ;
}
}
// initialize
doMainFormResize( null ) ;
@ -117,6 +134,18 @@ public partial class MainForm : Form
loadSearchResults( mChartImages.Values ) ;
}
private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
{
// save the window state
// NOTE: RestoreBounds doesn't work properly under Mono :-/
Rectangle rc = (WindowState == FormWindowState.Maximized) ? RestoreBounds : DesktopBounds ;
Program.appConfig.setIntVal( new string[]{"MainWindow","X"}, rc.X, false ) ;
Program.appConfig.setIntVal( new string[]{"MainWindow","Y"}, rc.Y, false ) ;
Program.appConfig.setIntVal( new string[]{"MainWindow","Width"}, rc.Width, false ) ;
Program.appConfig.setIntVal( new string[]{"MainWindow","Height"}, rc.Height, false ) ;
Program.appConfig.setStringVal( new string[]{"MainWindow","WindowState"}, WindowState.ToString() ) ;
}
private void MainForm_Resize( object sender, EventArgs e ) { doMainFormResize( null ) ; }
private void doMainFormResize( Point? clientMousePos )

@ -53,7 +53,7 @@ public static class Program
if ( ! File.Exists( fname ) ) {
fname = Path.Combine( mDataDir, fname0 ) ;
if ( ! File.Exists( fname ) )
fname = Path.Combine( mBaseDir, "../"+fname0 ) ;
fname = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fname0 ) ;
}
mAppConfig = new JsonConfig( "app config", fname ) ;

Loading…
Cancel
Save