Skip to content

Commit

Permalink
Added new serializer classes, supporting async serialization scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
X39 committed Apr 19, 2021
1 parent a3bfc89 commit a204d49
Show file tree
Hide file tree
Showing 3 changed files with 441 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at https://opensource.org/licenses/MS-PL
************************************************************************/

using System;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace AvalonDock.Layout.Serialization
{
/// <summary>Implements a layout serialization/deserialization method of the docking framework.</summary>
public class AsyncXmlLayoutSerializer : LayoutSerializerBase
{
#region Constructors

/// <summary>
/// Class constructor from <see cref="DockingManager"/> instance.
/// </summary>
/// <param name="manager"></param>
public AsyncXmlLayoutSerializer(DockingManager manager)
: base(manager)
{
}

#endregion Constructors

#region Public Methods

/// <summary>Serialize the layout into a <see cref="XmlWriter"/>.</summary>
/// <param name="writer"></param>
public void Serialize(XmlWriter writer)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
serializer.Serialize(writer, Manager.Layout);
}

/// <summary>Serialize the layout into a <see cref="TextWriter"/>.</summary>
/// <param name="writer"></param>
public void Serialize(TextWriter writer)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
serializer.Serialize(writer, Manager.Layout);
}

/// <summary>Serialize the layout into a <see cref="Stream"/>.</summary>
/// <param name="stream"></param>
public void Serialize(Stream stream)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
serializer.Serialize(stream, Manager.Layout);
}

/// <summary>Serialize the layout into a file using a <see cref="StreamWriter"/>.</summary>
/// <param name="filepath"></param>
public void Serialize(string filepath)
{
using (var stream = new StreamWriter(filepath))
{
Serialize(stream);
}
}

/// <summary>Deserialize the layout a file from a <see cref="Stream"/>.</summary>
/// <param name="stream"></param>
public async Task Deserialize(System.IO.Stream stream)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
var layout = (LayoutRoot)serializer.Deserialize(stream);
await FixupLayout(layout);
Manager.Layout = layout;
}

/// <summary>Deserialize the layout a file from a <see cref="TextReader"/>.</summary>
/// <param name="reader"></param>
public async Task Deserialize(TextReader reader)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
var layout = (LayoutRoot)serializer.Deserialize(reader);
await FixupLayout(layout);
Manager.Layout = layout;
}

/// <summary>Deserialize the layout a file from a <see cref="XmlReader"/>.</summary>
/// <param name="reader"></param>
public async Task Deserialize(XmlReader reader)
{
var serializer = new XmlSerializer(typeof(LayoutRoot));
var layout = (LayoutRoot)serializer.Deserialize(reader);
await FixupLayout(layout);
Manager.Layout = layout;
}

/// <summary>Deserialize the layout from a file using a <see cref="StreamReader"/>.</summary>
/// <param name="filepath"></param>
public async Task Deserialize(string filepath)
{
using (var stream = new StreamReader(filepath))
{
await Deserialize(stream);
}
}

#endregion Public Methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at https://opensource.org/licenses/MS-PL
************************************************************************/

using System.ComponentModel;

namespace AvalonDock.Layout.Serialization
{
/// <summary>
/// Implements an event that can be used to communicate between deserialization method
/// and client application that a new item (LayoutAnchorable or Document) is about to
/// be constructed and should be attached to a corresponding viewmodel.
///
/// The client application can use this event to Cancel reloading the item or
/// attach (a viewmodel) content to the view item that is about to be reloaded and presented in the UI.
///
/// Use the Cancel property to indicate the case in which an item should not be deserialized.
/// </summary>
public class LayoutRestoreEventArgs
{
#region constructors

/// <summary>
/// Class constructor from <see cref="LayoutContent"/> and <paramref name="previousContent"/> object.
/// </summary>
/// <param name="model">The model of the view that has been deserialized.</param>
/// <param name="previousContent">The content if it was available in previous layout.</param>
public LayoutRestoreEventArgs(LayoutContent model, object previousContent)
{
Cancel = false; // reloading an item is not by cancelled by default
Handled = false; // an item is not handled by default
Model = model;
Content = previousContent;
}

#endregion constructors

#region Properties

/// <summary>
/// Gets or sets a value indicating whether the event should be canceled.
/// </summary>
public bool Cancel
{
get; private set;
}

/// <summary>
/// Gets or sets a value indicating whether the event should continue processing.
/// </summary>
public bool Handled
{
get; private set;
}

/// <summary>
/// Gets the model of the view that is about to be deserialized.
/// </summary>
public LayoutContent Model
{
get; private set;
}

/// <summary>
/// Gets/sets the content for the <see cref="Model"/> that is about to be deserialized.
/// </summary>
public object Content { get; set; }

#endregion Properties
}
}
Loading

0 comments on commit a204d49

Please sign in to comment.