committed by
GitHub
85 changed files with 1983 additions and 640 deletions
@ -0,0 +1,73 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Reactive; |
||||
|
using System.Threading.Tasks; |
||||
|
using Avalonia.Controls; |
||||
|
using ReactiveUI; |
||||
|
|
||||
|
namespace ControlCatalog.ViewModels |
||||
|
{ |
||||
|
public class ContextMenuPageViewModel |
||||
|
{ |
||||
|
public ContextMenuPageViewModel() |
||||
|
{ |
||||
|
OpenCommand = ReactiveCommand.CreateFromTask(Open); |
||||
|
SaveCommand = ReactiveCommand.Create(Save); |
||||
|
OpenRecentCommand = ReactiveCommand.Create<string>(OpenRecent); |
||||
|
|
||||
|
MenuItems = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel { Header = "_Open...", Command = OpenCommand }, |
||||
|
new MenuItemViewModel { Header = "Save", Command = SaveCommand }, |
||||
|
new MenuItemViewModel { Header = "-" }, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "Recent", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File1.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File1.txt" |
||||
|
}, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File2.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File2.txt" |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public IReadOnlyList<MenuItemViewModel> MenuItems { get; set; } |
||||
|
public ReactiveCommand<Unit, Unit> OpenCommand { get; } |
||||
|
public ReactiveCommand<Unit, Unit> SaveCommand { get; } |
||||
|
public ReactiveCommand<string, Unit> OpenRecentCommand { get; } |
||||
|
|
||||
|
public async Task Open() |
||||
|
{ |
||||
|
var dialog = new OpenFileDialog(); |
||||
|
var result = await dialog.ShowAsync(App.Current.MainWindow); |
||||
|
|
||||
|
if (result != null) |
||||
|
{ |
||||
|
foreach (var path in result) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Opened: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Save() |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine("Save"); |
||||
|
} |
||||
|
|
||||
|
public void OpenRecent(string path) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Windows.Input; |
||||
|
|
||||
|
namespace ControlCatalog.ViewModels |
||||
|
{ |
||||
|
public class MenuItemViewModel |
||||
|
{ |
||||
|
public string Header { get; set; } |
||||
|
public ICommand Command { get; set; } |
||||
|
public object CommandParameter { get; set; } |
||||
|
public IList<MenuItemViewModel> Items { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Reactive; |
||||
|
using System.Threading.Tasks; |
||||
|
using Avalonia.Controls; |
||||
|
using ReactiveUI; |
||||
|
|
||||
|
namespace ControlCatalog.ViewModels |
||||
|
{ |
||||
|
public class MenuPageViewModel |
||||
|
{ |
||||
|
public MenuPageViewModel() |
||||
|
{ |
||||
|
OpenCommand = ReactiveCommand.CreateFromTask(Open); |
||||
|
SaveCommand = ReactiveCommand.Create(Save); |
||||
|
OpenRecentCommand = ReactiveCommand.Create<string>(OpenRecent); |
||||
|
|
||||
|
MenuItems = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "_File", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel { Header = "_Open...", Command = OpenCommand }, |
||||
|
new MenuItemViewModel { Header = "Save", Command = SaveCommand }, |
||||
|
new MenuItemViewModel { Header = "-" }, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "Recent", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File1.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File1.txt" |
||||
|
}, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File2.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File2.txt" |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "_Edit", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel { Header = "_Copy" }, |
||||
|
new MenuItemViewModel { Header = "_Paste" }, |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public IReadOnlyList<MenuItemViewModel> MenuItems { get; set; } |
||||
|
public ReactiveCommand<Unit, Unit> OpenCommand { get; } |
||||
|
public ReactiveCommand<Unit, Unit> SaveCommand { get; } |
||||
|
public ReactiveCommand<string, Unit> OpenRecentCommand { get; } |
||||
|
|
||||
|
public async Task Open() |
||||
|
{ |
||||
|
var dialog = new OpenFileDialog(); |
||||
|
var result = await dialog.ShowAsync(App.Current.MainWindow); |
||||
|
|
||||
|
if (result != null) |
||||
|
{ |
||||
|
foreach (var path in result) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Opened: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Save() |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine("Save"); |
||||
|
} |
||||
|
|
||||
|
public void OpenRecent(string path) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,53 +0,0 @@ |
|||||
using System; |
|
||||
using Avalonia.Data; |
|
||||
|
|
||||
namespace Avalonia.Logging |
|
||||
{ |
|
||||
internal static class LoggerExtensions |
|
||||
{ |
|
||||
public static void LogIfError( |
|
||||
this BindingNotification notification, |
|
||||
object source, |
|
||||
AvaloniaProperty property) |
|
||||
{ |
|
||||
if (notification.ErrorType == BindingErrorType.Error) |
|
||||
{ |
|
||||
if (notification.Error is AggregateException aggregate) |
|
||||
{ |
|
||||
foreach (var inner in aggregate.InnerExceptions) |
|
||||
{ |
|
||||
LogError(source, property, inner); |
|
||||
} |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
LogError(source, property, notification.Error); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private static void LogError(object source, AvaloniaProperty property, Exception e) |
|
||||
{ |
|
||||
var level = LogEventLevel.Warning; |
|
||||
|
|
||||
if (e is BindingChainException b && |
|
||||
!string.IsNullOrEmpty(b.Expression) && |
|
||||
string.IsNullOrEmpty(b.ExpressionErrorPoint)) |
|
||||
{ |
|
||||
// The error occurred at the root of the binding chain: it's possible that the
|
|
||||
// DataContext isn't set up yet, so log at Information level instead of Warning
|
|
||||
// to prevent spewing hundreds of errors.
|
|
||||
level = LogEventLevel.Information; |
|
||||
} |
|
||||
|
|
||||
Logger.Log( |
|
||||
level, |
|
||||
LogArea.Binding, |
|
||||
source, |
|
||||
"Error in binding to {Target}.{Property}: {Message}", |
|
||||
source, |
|
||||
property, |
|
||||
e.Message); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,192 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Avalonia.Controls.Generators; |
||||
|
using Avalonia.Controls.Platform; |
||||
|
using Avalonia.Controls.Primitives; |
||||
|
using Avalonia.Controls.Templates; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Interactivity; |
||||
|
using Avalonia.LogicalTree; |
||||
|
|
||||
|
namespace Avalonia.Controls |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Base class for menu controls.
|
||||
|
/// </summary>
|
||||
|
public abstract class MenuBase : SelectingItemsControl, IFocusScope, IMenu |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="IsOpen"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly DirectProperty<Menu, bool> IsOpenProperty = |
||||
|
AvaloniaProperty.RegisterDirect<Menu, bool>( |
||||
|
nameof(IsOpen), |
||||
|
o => o.IsOpen); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="MenuOpened"/> event.
|
||||
|
/// </summary>
|
||||
|
public static readonly RoutedEvent<RoutedEventArgs> MenuOpenedEvent = |
||||
|
RoutedEvent.Register<MenuItem, RoutedEventArgs>(nameof(MenuOpened), RoutingStrategies.Bubble); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="MenuClosed"/> event.
|
||||
|
/// </summary>
|
||||
|
public static readonly RoutedEvent<RoutedEventArgs> MenuClosedEvent = |
||||
|
RoutedEvent.Register<MenuItem, RoutedEventArgs>(nameof(MenuClosed), RoutingStrategies.Bubble); |
||||
|
|
||||
|
private bool _isOpen; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="MenuBase"/> class.
|
||||
|
/// </summary>
|
||||
|
public MenuBase() |
||||
|
{ |
||||
|
InteractionHandler = new DefaultMenuInteractionHandler(false); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="MenuBase"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="interactionHandler">The menu interaction handler.</param>
|
||||
|
public MenuBase(IMenuInteractionHandler interactionHandler) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(interactionHandler != null); |
||||
|
|
||||
|
InteractionHandler = interactionHandler; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes static members of the <see cref="MenuBase"/> class.
|
||||
|
/// </summary>
|
||||
|
static MenuBase() |
||||
|
{ |
||||
|
MenuItem.SubmenuOpenedEvent.AddClassHandler<MenuBase>(x => x.OnSubmenuOpened); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets a value indicating whether the menu is open.
|
||||
|
/// </summary>
|
||||
|
public bool IsOpen |
||||
|
{ |
||||
|
get { return _isOpen; } |
||||
|
protected set { SetAndRaise(IsOpenProperty, ref _isOpen, value); } |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
IMenuInteractionHandler IMenu.InteractionHandler => InteractionHandler; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
IMenuItem IMenuElement.SelectedItem |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
var index = SelectedIndex; |
||||
|
return (index != -1) ? |
||||
|
(IMenuItem)ItemContainerGenerator.ContainerFromIndex(index) : |
||||
|
null; |
||||
|
} |
||||
|
set |
||||
|
{ |
||||
|
SelectedIndex = ItemContainerGenerator.IndexFromContainer(value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
IEnumerable<IMenuItem> IMenuElement.SubItems |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return ItemContainerGenerator.Containers |
||||
|
.Select(x => x.ContainerControl) |
||||
|
.OfType<IMenuItem>(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the interaction handler for the menu.
|
||||
|
/// </summary>
|
||||
|
protected IMenuInteractionHandler InteractionHandler { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Occurs when a <see cref="Menu"/> is opened.
|
||||
|
/// </summary>
|
||||
|
public event EventHandler<RoutedEventArgs> MenuOpened |
||||
|
{ |
||||
|
add { AddHandler(MenuOpenedEvent, value); } |
||||
|
remove { RemoveHandler(MenuOpenedEvent, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Occurs when a <see cref="Menu"/> is closed.
|
||||
|
/// </summary>
|
||||
|
public event EventHandler<RoutedEventArgs> MenuClosed |
||||
|
{ |
||||
|
add { AddHandler(MenuClosedEvent, value); } |
||||
|
remove { RemoveHandler(MenuClosedEvent, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Closes the menu.
|
||||
|
/// </summary>
|
||||
|
public abstract void Close(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Opens the menu.
|
||||
|
/// </summary>
|
||||
|
public abstract void Open(); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override IItemContainerGenerator CreateItemContainerGenerator() |
||||
|
{ |
||||
|
return new ItemContainerGenerator<MenuItem>(this, MenuItem.HeaderProperty, null); |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void OnKeyDown(KeyEventArgs e) |
||||
|
{ |
||||
|
// Don't handle here: let the interaction handler handle it.
|
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) |
||||
|
{ |
||||
|
base.OnAttachedToVisualTree(e); |
||||
|
InteractionHandler.Attach(this); |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) |
||||
|
{ |
||||
|
base.OnDetachedFromVisualTree(e); |
||||
|
InteractionHandler.Detach(this); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Called when a submenu opens somewhere in the menu.
|
||||
|
/// </summary>
|
||||
|
/// <param name="e">The event args.</param>
|
||||
|
protected virtual void OnSubmenuOpened(RoutedEventArgs e) |
||||
|
{ |
||||
|
if (e.Source is MenuItem menuItem && menuItem.Parent == this) |
||||
|
{ |
||||
|
foreach (var child in this.GetLogicalChildren().OfType<MenuItem>()) |
||||
|
{ |
||||
|
if (child != menuItem && child.IsSubMenuOpen) |
||||
|
{ |
||||
|
child.IsSubMenuOpen = false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
IsOpen = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
using System; |
||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Avalonia.Layout |
||||
|
{ |
||||
|
internal class LayoutQueue<T> : IReadOnlyCollection<T> |
||||
|
{ |
||||
|
private struct Info |
||||
|
{ |
||||
|
public bool Active; |
||||
|
public int Count; |
||||
|
} |
||||
|
|
||||
|
public LayoutQueue(Func<T, bool> shouldEnqueue) |
||||
|
{ |
||||
|
_shouldEnqueue = shouldEnqueue; |
||||
|
} |
||||
|
|
||||
|
private Func<T, bool> _shouldEnqueue; |
||||
|
private Queue<T> _inner = new Queue<T>(); |
||||
|
private Dictionary<T, Info> _loopQueueInfo = new Dictionary<T, Info>(); |
||||
|
private int _maxEnqueueCountPerLoop = 1; |
||||
|
|
||||
|
public int Count => _inner.Count; |
||||
|
|
||||
|
public IEnumerator<T> GetEnumerator() => (_inner as IEnumerable<T>).GetEnumerator(); |
||||
|
|
||||
|
IEnumerator IEnumerable.GetEnumerator() => _inner.GetEnumerator(); |
||||
|
|
||||
|
public T Dequeue() |
||||
|
{ |
||||
|
var result = _inner.Dequeue(); |
||||
|
|
||||
|
if (_loopQueueInfo.TryGetValue(result, out var info)) |
||||
|
{ |
||||
|
info.Active = false; |
||||
|
_loopQueueInfo[result] = info; |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public void Enqueue(T item) |
||||
|
{ |
||||
|
_loopQueueInfo.TryGetValue(item, out var info); |
||||
|
|
||||
|
if (!info.Active && info.Count < _maxEnqueueCountPerLoop) |
||||
|
{ |
||||
|
_inner.Enqueue(item); |
||||
|
_loopQueueInfo[item] = new Info() { Active = true, Count = info.Count + 1 }; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void BeginLoop(int maxEnqueueCountPerLoop) |
||||
|
{ |
||||
|
_maxEnqueueCountPerLoop = maxEnqueueCountPerLoop; |
||||
|
} |
||||
|
|
||||
|
public void EndLoop() |
||||
|
{ |
||||
|
var notfinalized = _loopQueueInfo.Where(v => v.Value.Count == _maxEnqueueCountPerLoop).ToArray(); |
||||
|
|
||||
|
_loopQueueInfo.Clear(); |
||||
|
|
||||
|
//prevent layout cycle but add to next layout the non arranged/measured items that might have caused cycle
|
||||
|
//one more time as a final attempt
|
||||
|
foreach (var item in notfinalized) |
||||
|
{ |
||||
|
if (_shouldEnqueue(item.Key)) |
||||
|
{ |
||||
|
_loopQueueInfo[item.Key] = new Info() { Active = true, Count = item.Value.Count + 1 }; |
||||
|
_inner.Enqueue(item.Key); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,6 +1,10 @@ |
|||||
// Copyright (c) The Avalonia Project. All rights reserved.
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
using Avalonia.Metadata; |
using Avalonia.Metadata; |
||||
|
|
||||
|
[assembly: InternalsVisibleTo("Avalonia.Layout.UnitTests")] |
||||
|
|
||||
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Layout")] |
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Layout")] |
||||
|
|
||||
|
|||||
Binary file not shown.
@ -0,0 +1,99 @@ |
|||||
|
using System; |
||||
|
using System.ComponentModel; |
||||
|
using System.Globalization; |
||||
|
using System.Linq; |
||||
|
using System.Linq.Expressions; |
||||
|
using System.Reflection; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Markup.Xaml.PortableXaml; |
||||
|
using Portable.Xaml; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.Converters |
||||
|
{ |
||||
|
internal class AvaloniaEventConverter : TypeConverter |
||||
|
{ |
||||
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
||||
|
{ |
||||
|
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); |
||||
|
} |
||||
|
|
||||
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
||||
|
{ |
||||
|
var text = value as string; |
||||
|
if (text != null) |
||||
|
{ |
||||
|
var rootObjectProvider = context.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider; |
||||
|
var destinationTypeProvider = context.GetService(typeof(IDestinationTypeProvider)) as IDestinationTypeProvider; |
||||
|
if (rootObjectProvider != null && destinationTypeProvider != null) |
||||
|
{ |
||||
|
var target = rootObjectProvider.RootObject; |
||||
|
var eventType = destinationTypeProvider.GetDestinationType(); |
||||
|
var eventParameters = eventType.GetRuntimeMethods().First(r => r.Name == "Invoke").GetParameters(); |
||||
|
// go in reverse to match System.Xaml behaviour
|
||||
|
var methods = target.GetType().GetRuntimeMethods().Reverse(); |
||||
|
|
||||
|
// find based on exact match parameter types first
|
||||
|
foreach (var method in methods) |
||||
|
{ |
||||
|
if (method.Name != text) |
||||
|
continue; |
||||
|
var parameters = method.GetParameters(); |
||||
|
if (eventParameters.Length != parameters.Length) |
||||
|
continue; |
||||
|
if (parameters.Length == 0) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
|
||||
|
for (int i = 0; i < parameters.Length; i++) |
||||
|
{ |
||||
|
var param = parameters[i]; |
||||
|
var eventParam = eventParameters[i]; |
||||
|
if (param.ParameterType != eventParam.ParameterType) |
||||
|
break; |
||||
|
if (i == parameters.Length - 1) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// EnhancedXaml: Find method with compatible base class parameters
|
||||
|
foreach (var method in methods) |
||||
|
{ |
||||
|
if (method.Name != text) |
||||
|
continue; |
||||
|
var parameters = method.GetParameters(); |
||||
|
if (parameters.Length == 0 || eventParameters.Length != parameters.Length) |
||||
|
continue; |
||||
|
|
||||
|
for (int i = 0; i < parameters.Length; i++) |
||||
|
{ |
||||
|
var param = parameters[i]; |
||||
|
var eventParam = eventParameters[i]; |
||||
|
if (!param.ParameterType.GetTypeInfo().IsAssignableFrom(eventParam.ParameterType.GetTypeInfo())) |
||||
|
break; |
||||
|
if (i == parameters.Length - 1) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var contextProvider = (IXamlSchemaContextProvider)context.GetService(typeof(IXamlSchemaContextProvider)); |
||||
|
var avaloniaContext = (AvaloniaXamlSchemaContext)contextProvider.SchemaContext; |
||||
|
|
||||
|
if (avaloniaContext.IsDesignMode) |
||||
|
{ |
||||
|
// We want to ignore missing events in the designer, so if event handler
|
||||
|
// wasn't found create an empty delegate.
|
||||
|
var lambdaExpression = Expression.Lambda( |
||||
|
eventType, |
||||
|
Expression.Empty(), |
||||
|
eventParameters.Select(x => Expression.Parameter(x.ParameterType))); |
||||
|
return lambdaExpression.Compile(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
throw new XamlObjectWriterException($"Referenced value method {text} in type {target.GetType()} indicated by event {eventType.FullName} was not found"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return base.ConvertFrom(context, culture, value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Avalonia.Controls.Shapes; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Controls.UnitTests.Shapes |
||||
|
{ |
||||
|
public class PathTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Path_With_Null_Data_Does_Not_Throw_On_Measure() |
||||
|
{ |
||||
|
var target = new Path(); |
||||
|
|
||||
|
target.Measure(Size.Infinity); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,196 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Layout.UnitTests |
||||
|
{ |
||||
|
public class LayoutQueueTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Enqueue() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => true); |
||||
|
var refQueue = new Queue<string>(); |
||||
|
var items = new[] { "1", "2", "3" }; |
||||
|
|
||||
|
foreach (var item in items) |
||||
|
{ |
||||
|
target.Enqueue(item); |
||||
|
refQueue.Enqueue(item); |
||||
|
} |
||||
|
|
||||
|
Assert.Equal(refQueue, target); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Dequeue() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => true); |
||||
|
var refQueue = new Queue<string>(); |
||||
|
var items = new[] { "1", "2", "3" }; |
||||
|
|
||||
|
foreach (var item in items) |
||||
|
{ |
||||
|
target.Enqueue(item); |
||||
|
refQueue.Enqueue(item); |
||||
|
} |
||||
|
|
||||
|
while (refQueue.Count > 0) |
||||
|
{ |
||||
|
Assert.Equal(refQueue.Dequeue(), target.Dequeue()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Enqueue_UniqueElements() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => true); |
||||
|
|
||||
|
var items = new[] { "1", "2", "3", "1" }; |
||||
|
|
||||
|
foreach (var item in items) |
||||
|
{ |
||||
|
target.Enqueue(item); |
||||
|
} |
||||
|
|
||||
|
Assert.Equal(3, target.Count); |
||||
|
Assert.Equal(items.Take(3), target); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Shouldnt_Enqueue_More_Than_Limit_In_Loop() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => true); |
||||
|
|
||||
|
//1
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.BeginLoop(3); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//2
|
||||
|
target.Enqueue("Foo"); |
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//3
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//4 more than limit shouldn't be added
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(0, target.Count); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Shouldnt_Count_Unique_Enqueue_For_Limit_In_Loop() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => true); |
||||
|
|
||||
|
//1
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.BeginLoop(3); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//2
|
||||
|
target.Enqueue("Foo"); |
||||
|
target.Enqueue("Foo"); |
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//3
|
||||
|
target.Enqueue("Foo"); |
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//4 more than limit shouldn't be added
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(0, target.Count); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Enqueue_When_Condition_True_After_Loop_When_Limit_Met() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => true); |
||||
|
|
||||
|
//1
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.BeginLoop(3); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//2
|
||||
|
target.Enqueue("Foo"); |
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//3
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//4 more than limit shouldn't be added to queue
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(0, target.Count); |
||||
|
|
||||
|
target.EndLoop(); |
||||
|
|
||||
|
//after loop should be added once
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
Assert.Equal("Foo", target.First()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Shouldnt_Enqueue_When_Condition_False_After_Loop_When_Limit_Met() |
||||
|
{ |
||||
|
var target = new LayoutQueue<string>(_ => false); |
||||
|
|
||||
|
//1
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.BeginLoop(3); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//2
|
||||
|
target.Enqueue("Foo"); |
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//3
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(1, target.Count); |
||||
|
|
||||
|
target.Dequeue(); |
||||
|
|
||||
|
//4 more than limit shouldn't be added
|
||||
|
target.Enqueue("Foo"); |
||||
|
|
||||
|
Assert.Equal(0, target.Count); |
||||
|
|
||||
|
target.EndLoop(); |
||||
|
|
||||
|
Assert.Equal(0, target.Count); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Input; |
||||
|
using Portable.Xaml; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
||||
|
{ |
||||
|
public class EventTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Event_Is_Attached() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='OnClick'/>"; |
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
loader.Load(xaml, rootInstance: target); |
||||
|
RaiseClick(target); |
||||
|
|
||||
|
Assert.True(target.Clicked); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Exception_Is_Thrown_If_Event_Not_Found() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
Assert.Throws<XamlObjectWriterException>(() => loader.Load(xaml, rootInstance: target)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Exception_Is_Not_Thrown_If_Event_Not_Found_In_Design_Mode() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
||||
|
var loader = new AvaloniaXamlLoader { IsDesignMode = true }; |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
loader.Load(xaml, rootInstance: target); |
||||
|
} |
||||
|
|
||||
|
private void RaiseClick(MyButton target) |
||||
|
{ |
||||
|
target.RaiseEvent(new KeyEventArgs |
||||
|
{ |
||||
|
RoutedEvent = Button.KeyDownEvent, |
||||
|
Key = Key.Enter, |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
class MyButton : Button |
||||
|
{ |
||||
|
public bool Clicked { get; private set; } |
||||
|
|
||||
|
public void OnClick(object sender, EventArgs e) |
||||
|
{ |
||||
|
Clicked = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue