Skip to content

Commit

Permalink
Issues #24 Made width and height attributes as optional for windowSiz…
Browse files Browse the repository at this point in the history
…eItems section
  • Loading branch information
AlexanderPro committed Jun 24, 2024
1 parent 36aff69 commit f66386c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 98 deletions.
2 changes: 1 addition & 1 deletion SmartContextMenu/ContextMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions SmartContextMenu/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
66 changes: 17 additions & 49 deletions SmartContextMenu/Forms/SizeForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
}
Expand Down
39 changes: 6 additions & 33 deletions SmartContextMenu/Forms/SizeSettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions SmartContextMenu/Settings/ApplicationSettingsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())))),
Expand Down
8 changes: 4 additions & 4 deletions SmartContextMenu/Settings/WindowSizeMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions SmartContextMenu/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f66386c

Please sign in to comment.