From b4def2e2d6bd4d703c396c3d0931aada2a86f7a8 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Wed, 21 Jul 2021 23:13:14 -0400 Subject: [PATCH] Remember screen position. Issue #61 --- FileManager/Configuration.cs | 32 +++++++++ LibationLauncher/LibationLauncher.csproj | 2 +- LibationWinForms/Form1.Designer.cs | 5 +- LibationWinForms/Form1.cs | 85 ++++++++++++++++++++++-- 4 files changed, 117 insertions(+), 7 deletions(-) diff --git a/FileManager/Configuration.cs b/FileManager/Configuration.cs index 5b88c282..3cec142b 100644 --- a/FileManager/Configuration.cs +++ b/FileManager/Configuration.cs @@ -65,6 +65,38 @@ namespace FileManager 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 + [Description("Location for book storage. Includes destination of newly liberated books")] public string Books { diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 2af0a7f2..1f285814 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 5.3.3.1 + 5.3.4.1 diff --git a/LibationWinForms/Form1.Designer.cs b/LibationWinForms/Form1.Designer.cs index 91203c3a..0d34e4f5 100644 --- a/LibationWinForms/Form1.Designer.cs +++ b/LibationWinForms/Form1.Designer.cs @@ -239,14 +239,14 @@ // accountsToolStripMenuItem // this.accountsToolStripMenuItem.Name = "accountsToolStripMenuItem"; - this.accountsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.accountsToolStripMenuItem.Size = new System.Drawing.Size(133, 22); this.accountsToolStripMenuItem.Text = "&Accounts..."; this.accountsToolStripMenuItem.Click += new System.EventHandler(this.accountsToolStripMenuItem_Click); // // basicSettingsToolStripMenuItem // this.basicSettingsToolStripMenuItem.Name = "basicSettingsToolStripMenuItem"; - this.basicSettingsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.basicSettingsToolStripMenuItem.Size = new System.Drawing.Size(133, 22); this.basicSettingsToolStripMenuItem.Text = "&Settings..."; this.basicSettingsToolStripMenuItem.Click += new System.EventHandler(this.basicSettingsToolStripMenuItem_Click); // @@ -316,6 +316,7 @@ 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 772c7138..a296b339 100644 --- a/LibationWinForms/Form1.cs +++ b/LibationWinForms/Form1.cs @@ -40,6 +40,8 @@ namespace LibationWinForms if (this.DesignMode) return; + RestoreSizeAndLocation(); + // load default/missing cover images. this will also initiate the background image downloader var format = System.Drawing.Imaging.ImageFormat.Jpeg; PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format)); @@ -61,8 +63,83 @@ namespace LibationWinForms setBackupCounts(null, null); } - #region reload grid - bool isProcessingGridSelect = false; + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + SaveSizeAndLocation(); + } + + private void RestoreSizeAndLocation() + { + 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; + } + + #region reload grid + bool isProcessingGridSelect = false; private void reloadGrid() { // suppressed filter while init'ing UI @@ -395,6 +472,6 @@ namespace LibationWinForms private void accountsToolStripMenuItem_Click(object sender, EventArgs e) => new AccountsDialog(this).ShowDialog(); private void basicSettingsToolStripMenuItem_Click(object sender, EventArgs e) => new SettingsDialog().ShowDialog(); - #endregion - } + #endregion + } }