Skip to content

Commit

Permalink
Nullable support in Menees.Diffs.Windows.Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Dec 27, 2021
1 parent ecb39b0 commit 1e2ca51
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 175 deletions.
4 changes: 2 additions & 2 deletions src/Menees.Diffs.Windows.Forms/Caret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void Dispose()

#region Private Methods

private void ControlGotFocus(object sender, EventArgs e)
private void ControlGotFocus(object? sender, EventArgs e)
{
// Sometimes in the debugger, we'll get focus without ever having been sent
// the LostFocus event. So I'll fire it now just to make things balance out.
Expand All @@ -146,7 +146,7 @@ private void ControlGotFocus(object sender, EventArgs e)
}
}

private void ControlLostFocus(object sender, EventArgs e)
private void ControlLostFocus(object? sender, EventArgs e)
{
if (this.createdCaret)
{
Expand Down
65 changes: 30 additions & 35 deletions src/Menees.Diffs.Windows.Forms/DiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public DiffControl()

#region Public Events

public event EventHandler LineDiffSizeChanged;
public event EventHandler? LineDiffSizeChanged;

public event EventHandler RecompareNeeded;
public event EventHandler? RecompareNeeded;

public event EventHandler<DifferenceEventArgs> ShowTextDifferences;
public event EventHandler<DifferenceEventArgs>? ShowTextDifferences;

#endregion

Expand Down Expand Up @@ -163,11 +163,6 @@ public bool ShowToolBar
}

[DefaultValue(false)]
[SuppressMessage(
"Microsoft.Naming",
"CA1702:CompoundWordsShouldBeCasedCorrectly",
MessageId = "InLine",
Justification = "'inline' is not an appropriate term here.")]
public bool ShowWhiteSpaceInLineDiff
{
get
Expand Down Expand Up @@ -258,7 +253,7 @@ public bool CompareSelectedText()
string textB = this.ViewB.SelectedText;

DifferenceEventArgs diffArgs = new(textA, textB);
this.ShowTextDifferences(this, diffArgs);
this.ShowTextDifferences?.Invoke(this, diffArgs);
result = true;
}

Expand Down Expand Up @@ -307,7 +302,7 @@ public bool Recompare()

if (this.CanRecompare)
{
this.RecompareNeeded(this, EventArgs.Empty);
this.RecompareNeeded?.Invoke(this, EventArgs.Empty);
result = true;
}

Expand Down Expand Up @@ -402,7 +397,7 @@ public void ViewFile()

#region Internal Methods

internal static void PaintColorLegendItem(ToolStripItem item, PaintEventArgs e)
internal static void PaintColorLegendItem(ToolStripItem? item, PaintEventArgs e)
{
if (item != null)
{
Expand Down Expand Up @@ -434,57 +429,57 @@ internal static void PaintColorLegendItem(ToolStripItem item, PaintEventArgs e)

#region Private Methods

private void Copy_Click(object sender, EventArgs e)
private void Copy_Click(object? sender, EventArgs e)
{
this.Copy();
}

private void Find_Click(object sender, EventArgs e)
private void Find_Click(object? sender, EventArgs e)
{
this.Find();
}

private void FindNext_Click(object sender, EventArgs e)
private void FindNext_Click(object? sender, EventArgs e)
{
this.FindNext();
}

private void FindPrevious_Click(object sender, EventArgs e)
private void FindPrevious_Click(object? sender, EventArgs e)
{
this.FindPrevious();
}

private void FirstDiff_Click(object sender, EventArgs e)
private void FirstDiff_Click(object? sender, EventArgs e)
{
this.GoToFirstDiff();
}

private void GotoLine_Click(object sender, EventArgs e)
private void GotoLine_Click(object? sender, EventArgs e)
{
this.GoToLine();
}

private void LastDiff_Click(object sender, EventArgs e)
private void LastDiff_Click(object? sender, EventArgs e)
{
this.GoToLastDiff();
}

private void NextDiff_Click(object sender, EventArgs e)
private void NextDiff_Click(object? sender, EventArgs e)
{
this.GoToNextDiff();
}

private void PrevDiff_Click(object sender, EventArgs e)
private void PrevDiff_Click(object? sender, EventArgs e)
{
this.GoToPreviousDiff();
}

private void Recompare_Click(object sender, EventArgs e)
private void Recompare_Click(object? sender, EventArgs e)
{
this.Recompare();
}

private void ViewFile_Click(object sender, EventArgs e)
private void ViewFile_Click(object? sender, EventArgs e)
{
this.ViewFile();
}
Expand All @@ -495,17 +490,17 @@ private void ColorLegend_Paint(object sender, PaintEventArgs e)
PaintColorLegendItem(sender as ToolStripItem, e);
}

private void DiffControl_SizeChanged(object sender, EventArgs e)
private void DiffControl_SizeChanged(object? sender, EventArgs e)
{
this.pnlLeft.Width = (this.Width - this.pnlLeft.Left - this.MiddleSplitter.Width) / 2;
}

private void DiffOptionsChanged(object sender, EventArgs e)
private void DiffOptionsChanged(object? sender, EventArgs e)
{
this.UpdateColors();
}

private void TextDiff_Click(object sender, EventArgs e)
private void TextDiff_Click(object? sender, EventArgs e)
{
this.CompareSelectedText();
}
Expand Down Expand Up @@ -555,19 +550,19 @@ private void UpdateLineDiff()
{
this.currentDiffLine = line;

DiffViewLine lineOne = null;
DiffViewLine lineTwo = null;
DiffViewLine? lineOne = null;
DiffViewLine? lineTwo = null;
if (line < this.ViewA.LineCount)
{
lineOne = this.ViewA.Lines[line];
lineOne = this.ViewA.Lines?[line];
}

// Normally, ViewA.LineCount == ViewB.LineCount, but during
// SetData they'll be mismatched momentarily as each view
// rebuilds its lines.
if (line < this.ViewB.LineCount)
{
lineTwo = this.ViewB.Lines[line];
lineTwo = this.ViewB.Lines?[line];
}

if (lineOne != null && lineTwo != null)
Expand All @@ -577,7 +572,7 @@ private void UpdateLineDiff()
}
}

private void View_PositionChanged(object sender, EventArgs e)
private void View_PositionChanged(object? sender, EventArgs e)
{
DiffView view = this.ActiveView;
DiffViewPosition pos = view.Position;
Expand All @@ -590,27 +585,27 @@ private void View_PositionChanged(object sender, EventArgs e)
}
}

private void ViewA_HScrollPosChanged(object sender, EventArgs e)
private void ViewA_HScrollPosChanged(object? sender, EventArgs e)
{
this.ViewB.HScrollPos = this.ViewA.HScrollPos;
}

private void ViewA_VScrollPosChanged(object sender, EventArgs e)
private void ViewA_VScrollPosChanged(object? sender, EventArgs e)
{
this.ViewB.VScrollPos = this.ViewA.VScrollPos;
}

private void ViewB_HScrollPosChanged(object sender, EventArgs e)
private void ViewB_HScrollPosChanged(object? sender, EventArgs e)
{
this.ViewA.HScrollPos = this.ViewB.HScrollPos;
}

private void ViewB_VScrollPosChanged(object sender, EventArgs e)
private void ViewB_VScrollPosChanged(object? sender, EventArgs e)
{
this.ViewA.VScrollPos = this.ViewB.VScrollPos;
}

private void ViewLineDiff_SizeChanged(object sender, EventArgs e)
private void ViewLineDiff_SizeChanged(object? sender, EventArgs e)
{
this.LineDiffSizeChanged?.Invoke(this, e);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Menees.Diffs.Windows.Forms/DiffOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class DiffOptions

#region Public Events

public static event EventHandler OptionsChanged;
public static event EventHandler? OptionsChanged;

#endregion

Expand Down Expand Up @@ -211,9 +211,9 @@ public static void Save(ISettingsNode node)
node.SetValue(nameof(HatchDeadSpace), HatchDeadSpace);
}

public static Brush TryCreateDeadSpaceBrush(Color backColor)
public static Brush? TryCreateDeadSpaceBrush(Color backColor)
{
Brush result = null;
Brush? result = null;
if (HatchDeadSpace)
{
result = new HatchBrush(HatchStyle.Percent25, SystemColors.ControlDark, backColor);
Expand Down
26 changes: 11 additions & 15 deletions src/Menees.Diffs.Windows.Forms/DiffOverview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal sealed class DiffOverview : Control
private readonly BorderStyle borderStyle = BorderStyle.Fixed3D;
private bool dragging;
private bool useTranslucentView = true;
private Bitmap image;
private DiffView view;
private Bitmap? image;
private DiffView? view;
private Rectangle viewRect;

#endregion
Expand Down Expand Up @@ -52,17 +52,13 @@ public DiffOverview()
/// <summary>
/// Fired when the user clicks and/or drags to move the view.
/// </summary>
public event EventHandler<DiffLineClickEventArgs> LineClick;
public event EventHandler<DiffLineClickEventArgs>? LineClick;

#endregion

#region Public Properties

[SuppressMessage(
"Microsoft.Performance",
"CA1811:AvoidUncalledPrivateCode",
Justification = "The get_DiffView accessor is only called by the Windows Forms designer via reflection.")]
public DiffView DiffView
public DiffView? DiffView
{
get
{
Expand Down Expand Up @@ -212,7 +208,7 @@ protected override void OnPaint(PaintEventArgs e)
// Repaint the view window if any of it is invalid
if (r.IntersectsWith(this.viewRect))
{
Pen disposablePen = null;
Pen? disposablePen = null;
try
{
Pen pen = SystemPens.Highlight;
Expand Down Expand Up @@ -289,21 +285,21 @@ private void CalculateViewRect()
}
}

private void DiffOptionsChanged(object sender, EventArgs e)
private void DiffOptionsChanged(object? sender, EventArgs e)
{
// The diff colors changed, so we need to rerender the
// image. The current view rect should still be valid.
this.RenderImage();
this.Invalidate();
}

private void DiffView_LinesChanged(object sender, EventArgs e)
private void DiffView_LinesChanged(object? sender, EventArgs e)
{
// If the Lines changed, we need to update everything.
this.UpdateAll();
}

private void DiffView_SizeChanged(object sender, EventArgs e)
private void DiffView_SizeChanged(object? sender, EventArgs e)
{
// If the DiffView size changed, then our view window
// may be longer or shorter, but the rendered image is
Expand All @@ -313,7 +309,7 @@ private void DiffView_SizeChanged(object sender, EventArgs e)
this.Invalidate();
}

private void DiffView_VScrollPosChanged(object sender, EventArgs e)
private void DiffView_VScrollPosChanged(object? sender, EventArgs e)
{
// The DiffView's FirstVisibleLine has changed, so we
// just need to invalidate our view.
Expand Down Expand Up @@ -425,7 +421,7 @@ private void RenderImage()

// Draw delete on the left and dead space on the right.
g.FillRectangle(backBrush, GutterWidth, y, fullFillWidth / 2, lineHeight);
using (Brush deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color))
using (Brush? deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color))
{
g.FillRectangle(deadBrush ?? backBrush, GutterWidth + (fullFillWidth / 2), y, fullFillWidth / 2, lineHeight);
}
Expand All @@ -435,7 +431,7 @@ private void RenderImage()
case EditType.Insert:

// Draw dead space on the left and insert on the right.
using (Brush deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color))
using (Brush? deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color))
{
g.FillRectangle(deadBrush ?? backBrush, GutterWidth, y, fullFillWidth / 2, lineHeight);
}
Expand Down
Loading

0 comments on commit 1e2ca51

Please sign in to comment.