Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced LayoutSerializer with Async variant #265

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Added new serializer classes, supporting async serialization scheme
  • Loading branch information
X39 committed Apr 19, 2021
commit a204d490719d7f315cc5710447124f8301c53698
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