Make SaveSizeAndLocation and RestoreSizeAndLocation a form extension.

This commit is contained in:
Mbucari 2021-09-03 22:44:02 -06:00
parent bcab2dd440
commit 37970222f3
5 changed files with 98 additions and 110 deletions

View File

@ -46,6 +46,7 @@ namespace FileManager
private PersistentDictionary persistentDictionary;
public T GetNonString<T>(string propertyName) => persistentDictionary.GetNonString<T>(propertyName);
public object GetObject(string propertyName) => persistentDictionary.GetObject(propertyName);
public void SetObject(string propertyName, object newValue) => persistentDictionary.SetNonString(propertyName, newValue);
@ -67,39 +68,7 @@ namespace FileManager
return attribute?.Description;
}
public bool Exists(string propertyName) => persistentDictionary.Exists(propertyName);
#region MainForm: X, Y, Width, Height, MainFormIsMaximized
public int MainFormX
{
get => persistentDictionary.GetNonString<int>(nameof(MainFormX));
set => persistentDictionary.SetNonString(nameof(MainFormX), value);
}
public int MainFormY
{
get => persistentDictionary.GetNonString<int>(nameof(MainFormY));
set => persistentDictionary.SetNonString(nameof(MainFormY), value);
}
public int MainFormWidth
{
get => persistentDictionary.GetNonString<int>(nameof(MainFormWidth));
set => persistentDictionary.SetNonString(nameof(MainFormWidth), value);
}
public int MainFormHeight
{
get => persistentDictionary.GetNonString<int>(nameof(MainFormHeight));
set => persistentDictionary.SetNonString(nameof(MainFormHeight), value);
}
public bool MainFormIsMaximized
{
get => persistentDictionary.GetNonString<bool>(nameof(MainFormIsMaximized));
set => persistentDictionary.SetNonString(nameof(MainFormIsMaximized), value);
}
#endregion
public bool Exists(string propertyName) => persistentDictionary.Exists(propertyName);
[Description("Location for book storage. Includes destination of newly liberated books")]
public string Books

View File

@ -1,6 +1,7 @@
using ApplicationServices;
using DataLayer;
using Dinah.Core.DataBinding;
using FileManager;
using InternalUtilities;
using LibationWinForms.Login;
using System;
@ -28,6 +29,10 @@ namespace LibationWinForms.Dialogs
_accounts = accounts;
InitializeComponent();
this.Load += (_, _) => this.RestoreSizeAndLocation(Configuration.Instance);
this.FormClosing += (_, _) => this.SaveSizeAndLocation(Configuration.Instance);
_labelFormat = label1.Text;
_dataGridView.CellContentClick += (_, _) => _dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);

View File

@ -353,7 +353,6 @@
this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.Name = "Form1";
this.Text = "Libation: Liberate your Library";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();

View File

@ -29,8 +29,9 @@ namespace LibationWinForms
return;
// independent UI updates
this.Load += restoreSizeAndLocation;
this.Load += (_, _) => this.RestoreSizeAndLocation(Configuration.Instance);
this.Load += RefreshImportMenu;
this.FormClosing += (_, _) => this.SaveSizeAndLocation(Configuration.Instance);
LibraryCommands.LibrarySizeChanged += reloadGridAndUpdateBottomNumbers;
LibraryCommands.BookUserDefinedItemCommitted += setBackupCounts;
@ -53,81 +54,6 @@ namespace LibationWinForms
loadInitialQuickFilterState();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SaveSizeAndLocation();
}
private void restoreSizeAndLocation(object _ = null, object __ = null)
{
var config = Configuration.Instance;
var width = config.MainFormWidth;
var height = config.MainFormHeight;
// too small -- something must have gone wrong. use defaults
if (width < 25 || height < 25)
{
width = 1023;
height = 578;
}
// Fit to the current screen size in case the screen resolution changed since the size was last persisted
if (width > Screen.PrimaryScreen.WorkingArea.Width)
width = Screen.PrimaryScreen.WorkingArea.Width;
if (height > Screen.PrimaryScreen.WorkingArea.Height)
height = Screen.PrimaryScreen.WorkingArea.Height;
var x = config.MainFormX;
var y = config.MainFormY;
var rect = new System.Drawing.Rectangle(x, y, width, height);
// is proposed rect on a screen?
if (Screen.AllScreens.Any(screen => screen.WorkingArea.Contains(rect)))
{
this.StartPosition = FormStartPosition.Manual;
this.DesktopBounds = rect;
}
else
{
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
this.Size = rect.Size;
}
// FINAL: for Maximized: start normal state, set size and location, THEN set max state
this.WindowState = config.MainFormIsMaximized ? FormWindowState.Maximized : FormWindowState.Normal;
}
private void SaveSizeAndLocation()
{
System.Drawing.Point location;
System.Drawing.Size size;
// save location and size if the state is normal
if (this.WindowState == FormWindowState.Normal)
{
location = this.Location;
size = this.Size;
}
else
{
// save the RestoreBounds if the form is minimized or maximized
location = this.RestoreBounds.Location;
size = this.RestoreBounds.Size;
}
var config = Configuration.Instance;
config.MainFormX = location.X;
config.MainFormY = location.Y;
config.MainFormWidth = size.Width;
config.MainFormHeight = size.Height;
config.MainFormIsMaximized = this.WindowState == FormWindowState.Maximized;
}
private void reloadGridAndUpdateBottomNumbers(object _ = null, object __ = null)
{
// suppressed filter while init'ing UI

View File

@ -0,0 +1,89 @@
using FileManager;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace LibationWinForms
{
public static class FormSaveExtension
{
public static void RestoreSizeAndLocation(this Form form, Configuration config)
{
FormSizeAndPosition savedState = config.GetNonString<FormSizeAndPosition>(form.Name);
if (savedState is null)
return;
// too small -- something must have gone wrong. use defaults
if (savedState.Width < 25 || savedState.Height < 25)
{
savedState.Width = form.Width;
savedState.Height = form.Height;
}
// Fit to the current screen size in case the screen resolution changed since the size was last persisted
if (savedState.Width > Screen.PrimaryScreen.WorkingArea.Width)
savedState.Width = Screen.PrimaryScreen.WorkingArea.Width;
if (savedState.Height > Screen.PrimaryScreen.WorkingArea.Height)
savedState.Height = Screen.PrimaryScreen.WorkingArea.Height;
var x = savedState.X;
var y = savedState.Y;
var rect = new Rectangle(x, y, savedState.Width, savedState.Height);
// is proposed rect on a screen?
if (Screen.AllScreens.Any(screen => screen.WorkingArea.Contains(rect)))
{
form.StartPosition = FormStartPosition.Manual;
form.DesktopBounds = rect;
}
else
{
form.StartPosition = FormStartPosition.WindowsDefaultLocation;
form.Size = rect.Size;
}
// FINAL: for Maximized: start normal state, set size and location, THEN set max state
form.WindowState = savedState.IsMaximized ? FormWindowState.Maximized : FormWindowState.Normal;
}
public static void SaveSizeAndLocation(this Form form, Configuration config)
{
Point location;
Size size;
var saveState = new FormSizeAndPosition();
// save location and size if the state is normal
if (form.WindowState == FormWindowState.Normal)
{
location = form.Location;
size = form.Size;
}
else
{
// save the RestoreBounds if the form is minimized or maximized
location = form.RestoreBounds.Location;
size = form.RestoreBounds.Size;
}
saveState.X = location.X;
saveState.Y = location.Y;
saveState.Width = size.Width;
saveState.Height = size.Height;
saveState.IsMaximized = form.WindowState == FormWindowState.Maximized;
config.SetObject(form.Name, saveState);
}
}
class FormSizeAndPosition
{
public int X;
public int Y;
public int Height;
public int Width;
public bool IsMaximized;
}
}