From f66386c684d5dd45aae8be1c50fbc4c938a11d2d Mon Sep 17 00:00:00 2001 From: Alexander Illarionov Date: Mon, 24 Jun 2024 14:20:09 +0300 Subject: [PATCH] Issues #24 Made width and height attributes as optional for windowSizeItems section --- SmartContextMenu/ContextMenuManager.cs | 2 +- SmartContextMenu/Forms/MainForm.cs | 10 ++- SmartContextMenu/Forms/SizeForm.cs | 66 +++++-------------- SmartContextMenu/Forms/SizeSettingsForm.cs | 39 ++--------- .../Settings/ApplicationSettingsFile.cs | 12 ++-- .../Settings/WindowSizeMenuItem.cs | 8 +-- SmartContextMenu/Window.cs | 8 ++- 7 files changed, 47 insertions(+), 98 deletions(-) diff --git a/SmartContextMenu/ContextMenuManager.cs b/SmartContextMenu/ContextMenuManager.cs index fa85124..08c112d 100644 --- a/SmartContextMenu/ContextMenuManager.cs +++ b/SmartContextMenu/ContextMenuManager.cs @@ -253,7 +253,7 @@ private static void SetChecked(ToolStripMenuItem toolStripMenuItem, Settings.Men private static void SetChecked(ToolStripMenuItem toolStripMenuItem, Window window, WindowSizeMenuItem menuItem) { var size = window.Size; - toolStripMenuItem.Checked = menuItem.Width == size.Width && menuItem.Height == size.Height; + toolStripMenuItem.Checked = menuItem.Width.HasValue && menuItem.Height.HasValue && menuItem.Width == size.Width && menuItem.Height == size.Height; } private static void SetChecked(ToolStripMenuItem toolStripMenuItem, Window window, MoveToMenuItem menuItem) diff --git a/SmartContextMenu/Forms/MainForm.cs b/SmartContextMenu/Forms/MainForm.cs index 98eb559..cc2b44f 100644 --- a/SmartContextMenu/Forms/MainForm.cs +++ b/SmartContextMenu/Forms/MainForm.cs @@ -308,11 +308,17 @@ private void MenuItemClick(Window window, Settings.MenuItem menuItem) else if (_settings.Sizer == WindowSizerType.WindowWithoutMargins) { var margins = window.GetSystemMargins(); - window.SetSize(sizeForm.WindowWidth + margins.Left + margins.Right, sizeForm.WindowHeight + margins.Top + margins.Bottom, sizeForm.WindowLeft, sizeForm.WindowTop); + window.SetSize(sizeForm.WindowWidth == null ? null : (sizeForm.WindowWidth + margins.Left + margins.Right), + sizeForm.WindowHeight == null ? null : (sizeForm.WindowHeight + margins.Top + margins.Bottom), + sizeForm.WindowLeft, + sizeForm.WindowTop); } else { - window.SetSize(sizeForm.WindowWidth + (window.Size.Width - window.ClientSize.Width), sizeForm.WindowHeight + (window.Size.Height - window.ClientSize.Height), sizeForm.WindowLeft, sizeForm.WindowTop); + window.SetSize(sizeForm.WindowWidth == null ? null : (sizeForm.WindowWidth + (window.Size.Width - window.ClientSize.Width)), + sizeForm.WindowHeight == null ? null : (sizeForm.WindowHeight + (window.Size.Height - window.ClientSize.Height)), + sizeForm.WindowLeft, + sizeForm.WindowTop); } } } diff --git a/SmartContextMenu/Forms/SizeForm.cs b/SmartContextMenu/Forms/SizeForm.cs index 89a55c6..5476d7e 100644 --- a/SmartContextMenu/Forms/SizeForm.cs +++ b/SmartContextMenu/Forms/SizeForm.cs @@ -5,13 +5,13 @@ namespace SmartContextMenu.Forms { partial class SizeForm : Form { - public int WindowLeft { get; private set; } + public int? WindowLeft { get; private set; } - public int WindowTop { get; private set; } + public int? WindowTop { get; private set; } - public int WindowWidth { get; private set; } + public int? WindowWidth { get; private set; } - public int WindowHeight { get; private set; } + public int? WindowHeight { get; private set; } public SizeForm(LanguageManager manager, Window window) { @@ -28,59 +28,27 @@ private void InitializeControls(LanguageManager manager, Window window) btnApply.Text = manager.GetString("size_btn_apply"); Text = manager.GetString("size_form"); - var left = window.Size.Left; - var top = window.Size.Top; - var width = window.Size.Width; - var height = window.Size.Height; + var size = window.Size; - WindowLeft = left; - WindowTop = top; - WindowWidth = width; - WindowHeight = height; + WindowLeft = size.Left; + WindowTop = size.Top; + WindowWidth = size.Width; + WindowHeight = size.Height; - txtLeft.Text = left.ToString(); - txtTop.Text = top.ToString(); - txtWidth.Text = width.ToString(); - txtHeight.Text = height.ToString(); + txtLeft.Text = size.Left.ToString(); + txtTop.Text = size.Top.ToString(); + txtWidth.Text = size.Width.ToString(); + txtHeight.Text = size.Height.ToString(); DialogResult = DialogResult.Cancel; } private void ButtonApplyClick(object sender, EventArgs e) { - if (!int.TryParse(txtLeft.Text, out var left)) - { - txtLeft.SelectAll(); - txtLeft.Focus(); - return; - } - - if (!int.TryParse(txtTop.Text, out var top)) - { - txtTop.SelectAll(); - txtTop.Focus(); - return; - } - - if (!int.TryParse(txtWidth.Text, out var width)) - { - txtWidth.SelectAll(); - txtWidth.Focus(); - return; - } - - if (!int.TryParse(txtHeight.Text, out var height)) - { - txtHeight.SelectAll(); - txtHeight.Focus(); - return; - } - - WindowLeft = left; - WindowTop = top; - WindowWidth = width; - WindowHeight = height; - + WindowLeft = int.TryParse(txtLeft.Text, out var left) ? left : null; + WindowTop = int.TryParse(txtTop.Text, out var top) ? top : null; + WindowWidth = int.TryParse(txtWidth.Text, out var width) ? width : null; + WindowHeight = int.TryParse(txtHeight.Text, out var height) ? height : null; DialogResult = DialogResult.OK; Close(); } diff --git a/SmartContextMenu/Forms/SizeSettingsForm.cs b/SmartContextMenu/Forms/SizeSettingsForm.cs index 4d427d4..e84187c 100644 --- a/SmartContextMenu/Forms/SizeSettingsForm.cs +++ b/SmartContextMenu/Forms/SizeSettingsForm.cs @@ -36,8 +36,8 @@ private void InitializeControls(LanguageManager manager, WindowSizeMenuItem menu txtTitle.Text = menuItem.Title; txtLeft.Text = menuItem.Left == null ? string.Empty : menuItem.Left.Value.ToString(); txtTop.Text = menuItem.Top == null ? string.Empty : menuItem.Top.Value.ToString(); - txtWidth.Text = menuItem.Width.ToString(); - txtHeight.Text = menuItem.Height.ToString(); + txtWidth.Text = menuItem.Width == null ? string.Empty : menuItem.Width.ToString(); + txtHeight.Text = menuItem.Height == null ? string.Empty : menuItem.Height.ToString(); cmbKey1.ValueMember = "Id"; cmbKey1.DisplayMember = "Text"; @@ -78,37 +78,10 @@ private void ButtonApplyClick(object sender, EventArgs e) menuItem.Key2 = (VirtualKeyModifier)cmbKey2.SelectedValue; menuItem.Key3 = (VirtualKey)cmbKey3.SelectedValue; - if (int.TryParse(txtWidth.Text, out var width)) - { - menuItem.Width = width; - } - else - { - txtWidth.SelectAll(); - txtWidth.Focus(); - return; - } - - if (int.TryParse(txtHeight.Text, out var height)) - { - menuItem.Height = height; - } - else - { - txtHeight.SelectAll(); - txtHeight.Focus(); - return; - } - - if (int.TryParse(txtLeft.Text, out var left)) - { - menuItem.Left = left; - } - - if (int.TryParse(txtTop.Text, out var top)) - { - menuItem.Top = top; - } + menuItem.Width = int.TryParse(txtWidth.Text, out var width) ? width : null; + menuItem.Height = int.TryParse(txtHeight.Text, out var height) ? height : null; + menuItem.Left = int.TryParse(txtLeft.Text, out var left) ? left : null; + menuItem.Top = int.TryParse(txtTop.Text, out var top) ? top : null; MenuItem = menuItem; DialogResult = DialogResult.OK; diff --git a/SmartContextMenu/Settings/ApplicationSettingsFile.cs b/SmartContextMenu/Settings/ApplicationSettingsFile.cs index c3be497..2c674c1 100644 --- a/SmartContextMenu/Settings/ApplicationSettingsFile.cs +++ b/SmartContextMenu/Settings/ApplicationSettingsFile.cs @@ -103,10 +103,10 @@ private static ApplicationSettings Read(Stream stream) .Select(x => new WindowSizeMenuItem { Title = x.Attribute("title") != null ? x.Attribute("title").Value : "", - Left = !string.IsNullOrEmpty(x.Attribute("left").Value) ? int.Parse(x.Attribute("left").Value) : (int?)null, - Top = !string.IsNullOrEmpty(x.Attribute("top").Value) ? int.Parse(x.Attribute("top").Value) : (int?)null, - Width = int.Parse(x.Attribute("width").Value), - Height = int.Parse(x.Attribute("height").Value), + Left = !string.IsNullOrEmpty(x.Attribute("left").Value) ? int.Parse(x.Attribute("left").Value) : null, + Top = !string.IsNullOrEmpty(x.Attribute("top").Value) ? int.Parse(x.Attribute("top").Value) : null, + Width = !string.IsNullOrEmpty(x.Attribute("width").Value) ? int.Parse(x.Attribute("width").Value) : null, + Height = !string.IsNullOrEmpty(x.Attribute("height").Value) ? int.Parse(x.Attribute("height").Value) : null, Key1 = x.Attribute("key1") != null && !string.IsNullOrEmpty(x.Attribute("key1").Value) ? (VirtualKeyModifier)int.Parse(x.Attribute("key1").Value) : VirtualKeyModifier.None, Key2 = x.Attribute("key2") != null && !string.IsNullOrEmpty(x.Attribute("key2").Value) ? (VirtualKeyModifier)int.Parse(x.Attribute("key2").Value) : VirtualKeyModifier.None, Key3 = x.Attribute("key3") != null && !string.IsNullOrEmpty(x.Attribute("key3").Value) ? (VirtualKey)int.Parse(x.Attribute("key3").Value) : VirtualKey.None @@ -282,8 +282,8 @@ private static void Save(string fileName, ApplicationSettings settings) new XAttribute("title", x.Title), new XAttribute("left", x.Left == null ? "" : x.Left.Value.ToString()), new XAttribute("top", x.Top == null ? "" : x.Top.Value.ToString()), - new XAttribute("width", x.Width), - new XAttribute("height", x.Height), + new XAttribute("width", x.Width == null ? "" : x.Width.ToString()), + new XAttribute("height", x.Height == null ? "" : x.Height.ToString()), new XAttribute("key1", x.Key1 == VirtualKeyModifier.None ? "" : ((int)x.Key1).ToString()), new XAttribute("key2", x.Key2 == VirtualKeyModifier.None ? "" : ((int)x.Key2).ToString()), new XAttribute("key3", x.Key3 == VirtualKey.None ? "" : ((int)x.Key3).ToString())))), diff --git a/SmartContextMenu/Settings/WindowSizeMenuItem.cs b/SmartContextMenu/Settings/WindowSizeMenuItem.cs index 4a779c3..9dcbed1 100644 --- a/SmartContextMenu/Settings/WindowSizeMenuItem.cs +++ b/SmartContextMenu/Settings/WindowSizeMenuItem.cs @@ -12,9 +12,9 @@ public class WindowSizeMenuItem : ICloneable public int? Top { get; set; } - public int Width { get; set; } + public int? Width { get; set; } - public int Height { get; set; } + public int? Height { get; set; } public VirtualKeyModifier Key1 { get; set; } @@ -27,8 +27,8 @@ public WindowSizeMenuItem() Title = string.Empty; Left = null; Top = null; - Width = 0; - Height = 0; + Width = null; + Height = null; Key1 = VirtualKeyModifier.None; Key2 = VirtualKeyModifier.None; Key3 = VirtualKey.None; diff --git a/SmartContextMenu/Window.cs b/SmartContextMenu/Window.cs index f0c92f1..8c4e4a4 100644 --- a/SmartContextMenu/Window.cs +++ b/SmartContextMenu/Window.cs @@ -335,12 +335,14 @@ public void SetHeight(int height) MoveWindow(Handle, size.Left, size.Top, size.Width, height, true); } - public void SetSize(int width, int height, int? left = null, int? top = null) + public void SetSize(int? width = null, int? height = null, int? left = null, int? top = null) { var size = Size; var sizeLeft = left == null ? size.Left : left.Value; - var sizeTop = top == null ? Size.Top : top.Value; - MoveWindow(Handle, sizeLeft, sizeTop, width, height, true); + var sizeTop = top == null ? size.Top : top.Value; + var sizeWidth = width == null ? size.Width : width.Value; + var sizeHeight = height == null ? size.Height : height.Value; + MoveWindow(Handle, sizeLeft, sizeTop, sizeWidth, sizeHeight, true); } public void SetLeft(int left)