Dock panel serialisation now persists group states.

This commit is contained in:
Robin 2015-12-31 23:35:02 +00:00
parent 31eaec9cb1
commit a0ded1a34d
7 changed files with 132 additions and 40 deletions

View File

@ -127,8 +127,10 @@
<Compile Include="Docking\DarkDockSplitter.cs" /> <Compile Include="Docking\DarkDockSplitter.cs" />
<Compile Include="Docking\DarkDockTab.cs" /> <Compile Include="Docking\DarkDockTab.cs" />
<Compile Include="Docking\DarkDockTabArea.cs" /> <Compile Include="Docking\DarkDockTabArea.cs" />
<Compile Include="Docking\DockContentEvenArgs.cs" /> <Compile Include="Docking\DockContentEventArgs.cs" />
<Compile Include="Docking\DockGroupState.cs" />
<Compile Include="Docking\DockPanelState.cs" /> <Compile Include="Docking\DockPanelState.cs" />
<Compile Include="Docking\DockRegionState.cs" />
<Compile Include="Extensions\BitmapExtensions.cs" /> <Compile Include="Extensions\BitmapExtensions.cs" />
<Compile Include="Extensions\IEnumerableExtensions.cs" /> <Compile Include="Extensions\IEnumerableExtensions.cs" />
<Compile Include="Forms\DarkDialog.cs"> <Compile Include="Forms\DarkDialog.cs">

View File

@ -201,26 +201,77 @@ namespace DarkUI.Docking
{ {
var state = new DockPanelState(); var state = new DockPanelState();
foreach (var content in _contents) state.Regions.Add(new DockRegionState(DarkDockArea.Document));
state.OpenContent.Add(content.SerializationKey); state.Regions.Add(new DockRegionState(DarkDockArea.Left, _regions[DarkDockArea.Left].Size));
state.Regions.Add(new DockRegionState(DarkDockArea.Right, _regions[DarkDockArea.Right].Size));
state.Regions.Add(new DockRegionState(DarkDockArea.Bottom, _regions[DarkDockArea.Bottom].Size));
state.LeftRegionSize = _regions[DarkDockArea.Left].Size; var _groupStates = new Dictionary<DarkDockGroup, DockGroupState>();
state.RightRegionSize = _regions[DarkDockArea.Right].Size;
state.BottomRegionSize = _regions[DarkDockArea.Bottom].Size; foreach (var content in _contents)
{
foreach (var region in state.Regions)
{
if (region.Area == content.DockArea)
{
DockGroupState groupState;
if (_groupStates.ContainsKey(content.DockGroup))
{
groupState = _groupStates[content.DockGroup];
}
else
{
groupState = new DockGroupState();
region.Groups.Add(groupState);
_groupStates.Add(content.DockGroup, groupState);
}
groupState.Contents.Add(content.SerializationKey);
}
}
}
return state; return state;
} }
public void RestoreDockPanelRegions(DockPanelState state) public void RestoreDockPanelState(DockPanelState state, Func<string, DarkDockContent> getContentBySerializationKey)
{ {
if (state.LeftRegionSize.Width > 0 && state.LeftRegionSize.Height > 0) foreach (var region in state.Regions)
_regions[DarkDockArea.Left].Size = state.LeftRegionSize; {
switch (region.Area)
{
case DarkDockArea.Left:
_regions[DarkDockArea.Left].Size = region.Size;
break;
case DarkDockArea.Right:
_regions[DarkDockArea.Right].Size = region.Size;
break;
case DarkDockArea.Bottom:
_regions[DarkDockArea.Bottom].Size = region.Size;
break;
}
if (state.RightRegionSize.Width > 0 && state.RightRegionSize.Height > 0) foreach (var group in region.Groups)
_regions[DarkDockArea.Right].Size = state.RightRegionSize; {
DarkDockContent previousContent = null;
if (state.BottomRegionSize.Width > 0 && state.BottomRegionSize.Height > 0) foreach (var contentKey in group.Contents)
_regions[DarkDockArea.Bottom].Size = state.BottomRegionSize; {
var content = getContentBySerializationKey(contentKey);
if (content == null)
continue;
if (previousContent == null)
AddContent(content);
else
AddContent(content, previousContent.DockGroup);
previousContent = content;
}
}
}
} }
#endregion #endregion

View File

@ -0,0 +1,22 @@
using System.Collections.Generic;
namespace DarkUI.Docking
{
public class DockGroupState
{
#region Property Region
public List<string> Contents { get; set; }
#endregion
#region Constructor Region
public DockGroupState()
{
Contents = new List<string>();
}
#endregion
}
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
namespace DarkUI.Docking namespace DarkUI.Docking
{ {
@ -7,13 +6,7 @@ namespace DarkUI.Docking
{ {
#region Property Region #region Property Region
public List<string> OpenContent { get; set; } public List<DockRegionState> Regions { get; set; }
public Size LeftRegionSize { get; set; }
public Size RightRegionSize { get; set; }
public Size BottomRegionSize { get; set; }
#endregion #endregion
@ -21,16 +14,7 @@ namespace DarkUI.Docking
public DockPanelState() public DockPanelState()
{ {
OpenContent = new List<string>(); Regions = new List<DockRegionState>();
}
public DockPanelState(List<string> openContent, Size leftRegionSize, Size rightRegionSize, Size bottomRegionSize)
: this()
{
OpenContent = openContent;
LeftRegionSize = leftRegionSize;
RightRegionSize = rightRegionSize;
BottomRegionSize = bottomRegionSize;
} }
#endregion #endregion

View File

@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Drawing;
namespace DarkUI.Docking
{
public class DockRegionState
{
#region Property Region
public DarkDockArea Area { get; set; }
public Size Size { get; set; }
public List<DockGroupState> Groups { get; set; }
#endregion
#region Constructor Region
public DockRegionState()
{
Groups = new List<DockGroupState>();
}
public DockRegionState(DarkDockArea area)
: this()
{
Area = area;
}
public DockRegionState(DarkDockArea area, Size size)
: this(area)
{
Size = size;
}
#endregion
}
}

View File

@ -203,15 +203,7 @@ namespace Example
private void DeserializeDockPanel(string path) private void DeserializeDockPanel(string path)
{ {
var state = SerializerHelper.Deserialize<DockPanelState>(path); var state = SerializerHelper.Deserialize<DockPanelState>(path);
DockPanel.RestoreDockPanelRegions(state); DockPanel.RestoreDockPanelState(state, GetContentBySerializationKey);
foreach (var key in state.OpenContent)
{
var content = GetContentBySerializationKey(key);
if (content != null)
DockPanel.AddContent(content);
}
} }
private DarkDockContent GetContentBySerializationKey(string key) private DarkDockContent GetContentBySerializationKey(string key)

View File

@ -11,7 +11,9 @@ namespace Example
using (var fs = File.CreateText(file)) using (var fs = File.CreateText(file))
{ {
var serializer = new JsonSerializer(); var serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
serializer.Formatting = Formatting.Indented; serializer.Formatting = Formatting.Indented;
serializer.Serialize(fs, obj); serializer.Serialize(fs, obj);
} }
} }