diff --git a/Pinta.Core/Actions/AdjustmentsActions.cs b/Pinta.Core/Actions/AdjustmentsActions.cs index 47327032a..5e1dff63c 100644 --- a/Pinta.Core/Actions/AdjustmentsActions.cs +++ b/Pinta.Core/Actions/AdjustmentsActions.cs @@ -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 Actions { get; } +namespace Pinta.Core; - public AdjustmentsActions () - { - Actions = new List (); - } +public sealed class AdjustmentsActions +{ + public Collection 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 } diff --git a/Pinta.Core/Actions/EffectsActions.cs b/Pinta.Core/Actions/EffectsActions.cs index 2d8dcef90..50fd7ef69 100644 --- a/Pinta.Core/Actions/EffectsActions.cs +++ b/Pinta.Core/Actions/EffectsActions.cs @@ -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 Menus { get; } = new (); - public List Actions { get; } = new (); + public Collection Actions { get; } = new (); public EffectsActions () { diff --git a/Pinta.Core/Managers/WorkspaceManager.cs b/Pinta.Core/Managers/WorkspaceManager.cs index 50d48a17c..4d5285d63 100644 --- a/Pinta.Core/Managers/WorkspaceManager.cs +++ b/Pinta.Core/Managers/WorkspaceManager.cs @@ -26,6 +26,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using Cairo; @@ -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 (); + open_documents = new List (); + OpenDocuments = new ReadOnlyCollection (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 OpenDocuments { get; } - public bool HasOpenDocuments { get { return OpenDocuments.Count > 0; } } + private readonly List open_documents; + public ReadOnlyCollection OpenDocuments { get; } + public bool HasOpenDocuments => open_documents.Count > 0; public Document CreateAndActivateDocument (Gio.File? file, string? file_type, Size size) { @@ -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); @@ -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 @@ -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 () { @@ -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)}." @@ -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) @@ -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); @@ -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 (); @@ -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); @@ -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) { diff --git a/Pinta.Core/Pinta.Core.csproj b/Pinta.Core/Pinta.Core.csproj index a84930576..7e8a4ff7e 100644 --- a/Pinta.Core/Pinta.Core.csproj +++ b/Pinta.Core/Pinta.Core.csproj @@ -1,4 +1,4 @@ - + true diff --git a/Pinta.Core/Widgets/ToolBarDropDownButton.cs b/Pinta.Core/Widgets/ToolBarDropDownButton.cs index f5bd0e2d7..a7980e0ca 100644 --- a/Pinta.Core/Widgets/ToolBarDropDownButton.cs +++ b/Pinta.Core/Widgets/ToolBarDropDownButton.cs @@ -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"; @@ -13,13 +14,15 @@ public class ToolBarDropDownButton : Gtk.MenuButton private readonly Gio.SimpleActionGroup action_group; private ToolBarItem? selected_item; - public List Items { get; } + private readonly List items; + public ReadOnlyCollection Items { get; } public ToolBarDropDownButton (bool showLabel = false) { this.show_label = showLabel; - Items = new List (); + items = new List (); + Items = new ReadOnlyCollection (items); AlwaysShowArrow = true; dropdown = Gio.Menu.New (); @@ -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) @@ -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; @@ -88,7 +91,7 @@ protected void SetSelectedItem (ToolBarItem item) OnSelectedItemChanged (); } - protected void OnSelectedItemChanged () + private void OnSelectedItemChanged () { if (SelectedItemChanged != null) SelectedItemChanged (this, EventArgs.Empty); @@ -97,7 +100,7 @@ protected void OnSelectedItemChanged () public event EventHandler? SelectedItemChanged; } - public class ToolBarItem + public sealed class ToolBarItem { public ToolBarItem (string text, string imageId) { @@ -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 defaultValue) { diff --git a/Pinta.Gui.Addins/InstallDialog.cs b/Pinta.Gui.Addins/InstallDialog.cs index 34abda053..373b56a6c 100644 --- a/Pinta.Gui.Addins/InstallDialog.cs +++ b/Pinta.Gui.Addins/InstallDialog.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,7 +11,7 @@ namespace Pinta.Gui.Addins; -internal class InstallDialog : Adw.Window +internal sealed class InstallDialog : Adw.Window { private readonly SetupService service; private readonly PackageCollection packages_to_install = new (); @@ -310,28 +311,35 @@ private Task Uninstall () } } -internal class InstallErrorReporter : IErrorReporter +internal sealed class InstallErrorReporter : IErrorReporter { - public List Errors { get; private init; } = new (); - public List Warnings { get; private init; } = new (); + private readonly List errors; + public ReadOnlyCollection Errors { get; } + + private readonly List warnings; + public ReadOnlyCollection Warnings { get; } public InstallErrorReporter () { + errors = new List (); + Errors = new ReadOnlyCollection (errors); + warnings = new List (); + Warnings = new ReadOnlyCollection (warnings); } public void ReportError (string message, Exception exception) { - Errors.Add (message); + errors.Add (message); } public void ReportWarning (string message) { - Warnings.Add (message); + warnings.Add (message); } public void Clear () { - Errors.Clear (); - Warnings.Clear (); + errors.Clear (); + warnings.Clear (); } }