From 37970222f3c7b31ccba3731f13c165b413b4a863 Mon Sep 17 00:00:00 2001 From: Mbucari <37587114+Mbucari@users.noreply.github.com> Date: Fri, 3 Sep 2021 22:44:02 -0600 Subject: [PATCH] Make SaveSizeAndLocation and RestoreSizeAndLocation a form extension. --- FileManager/Configuration.cs | 35 +------- LibationWinForms/Dialogs/RemoveBooksDialog.cs | 5 ++ LibationWinForms/Form1.Designer.cs | 1 - LibationWinForms/Form1.cs | 78 +--------------- LibationWinForms/FormSaveExtension.cs | 89 +++++++++++++++++++ 5 files changed, 98 insertions(+), 110 deletions(-) create mode 100644 LibationWinForms/FormSaveExtension.cs diff --git a/FileManager/Configuration.cs b/FileManager/Configuration.cs index 67080c76..770ce1c4 100644 --- a/FileManager/Configuration.cs +++ b/FileManager/Configuration.cs @@ -46,6 +46,7 @@ namespace FileManager private PersistentDictionary persistentDictionary; + public T GetNonString(string propertyName) => persistentDictionary.GetNonString(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(nameof(MainFormX)); - set => persistentDictionary.SetNonString(nameof(MainFormX), value); - } - - public int MainFormY - { - get => persistentDictionary.GetNonString(nameof(MainFormY)); - set => persistentDictionary.SetNonString(nameof(MainFormY), value); - } - - public int MainFormWidth - { - get => persistentDictionary.GetNonString(nameof(MainFormWidth)); - set => persistentDictionary.SetNonString(nameof(MainFormWidth), value); - } - - public int MainFormHeight - { - get => persistentDictionary.GetNonString(nameof(MainFormHeight)); - set => persistentDictionary.SetNonString(nameof(MainFormHeight), value); - } - - public bool MainFormIsMaximized - { - get => persistentDictionary.GetNonString(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 diff --git a/LibationWinForms/Dialogs/RemoveBooksDialog.cs b/LibationWinForms/Dialogs/RemoveBooksDialog.cs index faa829fc..c4faa3f1 100644 --- a/LibationWinForms/Dialogs/RemoveBooksDialog.cs +++ b/LibationWinForms/Dialogs/RemoveBooksDialog.cs @@ -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); diff --git a/LibationWinForms/Form1.Designer.cs b/LibationWinForms/Form1.Designer.cs index 1710dccc..7c1c0ec9 100644 --- a/LibationWinForms/Form1.Designer.cs +++ b/LibationWinForms/Form1.Designer.cs @@ -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(); diff --git a/LibationWinForms/Form1.cs b/LibationWinForms/Form1.cs index 3b1fe2a0..38922123 100644 --- a/LibationWinForms/Form1.cs +++ b/LibationWinForms/Form1.cs @@ -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 diff --git a/LibationWinForms/FormSaveExtension.cs b/LibationWinForms/FormSaveExtension.cs new file mode 100644 index 00000000..3acf13b2 --- /dev/null +++ b/LibationWinForms/FormSaveExtension.cs @@ -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(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; + } +}