From d6918254e9d403bd7e8a72b29492f541156c0cf3 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 7 Apr 2011 21:48:01 +0200 Subject: [PATCH] Preserve active document (and order of documents) on layout switches. Based on Andrew Olson's suggestion http://community.sharpdevelop.net/forums/t/13034.aspx. --- .../AvalonDock/AvalonDock/DockingManager.cs | 19 ++++++------------- .../AvalonDock/AvalonDock/DocumentContent.cs | 1 + .../AvalonDock/AvalonDock/ManagedContent.cs | 9 ++------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/Libraries/AvalonDock/AvalonDock/DockingManager.cs b/src/Libraries/AvalonDock/AvalonDock/DockingManager.cs index c8a8ab8fad..c83cdf0d76 100644 --- a/src/Libraries/AvalonDock/AvalonDock/DockingManager.cs +++ b/src/Libraries/AvalonDock/AvalonDock/DockingManager.cs @@ -294,8 +294,8 @@ namespace AvalonDock if (ActiveDocument == null) { - var docToActivate = Documents.OrderBy(d => d.LastActivation).FirstOrDefault(); - if (docToActivate != null) + var docToActivate = Documents.OrderBy(d => d.LastActivation).LastOrDefault(); + if (docToActivate != null && docToActivate.LastActivation != DateTime.MinValue) docToActivate.Activate(); } } @@ -3775,7 +3775,8 @@ namespace AvalonDock pane.ToggleAutoHide(); DockableContent[] actualContents = DockableContents.ToArray(); - DocumentContent[] actualDocuments = Documents.ToArray(); + // preserve the order of the documents + DocumentContent[] actualDocuments = Documents.OrderBy(d => d.ContainerPane != null ? d.ContainerPane.Items.IndexOf(d) : -1).ToArray(); //first detach all my actual contents @@ -3892,16 +3893,8 @@ namespace AvalonDock ClearEmptyPanes(); RefreshContents(); - if (ActiveDocument != null && - (ActiveDocument.ContainerPane == null || - ActiveDocument.ContainerPane.GetManager() != this)) - { - if (Documents.Count > 0) - ActiveDocument = Documents[0]; - else - ActiveDocument = null; - } - + // by setting ActiveDocument to null, we activate the previously activated document (as per LastActivation) + ActiveDocument = null; ActiveContent = ActiveDocument; } #endregion diff --git a/src/Libraries/AvalonDock/AvalonDock/DocumentContent.cs b/src/Libraries/AvalonDock/AvalonDock/DocumentContent.cs index 743e8e653d..3983a66873 100644 --- a/src/Libraries/AvalonDock/AvalonDock/DocumentContent.cs +++ b/src/Libraries/AvalonDock/AvalonDock/DocumentContent.cs @@ -340,6 +340,7 @@ namespace AvalonDock /// internal void InternalClose() { + this.LastActivation = DateTime.MinValue; // make this content ineligible for ActiveDocument selection when ActiveDocument is set to null DockingManager manager = Manager; if (manager != null) diff --git a/src/Libraries/AvalonDock/AvalonDock/ManagedContent.cs b/src/Libraries/AvalonDock/AvalonDock/ManagedContent.cs index d48394e499..897e90bcb8 100644 --- a/src/Libraries/AvalonDock/AvalonDock/ManagedContent.cs +++ b/src/Libraries/AvalonDock/AvalonDock/ManagedContent.cs @@ -559,12 +559,7 @@ namespace AvalonDock ((ManagedContent)d).OnIsActiveContentChanged(e); } - DateTime _lastActivation = DateTime.MinValue; - - internal DateTime LastActivation - { - get { return _lastActivation; } - } + internal DateTime LastActivation = DateTime.MinValue; /// /// Provides derived classes an opportunity to handle changes to the IsActiveContent property. @@ -572,7 +567,7 @@ namespace AvalonDock protected virtual void OnIsActiveContentChanged(DependencyPropertyChangedEventArgs e) { if (IsActiveContent) - _lastActivation = DateTime.Now; + this.LastActivation = DateTime.UtcNow; FocusContent();