Small code cleanup of the places I already touched in this PR.

This commit is contained in:
Robin Krom 2022-03-04 23:04:14 +01:00
commit 8bc6fbdcc4
No known key found for this signature in database
GPG key ID: BCC01364F1371490
10 changed files with 175 additions and 195 deletions

View file

@ -136,7 +136,7 @@ namespace Greenshot.Editor.Drawing
}
/// <summary>
/// Make a following bounds change on this containerlist undoable!
/// Make a following bounds change on this DrawableContainerList undoable!
/// </summary>
/// <param name="allowMerge">true means allow the moves to be merged</param>
public void MakeBoundsChangeUndoable(bool allowMerge)
@ -301,8 +301,7 @@ namespace Greenshot.Editor.Drawing
{
return Rectangle.Empty;
}
else
{
var result = this[0].DrawingBounds;
for (int i = 1; i < Count; i++)
{
@ -312,14 +311,13 @@ namespace Greenshot.Editor.Drawing
return result;
}
}
}
/// <summary>
/// Triggers all elements in the list ot be redrawn.
/// </summary>
/// <param name="g">the to the bitmap related Graphics object</param>
/// <param name="bitmap">Bitmap to draw</param>
/// <param name="renderMode">the rendermode in which the element is to be drawn</param>
/// <param name="renderMode">the RenderMode in which the element is to be drawn</param>
/// <param name="clipRectangle"></param>
public void Draw(Graphics g, Bitmap bitmap, RenderMode renderMode, Rectangle clipRectangle)
{
@ -584,9 +582,7 @@ namespace Greenshot.Editor.Drawing
return;
}
var dc = this[index1];
this[index1] = this[index2];
this[index2] = dc;
(this[index1], this[index2]) = (this[index2], this[index1]);
Parent.Modified = true;
}

View file

@ -711,7 +711,7 @@ namespace Greenshot.Editor.Drawing
BinaryFormatter binaryRead = new BinaryFormatter();
IDrawableContainerList loadedElements = (IDrawableContainerList) binaryRead.Deserialize(streamRead);
loadedElements.Parent = this;
// Make sure the steplabels are sorted accoring to their number
// Make sure the steplabels are sorted according to their number
_stepLabels.Sort((p1, p2) => p1.Number.CompareTo(p2.Number));
DeselectAllElements();
AddElements(loadedElements);
@ -1119,8 +1119,8 @@ namespace Greenshot.Editor.Drawing
/// <param name="message">Message itself</param>
public void SendMessageEvent(object source, SurfaceMessageTyp messageType, string message)
{
if (_surfaceMessage != null)
{
if (_surfaceMessage == null) return;
var eventArgs = new SurfaceMessageEventArgs
{
Message = message,
@ -1129,81 +1129,76 @@ namespace Greenshot.Editor.Drawing
};
_surfaceMessage(source, eventArgs);
}
}
/// <summary>
/// Use to update UI when pressing a key to change the foreground color
/// </summary>
/// <param name="source">Who send</param>
/// <param name="color">new color</param>
public void UpdateForegroundColorEvent(object source, Color color)
{
if (_foregroundColorChanged != null)
private void UpdateForegroundColorEvent(object source, Color color)
{
if (_foregroundColorChanged == null) return;
var eventArgs = new SurfaceForegroundColorEventArgs
{
Color = color,
};
_foregroundColorChanged(source, eventArgs);
}
}
/// <summary>
/// Use to update UI when pressing a key to change the background color
/// </summary>
/// <param name="source">Who send</param>
/// <param name="color">new color</param>
public void UpdateBackgroundColorEvent(object source, Color color)
{
if (_lineThicknessChanged != null)
private void UpdateBackgroundColorEvent(object source, Color color)
{
if (_lineThicknessChanged == null) return;
var eventArgs = new SurfaceBackgroundColorEventArgs
{
Color = color,
};
_backgroundColorChanged(source, eventArgs);
}
}
/// <summary>
/// Use to update UI when pressing a key to change the line thickness
/// </summary>
/// <param name="source">Who send</param>
/// <param name="thickness">new thickness</param>
public void UpdateLineThicknessEvent(object source, int thickness)
{
if (_lineThicknessChanged != null)
private void UpdateLineThicknessEvent(object source, int thickness)
{
if (_lineThicknessChanged == null) return;
var eventArgs = new SurfaceLineThicknessEventArgs
{
Thickness = thickness,
};
_lineThicknessChanged(source, eventArgs);
}
}
/// <summary>
/// Use to update UI when pressing the key to show/hide the shadow
/// </summary>
/// <param name="source">Who send</param>
/// <param name="hasShadow">has shadow</param>
public void UpdateShadowEvent(object source, bool hasShadow)
{
if (_shadowChanged != null)
private void UpdateShadowEvent(object source, bool hasShadow)
{
if (_shadowChanged == null) return;
var eventArgs = new SurfaceShadowEventArgs
{
HasShadow = hasShadow,
};
_shadowChanged(source, eventArgs);
}
}
/// <summary>
/// Crop the surface
/// </summary>
/// <param name="cropRectangle">rectangle that remains</param>
/// <returns></returns>
/// <returns>bool</returns>
public bool ApplyCrop(Rectangle cropRectangle)
{
if (!IsCropPossible(ref cropRectangle, CropContainer.CropModes.Default)) return false;
@ -1224,7 +1219,7 @@ namespace Greenshot.Editor.Drawing
throw;
}
Matrix matrix = new Matrix();
var matrix = new Matrix();
matrix.Translate(-cropRectangle.Left, -cropRectangle.Top, MatrixOrder.Append);
// Make undoable
MakeUndoable(new SurfaceBackgroundChangeMemento(this, matrix), false);
@ -1246,8 +1241,8 @@ namespace Greenshot.Editor.Drawing
/// Splits the image in 3 parts(top, middle, bottom). Crop out the middle and joins top and bottom.
/// </summary>
/// <param name="cropRectangle">rectangle of the middle part</param>
/// <returns></returns>
public bool ApplyHorizontalCrop(Rectangle cropRectangle)
/// <returns>bool</returns>
private bool ApplyHorizontalCrop(Rectangle cropRectangle)
{
if (!IsCropPossible(ref cropRectangle, CropContainer.CropModes.Horizontal)) return false;
@ -1255,12 +1250,12 @@ namespace Greenshot.Editor.Drawing
var topRectangle = new Rectangle(0, 0, Image.Size.Width, cropRectangle.Top);
var bottomRectangle = new Rectangle(0, cropRectangle.Top + cropRectangle.Height, Image.Size.Width, Image.Size.Height - cropRectangle.Top - cropRectangle.Height);
Bitmap tmpNewimage;
List<Bitmap> imageParts = new List<Bitmap>();
Bitmap newImage;
var imageParts = new List<Bitmap>();
// Make sure we have information, this this fails
try
{
tmpNewimage = new Bitmap(Image.Size.Width, Image.Size.Height - cropRectangle.Height);
newImage = new Bitmap(Image.Size.Width, Image.Size.Height - cropRectangle.Height);
if (topRectangle.Height > 0)
{
@ -1280,7 +1275,7 @@ namespace Greenshot.Editor.Drawing
ex.Data.Add("Pixelformat", Image.PixelFormat);
throw;
}
using Graphics g = Graphics.FromImage(tmpNewimage);
using Graphics g = Graphics.FromImage(newImage);
var insertPositionTop = 0;
foreach (var image in imageParts)
@ -1290,16 +1285,16 @@ namespace Greenshot.Editor.Drawing
image.Dispose();
}
Matrix matrix = new Matrix();
var matrix = new Matrix();
matrix.Translate(0, -(cropRectangle.Top + cropRectangle.Height), MatrixOrder.Append);
// Make undoable
MakeUndoable(new SurfaceBackgroundChangeMemento(this, matrix), false);
// Do not dispose otherwise we can't undo the image!
SetImage(tmpNewimage, false);
SetImage(newImage, false);
_elements.Transform(matrix);
if (_surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, tmpNewimage.Size)))
if (_surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, newImage.Size)))
{
_surfaceSizeChanged(this, null);
}
@ -1313,20 +1308,20 @@ namespace Greenshot.Editor.Drawing
/// Splits the image in 3 parts(left, middle, right). Crop out the middle and joins top and bottom.
/// </summary>
/// <param name="cropRectangle">rectangle of the middle part</param>
/// <returns></returns>
public bool ApplyVerticalCrop(Rectangle cropRectangle)
/// <returns>bool</returns>
private bool ApplyVerticalCrop(Rectangle cropRectangle)
{
if (!IsCropPossible(ref cropRectangle, CropContainer.CropModes.Vertical)) return false;
var imageRectangle = new Rectangle(Point.Empty, Image.Size);
var leftRectangle = new Rectangle(0, 0, cropRectangle.Left, Image.Size.Height);
var rightRectangle = new Rectangle(cropRectangle.Left + cropRectangle.Width, 0, Image.Size.Width - cropRectangle.Width - cropRectangle.Left, Image.Size.Height);
Bitmap tmpNewimage;
List<Bitmap> imageParts = new List<Bitmap>();
Bitmap newImage;
var imageParts = new List<Bitmap>();
// Make sure we have information, this this fails
try
{
tmpNewimage = new Bitmap(Image.Size.Width - cropRectangle.Width, Image.Size.Height);
newImage = new Bitmap(Image.Size.Width - cropRectangle.Width, Image.Size.Height);
if (leftRectangle.Width > 0)
{
@ -1346,7 +1341,7 @@ namespace Greenshot.Editor.Drawing
ex.Data.Add("Pixelformat", Image.PixelFormat);
throw;
}
using Graphics g = Graphics.FromImage(tmpNewimage);
using Graphics g = Graphics.FromImage(newImage);
var insertPositionLeft = 0;
foreach (var image in imageParts)
@ -1356,17 +1351,16 @@ namespace Greenshot.Editor.Drawing
image.Dispose();
}
Matrix matrix = new Matrix();
var matrix = new Matrix();
matrix.Translate(-cropRectangle.Left - cropRectangle.Width, 0, MatrixOrder.Append);
// Make undoable
MakeUndoable(new SurfaceBackgroundChangeMemento(this, matrix), false);
// Do not dispose otherwise we can't undo the image!
SetImage(tmpNewimage, false);
SetImage(newImage, false);
_elements.Transform(matrix);
if (_surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, tmpNewimage.Size)))
if (_surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, newImage.Size)))
{
_surfaceSizeChanged(this, null);
}
@ -1404,8 +1398,8 @@ namespace Greenshot.Editor.Drawing
{
foreach (IAdorner adorner in drawableContainer.Adorners)
{
if (adorner.IsActive || adorner.HitTest(mouseEventArgs.Location))
{
if (!adorner.IsActive && !adorner.HitTest(mouseEventArgs.Location)) continue;
if (adorner.Cursor != null)
{
Cursor = adorner.Cursor;
@ -1414,7 +1408,6 @@ namespace Greenshot.Editor.Drawing
return adorner;
}
}
}
return null;
}
@ -1643,8 +1636,8 @@ namespace Greenshot.Editor.Drawing
Cursor = DrawingMode != DrawingModes.None ? Cursors.Cross : Cursors.Default;
if (_mouseDown)
{
if (!_mouseDown) return;
if (_mouseDownElement != null)
{
// an element is currently dragged
@ -1686,7 +1679,6 @@ namespace Greenshot.Editor.Drawing
_modified = true;
}
}
}
/// <summary>
/// This event handler is called when the surface is double clicked.
@ -2018,7 +2010,7 @@ namespace Greenshot.Editor.Drawing
Invalidate();
}
if (makeUndoable && elementToRemove.IsUndoable)
if (makeUndoable && elementToRemove is { IsUndoable: true })
{
MakeUndoable(new DeleteElementMemento(this, elementToRemove), false);
}
@ -2085,37 +2077,32 @@ namespace Greenshot.Editor.Drawing
/// </summary>
public void CutSelectedElements()
{
if (HasSelectedElements)
{
if (!HasSelectedElements) return;
ClipboardHelper.SetClipboardData(typeof(IDrawableContainerList), selectedElements);
RemoveSelectedElements();
}
}
/// <summary>
/// Copy the selected elements to the clipboard
/// </summary>
public void CopySelectedElements()
{
if (HasSelectedElements)
{
if (!HasSelectedElements) return;
ClipboardHelper.SetClipboardData(typeof(IDrawableContainerList), selectedElements);
}
}
/// <summary>
/// This method is called to confirm/cancel "confirmable" elements, like the crop-container.
/// Called when pressing enter or using the "check" in the editor.
/// </summary>
/// <param name="confirm"></param>
/// <param name="confirm">bool</param>
public void ConfirmSelectedConfirmableElements(bool confirm)
{
// create new collection so that we can iterate safely (selectedElements might change due with confirm/cancel)
List<IDrawableContainer> selectedDCs = new List<IDrawableContainer>(selectedElements);
foreach (IDrawableContainer dc in selectedDCs)
{
if (dc.Equals(_cropContainer))
{
if (!dc.Equals(_cropContainer)) continue;
DrawingMode = DrawingModes.None;
// No undo memento for the cropcontainer itself, only for the effect
RemoveElement(_cropContainer, false);
@ -2153,7 +2140,6 @@ namespace Greenshot.Editor.Drawing
break;
}
}
}
public void RemoveCropContainer()
{
@ -2337,13 +2323,13 @@ namespace Greenshot.Editor.Drawing
/// <summary>
/// Get the rectangle bounding all selected elements (in surface coordinates space),
/// or empty rectangle if nothing is selcted.
/// or empty rectangle if nothing is selected.
/// </summary>
public Rectangle GetSelectionRectangle()
=> ToSurfaceCoordinates(selectedElements.DrawingBounds);
/// <summary>
/// Duplicate all the selecteded elements
/// Duplicate all the selected elements
/// </summary>
public void DuplicateSelectedElements()
{
@ -2680,7 +2666,7 @@ namespace Greenshot.Editor.Drawing
return _elements.CanPushDown(selectedElements);
}
public void Element_FieldChanged(object sender, FieldChangedEventArgs e)
private void Element_FieldChanged(object sender, FieldChangedEventArgs e)
{
selectedElements.HandleFieldChangedEvent(sender, e);
}
@ -2738,8 +2724,7 @@ namespace Greenshot.Editor.Drawing
{
return rc;
}
else
{
Point[] points =
{
rc.Location, rc.Location + rc.Size
@ -2754,4 +2739,3 @@ namespace Greenshot.Editor.Drawing
}
}
}
}

View file

@ -44,7 +44,7 @@ namespace Greenshot.Editor.Memento
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
//if (disposing) { }
_drawableContainer = null;

View file

@ -43,7 +43,7 @@ namespace Greenshot.Editor.Memento
Dispose(true);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (disposing)
{

View file

@ -44,7 +44,7 @@ namespace Greenshot.Editor.Memento
Dispose(true);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (disposing)
{

View file

@ -45,7 +45,7 @@ namespace Greenshot.Editor.Memento
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (!disposing) return;

View file

@ -43,7 +43,7 @@ namespace Greenshot.Editor.Memento
Dispose(true);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (disposing)
{

View file

@ -34,30 +34,30 @@ namespace Greenshot.Editor.Memento
{
private readonly List<Point> _points = new();
private readonly List<Size> _sizes = new();
private IDrawableContainerList _listOfdrawableContainer;
private IDrawableContainerList _listOfDrawableContainer;
private void StoreBounds()
{
foreach (IDrawableContainer drawableContainer in _listOfdrawableContainer)
foreach (IDrawableContainer drawableContainer in _listOfDrawableContainer)
{
_points.Add(drawableContainer.Location);
_sizes.Add(drawableContainer.Size);
}
}
public DrawableContainerBoundsChangeMemento(IDrawableContainerList listOfdrawableContainer)
public DrawableContainerBoundsChangeMemento(IDrawableContainerList listOfDrawableContainer)
{
_listOfdrawableContainer = listOfdrawableContainer;
_listOfDrawableContainer = listOfDrawableContainer;
StoreBounds();
}
public DrawableContainerBoundsChangeMemento(IDrawableContainer drawableContainer)
{
_listOfdrawableContainer = new DrawableContainerList
_listOfDrawableContainer = new DrawableContainerList
{
drawableContainer
};
_listOfdrawableContainer.Parent = drawableContainer.Parent;
_listOfDrawableContainer.Parent = drawableContainer.Parent;
StoreBounds();
}
@ -66,21 +66,21 @@ namespace Greenshot.Editor.Memento
Dispose(true);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (disposing)
{
_listOfdrawableContainer?.Dispose();
_listOfDrawableContainer?.Dispose();
}
_listOfdrawableContainer = null;
_listOfDrawableContainer = null;
}
public bool Merge(IMemento otherMemento)
{
if (otherMemento is not DrawableContainerBoundsChangeMemento other) return false;
if (ObjectExtensions.CompareLists(_listOfdrawableContainer, other._listOfdrawableContainer))
if (ObjectExtensions.CompareLists(_listOfDrawableContainer, other._listOfDrawableContainer))
{
// Lists are equal, as we have the state already we can ignore the new memento
return true;
@ -91,10 +91,10 @@ namespace Greenshot.Editor.Memento
public IMemento Restore()
{
var oldState = new DrawableContainerBoundsChangeMemento(_listOfdrawableContainer);
for (int index = 0; index < _listOfdrawableContainer.Count; index++)
var oldState = new DrawableContainerBoundsChangeMemento(_listOfDrawableContainer);
for (int index = 0; index < _listOfDrawableContainer.Count; index++)
{
IDrawableContainer drawableContainer = _listOfdrawableContainer[index];
IDrawableContainer drawableContainer = _listOfDrawableContainer[index];
// Before
drawableContainer.Invalidate();
drawableContainer.Left = _points[index].X;

View file

@ -52,7 +52,7 @@ namespace Greenshot.Editor.Memento
Dispose(true);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (!disposing) return;

View file

@ -43,7 +43,7 @@ namespace Greenshot.Editor.Memento
Dispose(true);
}
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (disposing)
{