diff --git a/Pinta.Core/Actions/EditActions.cs b/Pinta.Core/Actions/EditActions.cs index c0dc0757cd..45508e7f1e 100644 --- a/Pinta.Core/Actions/EditActions.cs +++ b/Pinta.Core/Actions/EditActions.cs @@ -53,6 +53,7 @@ public sealed class EditActions public Command ResizePalette { get; } private Gio.File? last_palette_dir = null; + private Document? active_document = null; private readonly ChromeManager chrome; private readonly PaletteFormatManager palette_formats; @@ -616,14 +617,41 @@ void HandleInvertSelectionActivated (object sender, EventArgs e) private void WorkspaceActiveDocumentChanged (object? sender, EventArgs e) { - if (!workspace.HasOpenDocuments) { - Undo.Sensitive = false; + if (active_document != null) { + active_document.History.HistoryItemAdded -= OnDocumentHistoryChanged; + active_document.History.ActionRedone -= OnDocumentHistoryChanged; + active_document.History.ActionUndone -= OnDocumentHistoryChanged; + } + + active_document = workspace.ActiveDocumentOrDefault; + + // Register event handlers for the new document to update the undo/redo state. + if (active_document != null) { + active_document.History.HistoryItemAdded += OnDocumentHistoryChanged; + active_document.History.ActionRedone += OnDocumentHistoryChanged; + active_document.History.ActionUndone += OnDocumentHistoryChanged; + } + + // Force an update of the undo/redo state after switching documents. + UpdateUndoRedoActions (); + } + + private void OnDocumentHistoryChanged (object? sender, EventArgs e) + { + UpdateUndoRedoActions (); + } + + private void UpdateUndoRedoActions () + { + if (active_document != null) { + Redo.Sensitive = active_document.Workspace.History.CanRedo; + Undo.Sensitive = active_document.Workspace.History.CanUndo; + } else { Redo.Sensitive = false; - return; + Undo.Sensitive = false; } - Redo.Sensitive = workspace.ActiveWorkspace.History.CanRedo; - Undo.Sensitive = workspace.ActiveWorkspace.History.CanUndo; } + #endregion } diff --git a/Pinta.Core/Classes/DocumentHistory.cs b/Pinta.Core/Classes/DocumentHistory.cs index 402babbf50..5a25ed7047 100644 --- a/Pinta.Core/Classes/DocumentHistory.cs +++ b/Pinta.Core/Classes/DocumentHistory.cs @@ -1,21 +1,21 @@ -// +// // DocumentHistory.cs -// +// // Author: // Jonathan Pobst -// +// // Copyright (c) 2010 Jonathan Pobst -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -31,8 +31,6 @@ namespace Pinta.Core; public sealed class DocumentHistory { - private readonly EditActions edit; - private readonly Document document; private readonly List history = []; @@ -42,11 +40,8 @@ public sealed class DocumentHistory public event EventHandler? ActionUndone; public event EventHandler? ActionRedone; - internal DocumentHistory ( - EditActions edit, - Document document) + internal DocumentHistory (Document document) { - this.edit = edit; this.document = document; } @@ -81,11 +76,6 @@ public void PushNewItem (BaseHistoryItem newItem) if (newItem.CausesDirty) document.IsDirty = true; - if (history.Count > 1) - edit.Undo.Sensitive = true; - - edit.Redo.Sensitive = false; - HistoryItemAdded?.Invoke (this, new HistoryItemAddedEventArgs (newItem)); } @@ -106,11 +96,6 @@ public void Undo () if (Pointer == clean_pointer) document.IsDirty = false; - if (Pointer == 0) - edit.Undo.Sensitive = false; - - edit.Redo.Sensitive = true; - ActionUndone?.Invoke (this, EventArgs.Empty); } @@ -125,17 +110,11 @@ public void Redo () item.Redo (); item.State = HistoryItemState.Undo; - if (Pointer == history.Count - 1) - edit.Redo.Sensitive = false; - if (Pointer == clean_pointer) document.IsDirty = false; else if (item.CausesDirty) document.IsDirty = true; - if (history.Count > 1) - edit.Undo.Sensitive = true; - ActionRedone?.Invoke (this, EventArgs.Empty); } @@ -167,8 +146,5 @@ public void Clear () clean_pointer = -1; document.IsDirty = false; - - edit.Redo.Sensitive = false; - edit.Undo.Sensitive = false; } } diff --git a/Pinta.Core/Classes/DocumentWorkspace.cs b/Pinta.Core/Classes/DocumentWorkspace.cs index b9af931a07..924fb50318 100644 --- a/Pinta.Core/Classes/DocumentWorkspace.cs +++ b/Pinta.Core/Classes/DocumentWorkspace.cs @@ -49,7 +49,7 @@ internal DocumentWorkspace ( this.document = document; - History = new DocumentHistory (actions.Edit, document); + History = new DocumentHistory (document); } #region Public Events