Skip to content

Commit

Permalink
Converted several switch statements to switch expressions (PintaP…
Browse files Browse the repository at this point in the history
…roject#566)

Co-authored-by: Lehonti Ramos <lehonti@ramos>
  • Loading branch information
Lehonti and Lehonti Ramos committed Oct 12, 2023
1 parent 489b939 commit f983d29
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 92 deletions.
19 changes: 5 additions & 14 deletions Pinta.Core/Extensions/GioStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 170,11 @@ public override long Seek (long offset, System.IO.SeekOrigin origin)
if (is_disposed)
throw new ObjectDisposedException ("The stream is closed");
var seekable = (Gio.Seekable) stream;

GLib.SeekType seek_type;
switch (origin) {
case System.IO.SeekOrigin.Current:
seek_type = GLib.SeekType.Cur;
break;
case System.IO.SeekOrigin.End:
seek_type = GLib.SeekType.End;
break;
case System.IO.SeekOrigin.Begin:
default:
seek_type = GLib.SeekType.Set;
break;
}
var seek_type = origin switch {
System.IO.SeekOrigin.Current => GLib.SeekType.Cur,
System.IO.SeekOrigin.End => GLib.SeekType.End,
_ => GLib.SeekType.Set,
};
seekable.Seek (offset, seek_type, null);
return Position;
}
Expand Down
16 changes: 6 additions & 10 deletions Pinta.Core/Extensions/OtherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 30,12 @@ public static class OtherExtensions
/// </returns>
public static ReadOnlyCollection<T> ToReadOnlyCollection<T> (this IEnumerable<T> values)
{
switch (values) {
case ImmutableBackedReadOnlyCollection<T> transparent:
return transparent;
case ImmutableArray<T> array:
return array.ToReadOnlyCollection ();
case ImmutableList<T> list:
return list.ToReadOnlyCollection ();
default:
return values.ToImmutableArray ().ToReadOnlyCollection ();
}
return values switch {
ImmutableBackedReadOnlyCollection<T> transparent => transparent,
ImmutableArray<T> array => array.ToReadOnlyCollection (),
ImmutableList<T> list => list.ToReadOnlyCollection (),
_ => values.ToImmutableArray ().ToReadOnlyCollection (),
};
}

/// <summary>
Expand Down
53 changes: 18 additions & 35 deletions Pinta.Core/ImageFormats/OraFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,41 221,24 @@ public void Export (Document document, Gio.File file, Gtk.Window parent)

private static string BlendModeToStandard (BlendMode mode)
{
switch (mode) {
case BlendMode.Normal:
default:
return "svg:src-over";
case BlendMode.Multiply:
return "svg:multiply";
case BlendMode.ColorBurn:
return "svg:color-burn";
case BlendMode.ColorDodge:
return "svg:color-dodge";
case BlendMode.Overlay:
return "svg:overlay";
case BlendMode.Difference:
return "svg:difference";
case BlendMode.Lighten:
return "svg:lighten";
case BlendMode.Darken:
return "svg:darken";
case BlendMode.Screen:
return "svg:screen";
case BlendMode.Xor:
return "svg:xor";
case BlendMode.HardLight:
return "svg:hard-light";
case BlendMode.SoftLight:
return "svg:soft-light";
case BlendMode.Color:
return "svg:color";
case BlendMode.Luminosity:
return "svg:luminosity";
case BlendMode.Hue:
return "svg:hue";
case BlendMode.Saturation:
return "svg:saturation";
}
return mode switch {
BlendMode.Multiply => "svg:multiply",
BlendMode.ColorBurn => "svg:color-burn",
BlendMode.ColorDodge => "svg:color-dodge",
BlendMode.Overlay => "svg:overlay",
BlendMode.Difference => "svg:difference",
BlendMode.Lighten => "svg:lighten",
BlendMode.Darken => "svg:darken",
BlendMode.Screen => "svg:screen",
BlendMode.Xor => "svg:xor",
BlendMode.HardLight => "svg:hard-light",
BlendMode.SoftLight => "svg:soft-light",
BlendMode.Color => "svg:color",
BlendMode.Luminosity => "svg:luminosity",
BlendMode.Hue => "svg:hue",
BlendMode.Saturation => "svg:saturation",
_ => "svg:src-over",
};
}

private static BlendMode StandardToBlendMode (string mode)
Expand Down
68 changes: 44 additions & 24 deletions Pinta.Tools/Editable/Shapes/ShapeEngineCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,32 184,52 @@ protected ShapeEngine (ShapeEngine src)
public ShapeEngine Convert (BaseEditEngine.ShapeTypes newShapeType, int shapeIndex)
{
//Remove the old ShapeEngine instance.
BaseEditEngine.SEngines.Remove (this);

ShapeEngine clone;

switch (newShapeType) {
case BaseEditEngine.ShapeTypes.ClosedLineCurveSeries:
clone = new LineCurveSeriesEngine (parent_layer, DrawingLayer, newShapeType, AntiAliasing, true,
OutlineColor, FillColor, BrushWidth);

break;
case BaseEditEngine.ShapeTypes.Ellipse:
clone = new EllipseEngine (parent_layer, DrawingLayer, AntiAliasing, OutlineColor, FillColor, BrushWidth);

break;
case BaseEditEngine.ShapeTypes.RoundedLineSeries:
clone = new RoundedLineEngine (parent_layer, DrawingLayer, RoundedLineEditEngine.DefaultRadius,
AntiAliasing, OutlineColor, FillColor, BrushWidth);

break;
default:
//Defaults to OpenLineCurveSeries.
clone = new LineCurveSeriesEngine (parent_layer, DrawingLayer, newShapeType, AntiAliasing, false,
OutlineColor, FillColor, BrushWidth);
BaseEditEngine.SEngines.Remove (this);

break;
}
ShapeEngine clone = newShapeType switch {

BaseEditEngine.ShapeTypes.ClosedLineCurveSeries => new LineCurveSeriesEngine (
parent_layer,
DrawingLayer,
newShapeType,
AntiAliasing,
true,
OutlineColor,
FillColor,
BrushWidth
),

BaseEditEngine.ShapeTypes.Ellipse => new EllipseEngine (
parent_layer,
DrawingLayer,
AntiAliasing,
OutlineColor,
FillColor,
BrushWidth
),

BaseEditEngine.ShapeTypes.RoundedLineSeries => new RoundedLineEngine (
parent_layer,
DrawingLayer,
RoundedLineEditEngine.DefaultRadius,
AntiAliasing,
OutlineColor,
FillColor,
BrushWidth
),

_ => new LineCurveSeriesEngine (
parent_layer,
DrawingLayer,
newShapeType,
AntiAliasing,
false,
OutlineColor,
FillColor,
BrushWidth
),//Defaults to OpenLineCurveSeries.
};

// Don't clone the GeneratedPoints or OrganizedPoints, as they will be calculated.
clone.ControlPoints = ControlPoints.Select (i => i.Clone ()).ToList ();
Expand Down
14 changes: 5 additions & 9 deletions Pinta/Dialogs/NewImageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 313,11 @@ public BackgroundType NewImageBackgroundType {

public Cairo.Color NewImageBackground {
get {
switch (NewImageBackgroundType) {
case BackgroundType.White:
return new Cairo.Color (1, 1, 1);
case BackgroundType.Transparent:
return new Cairo.Color (1, 1, 1, 0);
case BackgroundType.SecondaryColor:
default:
return PintaCore.Palette.SecondaryColor;
}
return NewImageBackgroundType switch {
BackgroundType.White => new Cairo.Color (1, 1, 1),
BackgroundType.Transparent => new Cairo.Color (1, 1, 1, 0),
_ => PintaCore.Palette.SecondaryColor,
};
}
}

Expand Down

0 comments on commit f983d29

Please sign in to comment.