Added DockPanel serialization.

This commit is contained in:
Robin 2015-12-22 13:02:25 +00:00
parent 7a7607c959
commit 06e5d26801
13 changed files with 178 additions and 13 deletions

View File

@ -128,6 +128,7 @@
<Compile Include="Docking\DarkDockTab.cs" />
<Compile Include="Docking\DarkDockTabArea.cs" />
<Compile Include="Docking\DockContentEvenArgs.cs" />
<Compile Include="Docking\DockPanelState.cs" />
<Compile Include="Extensions\BitmapExtensions.cs" />
<Compile Include="Extensions\IEnumerableExtensions.cs" />
<Compile Include="Forms\DarkDialog.cs">

View File

@ -51,6 +51,10 @@ namespace DarkUI.Docking
[DefaultValue(DarkDockArea.Document)]
public DarkDockArea DockArea { get; set; }
[Category("Behavior")]
[Description("Determines the key used by this content in the dock serialization.")]
public string SerializationKey { get; set; }
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DarkDockPanel DockPanel { get; internal set; }

View File

@ -192,6 +192,36 @@ namespace DarkUI.Docking
}
#endregion
#region Serialization Region
public DockPanelState GetDockPanelState()
{
var state = new DockPanelState();
foreach (var content in _contents)
state.OpenContent.Add(content.SerializationKey);
state.LeftRegionSize = _regions[DarkDockArea.Left].Size;
state.RightRegionSize = _regions[DarkDockArea.Right].Size;
state.BottomRegionSize = _regions[DarkDockArea.Bottom].Size;
return state;
}
public void RestoreDockPanelRegions(DockPanelState state)
{
if (state.LeftRegionSize.Width > 0 && state.LeftRegionSize.Height > 0)
_regions[DarkDockArea.Left].Size = state.LeftRegionSize;
if (state.RightRegionSize.Width > 0 && state.RightRegionSize.Height > 0)
_regions[DarkDockArea.Right].Size = state.RightRegionSize;
if (state.BottomRegionSize.Width > 0 && state.BottomRegionSize.Height > 0)
_regions[DarkDockArea.Bottom].Size = state.BottomRegionSize;
}
#endregion
}
}

View File

@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Drawing;
namespace DarkUI.Docking
{
public class DockPanelState
{
#region Property Region
public List<string> OpenContent { get; set; }
public Size LeftRegionSize { get; set; }
public Size RightRegionSize { get; set; }
public Size BottomRegionSize { get; set; }
#endregion
#region Constructor Region
public DockPanelState()
{
OpenContent = new List<string>();
}
public DockPanelState(List<string> openContent, Size leftRegionSize, Size rightRegionSize, Size bottomRegionSize)
: this()
{
OpenContent = openContent;
LeftRegionSize = leftRegionSize;
RightRegionSize = rightRegionSize;
BottomRegionSize = bottomRegionSize;
}
#endregion
}
}

View File

@ -34,6 +34,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>.\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
@ -95,6 +98,7 @@
<Compile Include="Forms\MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Helpers\SerializerHelper.cs" />
<Compile Include="Icons.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>

View File

@ -31,7 +31,7 @@ namespace Example
/// </summary>
private void InitializeComponent()
{
this.lstConsole = new DarkListView();
this.lstConsole = new DarkUI.Controls.DarkListView();
this.SuspendLayout();
//
// lstConsole
@ -49,11 +49,12 @@ namespace Example
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.lstConsole);
this.DockArea = DarkDockArea.Bottom;
this.DockArea = DarkUI.Docking.DarkDockArea.Bottom;
this.DockText = "Console";
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Icon = global::Example.Icons.Console;
this.Name = "DockConsole";
this.SerializationKey = "DockConsole";
this.Size = new System.Drawing.Size(500, 200);
this.ResumeLayout(false);

View File

@ -49,11 +49,12 @@ namespace Example
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.lstHistory);
this.DockArea = DarkDockArea.Right;
this.DockArea = DarkUI.Docking.DarkDockArea.Right;
this.DockText = "History";
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Icon = global::Example.Icons.RefactoringLog_12810;
this.Name = "DockHistory";
this.SerializationKey = "DockHistory";
this.Size = new System.Drawing.Size(280, 450);
this.ResumeLayout(false);

View File

@ -31,7 +31,7 @@ namespace Example
/// </summary>
private void InitializeComponent()
{
this.lstLayers = new DarkListView();
this.lstLayers = new DarkUI.Controls.DarkListView();
this.SuspendLayout();
//
// lstLayers
@ -48,11 +48,12 @@ namespace Example
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.lstLayers);
this.DockArea = DarkDockArea.Right;
this.DockArea = DarkUI.Docking.DarkDockArea.Right;
this.DockText = "Layers";
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Icon = global::Example.Icons.Collection_16xLG;
this.Name = "DockLayers";
this.SerializationKey = "DockLayers";
this.Size = new System.Drawing.Size(280, 450);
this.ResumeLayout(false);

View File

@ -32,7 +32,7 @@ namespace Example
/// </summary>
private void InitializeComponent()
{
this.treeProject = new DarkTreeView();
this.treeProject = new DarkUI.Controls.DarkTreeView();
this.SuspendLayout();
//
// treeProject
@ -40,6 +40,7 @@ namespace Example
this.treeProject.AllowMoveNodes = true;
this.treeProject.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeProject.Location = new System.Drawing.Point(0, 25);
this.treeProject.MaxDragChange = 20;
this.treeProject.MultiSelect = true;
this.treeProject.Name = "treeProject";
this.treeProject.ShowIcons = true;
@ -52,11 +53,12 @@ namespace Example
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.treeProject);
this.DockArea = DarkDockArea.Left;
this.DockArea = DarkUI.Docking.DarkDockArea.Left;
this.DockText = "Project Explorer";
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Icon = global::Example.Icons.application_16x;
this.Name = "DockProject";
this.SerializationKey = "DockProject";
this.Size = new System.Drawing.Size(280, 450);
this.ResumeLayout(false);

View File

@ -177,11 +177,12 @@ namespace Example
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.pnlMain);
this.DockArea = DarkDockArea.Right;
this.DockArea = DarkUI.Docking.DarkDockArea.Right;
this.DockText = "Properties";
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Icon = global::Example.Icons.properties_16xLG;
this.Name = "DockProperties";
this.SerializationKey = "DockProperties";
this.Size = new System.Drawing.Size(280, 450);
this.pnlMain.ResumeLayout(false);
this.pnlMain.PerformLayout();

View File

@ -3,6 +3,7 @@ using DarkUI.Forms;
using DarkUI.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace Example
@ -52,12 +53,20 @@ namespace Example
_toolWindows.Add(_dockLayers);
_toolWindows.Add(_dockHistory);
// Add the tool window list contents to the dock panel
foreach (var toolWindow in _toolWindows)
DockPanel.AddContent(toolWindow);
// Deserialize if a previous state is stored
if (File.Exists("dockpanel.config"))
{
DeserializeDockPanel("dockpanel.config");
}
else
{
// Add the tool window list contents to the dock panel
foreach (var toolWindow in _toolWindows)
DockPanel.AddContent(toolWindow);
// Add the history panel to the layer panel group
DockPanel.AddContent(_dockHistory, _dockLayers.DockGroup);
// Add the history panel to the layer panel group
DockPanel.AddContent(_dockHistory, _dockLayers.DockGroup);
}
// Check window menu items which are contained in the dock panel
BuildWindowMenu();
@ -74,6 +83,8 @@ namespace Example
private void HookEvents()
{
FormClosing += MainForm_FormClosing;
DockPanel.ContentAdded += DockPanel_ContentAdded;
DockPanel.ContentRemoved += DockPanel_ContentRemoved;
@ -114,6 +125,11 @@ namespace Example
#region Event Handler Region
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
SerializeDockPanel("dockpanel.config");
}
private void DockPanel_ContentAdded(object sender, DockContentEventArgs e)
{
if (_toolWindows.Contains(e.Content))
@ -175,5 +191,40 @@ namespace Example
}
#endregion
#region Serialization Region
private void SerializeDockPanel(string path)
{
var state = DockPanel.GetDockPanelState();
SerializerHelper.Serialize(state, path);
}
private void DeserializeDockPanel(string path)
{
var state = SerializerHelper.Deserialize<DockPanelState>(path);
DockPanel.RestoreDockPanelRegions(state);
foreach (var key in state.OpenContent)
{
var content = GetContentBySerializationKey(key);
if (content != null)
DockPanel.AddContent(content);
}
}
private DarkDockContent GetContentBySerializationKey(string key)
{
foreach (var window in _toolWindows)
{
if (window.SerializationKey == key)
return window;
}
return null;
}
#endregion
}
}

View File

@ -0,0 +1,31 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.IO;
namespace Example
{
public class SerializerHelper
{
public static void Serialize<T>(T obj, string file)
{
using (var fs = File.CreateText(file))
{
var serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(fs, obj);
}
}
public static T Deserialize<T>(string file) where T : class
{
using (var fs = File.OpenText(file))
{
var serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
var result = serializer.Deserialize(fs, typeof(T));
return result as T;
}
}
}
}

BIN
Example/Newtonsoft.Json.dll Normal file

Binary file not shown.