Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions SVGControl/ButtonSVG.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -52,7 +53,7 @@ private void ImageSVG_PropertyChanged(object sender, PropertyChangedEventArgs e)
//this.Image = ImageSVG.Render();
}

public static byte[] ObjectToByteArray(Object obj)
public static byte[] ObjectToByteArray(object? obj)
{
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
Expand All @@ -73,7 +74,7 @@ private void ButtonSVG_ParentChanged(object sender, EventArgs e)
//ImageSVG.ResourceNames = names;
}

private static string GetStringForValue(object value)
private static string GetStringForValue(object? value)
{
if (value == null)
return "null";
Expand Down
11 changes: 7 additions & 4 deletions SVGControl/DropDownEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
Expand All @@ -18,7 +19,7 @@ namespace SVGControl
// used by the property grid to display item in edit mode
public class DropDownEditor : UITypeEditor
{
private IWindowsFormsEditorService _editorService;
private IWindowsFormsEditorService? _editorService;

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
Expand All @@ -32,10 +33,12 @@ public override object EditValue(
object value
)
{
IDesignerHost host = provider.GetService(typeof(IDesignerHost)) as IDesignerHost;
// Null-forgiving preserves the pre-existing NRE-on-null behavior at
// host.RootComponentClassName if the service is unavailable.
IDesignerHost host = (provider.GetService(typeof(IDesignerHost)) as IDesignerHost)!;
string typName = host.RootComponentClassName;
Type typ = host.GetType(typName);
Assembly asm = null;
Assembly? asm = null;
if (typ == null)
{
MessageBox.Show("Please build project before attempting to set this property");
Expand Down
11 changes: 6 additions & 5 deletions SVGControl/ISvgResource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand All @@ -10,8 +11,8 @@ namespace SVGControl
[TypeConverter(typeof(SvgResourceConverter))]
public interface ISvgResource
{
string Name { get; }
byte[] Data { get; }
string? Name { get; }
byte[]? Data { get; }
}

public class SvgResource : ISvgResource
Expand All @@ -24,7 +25,7 @@ public SvgResource(string name, byte[] data)
Data = data;
}

public string Name { get; set; }
public byte[] Data { get; set; }
public string? Name { get; set; }
public byte[]? Data { get; set; }
}
}
3 changes: 2 additions & 1 deletion SVGControl/PictureBoxSVG.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand Down
7 changes: 4 additions & 3 deletions SVGControl/SVGFileNameEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
Expand All @@ -12,8 +13,8 @@ internal class SvgFileNameEditor : FileNameEditor
private string _currentValue = string.Empty;
private string _absoluteFilepath = string.Empty;
private string _fileName = string.Empty;
private string _appPath;
private OpenFileDialog _ofd;
private string _appPath = string.Empty;
private OpenFileDialog? _ofd;

public SvgFileNameEditor()
{
Expand Down
3 changes: 2 additions & 1 deletion SVGControl/SVGParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
Expand Down
37 changes: 26 additions & 11 deletions SVGControl/SvgImageSelector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -52,13 +53,13 @@ public SvgImageSelector(Size outer, Padding margin, AutoSize autoSize, bool useD
}

//private SvgDocument _doc;
private string _relativeImagePath;
private string _absoluteImagePath;
private string? _relativeImagePath;
private string? _absoluteImagePath;
private bool _saveRendering = false;
private bool _useDefaultImage = false;
private SvgRenderer _renderer;

internal String AboluteImagePath
internal String? AboluteImagePath
{
get { return _absoluteImagePath; }
}
Expand All @@ -77,7 +78,14 @@ public string ImagePath
}
else
{
return _relativeImagePath;
// The `set` accessor below is currently entirely commented out (a
// functional no-op), so _relativeImagePath is never actually assigned by
// this class today. The null-forgiving operator preserves the pre-existing
// behavior of returning whatever _relativeImagePath currently holds
// (including null) rather than introducing a `?? "(none)"` or other
// fallback, which would change the observable return value on this path.
// See docs/features/active/utilitiescs-nullable-svgcontrol/evidence/other/imagepath-judgment-call-decision.md.
return _relativeImagePath!;
}
}
set
Expand All @@ -102,10 +110,10 @@ public string ImagePath
}
}

private ISvgResource _svgResource = null;
private ISvgResource? _svgResource = null;

[Editor(typeof(DropDownEditor), typeof(UITypeEditor))]
public ISvgResource ResourceName
public ISvgResource? ResourceName
{
get => _svgResource;
set
Expand All @@ -127,7 +135,12 @@ public ISvgResource ResourceName
else
{
_svgResource = value;
_renderer.Document = SvgRenderer.GetSvgDocument(value.Data);
// value.Data is nullable (ISvgResource.Data); GetSvgDocument's `file`
// parameter is not, preserving the pre-existing behavior of passing
// whatever Data currently holds through unchanged (a genuinely-null
// Data would fail downstream exactly as it did before nullable
// annotations were introduced).
_renderer.Document = SvgRenderer.GetSvgDocument(value.Data!);
}
NotifyPropertyChanged("ResourceName");
}
Expand Down Expand Up @@ -165,7 +178,7 @@ public Padding Margin
set => _renderer.Margin = value;
}

public Bitmap Render() => _renderer.Render();
public Bitmap? Render() => _renderer.Render();

public bool UseDefaultImage
{
Expand Down Expand Up @@ -210,7 +223,9 @@ public bool SaveRendering
// Saves the Image via a FileStream created by the OpenFile method.
using FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
{
Image image = Render();
// The outer guard already confirms _renderer.Document != null, so
// Render() cannot return null here; preserves pre-existing behavior.
Image image = Render()!;
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
Expand Down Expand Up @@ -288,7 +303,7 @@ internal void SetDefaultImage()

#region EventHandlers

public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

private void Renderer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Expand Down
5 changes: 3 additions & 2 deletions SVGControl/SvgOptionsConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
Expand All @@ -20,7 +21,7 @@ Type destinationType
{
if (destinationType == typeof(string))
{
SvgImageSelector image = value as SvgImageSelector;
SvgImageSelector? image = value as SvgImageSelector;
if (image != null)
{
if (image.AboluteImagePath != null)
Expand Down
7 changes: 4 additions & 3 deletions SVGControl/SvgOptionsConverter2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
Expand All @@ -20,12 +21,12 @@ Type destinationType
{
if (destinationType == typeof(string))
{
SvgImageSelector image = value as SvgImageSelector;
SvgImageSelector? image = value as SvgImageSelector;
if (image != null)
{
if (image.ResourceName != null)
{
string resourceName = image.ResourceName.Name;
string? resourceName = image.ResourceName.Name;
string autoSizeCode;
switch (image.AutoSize)
{
Expand Down
36 changes: 23 additions & 13 deletions SVGControl/SvgRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -31,7 +32,7 @@ internal class SvgRenderer : INotifyPropertyChanged
private static int _resolverInstalled;

[ThreadStatic]
private static HashSet<string> _resolving;
private static HashSet<string>? _resolving;

static SvgRenderer()
{
Expand All @@ -41,7 +42,7 @@ static SvgRenderer()
}
}

private static System.Reflection.Assembly ResolveByNameAndKey(
private static System.Reflection.Assembly? ResolveByNameAndKey(
object sender,
ResolveEventArgs args
)
Expand Down Expand Up @@ -103,7 +104,7 @@ ResolveEventArgs args
return null;
}

private static bool PublicKeyTokensEqual(byte[] a, byte[] b)
private static bool PublicKeyTokensEqual(byte[]? a, byte[]? b)
{
if (a == null || b == null)
{
Expand All @@ -125,7 +126,10 @@ private static bool PublicKeyTokensEqual(byte[] a, byte[] b)

public SvgRenderer(byte[] doc, Size size, AutoSize autoSize)
{
_doc = GetSvgDocument(doc);
// GetSvgDocument is annotated SvgDocument? because it swallows load failures and
// returns null; this call site preserves pre-existing behavior (assume success and
// let a genuine failure surface as an NRE from Draw(), as it always has).
_doc = GetSvgDocument(doc)!;
_original = _doc.Draw().Size;
_margin = new Padding(0);
Size = CalcInnerSize(size, _margin);
Expand All @@ -134,7 +138,8 @@ public SvgRenderer(byte[] doc, Size size, AutoSize autoSize)

public SvgRenderer(byte[] doc, Size size, Padding margin, AutoSize autoSize)
{
_doc = GetSvgDocument(doc);
// See the other byte[]-doc constructor above for the rationale on the `!`.
_doc = GetSvgDocument(doc)!;
_original = _doc.Draw().Size;
_margin = margin;
Size = CalcInnerSize(size, _margin);
Expand Down Expand Up @@ -171,7 +176,7 @@ public SvgRenderer(Size outer, Padding margin, AutoSize autoSize)
private Size _outer;
private Size _original;
private Padding _margin;
private SvgDocument _doc;
private SvgDocument? _doc;
private AutoSize _autoSize;
private Size _size;

Expand Down Expand Up @@ -215,15 +220,18 @@ public AutoSize AutoSize
}

[NotifyParentProperty(true)]
public SvgDocument Document
public SvgDocument? Document
{
get => _doc;
set
{
_doc = value;
if (value != null)
{
_original = _doc.Draw().Size;
// _doc == value here (just assigned above); the null-forgiving operator
// reflects the guard on `value` that the compiler cannot see through the
// field assignment.
_original = _doc!.Draw().Size;
}
NotifyPropertyChanged();
}
Expand All @@ -236,7 +244,7 @@ private Size CalcInnerSize(Size outer, Padding margin)
return new Size(innerWidth, innerHeight);
}

public Bitmap Render()
public Bitmap? Render()
{
if (_doc == null)
{
Expand Down Expand Up @@ -273,8 +281,10 @@ public Bitmap Render()

private void AddMargins(int widthCurrent, int heightCurrent)
{
// _doc is expected to be set by the time this (currently unreferenced) helper runs;
// preserves the pre-existing implicit non-null assumption in this method.
var group = new SvgGroup();
_doc.Children.Add(group);
_doc!.Children.Add(group);
group.Children.Add(
new SvgRectangle
{
Expand Down Expand Up @@ -317,7 +327,7 @@ private Size AdjustSizeProportionately(Size proportions, Size targetSize)
return proportions;
}

public static SvgDocument GetSvgDocument(byte[] file)
public static SvgDocument? GetSvgDocument(byte[] file)
{
Stream stream = new MemoryStream(file);
try
Expand All @@ -332,7 +342,7 @@ public static SvgDocument GetSvgDocument(byte[] file)

#region EventHandlers

public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
Expand Down
8 changes: 6 additions & 2 deletions SVGControl/SvgResourceConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
Expand Down Expand Up @@ -32,7 +33,10 @@ Type destinationType
else
{
ISvgResource resource = (ISvgResource)value;
return resource.Name;
// ISvgResource.Name is nullable (SvgResource's parameterless constructor
// never assigns it); this preserves the pre-existing behavior of returning
// whatever Name currently holds, including null, without a new fallback.
return resource.Name!;
}
}
return "(none)";
Expand Down
Loading