This guide explains the RestoreDockable
functionality in the Dock library, its behavior regarding splitter management, and recent optimizations to prevent unwanted splitter creation.
The RestoreDockable
method is responsible for moving hidden dockables back to their original visible locations within the docking layout. This functionality is essential for applications that support hiding and showing dock panels dynamically.
The IFactory
interface provides two overloads for restoring dockables:
/// <summary>
/// Restores a hidden dockable to its original dock.
/// </summary>
/// <param name="dockable">The dockable to restore.</param>
void RestoreDockable(IDockable dockable);
/// <summary>
/// Restores a hidden dockable to its original dock.
/// </summary>
/// <param name="id">The dockable id.</param>
/// <returns>The restored dockable or null.</returns>
IDockable? RestoreDockable(string id);
HiddenDockables
collectionHiddenDockables
DockableRestored
eventOriginalOwner
, adds it back to that dock’s VisibleDockables
Owner
to the original owner and clears OriginalOwner
DockableAdded
eventThe string overload (RestoreDockable(string id)
) searches through all root docks in the factory’s DockControls
collection to find a hidden dockable with the specified ID. If found, it calls the main RestoreDockable(IDockable)
method.
RestoreDockable
does not create splitters automatically. The layout optimization system handles splitter creation and cleanup automatically as needed.
if (dockable.OriginalOwner is IDock owner)
{
AddVisibleDockable(owner, dockable);
OnDockableAdded(dockable);
dockable.Owner = owner;
dockable.OriginalOwner = null;
}
The layout system handles splitters through:
CleanupOrphanedSplitters()
: Removes splitters at beginnings/ends and consecutive splittersRemoveDockable()
: Automatically cleans up splitters after dockable removalSplitToDock()
: Creates splitters only when needed for new split operations// Hide a dockable
factory.HideDockable(myDocument);
// Later, restore it
factory.RestoreDockable(myDocument);
// Or restore by ID
var restored = factory.RestoreDockable("MyDocumentId");
factory.DockableRestored += (sender, args) =>
{
Console.WriteLine($"Restored: {args.Dockable.Title}");
};
factory.DockableAdded += (sender, args) =>
{
Console.WriteLine($"Added back to dock: {args.Dockable.Title}");
};
// Restore by ID with error handling
var restored = factory.RestoreDockable("MyDocumentId");
if (restored == null)
{
Console.WriteLine("Dockable not found or not hidden");
}
RestoreDockable
OriginalOwner
manually - let the factory manage this propertyCondition | Behavior |
---|---|
dockable is null |
Throws NullReferenceException |
Root dock not found | Method returns early, no action taken |
HiddenDockables is null |
Method returns early, no action taken |
Dockable not in HiddenDockables |
Method returns early, no action taken |
OriginalOwner is null |
Sets dockable’s Owner to null |
OriginalOwner.VisibleDockables is null |
Creates new collection and adds dockable |
The library includes comprehensive unit tests covering:
When working with RestoreDockable
: