diff --git a/OpenNest.WinForms.Tests/Controls/SeparatorPenLifetimeTests.cs b/OpenNest.WinForms.Tests/Controls/SeparatorPenLifetimeTests.cs index e65f7009..ebc6bbd0 100644 --- a/OpenNest.WinForms.Tests/Controls/SeparatorPenLifetimeTests.cs +++ b/OpenNest.WinForms.Tests/Controls/SeparatorPenLifetimeTests.cs @@ -75,7 +75,7 @@ public class SeparatorPenLifetimeTests horizontal ? new HorizontalLine() : new VerticalLine(); private static Pen GetPen(Control control, string name) => - Assert.IsType(control.GetType() + Assert.IsType(typeof(SeparatorLine) .GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(control)); private static bool GetStyle(Control control, ControlStyles style) => diff --git a/OpenNest/Controls/HorizontalLine.cs b/OpenNest/Controls/HorizontalLine.cs index c8d56386..0a8ef5e6 100644 --- a/OpenNest/Controls/HorizontalLine.cs +++ b/OpenNest/Controls/HorizontalLine.cs @@ -1,54 +1,12 @@ -using System; using System.Drawing; -using System.Windows.Forms; namespace OpenNest.Controls { - public class HorizontalLine : Control + public class HorizontalLine : SeparatorLine { - private readonly Pen lightPen; - private readonly Pen darkPen; + protected override int Across => Height; - public HorizontalLine() - { - SetStyle(ControlStyles.OptimizedDoubleBuffer, true); - SetStyle(ControlStyles.Selectable, false); - - lightPen = new Pen(ProfessionalColors.SeparatorLight); - darkPen = new Pen(ProfessionalColors.SeparatorDark); - } - - protected override void Dispose(bool disposing) - { - try - { - if (disposing) - { - lightPen.Dispose(); - darkPen.Dispose(); - } - } - finally - { - base.Dispose(disposing); - } - } - - protected override void OnResize(EventArgs e) - { - base.OnResize(e); - Invalidate(); - } - - protected override void OnPaint(PaintEventArgs e) - { - base.OnPaint(e); - - float midpoint = Height * 0.5f; - - e.Graphics.DrawLine(darkPen, 0, midpoint, Width, midpoint); - midpoint++; - e.Graphics.DrawLine(lightPen, 0, midpoint, Width, midpoint); - } + protected override void DrawLine(Graphics graphics, Pen pen, float position) => + graphics.DrawLine(pen, 0, position, Width, position); } } diff --git a/OpenNest/Controls/SeparatorLine.cs b/OpenNest/Controls/SeparatorLine.cs new file mode 100644 index 00000000..88eee54e --- /dev/null +++ b/OpenNest/Controls/SeparatorLine.cs @@ -0,0 +1,61 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace OpenNest.Controls +{ + /// An etched separator: a dark line at the middle with a light line beside it. + public abstract class SeparatorLine : Control + { + private readonly Pen lightPen; + private readonly Pen darkPen; + + protected SeparatorLine() + { + SetStyle(ControlStyles.OptimizedDoubleBuffer, true); + SetStyle(ControlStyles.Selectable, false); + + lightPen = new Pen(ProfessionalColors.SeparatorLight); + darkPen = new Pen(ProfessionalColors.SeparatorDark); + } + + /// The control's size across the line: Height for a horizontal line. + protected abstract int Across { get; } + + /// Draws the full-length line at across it. + protected abstract void DrawLine(Graphics graphics, Pen pen, float position); + + protected override void Dispose(bool disposing) + { + try + { + if (disposing) + { + lightPen.Dispose(); + darkPen.Dispose(); + } + } + finally + { + base.Dispose(disposing); + } + } + + protected override void OnResize(EventArgs e) + { + base.OnResize(e); + Invalidate(); + } + + protected override void OnPaint(PaintEventArgs e) + { + base.OnPaint(e); + + float midpoint = Across * 0.5f; + + DrawLine(e.Graphics, darkPen, midpoint); + midpoint++; + DrawLine(e.Graphics, lightPen, midpoint); + } + } +} diff --git a/OpenNest/Controls/VerticalLine.cs b/OpenNest/Controls/VerticalLine.cs index a0c23a59..e87c88c6 100644 --- a/OpenNest/Controls/VerticalLine.cs +++ b/OpenNest/Controls/VerticalLine.cs @@ -1,54 +1,12 @@ -using System; using System.Drawing; -using System.Windows.Forms; namespace OpenNest.Controls { - public class VerticalLine : Control + public class VerticalLine : SeparatorLine { - private readonly Pen lightPen; - private readonly Pen darkPen; + protected override int Across => Width; - public VerticalLine() - { - SetStyle(ControlStyles.OptimizedDoubleBuffer, true); - SetStyle(ControlStyles.Selectable, false); - - lightPen = new Pen(ProfessionalColors.SeparatorLight); - darkPen = new Pen(ProfessionalColors.SeparatorDark); - } - - protected override void Dispose(bool disposing) - { - try - { - if (disposing) - { - lightPen.Dispose(); - darkPen.Dispose(); - } - } - finally - { - base.Dispose(disposing); - } - } - - protected override void OnResize(EventArgs e) - { - base.OnResize(e); - Invalidate(); - } - - protected override void OnPaint(PaintEventArgs e) - { - base.OnPaint(e); - - float midpoint = Width * 0.5f; - - e.Graphics.DrawLine(darkPen, midpoint, 0, midpoint, Height); - midpoint++; - e.Graphics.DrawLine(lightPen, midpoint, 0, midpoint, Height); - } + protected override void DrawLine(Graphics graphics, Pen pen, float position) => + graphics.DrawLine(pen, position, 0, position, Height); } }