Skip to content

Commit f66386c

Browse files
committed
Issues #24 Made width and height attributes as optional for windowSizeItems section
1 parent 36aff69 commit f66386c

File tree

7 files changed

+47
-98
lines changed

7 files changed

+47
-98
lines changed

SmartContextMenu/ContextMenuManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private static void SetChecked(ToolStripMenuItem toolStripMenuItem, Settings.Men
253253
private static void SetChecked(ToolStripMenuItem toolStripMenuItem, Window window, WindowSizeMenuItem menuItem)
254254
{
255255
var size = window.Size;
256-
toolStripMenuItem.Checked = menuItem.Width == size.Width && menuItem.Height == size.Height;
256+
toolStripMenuItem.Checked = menuItem.Width.HasValue && menuItem.Height.HasValue && menuItem.Width == size.Width && menuItem.Height == size.Height;
257257
}
258258

259259
private static void SetChecked(ToolStripMenuItem toolStripMenuItem, Window window, MoveToMenuItem menuItem)

SmartContextMenu/Forms/MainForm.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,17 @@ private void MenuItemClick(Window window, Settings.MenuItem menuItem)
308308
else if (_settings.Sizer == WindowSizerType.WindowWithoutMargins)
309309
{
310310
var margins = window.GetSystemMargins();
311-
window.SetSize(sizeForm.WindowWidth + margins.Left + margins.Right, sizeForm.WindowHeight + margins.Top + margins.Bottom, sizeForm.WindowLeft, sizeForm.WindowTop);
311+
window.SetSize(sizeForm.WindowWidth == null ? null : (sizeForm.WindowWidth + margins.Left + margins.Right),
312+
sizeForm.WindowHeight == null ? null : (sizeForm.WindowHeight + margins.Top + margins.Bottom),
313+
sizeForm.WindowLeft,
314+
sizeForm.WindowTop);
312315
}
313316
else
314317
{
315-
window.SetSize(sizeForm.WindowWidth + (window.Size.Width - window.ClientSize.Width), sizeForm.WindowHeight + (window.Size.Height - window.ClientSize.Height), sizeForm.WindowLeft, sizeForm.WindowTop);
318+
window.SetSize(sizeForm.WindowWidth == null ? null : (sizeForm.WindowWidth + (window.Size.Width - window.ClientSize.Width)),
319+
sizeForm.WindowHeight == null ? null : (sizeForm.WindowHeight + (window.Size.Height - window.ClientSize.Height)),
320+
sizeForm.WindowLeft,
321+
sizeForm.WindowTop);
316322
}
317323
}
318324
}

SmartContextMenu/Forms/SizeForm.cs

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace SmartContextMenu.Forms
55
{
66
partial class SizeForm : Form
77
{
8-
public int WindowLeft { get; private set; }
8+
public int? WindowLeft { get; private set; }
99

10-
public int WindowTop { get; private set; }
10+
public int? WindowTop { get; private set; }
1111

12-
public int WindowWidth { get; private set; }
12+
public int? WindowWidth { get; private set; }
1313

14-
public int WindowHeight { get; private set; }
14+
public int? WindowHeight { get; private set; }
1515

1616
public SizeForm(LanguageManager manager, Window window)
1717
{
@@ -28,59 +28,27 @@ private void InitializeControls(LanguageManager manager, Window window)
2828
btnApply.Text = manager.GetString("size_btn_apply");
2929
Text = manager.GetString("size_form");
3030

31-
var left = window.Size.Left;
32-
var top = window.Size.Top;
33-
var width = window.Size.Width;
34-
var height = window.Size.Height;
31+
var size = window.Size;
3532

36-
WindowLeft = left;
37-
WindowTop = top;
38-
WindowWidth = width;
39-
WindowHeight = height;
33+
WindowLeft = size.Left;
34+
WindowTop = size.Top;
35+
WindowWidth = size.Width;
36+
WindowHeight = size.Height;
4037

41-
txtLeft.Text = left.ToString();
42-
txtTop.Text = top.ToString();
43-
txtWidth.Text = width.ToString();
44-
txtHeight.Text = height.ToString();
38+
txtLeft.Text = size.Left.ToString();
39+
txtTop.Text = size.Top.ToString();
40+
txtWidth.Text = size.Width.ToString();
41+
txtHeight.Text = size.Height.ToString();
4542

4643
DialogResult = DialogResult.Cancel;
4744
}
4845

4946
private void ButtonApplyClick(object sender, EventArgs e)
5047
{
51-
if (!int.TryParse(txtLeft.Text, out var left))
52-
{
53-
txtLeft.SelectAll();
54-
txtLeft.Focus();
55-
return;
56-
}
57-
58-
if (!int.TryParse(txtTop.Text, out var top))
59-
{
60-
txtTop.SelectAll();
61-
txtTop.Focus();
62-
return;
63-
}
64-
65-
if (!int.TryParse(txtWidth.Text, out var width))
66-
{
67-
txtWidth.SelectAll();
68-
txtWidth.Focus();
69-
return;
70-
}
71-
72-
if (!int.TryParse(txtHeight.Text, out var height))
73-
{
74-
txtHeight.SelectAll();
75-
txtHeight.Focus();
76-
return;
77-
}
78-
79-
WindowLeft = left;
80-
WindowTop = top;
81-
WindowWidth = width;
82-
WindowHeight = height;
83-
48+
WindowLeft = int.TryParse(txtLeft.Text, out var left) ? left : null;
49+
WindowTop = int.TryParse(txtTop.Text, out var top) ? top : null;
50+
WindowWidth = int.TryParse(txtWidth.Text, out var width) ? width : null;
51+
WindowHeight = int.TryParse(txtHeight.Text, out var height) ? height : null;
8452
DialogResult = DialogResult.OK;
8553
Close();
8654
}

SmartContextMenu/Forms/SizeSettingsForm.cs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ private void InitializeControls(LanguageManager manager, WindowSizeMenuItem menu
3636
txtTitle.Text = menuItem.Title;
3737
txtLeft.Text = menuItem.Left == null ? string.Empty : menuItem.Left.Value.ToString();
3838
txtTop.Text = menuItem.Top == null ? string.Empty : menuItem.Top.Value.ToString();
39-
txtWidth.Text = menuItem.Width.ToString();
40-
txtHeight.Text = menuItem.Height.ToString();
39+
txtWidth.Text = menuItem.Width == null ? string.Empty : menuItem.Width.ToString();
40+
txtHeight.Text = menuItem.Height == null ? string.Empty : menuItem.Height.ToString();
4141

4242
cmbKey1.ValueMember = "Id";
4343
cmbKey1.DisplayMember = "Text";
@@ -78,37 +78,10 @@ private void ButtonApplyClick(object sender, EventArgs e)
7878
menuItem.Key2 = (VirtualKeyModifier)cmbKey2.SelectedValue;
7979
menuItem.Key3 = (VirtualKey)cmbKey3.SelectedValue;
8080

81-
if (int.TryParse(txtWidth.Text, out var width))
82-
{
83-
menuItem.Width = width;
84-
}
85-
else
86-
{
87-
txtWidth.SelectAll();
88-
txtWidth.Focus();
89-
return;
90-
}
91-
92-
if (int.TryParse(txtHeight.Text, out var height))
93-
{
94-
menuItem.Height = height;
95-
}
96-
else
97-
{
98-
txtHeight.SelectAll();
99-
txtHeight.Focus();
100-
return;
101-
}
102-
103-
if (int.TryParse(txtLeft.Text, out var left))
104-
{
105-
menuItem.Left = left;
106-
}
107-
108-
if (int.TryParse(txtTop.Text, out var top))
109-
{
110-
menuItem.Top = top;
111-
}
81+
menuItem.Width = int.TryParse(txtWidth.Text, out var width) ? width : null;
82+
menuItem.Height = int.TryParse(txtHeight.Text, out var height) ? height : null;
83+
menuItem.Left = int.TryParse(txtLeft.Text, out var left) ? left : null;
84+
menuItem.Top = int.TryParse(txtTop.Text, out var top) ? top : null;
11285

11386
MenuItem = menuItem;
11487
DialogResult = DialogResult.OK;

SmartContextMenu/Settings/ApplicationSettingsFile.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ private static ApplicationSettings Read(Stream stream)
103103
.Select(x => new WindowSizeMenuItem
104104
{
105105
Title = x.Attribute("title") != null ? x.Attribute("title").Value : "",
106-
Left = !string.IsNullOrEmpty(x.Attribute("left").Value) ? int.Parse(x.Attribute("left").Value) : (int?)null,
107-
Top = !string.IsNullOrEmpty(x.Attribute("top").Value) ? int.Parse(x.Attribute("top").Value) : (int?)null,
108-
Width = int.Parse(x.Attribute("width").Value),
109-
Height = int.Parse(x.Attribute("height").Value),
106+
Left = !string.IsNullOrEmpty(x.Attribute("left").Value) ? int.Parse(x.Attribute("left").Value) : null,
107+
Top = !string.IsNullOrEmpty(x.Attribute("top").Value) ? int.Parse(x.Attribute("top").Value) : null,
108+
Width = !string.IsNullOrEmpty(x.Attribute("width").Value) ? int.Parse(x.Attribute("width").Value) : null,
109+
Height = !string.IsNullOrEmpty(x.Attribute("height").Value) ? int.Parse(x.Attribute("height").Value) : null,
110110
Key1 = x.Attribute("key1") != null && !string.IsNullOrEmpty(x.Attribute("key1").Value) ? (VirtualKeyModifier)int.Parse(x.Attribute("key1").Value) : VirtualKeyModifier.None,
111111
Key2 = x.Attribute("key2") != null && !string.IsNullOrEmpty(x.Attribute("key2").Value) ? (VirtualKeyModifier)int.Parse(x.Attribute("key2").Value) : VirtualKeyModifier.None,
112112
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)
282282
new XAttribute("title", x.Title),
283283
new XAttribute("left", x.Left == null ? "" : x.Left.Value.ToString()),
284284
new XAttribute("top", x.Top == null ? "" : x.Top.Value.ToString()),
285-
new XAttribute("width", x.Width),
286-
new XAttribute("height", x.Height),
285+
new XAttribute("width", x.Width == null ? "" : x.Width.ToString()),
286+
new XAttribute("height", x.Height == null ? "" : x.Height.ToString()),
287287
new XAttribute("key1", x.Key1 == VirtualKeyModifier.None ? "" : ((int)x.Key1).ToString()),
288288
new XAttribute("key2", x.Key2 == VirtualKeyModifier.None ? "" : ((int)x.Key2).ToString()),
289289
new XAttribute("key3", x.Key3 == VirtualKey.None ? "" : ((int)x.Key3).ToString())))),

SmartContextMenu/Settings/WindowSizeMenuItem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class WindowSizeMenuItem : ICloneable
1212

1313
public int? Top { get; set; }
1414

15-
public int Width { get; set; }
15+
public int? Width { get; set; }
1616

17-
public int Height { get; set; }
17+
public int? Height { get; set; }
1818

1919
public VirtualKeyModifier Key1 { get; set; }
2020

@@ -27,8 +27,8 @@ public WindowSizeMenuItem()
2727
Title = string.Empty;
2828
Left = null;
2929
Top = null;
30-
Width = 0;
31-
Height = 0;
30+
Width = null;
31+
Height = null;
3232
Key1 = VirtualKeyModifier.None;
3333
Key2 = VirtualKeyModifier.None;
3434
Key3 = VirtualKey.None;

SmartContextMenu/Window.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,14 @@ public void SetHeight(int height)
335335
MoveWindow(Handle, size.Left, size.Top, size.Width, height, true);
336336
}
337337

338-
public void SetSize(int width, int height, int? left = null, int? top = null)
338+
public void SetSize(int? width = null, int? height = null, int? left = null, int? top = null)
339339
{
340340
var size = Size;
341341
var sizeLeft = left == null ? size.Left : left.Value;
342-
var sizeTop = top == null ? Size.Top : top.Value;
343-
MoveWindow(Handle, sizeLeft, sizeTop, width, height, true);
342+
var sizeTop = top == null ? size.Top : top.Value;
343+
var sizeWidth = width == null ? size.Width : width.Value;
344+
var sizeHeight = height == null ? size.Height : height.Value;
345+
MoveWindow(Handle, sizeLeft, sizeTop, sizeWidth, sizeHeight, true);
344346
}
345347

346348
public void SetLeft(int left)

0 commit comments

Comments
 (0)