Skip to content

Commit

Permalink
Changed Extensions and Mimes in FormatDescriptor from `string[]…
Browse files Browse the repository at this point in the history
…` to `ReadOnlyCollection<string>` (#277)

* Changed `Extensions` and `Mimes` in `FormatDescriptor` from `string[]` to `ReadOnlyCollection<string>`

* Fix inconsistent line endings to resolve formatting issues

* Removed unnecessary null check

---------

Co-authored-by: John Doe <john@doe>
  • Loading branch information
Lehonti and John Doe authored Aug 5, 2023
1 parent 93fef11 commit 4d53a6b
Show file tree
Hide file tree
Showing 8 changed files with 616 additions and 562 deletions.
66 changes: 66 additions & 0 deletions Pinta.Core/Extensions/OtherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,77 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;

namespace Pinta.Core
{
public static class OtherExtensions
{
/// <summary>
/// In most cases, it creates a wrapped read-only copy of the values generated by the
/// <see cref="IEnumerable{T}"/> argument, except if the argument is of type
/// <see cref="ImmutableArray{T}"/> or <see cref="ImmutableList{T}"/>, in which case only
/// the wrapping (not the copying) is necessary; or if the argument is an object that has
/// been previously returned from this method, in which case the reference is returned as is.
/// </summary>
/// <param name="values">Sequence of values to be materialized</param>
/// <returns>
/// Read-only collection wrapper, suitable for class interfaces,
/// backed by an immutable collection type such as <see cref="ImmutableArray{T}"/>
/// </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 ();
}
}

/// <summary>
/// Wraps the <see cref="ImmutableArray{T}"/> in a custom
/// <see cref="ReadOnlyCollection{T}"/> and returns it.
/// </summary>
/// <param name="array">Immutable array to be wrapped</param>
/// <returns>
/// Read-only collection wrapper, suitable for class interfaces,
/// backed by the argument that has been passed
/// </returns>
public static ReadOnlyCollection<T> ToReadOnlyCollection<T> (this ImmutableArray<T> array)
{
return new ImmutableBackedReadOnlyCollection<T> (array);
}

/// <summary>
/// Wraps the <see cref="ImmutableList{T}"/> in a custom
/// <see cref="ReadOnlyCollection{T}"/> and returns it.
/// </summary>
/// <param name="list">Immutable list to be wrapped</param>
/// <returns>
/// Read-only collection wrapper, suitable for class interfaces,
/// backed by the argument that has been passed
/// </returns>
public static ReadOnlyCollection<T> ToReadOnlyCollection<T> (this ImmutableList<T> list)
{
return new ImmutableBackedReadOnlyCollection<T> (list);
}

private sealed class ImmutableBackedReadOnlyCollection<T> : ReadOnlyCollection<T>
{
internal ImmutableBackedReadOnlyCollection (ImmutableList<T> list) : base (list)
{
}
internal ImmutableBackedReadOnlyCollection (ImmutableArray<T> array) : base (array)
{
}
}

public static bool In<T> (this T enumeration, params T[] values)
{
if (enumeration is null)
Expand Down
31 changes: 13 additions & 18 deletions Pinta.Core/ImageFormats/FormatDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Text;

Expand All @@ -41,12 +43,12 @@ public sealed class FormatDescriptor
/// <summary>
/// A list of the supported extensions (for example, "jpeg" and "JPEG").
/// </summary>
public string[] Extensions { get; }
public ReadOnlyCollection<string> Extensions { get; }

/// <summary>
/// A list of supported MIME types (for example, "image/jpg" and "image/png").
/// </summary>
public string[] Mimes { get; }
public ReadOnlyCollection<string> Mimes { get; }

/// <summary>
/// The importer for this file format. This may be null if only exporting
Expand All @@ -73,22 +75,21 @@ public sealed class FormatDescriptor
/// <param name="mimes">A list of supported file MIME types (for example, "image/jpeg" and "image/png").</param>
/// <param name="importer">The importer for this file format, or null if importing is not supported.</param>
/// <param name="exporter">The exporter for this file format, or null if exporting is not supported.</param>
public FormatDescriptor (string displayPrefix, string[] extensions, string[] mimes,
public FormatDescriptor (string displayPrefix, IEnumerable<string> extensions, IEnumerable<string> mimes,
IImageImporter? importer, IImageExporter? exporter)
{
if (extensions == null || (importer == null && exporter == null)) {
throw new ArgumentNullException ("Format descriptor is initialized incorrectly");
}
if (importer == null && exporter == null)
throw new ArgumentException ("Format descriptor is initialized incorrectly", $"{nameof (importer)}, {nameof (exporter)}");

this.Extensions = extensions;
this.Mimes = mimes;
this.Extensions = extensions.ToReadOnlyCollection (); // Create a read-only copy
this.Mimes = mimes.ToReadOnlyCollection (); // Create a read-only copy
this.Importer = importer;
this.Exporter = exporter;

FileFilter ff = FileFilter.New ();
StringBuilder formatNames = new StringBuilder ();

foreach (string ext in extensions) {
foreach (string ext in Extensions) {
if (formatNames.Length > 0)
formatNames.Append (", ");

Expand All @@ -102,7 +103,7 @@ public FormatDescriptor (string displayPrefix, string[] extensions, string[] mim
// Windows does not understand MIME types natively.
// Adding a MIME filter on Windows would break the native file picker and force a GTK file picker instead.
if (SystemManager.GetOperatingSystem () != OS.Windows) {
foreach (string mime in mimes) {
foreach (string mime in Mimes) {
ff.AddMimeType (mime);
}
}
Expand All @@ -112,15 +113,9 @@ public FormatDescriptor (string displayPrefix, string[] extensions, string[] mim
}

[MemberNotNullWhen (returnValue: false, member: nameof (Exporter))]
public bool IsReadOnly ()
{
return Exporter == null;
}
public bool IsReadOnly () => Exporter == null;

[MemberNotNullWhen (returnValue: false, member: nameof (Importer))]
public bool IsWriteOnly ()
{
return Importer == null;
}
public bool IsWriteOnly () => Importer == null;
}
}
80 changes: 39 additions & 41 deletions Pinta.Docking/Dock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,59 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using Gtk;
using Pinta.Core;

namespace Pinta.Docking
namespace Pinta.Docking;

/// <summary>
/// The root widget, containing all dock items underneath it.
/// </summary>
public class Dock : Box
{
/// <summary>
/// The root widget, containing all dock items underneath it.
/// </summary>
public class Dock : Box
{
private readonly DockPanel right_panel = new DockPanel ();
private readonly Paned pane = Paned.New (Orientation.Horizontal);
private readonly DockPanel right_panel = new DockPanel ();
private readonly Paned pane = Paned.New (Orientation.Horizontal);

public Dock ()
{
SetOrientation (Orientation.Horizontal);
public Dock ()
{
SetOrientation (Orientation.Horizontal);

pane.EndChild = right_panel;
pane.ResizeEndChild = false;
pane.ShrinkEndChild = false;
Append (pane);
}
pane.EndChild = right_panel;
pane.ResizeEndChild = false;
pane.ShrinkEndChild = false;
Append (pane);
}

public void AddItem (DockItem item, DockPlacement placement)
{
switch (placement) {
case DockPlacement.Center:
pane.StartChild = item;
pane.ResizeStartChild = true;
pane.ShrinkStartChild = false;
break;
case DockPlacement.Right:
right_panel.AddItem (item);
break;
}
public void AddItem (DockItem item, DockPlacement placement)
{
switch (placement) {
case DockPlacement.Center:
pane.StartChild = item;
pane.ResizeStartChild = true;
pane.ShrinkStartChild = false;
break;
case DockPlacement.Right:
right_panel.AddItem (item);
break;
}
}

private const string RightSplitPosKey = "dock-right-splitpos";
private const string RightSplitPosKey = "dock-right-splitpos";

public void SaveSettings (ISettingsService settings)
{
public void SaveSettings (ISettingsService settings)
{
#if false
settings.PutSetting (RightSplitPosKey, pane.Position);
settings.PutSetting (RightSplitPosKey, pane.Position);
#endif
right_panel.SaveSettings (settings);
}
right_panel.SaveSettings (settings);
}

public void LoadSettings (ISettingsService settings)
{
// TODO-GTK3(docking) Disabled for now, as the size isn't quite restored properly (gradually increases over time)
public void LoadSettings (ISettingsService settings)
{
// TODO-GTK3(docking) Disabled for now, as the size isn't quite restored properly (gradually increases over time)
#if false
pane.Position = settings.GetSetting<int> (RightSplitPosKey, pane.Position);
pane.Position = settings.GetSetting<int> (RightSplitPosKey, pane.Position);
#endif
right_panel.LoadSettings (settings);
}
right_panel.LoadSettings (settings);
}
}
Loading

0 comments on commit 4d53a6b

Please sign in to comment.