Skip to content

Commit

Permalink
Turned most public List<T>-typed properties into Collection<T> or…
Browse files Browse the repository at this point in the history
… `ReadOnlyCollection<T>` (#281)

* Turned most public `List<T>`-typed properties into `Collection<T>` or `ReadOnlyCollection<T>`

* Made `protected` methods `private` in sealed classes

* simplified naming of backing fields and removed temporary variables

---------

Co-authored-by: Lehonti Ramos <john@doe>
  • Loading branch information
Lehonti and Lehonti Ramos authored Aug 7, 2023
1 parent 3ec8774 commit baf4143
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 77 deletions.
28 changes: 11 additions & 17 deletions Pinta.Core/Actions/AdjustmentsActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,19 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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

namespace Pinta.Core
{
public class AdjustmentsActions
{
public List<Command> Actions { get; }
namespace Pinta.Core;

public AdjustmentsActions ()
{
Actions = new List<Command> ();
}
public sealed class AdjustmentsActions
{
public Collection<Command> Actions { get; } = new ();

#region Public Methods
public void ToggleActionsSensitive (bool sensitive)
{
foreach (var a in Actions)
a.Sensitive = sensitive;
}
#endregion
#region Public Methods
public void ToggleActionsSensitive (bool sensitive)
{
foreach (var a in Actions)
a.Sensitive = sensitive;
}
#endregion
}
5 changes: 3 additions & 2 deletions Pinta.Core/Actions/EffectsActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
// THE SOFTWARE.

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

namespace Pinta.Core
{
public class EffectsActions
public sealed class EffectsActions
{
public Dictionary<string, Gio.Menu> Menus { get; } = new ();
public List<Command> Actions { get; } = new ();
public Collection<Command> Actions { get; } = new ();

public EffectsActions ()
{
Expand Down
68 changes: 30 additions & 38 deletions Pinta.Core/Managers/WorkspaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Cairo;
Expand All @@ -44,66 +45,62 @@ public interface IWorkspaceService
SelectionModeHandler SelectionHandler { get; }
}

public class WorkspaceManager : IWorkspaceService
public sealed class WorkspaceManager : IWorkspaceService
{
private int active_document_index = -1;
private int new_file_name = 1;

public WorkspaceManager ()
{
OpenDocuments = new List<Document> ();
open_documents = new List<Document> ();
OpenDocuments = new ReadOnlyCollection<Document> (open_documents);
SelectionHandler = new SelectionModeHandler ();
}

public int ActiveDocumentIndex {
get {
return active_document_index;
}
}
public int ActiveDocumentIndex => active_document_index;

public Document ActiveDocument {
get {
if (HasOpenDocuments)
return OpenDocuments[active_document_index];
return open_documents[active_document_index];

throw new InvalidOperationException ("Tried to get WorkspaceManager.ActiveDocument when there are no open Documents. Check HasOpenDocuments first.");
}
}

public Document? ActiveDocumentOrDefault => HasOpenDocuments ? OpenDocuments[active_document_index] : null;
public Document? ActiveDocumentOrDefault => HasOpenDocuments ? open_documents[active_document_index] : null;

public SelectionModeHandler SelectionHandler { get; }

public DocumentWorkspace ActiveWorkspace {
get {
if (HasOpenDocuments)
return OpenDocuments[active_document_index].Workspace;
return open_documents[active_document_index].Workspace;

throw new InvalidOperationException ("Tried to get WorkspaceManager.ActiveWorkspace when there are no open Documents. Check HasOpenDocuments first.");
}
}

public Size ImageSize {
get { return ActiveDocument.ImageSize; }
set { ActiveDocument.ImageSize = value; }
get => ActiveDocument.ImageSize;
set => ActiveDocument.ImageSize = value;
}

public Size CanvasSize {
get { return ActiveWorkspace.ViewSize; }
set { ActiveWorkspace.ViewSize = value; }
get => ActiveWorkspace.ViewSize;
set => ActiveWorkspace.ViewSize = value;
}

public PointD Offset {
get { return ActiveWorkspace.Offset; }
}
public PointD Offset => ActiveWorkspace.Offset;

public double Scale {
get { return ActiveWorkspace.Scale; }
set { ActiveWorkspace.Scale = value; }
get => ActiveWorkspace.Scale;
set => ActiveWorkspace.Scale = value;
}

public List<Document> OpenDocuments { get; }
public bool HasOpenDocuments { get { return OpenDocuments.Count > 0; } }
private readonly List<Document> open_documents;
public ReadOnlyCollection<Document> OpenDocuments { get; }
public bool HasOpenDocuments => open_documents.Count > 0;

public Document CreateAndActivateDocument (Gio.File? file, string? file_type, Size size)
{
Expand All @@ -116,7 +113,7 @@ public Document CreateAndActivateDocument (Gio.File? file, string? file_type, Si
} else
doc.DisplayName = Translations.GetString ("Unsaved Image {0}", new_file_name++);

OpenDocuments.Add (doc);
open_documents.Add (doc);
OnDocumentCreated (new DocumentEventArgs (doc));

SetActiveDocument (doc);
Expand All @@ -131,8 +128,8 @@ public void CloseActiveDocument ()

public void CloseDocument (Document document)
{
int index = OpenDocuments.IndexOf (document);
OpenDocuments.Remove (document);
int index = open_documents.IndexOf (document);
open_documents.Remove (document);

if (index == active_document_index) {
// If there's other documents open, switch to one of them
Expand Down Expand Up @@ -298,9 +295,7 @@ public RectangleI ClampToImageSize (RectangleI r)
return ActiveDocument.ClampToImageSize (r);
}

public bool ImageFitsInWindow {
get { return ActiveWorkspace.ImageFitsInWindow; }
}
public bool ImageFitsInWindow => ActiveWorkspace.ImageFitsInWindow;

internal void ResetTitle ()
{
Expand All @@ -312,7 +307,7 @@ internal void ResetTitle ()

public void SetActiveDocument (int index)
{
if (index >= OpenDocuments.Count)
if (index >= open_documents.Count)
throw new ArgumentOutOfRangeException (
nameof (index),
$"Tried to {nameof (WorkspaceManager)}.{nameof (SetActiveDocument)} greater than {nameof (OpenDocuments)}."
Expand All @@ -323,7 +318,7 @@ public void SetActiveDocument (int index)
$"Tried to {nameof (WorkspaceManager)}.{nameof (SetActiveDocument)} less that zero."
);

SetActiveDocument (OpenDocuments[index]);
SetActiveDocument (open_documents[index]);
}

public void SetActiveDocument (Document document)
Expand All @@ -335,17 +330,16 @@ internal void SetActiveDocumentInternal (Document document)
{
// Work around a case where we closed a document but haven't updated
// the active_document_index yet and it points to the closed document
if (HasOpenDocuments && active_document_index != -1 && OpenDocuments.Count > active_document_index)
if (HasOpenDocuments && active_document_index != -1 && open_documents.Count > active_document_index)
PintaCore.Tools.Commit ();

int index = OpenDocuments.IndexOf (document);
int index = open_documents.IndexOf (document);
active_document_index = index;

OnActiveDocumentChanged (EventArgs.Empty);
}

#region Protected Methods
protected void OnActiveDocumentChanged (EventArgs e)
private void OnActiveDocumentChanged (EventArgs e)
{
if (ActiveDocumentChanged != null)
ActiveDocumentChanged (this, EventArgs.Empty);
Expand All @@ -355,7 +349,7 @@ protected void OnActiveDocumentChanged (EventArgs e)
ResetTitle ();
}

protected internal void OnDocumentCreated (DocumentEventArgs e)
private void OnDocumentCreated (DocumentEventArgs e)
{
e.Document.SelectionChanged += (sender, args) => {
OnSelectionChanged ();
Expand All @@ -365,13 +359,13 @@ protected internal void OnDocumentCreated (DocumentEventArgs e)
DocumentCreated (this, e);
}

protected internal void OnDocumentOpened (DocumentEventArgs e)
private void OnDocumentOpened (DocumentEventArgs e)
{
if (DocumentOpened != null)
DocumentOpened (this, e);
}

protected internal void OnDocumentClosed (DocumentEventArgs e)
private void OnDocumentClosed (DocumentEventArgs e)
{
if (DocumentClosed != null)
DocumentClosed (this, e);
Expand All @@ -382,8 +376,6 @@ private void OnSelectionChanged ()
if (SelectionChanged != null)
SelectionChanged.Invoke (this, EventArgs.Empty);
}
#endregion


private static void ShowOpenFileErrorDialog (Window parent, string filename, string primary_text, string details)
{
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/Pinta.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down
25 changes: 14 additions & 11 deletions Pinta.Core/Widgets/ToolBarDropDownButton.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Pinta.Core
{
public class ToolBarDropDownButton : Gtk.MenuButton
public sealed class ToolBarDropDownButton : Gtk.MenuButton
{
private const string action_prefix = "tool";

Expand All @@ -13,13 +14,15 @@ public class ToolBarDropDownButton : Gtk.MenuButton
private readonly Gio.SimpleActionGroup action_group;
private ToolBarItem? selected_item;

public List<ToolBarItem> Items { get; }
private readonly List<ToolBarItem> items;
public ReadOnlyCollection<ToolBarItem> Items { get; }

public ToolBarDropDownButton (bool showLabel = false)
{
this.show_label = showLabel;

Items = new List<ToolBarItem> ();
items = new List<ToolBarItem> ();
Items = new ReadOnlyCollection<ToolBarItem> (items);
AlwaysShowArrow = true;

dropdown = Gio.Menu.New ();
Expand All @@ -40,7 +43,7 @@ public ToolBarItem AddItem (string text, string imageId, object? tag)
action_group.AddAction (item.Action);
dropdown.AppendItem (Gio.MenuItem.New (text, $"{action_prefix}.{item.Action.Name}"));

Items.Add (item);
items.Add (item);
item.Action.OnActivate += delegate { SetSelectedItem (item); };

if (selected_item == null)
Expand All @@ -63,19 +66,19 @@ public ToolBarItem SelectedItem {
}

public int SelectedIndex {
get => selected_item is null ? -1 : Items.IndexOf (selected_item);
get => selected_item is null ? -1 : items.IndexOf (selected_item);
set {
if (value < 0 || value >= Items.Count)
if (value < 0 || value >= items.Count)
return;

var item = Items[value];
var item = items[value];

if (item != selected_item)
SetSelectedItem (item);
}
}

protected void SetSelectedItem (ToolBarItem item)
private void SetSelectedItem (ToolBarItem item)
{
IconName = item.ImageId;

Expand All @@ -88,7 +91,7 @@ protected void SetSelectedItem (ToolBarItem item)
OnSelectedItemChanged ();
}

protected void OnSelectedItemChanged ()
private void OnSelectedItemChanged ()
{
if (SelectedItemChanged != null)
SelectedItemChanged (this, EventArgs.Empty);
Expand All @@ -97,7 +100,7 @@ protected void OnSelectedItemChanged ()
public event EventHandler? SelectedItemChanged;
}

public class ToolBarItem
public sealed class ToolBarItem
{
public ToolBarItem (string text, string imageId)
{
Expand All @@ -116,7 +119,7 @@ public ToolBarItem (string text, string imageId, object? tag) : this (text, imag
public string ImageId { get; set; }
public object? Tag { get; set; }
public string Text { get; set; }
public Gio.SimpleAction Action { get; private set; }
public Gio.SimpleAction Action { get; }

public T GetTagOrDefault<T> (T defaultValue)
{
Expand Down
Loading

0 comments on commit baf4143

Please sign in to comment.